0115. The lab calls the trajectory, the timeline and the web's rules
Source:
docs/decisions/0115-the-lab-calls-the-web-rules-through-node.md. Generated at build time; edit the source file.
Date: 2026-11-26. Status: accepted (lane labbridge, branch feat/lab-web-bridge, issue #125). Builds on ADRs 0104 (one implementation), 0105 (the manifest) and 0106 (the lab and cli lab).
Context
Section titled “Context”Four experiments wait on rules the lab cannot call. EXP-0015 needs the trajectory verdict (backend/crates/profile/src/coaching/trajectory.rs) and EXP-0014 the change states (backend/crates/profile-core/src/timeline/), both Rust with no cli lab step. EXP-0011 needs the weekly target and its progress (chooseTarget and focusProgress in apps/web/src/server/focus/goal.ts) and EXP-0012 the trainer’s choice of positions (SOURCES, primarySource, keepCandidate and isHeldOut in apps/web/src/server/train/model.ts), both TypeScript in the web app. ADR 0104 allows one implementation of each rule, so the lab may not write its own in Python, and porting the two TypeScript files to Rust for the lab would either leave two copies or move the app’s code for a reason no experiment has asked for.
Decision
Section titled “Decision”- Two more
cli labsteps over the production functions.cli lab trajectory(bench::lab_rules::trajectory) takes each player’s rated games, as the loader reads them (the player’s own stored rating, the result from their side, the analysis status, accuracy and blunders), the top weaknesses with their priors, and a seed, and returns the CoachingInput’strajectoryexactly as the coach receives it. The loader’s pure part is nowprofile::coaching::trajectory::trajectory_input, whichload_trajectoryand the step both call; nothing else changed.cli lab timelinetakes series points, the peer benchmark and the evaluation baselines (ADR 0101), adds the baselines as the build does (with_model_baselines) and returns, per series, what the build’sprofile::timeline_job::evaluate_allgives: the state,P_imp, the skipped points and everyweakness_state_historyrow, withdetector_version. Game ids are UUIDs, since the replay orders games of the same instant by id. - A Node step for the web’s rules.
apps/web/src/server/lab/bridge.tshas three steps,focus-target,focus-progressandtrainer-sources, which call the exported functions ofgoal.tsandmodel.tsand nothing else;apps/web/scripts/lab-bridge.tsreads the request on stdin and writes the response on stdout. The web’s own objects (Target,Progress) keep their camelCase, as the Rust steps keep the CoachingInput’s. - One pure module moved.
model.tsimported the free-capture checkobviousCapturefromprofile/strengths.ts, which imports the database. The function moved, unchanged, toapps/web/src/server/profile/obvious-capture.ts, whichstrengths.tsandmodel.tsboth import, so the trainer’s rules load without a database. The new file is part ofkeepCandidate’s rule, so the manifest digests it (recommendation.source_digest). The behaviour is identical: the web’s tests of the strengths and the trainer pass unchanged and the replay goldens are byte-identical, so it is a patch,algo-4.0.1. - The lab image runs Node, with a bundle built from the lockfile. A
webbridgestage oflab/Dockerfile, from the pinnednode:22.23.5-slimimage, installs the web’s dependencies withpnpm install --frozen-lockfile --filter "@ci/web..."and bundleslab-bridge.tswith esbuild (apps/web/scripts/build-lab-bridge.mjs, the version already in the lockfile, now a direct dev dependency) into one ES module with nonode_modules. The lab stage copies the Node binary and the bundle;rematch_labruns$REMATCH_WEB_BRIDGE(node /usr/local/lib/rematch/lab-bridge.mjs). Compose and CI pass the repository as therepobuild context. - One schema, one envelope. Every new message is in
lab/schemas/lab-v1.schema.json, additively: the existing messages and golden files are untouched, soschema_versionstayslab-v1. Every response carriesschema_versionandalgorithm_id; the Node step reads the id from the committed manifest, as the app does. - Determinism and the proof of one implementation. No step reads a clock or draws an unseeded number: the trajectory’s bootstrap seed is in the request (production’s is
trajectory:<account id>:<time class>). Each fixture inlab/fixtures/gives its golden file byte for byte, checked from three sides: the Rust test (backend/crates/cli/tests/lab.rs, also against direct calls ofcompute,contractandevaluate_all), the web test (apps/web/test/lab-bridge.test.ts, also against direct calls of the four functions, and that the bridge imports onlygoal.ts,model.tsand the manifest) and the lab test (lab/tests/test_lab_v2.py, through the bundle). The lab test also swaps both binaries for a stand-in that answers what no rule would; every Python wrapper must return that answer, so one that computed the rule itself fails.
Options considered
Section titled “Options considered”- Run the Node step in the web image. Rejected: the lab’s Python calls a subprocess in its own container, and reaching another container would need the Docker socket. The web image also carries the whole app.
- Run
tsxin the lab image with the web’snode_modules. Rejected: hundreds of megabytes of dependencies for four functions, and the image would depend on the host’s install. The bundle holds only what the steps import (about 0.9 MB) and is built from the frozen lockfile. - Node’s own type stripping. Rejected: the web’s relative imports end in
.jsand its JSON import has no import attribute, so the sources do not run as they are. - Port
goal.tsand the trainer’s selection to Rust. Rejected: two implementations, or the app’s code moved for the lab (#125). - Pass the trajectory’s pre-game ratings and current rating in. Rejected: which rating is pre-game (chess.com’s is the previous game’s) and which is current are the loader’s rule; the step takes the stored ratings and calls
trajectory_input.
Consequences
Section titled “Consequences”- EXP-0015 and EXP-0014 can register now; EXP-0011 and EXP-0012 wait only for Tier 2’s cohort (EXP-0004). Each registration names the step it calls.
- What stays the caller’s, because the production code reads it from the database: the trajectory’s games in the loader’s
(played_at, id)order (the step refuses another) and its top weaknesses as the builder’strajectory_weaknessespicks them (at most three, the step refuses more: the estimates withpriority > 0, by priority then key, each withprior_of); the timeline’s evaluation baselines (ananalysefact); the focus’s peer median is offered only when the weakness is compared with peers and is not W5 (focus/index.tscandidate), and a trainer candidate’s sources are the weakness series whose exemplar plies hold it, plusblunder(train/store.ts). Both are database reads; the lab passes the same inputs, and a registration that depends on them says so. - A change to
goal.ts,model.tsorobvious-capture.tschanges the bundle at the next lab image build; the golden files and the manifest catch it. - EXP-0001’s
SOURCE_PATHSholdbackend/crates/bench/src, which this change touches:backend/crates/bench/src/lab_rules.rsis new,backend/crates/bench/src/lib.rsdeclares it andbackend/crates/bench/src/lab.rssharescheck_version. None of it is on the bench run’s path, solab/source-reviews.jsonrecords the three files under this ADR (ADR 0109 Amendment 1) and the arena comparesalgo-4.0.1withalgo-4.0.0on the same players. - The lab image build needs the repository context and network access for pnpm, as the web image does.