# Alerts and on-call protocol

The part after lineage and SLOs. The published pages show what the checks catch on one month-end snapshot (the
scorecard), how faults move between layers and how recovery goes (pipeline recovery), what each pipeline layout costs
(pipeline cost), and how a code or source change reaches the dashboards (lineage and SLOs). None of them runs the
scorecard's own checks every morning and asks what happens to their alerts: who is told, how fast, through which route,
how many pages that takes, and what a failing check should do to the scheduled refresh. This part does that, on the
same public trip data and the same fault fixtures.

No model is called. Every number comes from a deterministic replay in `quality/reliability.py`; the frozen protocol is
`reliability/protocol.json`, committed with this file before any code computes a fault's outcome. Development runs
check only that the replay reproduces the scorecard's published month-end check results; they compute no routing,
refresh or quarantine outcome.

## The question

When the scorecard's checks run every morning on a month of daily loads, which way of routing their alerts tells the
right owner before a viewer reads a wrong number, and at what cost in pages? And when a check fails, is it better to let
the refresh run anyway, stop it, or quarantine the failing rows and mark the affected numbers as not final?

## Overlap with the published pages

Nearest are the scorecard (same checks, same faults, one month-end run, question: do the checks catch the fault) and
lineage and SLOs (same day-by-day replay idea, question: which lineage and service levels stop a code or source change
before a viewer sees it). This part keeps the scorecard's checks and faults and asks the next question, what the alerts
do, which neither page answers: lineage and SLOs had no routes, no paging cost, no quarantine and no refresh policy, and
its changes were code and source changes, not these faults. The event contracts study measures days until monitoring
finds a break in product events before release; its data and question differ.

## The replay

- **Data.** The scorecard's 10,000-row January 2024 sample from the pinned archive. Each trip date's file lands at
  01:00 the next day; the zone reference table is present from the start.
- **Faults.** Each fault is one of the study's existing fixtures, applied as written: the fault code gives the month's
  rows and each row's landing time. A row is in the landing table from its landing time on. A row the fault removes
  never lands. One fault per replay; faults never overlap; nothing is fixed during a replay.
- **Every morning at 06:00**, from January 2 to February 1 (31 mornings): the landing table takes every row landed so
  far, the scorecard's dbt models rebuild in-process on DuckDB (the same SQL the lineage part runs), and every check
  runs. The February 1 morning is the scorecard's own month-end snapshot (its clock is February 1 at 06:00), so the
  replay must reproduce the scorecard's recorded check results there; that is the replay's correctness test.
- **Dashboards** are the lineage part's three synthetic dashboards and ten tiles, read from `lineage_slo/protocol.json`
  unchanged: the operations dashboard is viewed every day, finance on Mondays and the executive dashboard on weekdays,
  at 09:00, from January 2 to February 1. A view is wrong when a tile shows a different value from the clean replay's
  same tile at the precision it displays, or fails, or shows nothing where the clean replay shows a number.

## Checks, owners, severity and routes

The checks are the scorecard's version 3 profile, as written (22 version 2 checks and the 5 version 3 rules), plus two
per-source arrival checks added here:

- **Trip file arrival** (source `raw_trips`): yesterday's trip file is expected at 01:00. By the 06:00 check it must
  have landed, and with at least half the median row count of up to 7 earlier trip dates (given at least 3).
- **Zone reference present** (source `raw_zones`): the zone table is expected once, before the first build, and must
  hold rows.

Two checks are adapted to a daily schedule, and only these two: the source freshness check measures the newest row's
age at each morning's 06:00 instead of February 1 at 06:00 (warn after 6 hours, error after 12, as declared in the
scorecard, and a warning counts as a failure as it does there), and `historical_freshness` compares with midnight after
each check's own morning instead of the literal February 2 at 00:00. Every other check runs its own SQL or code
unchanged. A check whose status is unknown (the anomaly check before it has 14 days of history) does not alert.

Three synthetic owners: the **ingestion engineer** (checks on how rows arrive: freshness, arrival, duplicates, copies),
the **analytics engineer** (tests on the models: not null, unique, relationships, month boundary, units, the two
bookkeeping identities), and the **trip feed owner** (checks on what the values say: missing fares, units, totals,
durations, flag shares, the missing-fare anomaly). Each check's owner, severity and the model or source it is attached
to are listed in the protocol.

