Skip to content

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).

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.

  1. Two more cli lab steps 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’s trajectory exactly as the coach receives it. The loader’s pure part is now profile::coaching::trajectory::trajectory_input, which load_trajectory and the step both call; nothing else changed. cli lab timeline takes 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’s profile::timeline_job::evaluate_all gives: the state, P_imp, the skipped points and every weakness_state_history row, with detector_version. Game ids are UUIDs, since the replay orders games of the same instant by id.
  2. A Node step for the web’s rules. apps/web/src/server/lab/bridge.ts has three steps, focus-target, focus-progress and trainer-sources, which call the exported functions of goal.ts and model.ts and nothing else; apps/web/scripts/lab-bridge.ts reads 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.
  3. One pure module moved. model.ts imported the free-capture check obviousCapture from profile/strengths.ts, which imports the database. The function moved, unchanged, to apps/web/src/server/profile/obvious-capture.ts, which strengths.ts and model.ts both import, so the trainer’s rules load without a database. The new file is part of keepCandidate’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.
  4. The lab image runs Node, with a bundle built from the lockfile. A webbridge stage of lab/Dockerfile, from the pinned node:22.23.5-slim image, installs the web’s dependencies with pnpm install --frozen-lockfile --filter "@ci/web..." and bundles lab-bridge.ts with esbuild (apps/web/scripts/build-lab-bridge.mjs, the version already in the lockfile, now a direct dev dependency) into one ES module with no node_modules. The lab stage copies the Node binary and the bundle; rematch_lab runs $REMATCH_WEB_BRIDGE (node /usr/local/lib/rematch/lab-bridge.mjs). Compose and CI pass the repository as the repo build context.
  5. One schema, one envelope. Every new message is in lab/schemas/lab-v1.schema.json, additively: the existing messages and golden files are untouched, so schema_version stays lab-v1. Every response carries schema_version and algorithm_id; the Node step reads the id from the committed manifest, as the app does.
  6. 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 in lab/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 of compute, contract and evaluate_all), the web test (apps/web/test/lab-bridge.test.ts, also against direct calls of the four functions, and that the bridge imports only goal.ts, model.ts and 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.
  1. 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.
  2. Run tsx in the lab image with the web’s node_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.
  3. Node’s own type stripping. Rejected: the web’s relative imports end in .js and its JSON import has no import attribute, so the sources do not run as they are.
  4. Port goal.ts and the trainer’s selection to Rust. Rejected: two implementations, or the app’s code moved for the lab (#125).
  5. 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.
  • 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’s trajectory_weaknesses picks them (at most three, the step refuses more: the estimates with priority > 0, by priority then key, each with prior_of); the timeline’s evaluation baselines (an analyse fact); the focus’s peer median is offered only when the weakness is compared with peers and is not W5 (focus/index.ts candidate), and a trainer candidate’s sources are the weakness series whose exemplar plies hold it, plus blunder (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.ts or obvious-capture.ts changes the bundle at the next lab image build; the golden files and the manifest catch it.
  • EXP-0001’s SOURCE_PATHS hold backend/crates/bench/src, which this change touches: backend/crates/bench/src/lab_rules.rs is new, backend/crates/bench/src/lib.rs declares it and backend/crates/bench/src/lab.rs shares check_version. None of it is on the bench run’s path, so lab/source-reviews.json records the three files under this ADR (ADR 0109 Amendment 1) and the arena compares algo-4.0.1 with algo-4.0.0 on the same players.
  • The lab image build needs the repository context and network access for pnpm, as the web image does.