ADR-R2-23. Reference build storage: lifecycle guards and per-metric band fallback
Source:
docs/decisions/ADR-R2-23-reference-build-storage.md. Generated at build time; edit the source file.
Date: 2026-11-21
Status: Accepted. Placeholder number; the controller renumbers R2 ADRs before merging. Task 17 (backend/crates/refdata), with the Task 17 review ruling. Related: ADR-R2-10 (explorer build), ADR-R2-11 (band reference method), ADR-R2-13 (Maia-only difficulty).
Context
Section titled “Context”Reference data (the opening explorer and the band references) comes from quarterly builds over the CC0 Lichess dump. Profiles compare a player with players at their rating, so a wrong or half-built reference set misleads every user. A band × time-class cell can also be thin: some metrics have fewer than 10 players (low_sample) while others in the same cell are fine.
Decision
Section titled “Decision”Build lifecycle
Section titled “Build lifecycle”- Status flows
building→active→superseded. The dead ends arefailed(rows deleted) andunverified(--verify nonedevelopment builds; rows kept, never active). activate_build(andactivate_build_in, which runs inside the caller’s transaction) holds a per-kind advisory lock. It supersedes the old active build, deletes that build’s rows, and only then activates the new one, as two separate statements. They must be separate becausereference_builds_one_active_uqis a plain unique index and cannot be deferred.- Activation is refused in each of these cases:
- no SHA-256 recorded (R-9);
- any status other than
building(anactivebuild is a no-op); - a
source_monththat is not newer than the active build’s, so an old dump never replaces a newer one. Months are validated asYYYY-MMat creation, so string order is month order.
- Every write is guarded. Each of these requires the build to be
building, and getsNotBuildingotherwise: explorer flush and prune; reference-game insert and status update; observations; band-reference rows; the SHA (exactly 64 lowercase hex characters); and the difficulty model. A served build is therefore immutable, and a failed build cannot gain rows back. - Observations must belong to their game’s build (
BuildMismatch), and an upsert never moves a row between builds. delete_stale_unverifiedremovesunverifiedbuilds 30 days after they finished (UNVERIFIED_RETENTION), with their rows via the FK cascade.difficulty_modelis stored exactly as given.Nonestays NULL (ADR-R2-13 owner override).
Per-metric band fallback (review ruling, option a)
Section titled “Per-metric band fallback (review ruling, option a)”load_band_reference(band, tc) serves each metric independently, from the nearest band in the same time class whose row for that metric is not low_sample:
- The search goes at most 2 bands away (
MAX_FALLBACK_BANDS). Beyond that, the metric is absent. - Of two equally near bands, the lower one wins, because unknown players are more likely beginners (ruling Q2).
- A
low_samplerow is never served, not even to label it. - The result records the band each metric was served from (
ServedStat.band,served_from,is_fallback), so reports can say “compared with B2 players” honestly. - Rows whose metric this version does not know are ignored, so a cell holding only unknown metrics falls back like an empty one.
Nonemeans there is no active build, or no metric within reach.
Alternatives rejected
Section titled “Alternatives rejected”- Whole-cell fallback (the first implementation). It treated a cell as low-sample only when every row was, and then served that cell’s low-sample rows as reliable. In the review’s case (B3 with a low-sample W1 next to healthy metrics), it served B3’s thin W1 as a benchmark.
- Unlimited fallback distance. It would compare a B5 player with B0 players, which is not a meaningful benchmark.
Consequences
Section titled “Consequences”- T32 must either commit its band-reference and observation writes before
activate_build, or write and activate in one transaction withactivate_build_in. It treatsNotBuildingfrom a late worker as “already done”. It must also handle jobs whosereference_gamesrow was deleted by a later supersede. - T20:
flush_exploreradds counts, so it is not retry-safe. The caller makes each chunk idempotent, or fails the build and starts over. - Consumers (T12, T15, T33) read
LoadedBandReference::reference()for the statistics andserved_fromfor labelling. A metric missing from the map means there is no benchmark.