Severity is declared by one rule before the run: **critical** when a failure means rows a dashboard counts are missing,
doubled or in the wrong unit; **warning** when a share of values looks off and may be real; **info** for bookkeeping
identities and the zone reference. One author assigned every severity knowing which checks the earlier rounds saw
fail, so the severity routing is a self-test.

## Routing rules (the arms)

- **R1, page on every failed check.** Every failing check pages its owner, every morning it fails.
- **R2, page only on critical models.** A failing check attached to a model the dashboards read (`trips`,
  `daily_trips`, `zones`) pages its owner; a failing check on the landing tables opens a ticket.
- **R3, route by severity.** Critical pages, warning opens a ticket, info goes to the daily digest.
- **R4, digest only.** Every failing check goes to the daily digest.

When an owner is told, by route:

- **Page:** told at 06:30 the same morning, any day of the week (a 30-minute acknowledgement).
- **Ticket:** told at 10:00 on the check's own day when it is a weekday, otherwise at 10:00 on the next Monday.
- **Digest:** the day's failures are sent at 17:00 and read at 09:30 on the next weekday.

So a page tells its owner before the 09:00 views by the schedule's design, and a ticket or a digest never does on the
same morning; the measures below count how often that difference matters.

## Refresh policy when a check fails (item two)

Every fault is replayed three ways:

