# Lineage and SLOs protocol

The next part after pipeline cost. The published pages show what the checks catch on one table (the scorecard), how
faults move between layers and how recovery goes (pipeline recovery), and what each pipeline layout costs (pipeline
cost). None of them follows a change downstream: which tables, metrics and dashboard tiles it breaks, who hears about
it and when, and which declared service levels or pre-deploy gate would have stopped it before someone looked at a wrong
number. The scorecard's lineage line (raw_trips to stg_trips to trips to daily_trips, every descendant inherits the
source score) is model-level dependency exposure; this part derives column-level lineage from the code, checks it
against what each change really broke, and measures what it is worth in days of wrong numbers.

No model is called. Every number comes from a deterministic replay of the month in `quality/lineage.py`; the frozen
protocol is `lineage_slo/protocol.json`, committed with this file before any code computes a change's outcome.
Development runs that only check the code come first; their numbers are not reported.

## The question

When an upstream change lands (a schema change, a late source, a logic change in one model), which downstream tables,
metrics and dashboards does it break, how quickly does each owner learn, and which declared SLOs and pre-deploy gates
would have caught it before a consumer did?

## Pipeline, metrics and dashboards

The scorecard's own dbt project, read unchanged from the vendored archive: `stg_trips` (casts the landing table),
`trips` (select from staging), `zones`, and `daily_trips` (per day and pickup area), with the scorecard's one edit
(staging takes `loaded_at` from the landing table). The ten metrics are the semantic layer's own (`semantic.yml`),
computed over `trips` for any window of trip dates. The models run in-process on DuckDB: `ref()` and `source()` are
rendered to table names, models build in dependency order, and a model that fails keeps its previous table while the
models downstream of it are skipped and keep theirs, as a dbt run does. Running dbt itself for every build of every
change would take far longer; the SQL is the same.

Three dashboards, declared here and synthetic, with ten tiles:

| Dashboard | Owner | Viewed at 09:00 | Tiles |
|---|---|---|---|
| Operations | Operations analyst | every day | trips yesterday; trips yesterday by pickup area; mean trip minutes, last 7 days; shared match rate, last 7 days |
| Finance | Finance analyst | Mondays | fares, month to date; tips, month to date; mean fare, last 7 days |
| Executive | Reporting lead | weekdays | trips, month to date; miles, month to date; fares yesterday |

Four tiles read `daily_trips` or `zones` through SQL; six read a metric. The ingestion engineer owns the landing
tables; the analytics engineer owns the models and metrics. All owners are synthetic.

## The replay

One file per trip date lands at 01:00 the next day and is appended to the landing table by column name (a column the
table lacks is ignored, a missing column is left empty, every value is text). Every model is rebuilt at 02:00, the SLOs
run at 06:00 and dashboards are viewed at 09:00. A code change is deployed at 15:00 on its day and first runs in the
next build. The clock is 06:00 on February 1, 2024. Each change runs on its own copy of the month; changes never
overlap. The truth is the same replay with no change.

## Lineage

Parsed with sqlglot from the rendered model SQL, the measure expressions and the tile SQL. Nodes are source columns,
model columns, one rows node per model, metrics and tiles. A column depends on the columns its select expression reads;
a model's rows node depends on the upstream rows nodes and on the columns read in its where, join, group by and having
clauses; every output column depends on its model's rows node. A change's changed nodes come from parsing the old and
the new SQL. Three readings are scored against what each change really broke: model-level (everything downstream of the
changed model or source), column lineage through select expressions only, and column lineage with rows nodes, which is
the one the impact analysis uses. The lineage is exact on this project in one sense: the author wrote every model and
tile, so every reference the parser sees is real and none is hidden in a query built at run time. That is true by construction
here and would not be in a warehouse with ad hoc queries.

## The 20 changes (declared, one author, a self-test)

| ID | Kind | Lands | Change |
|---|---|---|---|
| C01 | code | Jan 10 | staging renames pickup_zone_id; the mart is not updated |
| C02 | code | Jan 12 | the mart renames total_fare; the dashboard query is not updated |
| C03 | code | Jan 11 | staging casts trip_miles to integer |
| C04 | code | Jan 16 | staging drops trips with no distance |
| C05 | code | Jan 17 | the intermediate model drops rows with an empty fare |
| C06 | code | Jan 18 | the mart drops trips picked up outside Chicago |
| C07 | code | Jan 19 | the matched-trips measure reads the authorized flag |
| C08 | code | Jan 22 | the duration measure divides seconds by 3600 |
| C09 | code | Jan 23 | the mart groups by drop-off area under the pickup name |
| H01 | code, harmless | Jan 9 | the intermediate model lists its columns instead of select * |
| H02 | code, harmless | Jan 11 | the mart adds a column nothing reads |
| H03 | code, harmless | Jan 15 | the mart drops a column nothing reads |
| H04 | code, harmless | Jan 17 | staging stores fares as exact decimals |
| S01 | source | Jan 16 | the source renames tip from the January 15 file |
| S02 | source | Jan 14 | the source sends trip_miles with a unit from the January 13 file |
| S03 | source | Jan 19 | the January 18 file lands a day and a half late |
| S04 | source | Jan 20 | an upstream retention job deletes January 5 |
| S05 | source | Jan 22 | an upstream backfill delivers January 8 to 14 again |
| S06 | source | Jan 25 | the January 24 file lands with a third of its rows, the rest a day later |
| H05 | source, harmless | Jan 13 | the source adds a column the loader ignores |

