# Lakehouse recovery protocol

The part after table layers. The same three pipeline faults this study already planted (late data, a schema change, a
backfill) now run on a real lakehouse platform, Databricks Free Edition, and the question moves from "did the repair work?"
to "can a control total prove it?". The frozen protocol is `lakehouse_recovery/protocol.json`, committed with this file
before any fault or repair runs.

## Overlap test

Nearest published pages:

- Pipeline recovery: bronze, silver and gold Delta tables on the same month, written with the open-source `deltalake` package
  and read by DuckDB. Questions: do layer checks catch pipeline faults, and do recoveries put the published numbers back? It
  scored each recovery against the clean month, a reference no team has, and says it did not run on Spark.
- Table layers: raw, cleaned and modeled Delta tables read by DuckDB, with late and replayed micro-batches. Question: at which
  layer does each check catch each fault, and how fresh is each layer? It runs no recovery.

The new question: after a repair, which control totals can tell a repair that worked from one that did not, without the clean
reference? Neither page answers it. Pipeline recovery's control totals fire before a repair; here they are scored on the
repairs. The faults are ported from pipeline recovery on purpose, so they are not new evidence about catching faults, and
the page says so. New here: the platform, the repair attempts written by an independent author, and the scoring of each level
of total against the reference. It is a new round of this study, on its own page, linked from the entry page.

## Platform, and its terms

Databricks Free Edition: one 2X-Small serverless SQL warehouse, Delta tables in Unity Catalog, reached through the SQL
Statement Execution API and the Files API. The documentation's deciding sentence is "Free Edition accounts may not be used for
commercial purposes." and it describes the tier as "designed for students, educators, hobbyists, and anyone interested in
learning or experimenting with data and AI." This round is personal learning on public data, with no customer, client or
revenue attached. Outbound internet is restricted, so the sample is uploaded through the Files API. A quota stop ends the run
and is reported; nothing is bought and no trial is started.

A study shows the tool and a method. It says nothing about job experience with the tool.

## Data

The study's sealed sample: the first 10,000 City of Chicago trips of January 2024 by trip ID, read from the vendored archive
with its hash checked. Trip IDs are hashed; no coordinates or sub-day times. Each day becomes one CSV file in a Unity Catalog
volume. Nothing else is stored on the platform.

## Pipeline

- Bronze: every column as text, plus `source_file` and a simulated `loaded_at`. Loaded by `COPY INTO` with schema merge on, as
  a platform tutorial would set it, naming that morning's file.
- Silver: one row per trip ID, typed. `MERGE` on trip ID from that file's bronze rows, reading the registered column names.
- Gold: one row per trip date with six metrics: trips, fares present, shared matches, fare sum, miles sum, seconds sum.
  `INSERT INTO ... REPLACE WHERE` that day, computed from silver.
- January 1 to 20 load as one job; January 21 to 31 load one file a morning. Loads and arrival times are simulated.

## Faults

All three follow pipeline recovery's descriptions. Each runs in its own schema, and a fourth schema holds the clean reference.

1. `late_file`: the last 60 January 24 rows by trip ID are withheld and delivered as a separate file that no morning job names.
2. `renamed_column`: from January 26 the fare column arrives as `fare_amount`. Schema merge adds the column without an error,
   the job still reads `fare`, and silver and gold lose the fares.
3. `bad_backfill`: after the January 27 job, a backfill sets silver's fare to the trip total for January 15 to 22 (declared
   window January 15 to 21) and restates those gold days. January 28 to 31 then load normally.

## Repairs

- Own (10, a self-test; I wrote faults, repairs and levels): one or two right repairs per fault and the wrong ones listed in
  the protocol, including a restore that loses later good days, a gold-only repair, and a join by position that keeps every
  daily sum.
- Blind (12): an independent author who never saw the control totals, the levels, the sender's totals or my repairs writes four
  SQL repairs per fault: the one it would run, then three an able engineer in a hurry might run. Its brief holds the pipeline's
  SQL and the fault descriptions. Its statements run literally, with only three placeholders filled in (the landing folder and
  two table versions), and are never edited. The author is not a grader: the scoring is mechanical.
- A no-op runs on each fault as the positive control. Every level that fails on it has a recorded case of failing.

## Scoring

Truth: silver equals the clean reference run on all 12 business columns, and gold on all its columns, row for row in both
directions. A team does not have this reference; that is the point of the levels.

Levels, each scored on every attempt:

- Layer totals: bronze against silver against gold, per day, all inside the platform. This is the check the pipeline recovery
  page called control totals.
- Outside totals on gold: gold against the sender's per-day totals, held outside the platform.
- Outside totals on both: the same for silver as well.
- Row fingerprint: silver's per-day sum of row hashes over all 12 columns against the sender's.
- The bundle: outside totals on both, plus the fingerprint.

Counts match exactly; sums within 0.01, the tolerance pipeline recovery used. A false proof: a level passes and the data was
not restored. A false alarm: a level fails and the data was restored.

Limits of this scoring, stated before the run. The sender's totals are computed from the clean sample, so they are right by
construction; a real sender can be wrong. The fingerprint is the silver half of the truth in another form, and the bundle covers
every column the truth compares, so its zero false proofs follow from its definition unless a sum moves by under 0.01. What is
measured is how much each cheaper level gives up.

## Runs

