0106. Algorithm science: pre-registered experiments and a Python lab that drives the Rust algorithm
Source:
docs/decisions/0106-algorithm-science-lab.md. Generated at build time; edit the source file.
Date: 2026-11-23. Status: accepted (lane science, branch feat/science-lab, the owner-approved plan “put the algorithm on a scientific footing”). Builds on ADRs 0100, 0101 and 0104. Number 0105 is the algorithm manifest (lane feat/algorithm-manifest). Its rule that an experiment on an older version is historical and never rerun is replaced by ADR 0109: decided experiments stay live.
Context
Section titled “Context”ADR 0104 made the algorithm versioned and deterministic, and ADR 0100 gave it a ground truth, but nothing said how a change to it is decided. ADR 0101’s floor (RANK_MIN_EXCESS_PER_GAME = 0.02) was argued well, and still chosen after the first smoke run had been seen; the benchmark’s thresholds (recall at 2 of 0.8, coverage in [0.65, 0.95]) have no sample-size reasoning behind them, and one detection in the smoke suite moves recall by 20 points. Three open questions need statistics we have not written: the calibration of the 80% ranges (#13), whether fixed priors should give way to pooling across players (#11), and a Tier 2 design that does not leak the future into the past.
The owner asked for a lab in Python, “but not offline: just use our algorithm as it is in Rust from Python, so we have the lab still in version control and we keep track of our findings”, and for pre-registered experiments before any behaviour change.
Decision
Section titled “Decision”- Pre-registration is required for a behaviour change, which is any change that can move the replay goldens or a Tier 1 KPI (a major version, ADR 0104): a threshold, the ranking, a detector, a prior, a protocol. It ships only after an experiment, registered with
docs/experiments/TEMPLATE.mdbefore anything runs: the question, H0 and H1, the algorithm versions, one primary metric and its ground truth, the minimum effect that matters, an a-priori power analysis, the design (the player is the unit; paired seeds; blocks by rating band and flaw type), the analysis plan (effect sizes with 80% and 95% intervals, the multiple-comparison correction, diagnostics), the decision rule and the sha256 of the analysis code. A comparison ships when the two-sided 97.5% interval of the one primary effect lies wholly on the better side and no guarded KPI crosses its registered non-inferiority margin; it is rejected when the interval lies wholly below the minimum effect; otherwise one follow-up is allowed, with the candidate unchanged (the two looks split the 5% error, which is why the interval is 97.5%). The power analysis simulates that rule. A measurement study decides by whether its 95% interval lies inside a registered target band. Refactors, comments and copy changes need none.docs/SDLC.md, the pull request template and an issue form (experiment.yml, labelexperiment) carry the rule. - One implementation, driven from Python. The lab never re-implements the algorithm.
cli lab analyse|profile|rankis a versioned JSON interface (stdin to stdout, schemalab-v1inlab/schemas/lab-v1.schema.json, every response stamped withschema_versionandalgorithm_id) over the production code:analyseruns theanalyse-gamehandler with the profile hook andbuild-profilein a throwaway database (the bench harness, ADR 0100) and returns the facts the profile is built from;profilecallscompute_estimates;rankcallsrank_weaknesses, the ranking floor, the contract mapping, the coach’sselect_focusesanddrills_for. The logic lives inbench::lab, socligains no dependency.analyseproves its facts complete: their profile must equal theweakness_estimatesrows the build stored.rankkeeps a weakness with no exemplar position, which the CoachingInput builder drops; that is the one documented difference. - The lab is
lab/, Python 3.12 managed by uv, with every package pinned exactly (pymc 6.3.2, arviz 1.3.0, statsmodels 0.15.0, scipy 1.18.1, pingouin 0.7.0, scikit-learn 1.9.1, polars 2.1.0, matplotlib 3.11.2, pydantic 2.14.3, numpy 2.5.3, pandas 3.0.6, jsonschema 4.26.0, pytest 9.2.0), the lock committed, andexclude-newerseven days back.scripts/check-pypi-age.mjsapplies the crates’ and npm’s rule (ADR 0031): every locked version at least 7 days old, exact pins, PyPI only. Everything runs inlab/Dockerfile, which takescliand Stockfish from a Rematch build context and compiles no Rust; nothing runs on the host’s Python.rematch_labwraps the bridge in pydantic models that a test keeps equal to the schema, field for field. - Findings are in version control. Each experiment is
lab/experiments/EXP-NNNN/(pre-registration,analysis.py,results.json, figures, the critic’s review, the decision);lab/findings.mdlogs every decision, negative and inconclusive ones included; a shipped change cites its EXP id inalgorithm/CHANGELOG.md. - The rules are checked, not assumed.
.github/workflows/lab.yml(actions pinned by SHA, a read-only token) runs on pull requests that touchlab/or the algorithm’s crates, and weekly: the age rule, the contract tests (the fixture through the bridge equals the golden files byte for byte), and every committed experiment run twice. With the full history, it fails when the pre-registration oranalysis.pywas not committed strictly beforeresults.json, when sections 1 to 10 of the pre-registration changed after the results, whenanalysis.pylost its pre-registered sha256, when an input does not matchinputs.sha256, when two runs give differentresults.json, or when a run differs from the committed file while the experiment’salgorithm_idis the current one. An experiment that ran on an older version is historical: checked, never rerun on an algorithm it did not measure. Experiments draw randomness only fromrematch_lab.experiments.seed(exp_id, name). A Bayesian fit whose diagnostics fail is a failed result. - Methods from vetted skills. Nine of K-Dense’s Scientific Agent Skills (MIT; the pymc skill Apache-2.0, statsmodels and scikit-learn BSD-3-Clause) are vendored in
.claude/skills/science/, pinned to commitc21d13b:statistical-analysis,statistical-power,experimental-design,pymc,statsmodels,scikit-learn,scientific-critical-thinking,peer-reviewandhypothesis-generation. EverySKILL.mdand script was read before vendoring. The install steps, the network citation fetch and an OpenRouter figure step are removed, each change listed in the skill and inSOURCE.md. A fresh critic with the last two skills reviews each pre-registration and each result.
Options considered
Section titled “Options considered”- Re-implement the algorithm in Python for speed and convenience. Rejected: two implementations drift, and a finding about the copy says nothing certain about the product.
- PyO3 bindings now (
backend/crates/rematch-py, built with maturin). Not now: it adds third-party crates (a coordinator decision) and a second build. The JSON interface is fast enough for experiments of hundreds of players; bindings come with their own ADR if speed needs them. - Notebooks. Rejected: their hidden state and execution order defeat a byte-for-byte rerun. One script per experiment.
- Install the whole skills catalogue, or install it from upstream at run time. Rejected: many skills call remote services or need keys, and an unpinned install changes under us. Nine local skills, read and pinned.
- Pre-registration for every change. Rejected: a refactor’s proof is the replay goldens (ADR 0104), and an experiment there measures nothing.
Consequences
Section titled “Consequences”- The next behaviour change waits for its experiment. The first four are on the v0.3 roadmap: EXP-0001 sizes the benchmark, EXP-0002 measures the calibration of the 80% ranges (#13), EXP-0003 compares partial pooling with the current priors (#11), and EXP-0004 pre-registers the Tier 2 design.
- An experiment ages: once a version ships, the older experiments are historical, checked for their registration, freeze and inputs but not rerun.
algorithm_idis the build’s manifest id (common::algorithm::id(), ADR 0105), andrematch_lab.algorithm()returns the whole manifest (cli algo show --json). The golden files store a placeholder for the id, so a manifest bump alone does not move them. Exposingselect_focuses_ofchanged a file underai/src/validator, which moveslanguage.validator_digest, so this lane bumps the algorithm to 1.1.0 with no output change (algorithm/CHANGELOG.md).analysecovers chess.com rapid games, as the harness does. Tier 2’s Lichess games need the platform and time class as inputs, a schemalab-v2.- The bench crate exposes its steps (
Throwaway,memo_engines,insert_game_with_clock, the harness’sanalyseandbuild), andaiexposesselect_focuses_ofandallowed_drills_of; no behaviour changes. - Python stays out of every product image. The lab image is built only by the lab workflow and the opt-in
labcompose profile. - The vendored skills drift from upstream by design; the monthly sweep compares the pinned commit with upstream, and an update is a pull request that re-reads every changed file.
Amendment 1 (2026-11-24): external inputs
Section titled “Amendment 1 (2026-11-24): external inputs”EXP-0007 reads a snapshot of the Lichess puzzle database (1,177,486,537 bytes decompressed), which is too large for git and which Lichess replaces every month at the same URL. The owner asked that only the sample’s ids and checksums be committed.
- An input may be external. A line of
inputs.sha256may nameexternal:<path>, a file in the data directory ($REMATCH_DATA_DIR, by default the checkout’s git-ignoreddata/);analysis.pyreads it throughrematch_lab.experiments.external_input. When the file is present, its sha256 is checked like any other input’s. - No data, no rerun. When an external input is absent, as in CI,
python -m rematch_lab.experiments checkprints that the experiment is not rerun and why, and its other rules (registered first, frozen sections, the analysis hash, the compute record) are still checked. The byte-identical rerun is then the maintainer’s, on the machine that holds the snapshot, and the experiment’sdecision.mdrecords it. - The snapshot is named by its checksum, with its URL, download date, size and sha256 in section 7 of the pre-registration, so a copy can be verified wherever it comes from.
Option considered: committing the sample’s rows (CC0 data, a few megabytes) so CI could rerun it. Not now: the owner chose ids and checksums only. Archiving the snapshot as a release asset would let CI fetch it by checksum; that is a follow-up, not part of this amendment.