The exact patches and source transforms are in the protocol. The change mix is a declared assumption, not a sample of
real changes.

## Arms

- **A0, no lineage.** Owners learn only when a viewer notices a wrong tile. The noticing dashboard's owner, the analytics
  engineer and the change's owner are told; the cause is known after the trace delay (base case none).
- **A1, lineage impact analysis before deploy.** A0, plus: before each code deploy the impact analysis lists the affected
  assets and tells their owners. A change that leaves some asset reading a column that no longer exists is held and
  corrected before deploy. When a viewer notices a wrong tile, the cause is traced at once and every owner in the
  cause's impact list is told. Source changes do not pass through a deploy, so the pre-deploy step never sees them.
- **A2, lineage plus SLOs.** A1, plus declared SLOs on `trips` and `daily_trips` at 06:00: freshness (the day before the
  check is present), volume (that day holds at least 0.6 times the median of up to 14 prior days, given at least 7),
  history (an earlier day's count changes only when rows for it landed or a fix restated it), non-null (fare, tip,
  trip_miles, trip_seconds and trip_total at least 95% filled on that day, `trips` only), and accuracy (per day, the trip
  count and fare sum match the landing table's distinct trip IDs, fares within US$0.01). At the first breach the table's
  owner, the owners of every tile downstream of it and the change's owner are told, the cause is known, and those tiles
  show a data warning until the fix runs.
- **A3, plus a pre-deploy gate with rollback.** A2, plus a gate on every code deploy: production and changed code are
  each built on the landing table's last 7 trip dates, and the deploy is blocked when anything fails that did not, a
  published table loses a column or changes a column's type, a key appears on one side only, or a published number, a
  metric's daily value or a tile value moves by more than 1e-6 relative. A deployed code change upstream of an SLO breach
  is rolled back at the breach and the build re-runs before the 09:00 views.

A fix runs in the first build after the cause is known. The late file (S03) and the partial file (S06) resolve only when
their rows land.

## Measures

- **Consumer-visible wrong days** (the headline): per change and arm, the days on which at least one viewed tile showed a
  wrong value without a data warning. Wrong means the tile failed to render, showed nothing, or showed a different value
  at the precision it displays, however small. A secondary count keeps only tiles off by 1% or more, failures and empty
  tiles.
- **Flagged days**: days with at least one viewed tile under a data warning, the price of the SLOs.
- **Assets broken per change**: models, metrics and tiles that differ from the clean replay at any point, against what
  each lineage reading listed (precision and recall).
- **Time until each owner is told**: hours from the change landing to each owner of a broken asset hearing about it.
- **False blocks**: gate blocks on the harmless changes. **False alarms**: SLO breaches on the clean replay and on the
  harmless changes.
- **SLO breaches**: which fire, when, and which fire after a viewer already saw a wrong tile.

## Notice rule and sensitivity

A viewer notices a tile that fails to render, shows nothing, or is at least 10% off the right value (base case), unless
it carries a warning. That rule, the fix lag and the delay to trace a cause without lineage are assumptions that drive
the result, so the replay is repeated over a declared grid: notice thresholds of 5%, 10%, 25% and failures only; a fix in
the first or the second build after the cause is known; and a trace delay without lineage of none or one day. The page
reports the base case and the range over the grid, and whether the order of the arms holds in every cell.

## Headline rule

The h1 leads with the consumer-visible wrong days per arm in the base case, summed over the 20 changes, and says that the
changes and checks have one author.

## Limits, stated before the run

- One author wrote the pipeline's dashboards, the 20 changes, the SLOs and the gate, so every catch is a self-test.
- Lineage parsed from code is exact here because the author wrote all the code; real warehouses carry queries the parser
  never sees.
- The notice rule, the view schedules, the fix lag and the owners' behaviour are declared assumptions.
- One month of 10,000 public trips; each change runs alone.
- The SLOs and the gate read the same landing table the models read. An SLO that compares a table with the landing
  table cannot see a fault already in the landing table (an identity for source value changes), and says so per change.

## Amendment before the measured run

Decided during the development runs, before the measured run, and committed with the code. The development runs only
showed where the protocol left a step open; the frozen protocol file is unchanged.

- An SLO whose own query cannot run (it reads a column that a change removed) counts as a breach of that SLO.
- The volume SLO counts a day with no rows as zero in the prior median.
- For a change in the semantic layer, the model-level reading lists every metric and every tile that reads a metric (the
  semantic model over trips is the changed model).
- A breach is called late when it first fires, on the replay without any fix, after the first wrong view in the arm with
  no lineage and no SLOs.
- The gate's tiles are read at the morning after the deploy, as declared. Tiles that show yesterday then read the deploy
  day, which the sample does not hold, so they are empty on both sides; the published tables they read are still
  compared row by row.
- An owner told after the clock counts as not told.