One clean reference. Per fault: a no-op, the own repairs, the blind repairs, then the first own right repair again as a repeat.
SQL on fixed data has no randomness, so one run per attempt, plus that repeat. Before each attempt the three tables are restored to
the fault state's versions and their row counts compared with it. Development ran only on a tiny synthetic table, to see which
statements this workspace accepts, and on the clean reference; no fault or repair ran before this commit.

A statement the allowlist refuses, or one that fails on the platform, ends its attempt: reported as did-not-finish and left out
of the proof scores, not counted as wrong (a method failure is not a result).

## Predictions

Declared in `lakehouse_recovery/protocol.json`, each with its band, before any fault or repair runs. Every one is reported on
the page as held, missed or untestable.

## Headline rule

The h1 leads with the blind repairs: how many of those that ran restored the data, and how many of the ones that did not each level
passed. It says the sender's totals are assumed right.

## Honesty

Small numbers: 12 blind repairs and 10 of my own, one fault each of three kinds, on one month of 10,000 public trips. The counts
describe these repairs. They are not a rate for real teams. Any change to this protocol after this commit is written as "Change N
to the protocol", committed before it is used, and shown on the page.

## Changes to the protocol

Change 1, written after reading the independent author's one-line summaries and before any repair ran. The author's brief said
the backfill was "meant for January 15 to 21" and never said its fare logic was wrong, so some of its backfill repairs keep those
fares and undo only the overrun onto January 22. That is a fair reading of what it was given; the gap is in the brief. The
author is not run again and no repair is edited. The declared truth stays the headline measure. Every backfill attempt is also
compared with a second reference, the clean run with the backfill's fares kept on January 15 to 21 only, and the page reports
both. `protocol.json` stays as committed.

## Notes added after the measured run

The text above is unchanged since it was committed before any repair ran, apart from Change 1, which was committed before its use.

- Results: 12 of 12 blind repairs finished; 7 restored the data; outside totals on the published table passed 2 of the 5 that did
  not; layer totals, outside totals on both tables, the fingerprint and the bundle passed 0. 10 of 12 predictions held. Missed: 4 of 9
  hurried repairs failed, under the 5 predicted, and layer totals passed no wrong blind repair, against at least 1 predicted.
- The landing table is outside the reference. One blind repair restored silver and gold and never loaded bronze, so layer totals
  failed a repair the reference counts as restored; the bronze row count is stored with each attempt.
- Against Change 1's second reference, the author's first choice on the backfill is right, and every level fails it.
- No level was added or redefined after the results. The page's advice, and its section on what the platform did, were written after them.

## Notes added in review, after the results were seen

Two independent reviewers read the page. These notes, and the page changes they describe, came after the results. No level, prediction,
band, attempt or outcome changed, and `protocol.json` and `blind-repairs.json` are byte-identical to their commits.

- The full recount against Change 1's second reference. Change 1 said the page reports both references; the first results stored only the
  repair that newly counts as restored on it. Counted in both directions, `bad_backfill_1` turns right, while `bad_backfill_3` and the two right
  self-test backfill repairs turn wrong, and every level passes those three and fails `bad_backfill_1`. On the blind repairs, outside totals on gold then
  pass 3 of 5 wrong repairs, and layer totals, outside totals on both, the fingerprint and the bundle 1 each; every level fails 1 of 7 right
  ones, and layer totals 2. The headline stays on the declared truth. This is not a further change to the protocol: it reports what Change 1
  promised. `backfill_second_reference` in `results.json` holds it, marked as computed after the results.
- The page groups the predictions by what they test: 8 to 11 are on the blind repairs and could have failed (2 held); 1 to 7 are on the clean
  reference, the no-ops and the self-test repairs, and 12 follows from the bundle's definition, as its own text says (8 of 8 held). The key figure now
  shows the 2 of 4. `predictions_on_blind` in `results.json` holds the grouping.
- Layer totals read bronze's `fare` or `fare_amount`, as the protocol said before the runs. A layer check that read only `fare` would have passed
  the no-op on the rename, and failed `renamed_column_1` and `renamed_column_3`, the right blind repairs that never write bronze. That follows
  from the check's SQL and was not run. The page says so beside the layer totals result.

## Notes added in the second review, after the results were seen

Two more independent reviewers read the page. These notes, and the page changes they describe, came after the results. No level, prediction,
band, attempt or outcome changed, and `protocol.json` and `blind-repairs.json` are byte-identical to their commits.

- The headline's zeros now carry their limit. All 5 wrong blind repairs left silver's per-day totals off the sender's (on 1, 6, 7, 4 and 8
  days: `late_file_3`, `renamed_column_4`, `bad_backfill_1`, `bad_backfill_2`, `bad_backfill_4`), so every level that reads silver against
  a correct source was bound to fail all 5, and these repairs cannot rank layer totals, outside totals on both, the fingerprint and the
  bundle. The bundle covers every column the reference compares, so its zero follows from its definition, as prediction 12 says; the page
  says "by construction" at that zero and on its chart row. `blind_wrong_cleaned_off_sender` in `results.json` holds the day counts, marked
  as computed after the results.
- The main limitation adds that the reference scores only silver and gold, and that `late_file_2`, counted as restored, left bronze without
  the late file, which only layer totals saw.
- The page names the method, not a person, as the subject of the self-test repairs, the second reference and the post-results grouping.
  Two notes in the section above now say "the self-test repairs" for the same reason. The design up to Change 1 keeps its first wording,
  since it is frozen.
