0105. The algorithm manifest, stamping and the replay suite
Source:
docs/decisions/0105-algorithm-manifest-stamping-and-replay.md. Generated at build time; edit the source file.
Date: 2026-11-24. Status: accepted (lane algo, branch feat/algorithm-manifest). Implements ADR 0104 decisions 2, 3, 4, 5 and the data half of 7 (issues #49 to #54 and #56). Spec: docs/superpowers/specs/2026-11-24-factual-deterministic-evaluation-design.md, sections 2 to 4 and 9.
Context
Section titled “Context”ADR 0104 decided that every recommendation comes from one versioned, deterministic algorithm, and that a stored row names the version that produced it. It left the mechanism open: where the id is computed, how a writer reads it without a dependency cycle (ai and profile are among the components), how the database learns a new manifest, what “byte-identical” compares, and how CI tells a refactor from a change in output.
Decision
Section titled “Decision”- The manifest is generated from the code, by import. A new workspace crate,
backend/crates/algo(workspace dependencies only), buildsalgorithm/manifest.json:ANALYSIS_PROTOCOL,ENGINE_NODES, the newENGINE_THREADS(1) andENGINE_HASH_MB(16), everyenrich::protocolconstant,PLAYER_GAME_METRICS_VERSION,DETECTOR_VERSION,DECISIVE_DW,T_WIN,PEER_RATING_WINDOW, the newRANKING_VERSION(rank-v1) withRANK_MIN_EXCESS_PER_GAME,MODEL_LOSS_PER_WINNING_EPISODEandMIN_GAMES,INPUT_VERSION,WEEKLY_INPUT_VERSION,prompt_set_hashof the five prompt sets,AiModel::ALLandANTHROPIC_VERSION. The values that live outside Rust are read from their files: the Stockfish release, asset and sha256 frombackend/Dockerfile, the Syzygy manifest’s sha256, the Maia pin and Elo clamp, and the TSTRAINER_VERSION(trainer-v1),FOCUS_VERSION(focus-v1) andPHASE_METRICS_VERSION. Constants with no name of their own are covered bysource_digest: the sha256 of each listed source tree (chess-core,engine,analysis,enrich,profile-core,profile,coaching-input,drills.rs, the trainer and focus files) and of the validator, with CRLF normalised to LF. - The id is
algo-<version>+<first 12 hex of the sha256 of the canonical JSON of {version, components}>, with sorted keys, no whitespace, and whole-number floats written as integers, soscripts/algorithm/check.mjscomputes the same bytes in Node.algo::VERSIONis the version. The first one isalgo-1.0.0. - Writers read the committed file, not the crate.
common::algorithmcompilesalgorithm/manifest.jsonin withinclude_str!and givesid(),manifest()andregister_cte(). The web reads the same file (apps/web/src/lib/algorithm.ts).backend/Dockerfileand its.dockerignorecopyalgorithm/into the build. - Stamping registers the manifest in the writer’s own statement. Migration
20261124000000_algorithm_versions.sqladdsalgorithm_versions (id, manifest, created_at)and a nullablealgorithm_idforeign key oncoaching_inputs,report_runs,weekly_reviews,coach_messages,focuses,drill_items,kpi_snapshot,weakness_estimates,weakness_state_historyandgame_metrics. Each Rust writer prefixes its statement withWITH algorithm_row AS (INSERT INTO algorithm_versions … ON CONFLICT (id) DO NOTHING). The foreign key is checked at the end of the statement, so it sees the new row, and a fresh database needs no set-up step. The worker also registers its manifest at start-up (db::algorithm::register), so in production the row is committed before any job runs: a first write inside a long transaction would otherwise hold the new row’s key until it commits. The web registers in the writer’s transaction (registerAlgorithm). A chat answer is stamped when it finishes, not when its pending row is created.position_evalsgets no column.NULLmeans “before versioning”, and nothing is backfilled. - Explicit engine settings. The pool sends
setoption name Threads value 1andsetoption name Hash value 16to every engine. Both equal Stockfish 19’s defaults:engine/tests/uci_audit.rsreads the defaults from the binary’sucianswer, shows the evaluations of four fixture positions are identical with and without the options, and drives a fake engine to assertucinewgamebefore everypositionandgo nodes N, never a time limit. The protocol stayssf19-n300k-v1. - The replay suite.
backend/crates/algo/tests/replay.rsimports the 19 fixture games (backend/fixtures/pgn, the Lichess sample, the Lichess API and chess.com month fixtures), runsanalyse-gamewith the profile hook,build-profileand the CoachingInput builder withai::output::drills::drills_for, twice, in two throwaway databases, with Stockfish at 3,000 nodes per search through the benchmark’s memo. The dump removes wall-clock columns (every*_at,*_sinceand*_msexcept the games’ own times), row ids, the PGN and the algorithm id (asserted apart), replaces UUIDs by natural keys and writes floats in their shortest round-trip form. The two runs must be equal, and equal tobackend/fixtures/replay/*.golden.*.REPLAY_UPDATE=1rewrites the goldens only when the major version is above the goldens’ header. Tablebases and Maia are off, so the bytes do not depend on a machine’s data directory. Both runs take about 13 s together. - The guard.
backend/crates/algo/tests/manifest.rsfails with a readable diff when the committed manifest differs from the code, and when the changelog has no entry for its id.apps/web/test/algorithm.test.tschecks the TS fields and digests.scripts/algorithm/check.mjs, in the CItsjob against the pull request’s base, requires a major bump for a changed layer 1 to 3 field or for new goldens, a minor bump for a language-layer change, and a patch bump for a digest-only change. - The ban.
backend/crates/algo/tests/ban.rsscansprofile-core,enrich,coaching-inputandprofileforrand::,thread_rng,SystemTime::nowandOffsetDateTime::now_utc, outside comments. The allow-list has one entry:profile/src/hook_impl.rs’srun_atfor the next build-profile job, a queue time and never a fact.apps/web/test/determinism.test.tsbansMath.random,Date.now()and an argument-lessnew Date()inserver/stats,server/train/model.ts,server/focus/goal.ts,server/overviewandserver/profile. A planted call fails both. - “Why this?” data.
apps/web/src/server/whyreturns, for a top weakness, a plan item, the focus and a drill, the stored counts, the 80% range, the comparison and its group, the points lost a game, the rule in words (“Not ranked: not enough evidence yet” below ADR 0101’s floor) and the algorithm link, read from the stamped rows only. The report, the focus card and the trainer show one quiet line, “About this recommendation: computed by algorithm 1.0.0”, linked to the version’s changelog entry. The disclosure itself is v0.5’s. cli algo.cli algo manifest [--write],cli algo show [ID|FILE] [--json]andcli algo diff A B [--json], where a side iscurrent, a file, or an id inalgorithm_versions.diffexits 3 when the manifests differ.algo::Manifestandalgo::diff::Changeare serde types, so other tools can read the same JSON.
Options considered
Section titled “Options considered”- A writer computes the id from the
algocrate. Rejected:algodepends onaiandprofile, which are writers, so it would make a cycle. - Register the manifest only at worker start-up. Rejected as the only path: every DB test would need a set-up step, and the web writes before a new worker may have started. Kept as well, for the reason in decision 4.
- No foreign key, a plain text column. Rejected: an id with no manifest behind it cannot be explained, and the spec names the reference.
- Digest only the files the spec lists. Rejected: an edit to a feature extractor in
profile-core/src/featureswould then change facts without moving any digest. Whole source trees make every edit at least a patch bump, and the goldens make a patch bump cheap to prove. - Goldens at the production 300,000 nodes. Rejected: about 1,100 searches per run would take minutes in CI. The node count only scales the searches; determinism and the pipeline’s code paths are the same, and the header records the count.
- The golden header carries the full id. Rejected: every patch would rewrite 1.7 MB of goldens. The header carries the version, and the rule is by major.
Consequences
Section titled “Consequences”- Every new coaching input, report run, weekly review, finished chat answer, focus, drill item, estimate, state-history row, KPI snapshot and game-metrics row names its algorithm.
- Any edit under a digested tree, comments included, needs
cli algo manifest --writeand at least a patch entry inalgorithm/CHANGELOG.md. That is the price of no silent change. - The goldens (about 1.7 MB, mostly
kpi_snapshot) are rewritten on every major bump. - We do not know yet whether the bytes hold on the CI runner as they do locally. The first CI run of the suite is that measurement (spec section 13).
- The CHANGELOG link points at
main. An entry made on a branch resolves once it merges.