Skip to content

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.

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.

  1. The manifest is generated from the code, by import. A new workspace crate, backend/crates/algo (workspace dependencies only), builds algorithm/manifest.json: ANALYSIS_PROTOCOL, ENGINE_NODES, the new ENGINE_THREADS (1) and ENGINE_HASH_MB (16), every enrich::protocol constant, PLAYER_GAME_METRICS_VERSION, DETECTOR_VERSION, DECISIVE_DW, T_WIN, PEER_RATING_WINDOW, the new RANKING_VERSION (rank-v1) with RANK_MIN_EXCESS_PER_GAME, MODEL_LOSS_PER_WINNING_EPISODE and MIN_GAMES, INPUT_VERSION, WEEKLY_INPUT_VERSION, prompt_set_hash of the five prompt sets, AiModel::ALL and ANTHROPIC_VERSION. The values that live outside Rust are read from their files: the Stockfish release, asset and sha256 from backend/Dockerfile, the Syzygy manifest’s sha256, the Maia pin and Elo clamp, and the TS TRAINER_VERSION (trainer-v1), FOCUS_VERSION (focus-v1) and PHASE_METRICS_VERSION. Constants with no name of their own are covered by source_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.
  2. 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, so scripts/algorithm/check.mjs computes the same bytes in Node. algo::VERSION is the version. The first one is algo-1.0.0.
  3. Writers read the committed file, not the crate. common::algorithm compiles algorithm/manifest.json in with include_str! and gives id(), manifest() and register_cte(). The web reads the same file (apps/web/src/lib/algorithm.ts). backend/Dockerfile and its .dockerignore copy algorithm/ into the build.
  4. Stamping registers the manifest in the writer’s own statement. Migration 20261124000000_algorithm_versions.sql adds algorithm_versions (id, manifest, created_at) and a nullable algorithm_id foreign key on coaching_inputs, report_runs, weekly_reviews, coach_messages, focuses, drill_items, kpi_snapshot, weakness_estimates, weakness_state_history and game_metrics. Each Rust writer prefixes its statement with WITH 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_evals gets no column. NULL means “before versioning”, and nothing is backfilled.
  5. Explicit engine settings. The pool sends setoption name Threads value 1 and setoption name Hash value 16 to every engine. Both equal Stockfish 19’s defaults: engine/tests/uci_audit.rs reads the defaults from the binary’s uci answer, shows the evaluations of four fixture positions are identical with and without the options, and drives a fake engine to assert ucinewgame before every position and go nodes N, never a time limit. The protocol stays sf19-n300k-v1.
  6. The replay suite. backend/crates/algo/tests/replay.rs imports the 19 fixture games (backend/fixtures/pgn, the Lichess sample, the Lichess API and chess.com month fixtures), runs analyse-game with the profile hook, build-profile and the CoachingInput builder with ai::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, *_since and *_ms except 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 to backend/fixtures/replay/*.golden.*. REPLAY_UPDATE=1 rewrites 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.
  7. The guard. backend/crates/algo/tests/manifest.rs fails 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.ts checks the TS fields and digests. scripts/algorithm/check.mjs, in the CI ts job 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.
  8. The ban. backend/crates/algo/tests/ban.rs scans profile-core, enrich, coaching-input and profile for rand::, thread_rng, SystemTime::now and OffsetDateTime::now_utc, outside comments. The allow-list has one entry: profile/src/hook_impl.rs’s run_at for the next build-profile job, a queue time and never a fact. apps/web/test/determinism.test.ts bans Math.random, Date.now() and an argument-less new Date() in server/stats, server/train/model.ts, server/focus/goal.ts, server/overview and server/profile. A planted call fails both.
  9. “Why this?” data. apps/web/src/server/why returns, 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.
  10. cli algo. cli algo manifest [--write], cli algo show [ID|FILE] [--json] and cli algo diff A B [--json], where a side is current, a file, or an id in algorithm_versions. diff exits 3 when the manifests differ. algo::Manifest and algo::diff::Change are serde types, so other tools can read the same JSON.
  1. A writer computes the id from the algo crate. Rejected: algo depends on ai and profile, which are writers, so it would make a cycle.
  2. 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.
  3. No foreign key, a plain text column. Rejected: an id with no manifest behind it cannot be explained, and the spec names the reference.
  4. Digest only the files the spec lists. Rejected: an edit to a feature extractor in profile-core/src/features would 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.
  5. 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.
  6. 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.
  • 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 --write and at least a patch entry in algorithm/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.