- **Load:** the refresh always runs on everything that landed; checks only alert. The routing measures use this mode.
- **Stop:** the source checks run first, and when any critical source check fails the models are not rebuilt that
  morning (they keep the last morning's tables), as a build with error-severity source tests does.
- **Quarantine:** before the build, rows that break a row rule are moved to a quarantine table with their reason, the
  models build on the rest, and the refresh completes. The checks then run on the rows that passed the quarantine (the
  build's input). A trip date is **not final** when the quarantine holds rows of
  that date or when a failing check names that date; a failing check that names no date makes every date not final
  that morning. A tile view whose window includes a not-final date carries a not-final mark.

The row rules, each taken from an existing check's own condition and applied to single rows:

| Rule | From | A row is quarantined when |
|---|---|---|
| duplicate ID | `source_unique` | its trip ID already landed (the first landed copy stays) |
| content copy | `content_duplicates` | every field but the trip ID equals an earlier landed row's on the same date |
| units | `unit_invariant` | miles times 1609.344 differs from the audit metres field by more than 0.000001, or either is empty |
| negative or empty units | `nonnegative_units` | trip seconds or miles is empty or negative, or fare or tip is negative |
| totals | `totals_reconcile` | it has a fare and its total differs from fare plus tip plus charges by more than US$0.01 |
| duration | `duration_plausible` | trip seconds is zero or less, or the trip is over 1 mile at more than 100 miles per hour |
| month | `month_boundary` | its trip date is outside January 2024 |
| arrival | `arrival_window` | it landed before its trip date ended or more than 36 hours after |
| schema | the staging casts | a field fails its staging cast |

## Freshness per source, incident review and runbook (item three)

The two arrival checks above run in the replay with every other check and are reported against the scorecard's
table-level freshness check, fault by fault. The incident review is written for the worst fault, the one with the most
wrong metric-days in the load mode (a metric-day is one of the lineage part's ten metrics on one morning, wrong when its
value for any trip date differs from the clean replay by more than 1e-9 relative; ties go to the earlier landing). The
review takes its timeline, impact and earliest-firing check from the replay and names the follow-up owner from the
protocol. The one-page on-call runbook is written before the run and published as declared. The review and the
runbook are templates a reader can lift; nothing scores them.

## Faults (existing fixtures, 30)

- The scorecard's ten single-fault scenarios (`quality/pipeline.py`): late_partition, null_spike, duplicate_load,
  unit_change, null_dose_2, null_dose_5, null_dose_10, null_spike_other_day, fresh_id_duplicates and
  partial_late_partition. The combined scenario is left out: it is four of these at once.
- The first blind set's eight faults (`quality/heldout.py`) and the second blind set's eight (`quality/blind2.py`),
  written by an independent author who never saw the checks.
- The lineage part's four source faults on how files arrive (`lineage_slo/protocol.json`): S03 (a file a day and a
  half late), S04 (a landed day deleted upstream), S05 (a backfill delivered again) and S06 (a file landing a third at a
  time). Its schema and value source changes are left out; they are not daily-load faults of the scorecard's kind.

The clean month is replayed the same way. A fault's landing time is the earliest landing, in the fault or the clean
month, of any row the fault adds, removes or changes.

## Measures

Per routing rule, in the load mode:

- **Faults paged:** faults with at least one page, of 30.
- **Faults that reached a viewer first:** faults whose first wrong view came before any owner was told (including
  faults never told), of the faults with at least one wrong view.
- **False pages per week:** pages on the clean month per 7 mornings, plus, on the fault replays, pages from a check
  that also fails on the clean replay that morning.
- **Time to detect:** hours from a fault's landing to the first owner told; the median over faults told by the end of
  the replay, with the count never told.
- **Pages per fault:** pages on the morning a fault is first seen, and over the whole replay with no fix.

Per fault and refresh mode: rows quarantined (and how many the fault added or changed), the largest relative error of
the ten metrics' month values on the last morning, wrong views with no mark, views marked not final, and whether the
quarantine hid a real change: it removed rows and left at least one metric's month value further from the clean
month's than loading them as they came. The clean month's quarantined rows are reported as false quarantines.

## Headline rule

The h1 leads with faults that reached a viewer before an owner was told, under paging on every check against paging
by severity, with the pages per week each costs, and says the severities have one author.

## Predictions, written before the run

1. No rule pages on the clean month (0 false pages per week); the clean month passes every check at month end.
2. Paging on every failed check pages every fault any check catches, about 20 of 30.
3. Paging only on critical models pages at most 6 of 30, because most catching checks sit on the landing table.
4. Routing by severity lets no more faults reach a viewer first than paging only on critical models.
5. Under paging on every failed check, the median fault sends at least 2 pages on the morning it is first seen.
6. The totals row rule quarantines some rows of the clean month.
7. Quarantine leaves less metric error than loading on the duplicate faults, and hides the late file (S03) because the
   arrival rule quarantines rows that are late but right.

## Limits, stated before the run

- One author wrote the severities, the routes, the timings, the row rules and the runbook, and knew which checks the
  earlier rounds saw fail, so every routing result is a self-test of one declared policy, not a measured recall.
- 30 faults on one month of 10,000 public trips, one fault at a time. Real months carry several at once.
- Owners act at declared times; nobody is late, sick or ignoring a channel. Real paging has acknowledgement misses.
- A page tells its owner before the 09:00 views by construction; the page counts are the price of that.
- Nothing is fixed during a replay, so the pages over the whole replay are an upper bound for a team that fixes faults.
- The checks read the same landing table the models read; a fault already in the landing table cannot be seen by a
  check that compares the two.

## Readings before the measured run

Written with the code, after the development runs that compared only the February 1 morning with the scorecard's
records (28 of 28 failed-check sets equal), and before any replay of the other mornings. The frozen protocol file is
unchanged.

- The landing table is typed the way the scorecard's seed loader typed it: dates, decimal numbers, whole-number area
  codes and true or false flags. A value that fails its type is empty in the typed table and breaks the schema row rule.
- A check whose own query cannot run counts as failing, as in the lineage part.
- Source freshness fails when the newest row is more than 6 hours old at 06:00.
- The trip file arrival check's earlier dates are the 7 trip dates before yesterday that fall in January; a date with no
  rows counts as 0.
- In the stop mode the source checks run on the landing table before the build, and the model checks after it, on
  whatever tables the models then hold.
- Rows quarantined, per fault, are counted on the last morning, when the most rows have landed.
- A metric that cannot be computed on the last morning counts as a failed month value, worse than any number.

## Fixed after the measured run

- The fault landing time of S04 read as the deleted rows' first landing (January 6 at 01:00) instead of the deletion
  time the protocol declares (January 20 at 01:00). The code was corrected and S04 replayed again; its replays are
  identical, only its landing time and record counts changed in the results, and S04 was never told under any rule, so
  no reported measure moved. S04's removed count reads 0 after the fix, because its deleted rows count as present until
  the declared deletion time; no reported measure moved.
