name: synth-cross-row
description: Synth rows that depend on EARLIER rows — `row()` and `lag()` in rules and constraints, accepted-row order, per-partition lag, the panel reset, a recursive adstock recipe, and what profile capture ignores.
type: guide
kind: design
applies_to: inspect, predict, manifest
covers: [synth, row, lag, adstock, panel, partition, cross-row, accepted-row]
requires: [capability:synth]
Synth cross-row expressions
A rule or constraint may read the row ORDINAL (row()) and an EARLIER row’s value (lag). Generation is serial and row-major with no shuffle, so declaration order IS row order and “the previous row” is well defined.
Accepted-row order
row()is the 0-based ordinal of the ACCEPTED row. A constraint-rejected attempt redraws at the SAME ordinal, so the written cohort reads exactly0..row_count-1.monotonic_fromcounts the same way.lag("f", n[, d])readsf’s valuenaccepted rows back. A rejected attempt never enters the history.nis an integer literal 1–1024 and the name a declared-field string literal (PULSE_SYNTH_LAG_OFFSET_INVALID/PULSE_SYNTH_LAG_FIELD_INVALID).- Neither consumes RNG: adding one moves no other field’s draws.
- Before row
n,lagreturnsd, else null. A lagged NULL stays null (SQLLAG), neverd: guard with??or a default. In augment mode the history starts empty at the first generated row. - It returns the post-rule value BEFORE encode rounding, so on a
u*/f32field it can differ from the stored value. Self-lag works.
Recipes
Recursive adstock, with seasonal controls (internal/examples/synth/07_mmm_adstock.synth.json):
"rules": [
{"set_expr": {"week": "row()"}},
{"set_expr": {"true_adstock": "spend + 0.6 * lag(\"true_adstock\", 1, 0)"}},
{"set_expr": {"fourier_sin": "sin(2 * pi() * week / 52)"}}
]
week is its own rule so later rules see it (within one rule, expressions read the row as it stood BEFORE the rule).
Panel reset. Panels laid out contiguously (52 rows each) restart the recursion at each boundary:
{"set_expr": {"stock": "spend + 0.6 * (row() % 52 == 0 ? 0 : lag(\"stock\", 1, 0))"}}
Per-partition lag. A 4th argument naming a categorical_* field keys the history by its value, so interleaved geos each get their own series:
{"set_expr": {"stock": "spend + 0.6 * lag(\"stock\", 1, 0, \"geo\")"}}
- It reads the
n-th previous accepted row with THIS row’sgeo; each geo’s firstnrows return the default. A nullgeois its own partition. Writenilas the default for a null start:lag("x", 1, nil, "geo") ?? 0. - No interleaving guarantee: geos arrive in draw order, not blocks or a fixed rotation. Each geo’s history is its own rows in accepted order.
- The partition must be final when read: a rule at or after the reading rule that writes it (
set,set_expr,set_null, a non-gatenull_togethermember) is refused, as are a non-literal, undeclared or non-categorical partition (PULSE_SYNTH_LAG_PARTITION_INVALID). Constraints run after every rule, so any partition is final there.
Not seen by capture
profile create, the fidelity report and --suggest-rules know nothing of lag: a captured profile reconstructs marginals only, the report never scores serial dependence, and no detector proposes a lag rule. Write them by hand.
See
synth-rule-expressions— the row environment and coercion;synth-structural-rules— slots and order.synth-determinism— why none of this draws RNG.