ADR-R2-24. The analyse-game hook: per-game features at analysis time
Source:
docs/decisions/ADR-R2-24-analyse-hook.md. Generated at build time; edit the source file.
Date: 2026-11-22
Status: Accepted. Placeholder number; the controller renumbers R2 ADRs before merging. Task 31 (backend/crates/analysis/src/hook.rs, backend/crates/profile/src/{analyse_features,hook_impl,load,requeue}.rs). Related: ADR 0035 (analyse job fence), ADR-R2-13 (Maia-only difficulty), ADR-R2-14 (enrichment protocol), ADR-R2-15 (preview pass).
Context
Section titled “Context”A player’s profile is built from per-game rows: enriched move_features, game_metrics, game_openings and weakness_series_point. They must be written when a game’s analysis finishes, by code the R1 analysis crate cannot depend on (profile depends on analysis, not the reverse). The analysis itself must never be lost to a failure of this extra work, re-analysis must replace the rows, and R1’s fence, release and generation rules must still hold.
Decision
Section titled “Decision”The seam
Section titled “The seam”analysis::hook::{AnalyseHook, PreparedHook}.AnalyseGameDeps.hook = None(the default) keeps R1’s behaviour exactly; R1 code never touches the R2 columns or tables then.- The job calls
prepareafterbuild_move_records, while it still owns the game. Heartbeats keep running duringprepare(fenced, everyheartbeat_every): enrichment runs engines, and a silent run would look stale to the reconciler. A missed heartbeat cancels the run’s token and the attempt endsSuperseded. persistruns in the moves transaction, after the fenceddoneupdate and the moves insert, inside a savepoint.after_commitruns after thegame.analysedpublish.
Failure rules
Section titled “Failure rules”| Outcome | Game | Hook rows |
|---|---|---|
HookError::Cancelled (shutdown, lost lease) |
pending, job released (shutdown), as R1 |
none |
HookError::Cancelled after the deadline |
R1’s retryable timeout |
none |
HookError::Failed |
done with its moves |
none: every earlier hook row is deleted (clear_hook_rows), enrichment_protocol = NULL, preview deleted |
persist error |
done with its moves (savepoint rolled back) |
none: as for Failed |
| superseded | nothing written (fence) | none, no after_commit |
enrichment_protocol = NULL is what requeue_unenriched looks for, so the hook is always redone later. The earlier analysis’s move_features, game_metrics, game_openings and weakness_series_point rows are deleted in the same transaction (fix round 1, I1): they hang off games, not moves, so nothing else removes them, and the loaders would serve them as current. Whenever the game reaches done with a hook configured, its game_previews row is deleted in the same transaction as the done transition (R-13, T28 review). The final game.analysed is R1’s payload, {gameId, accuracy}, never preview: true.
What ProfileHook writes
Section titled “What ProfileHook writes”enrich_gamefor the player’s colour only (Task 25), thencompute_game_features:- book exit from the explorer of the player’s band (with T18’s band fallback); a read error or no theory evidence means no book exit, never a failure;
- phases recomputed with
detect_phases(fens, book_exit_ply.unwrap_or(20)); - the opening from the CC0 classification (
classify_line).game_openings.line_name/ecofall back to the platform’s values only when nothing is classified;line_key/line_kindstay NULL then (no series is keyed on a platform name); - the counted variants of assembly, observation, strengths and metrics; every skipped-input count goes to
job_metrics.stagesof theenrich-gamerow (T6, T8 and T9 hand-offs), next to Task 25’s stage timings andfeatures_ms.
- Rows are replaced, never appended:
move_features(delete + insert),game_metricsandgame_openings(upsert), the game’sweakness_series_pointrows (delete + insert). A re-analysis gives the same row counts. after_commitenqueuesbuild-profilededuped onprofile:<account>:<tc>withrun_at = now() + 30 s, so a batch of games builds once.
Requeue and the pending-Maia sweep
Section titled “Requeue and the pending-Maia sweep”requeue_unenrichedsetsdonegames withenrichment_protocol IS DISTINCT FROM 'enr-v1'topendingat tier 2 (scheduler priority 20/21, beta plan) withanalysis_attempts = 0, and enqueues them. It pages by keyset ongames_pkeywith an unfiltered window (as R1’s reconciler), pinned by a plan guard. Main evals come from the cache.- Difficulty is Maia only. With Maia down the moves stay
pending_maia.run_difficulty_sweepruns Task 25’s sweep and recomputes the metrics, opening and series rows of every game it changed, from the stored analysis (cached evals) and stored enrichment, without rewritingmove_features. The write happens underSELECT … FOR UPDATEof the game and only while it is stilldoneat the same generation and protocols; a game being re-analysed is skipped (its re-analysis recomputes it). - Durable recompute (fix round 1, I3). Migration20261121020400_games_profile_stale.sqladdsgames.profile_stale_atand a statement-level trigger onmove_features: any row going todifficulty_status = 'ok'marks its game in the same statement, so in the same transaction as the difficulty write.run_difficulty_sweeprecomputes the marked games (oldest first, 500 per run, partial indexgames_profile_stale_idx) and clears each mark in the recompute’s transaction, only if it was not set again meanwhile. A cancel, a DB error or a crash between the difficulty write and the recompute leaves the mark, and the next sweep recomputes the game. The hook clears the mark (it recomputes everything); so does a recompute that finds the game no longer current. - A recompute that cannot evaluate or decode the game deletes its derived rows and clears
enrichment_protocol(no stale rows served), sorequeue_unenrichedredoes it; a DB error keeps the mark. - The sweep runs as the recurring job
difficulty-sweep(every 10 minutes, deduped; a sweep with Maia down costs one/healthzprobe). The worker registers its runner and callsensure_difficulty_sweep_scheduledandrequeue_unenrichedat start (Task 35, ADR-R2-25).
Consequences
Section titled “Consequences”- An enrichment outage degrades to “analysed, not yet profiled”, never to a lost or failed analysis.
- A done game can be briefly
pendingagain after a requeue; its moves and evals are rewritten identically from the cache. build-profileis deduped onto a running job too: a game that finishes while its account’s build is running is only included by a later build (the next analysed game or sweep recompute enqueues one). Task 33 owns closing that gap (for example a re-enqueue when the build sees newer rows).moves.phasekeeps R1’s phases (opening until ply 20). The features use phases recomputed from the book exit, so an exemplar’smoves.phase(load_exemplar_moves) can differ from the phase its features used. Readers that need the feature phase recompute it withdetect_phases.