Skip to content

test(infra): pin every in-memory mongod to the harness version + cache dir (TASK-149) - #1923

Merged
lilyshen0722 merged 1 commit into
mainfrom
kai/task149-mongod-version-agreement
Sep 26, 2026
Merged

lilyshen0722 merged 1 commit into
mainfrom
kai/task149-mongod-version-agreement

Conversation

@lilyshen0722

@lilyshen0722 lilyshen0722 commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Closes half (a) of TASK-149. Row: TASK-149, pod 6a8f6dc7a1dccf2e02f31015. Gate: @vera (single).

The defect, measured at f5fd55bc

git grep -A2 'MongoMemoryServer.create({' backend/__tests__ — eight call sites pass binary options, and six of them disagree with the harness:

file pinned
services/agentEnsembleService.test.js:40 7.0.11
services/dmService.agentDm.test.ts:36 7.0.11
services/dmService.test.ts:30 7.0.11
unit/cap/session-token.test.js:67 7.0.11
unit/models/AgentInstallation.wakePolicy.test.js:15 7.0.14, no downloadDir
unit/models/agentRegistry.manifestRuntime.test.js:72 7.0.14, no downloadDir

against mongoBinaryConfig.MONGO_BINARY_VERSION = '7.0.14'. Two of the three versions are visibly resident on this box (~/.cache/mongodb-binaries holds 7.0.14, 7.0.11, 6.0.14) — the split is not hypothetical.

Why it matters, and why this half goes first

globalSetup warms the harness binary once, before any worker exists, and assigns process.env.MONGOMS_DOWNLOAD_DIR so per-file create() inherits it. An explicit version option beats the env MONGOMS_VERSION, so a mismatched pin misses that warm-up and reaches MongoBinary.download() inside the parallel worker phase — the download-lock window globalSetup exists to serialise. Those four are the only population in the tree that downloads after workers start.

CI has no binary cache to hide behind: MONGOMS / mongodb-binaries has zero references under .github/workflows, and the only cache: key in the test job is setup-node's ~/.npm (tests.yml:61, :216). The comment this PR corrects claimed the pinned dir "survives across test runs in CI cache"; it does not.

Read out of the library ([email protected]), because the candidate order decides which suites download:

  • generateOptions (DryMongoBinary.js:76) puts env MONGOMS_DOWNLOAD_DIR into downloadDir — the env beats the passed option; worker processes inherit it from globalSetup:39.
  • The two precedences are opposite, and that decides which half is load-bearing (Vera 74507): getEnsuredOptions (:66) is opts.version || defaultVersion — the option beats the env — while generateOptions is resolveConfig(DOWNLOAD_DIR) || ensuredOpts.downloadDir — the env beats the option. So the version agreement is what closes the worker-phase lock; the explicit downloadDir this PR adds is inert on a normal unit run. It is not decoration: globalSetup returns early under INTEGRATION_TEST=true before exporting either variable, so on that path a suite resolves purely from its own options and its own downloadDir is what fixes the lock path. Arm 1 guards the unit path, arm 2 the integration path.
  • generateDownloadPath probes resolveConfig → homeCache → modulesCache → relative, each as combineBinaryName(dir, binaryName) (:184, :150), and the binary name carries the version (mongod-<arch>-<dist>-<version>, :139). So inheriting the warm directory does not rescue a mismatched version: the filename is a different one.
  • modulesCache is find-cache-dir({name:'mongodb-memory-server'}) = node_modules/.cache/mongodb-memory-server — a different directory from the pinned node_modules/.cache/mongodb-binaries. The pinned dir is reachable only as resolveConfig, i.e. through the env var. Verified on this box: find-cache-dir → .../node_modules/.cache/mongodb-memory-server; mongoBinaryConfig → .../node_modules/.cache/mongodb-binaries.
  • When every candidate misses, the download target is paths.resolveConfig (:253) and the lock is lockfilePath(downloadDir, version) = <pinned dir>/7.0.11.lock (utils.js:308). All four 7.0.11 suites therefore contend on one identical lock path — which is the .lock in the error string, and this run would print 7.0.11.lock.

Simulated cold runner (temp HOME, pinned dir holding only the 7.0.14 file, env exported, generatePaths probed directly — constructed, not CI): for 7.0.14 candidate 1 exists → resolves, no download; for 7.0.11 all four candidates miss → download() in that worker. The two 7.0.14-without-downloadDir suites are safe for a reason unrelated to modulesCache: candidate 1 already matches. After this PR all eight call sites inherit one version — which, because the option beats the env, is the half that decides whether a worker reaches download() at all — so every suite resolves from candidate 1 and no CI worker downloads on the unit path. The explicit dir rides along for the INTEGRATION_TEST path, where no env var is exported; read arm 2 as guarding that path, not as what closed the lock.

Honest label: the cold-runner consequence is a code read (above) plus the measured absence of any cache step, backed by a constructed simulation of a cold runner — I have no runner access, so it is a prediction about cold CI, not an observation of CI.

What changed

  • the six suites take version and downloadDir from mongoBinaryConfig's constants (the shape service/agent-profile-memory-write.test.js:26 already used);
  • mongodBinaryPinAgreement.test.js — a static guard with three arms: (1) no version string literal at any call site; (2) every options object passed to a call site names downloadDir; (3) the options must be written inline — a hoisted variable is refused rather than parsed. It asserts a call-site count first, so an empty scan cannot pass vacuously;
  • one comment corrected in mongoBinaryConfig.js.

Evidence

  • Node 22, PATH=/opt/homebrew/opt/node@22/bin:$PATH: 7 suites / 56 tests pass (guard + the six changed suites).
  • Mutation ledger — guard reddens under M1 literal reinstated, M2 downloadDir dropped, M3 scan root emptied, M4 the hoisted rewrite (see the table above); green on the clean tree. Now 7 suites / 57 tests.
  • eslint clean on the new file. The pre-existing parsing error in agentEnsembleService.test.js (line 274 at f5fd55bc, 276 after this diff) is untouched and unrelated.

Scope

Half (a) only. Half (b) — moving the 27 suites that own a create() onto testUtils.setupMongoDb — is deliberately not here: it is a 27-file change and it does not depend on this one.

A residual, measured and left for half (b) (Vera 74512; facts re-verified here rather than taken on report). On the INTEGRATION_TEST=true path neither MONGOMS_VERSION nor MONGOMS_DOWNLOAD_DIR is ever exported, so a call site there resolves purely from its own options — and a bare call site resolves the library's DEFAULT_VERSION with no shared dir, invisible to both arms, which only read call sites that pass options.

Reachable in principle, harmless in practice today, on two measured facts:

  • CI does set it — tests.yml:205 (INTEGRATION_TEST: "true"), on the Tier-1 service job. But that job runs npx jest --testPathPattern="__tests__/service" --forceExit --runInBand (tests.yml:231), and under --runInBand there are no parallel workers, so a download lock cannot be contended there by construction. Worst case on that path is a wasted ~160 MB fetch, not a flake.
  • The population is empty: under backend/__tests__/service/ three suites mention MongoMemoryServer — agent-profile-memory-write.test.js:25 calls it with options (covered by the arms here), uploads.signedurl.integration.test.js:8 is a docblock, clawdbot-e2e.test.js:42 is a comment. Zero bare call sites on the integration path, so a fourth arm would police nothing.

So no fourth arm, and no row: half (b) carries the note. If a suite under __tests__/service/ ever starts a bare create(), it resolves the library default with no shared dir and neither arm can see it.

The guard's own escape, found by the hold on the first head

The two-arm guard at 0bd74037 was blind to a hoisted call site. Demonstrated, not argued: rewriting a suite's call as

const opts = { binary: { version: '7.0.11', skipMD5: true }, instance: { … } };
mongoServer = await MongoMemoryServer.create(opts);

— the exact defect this row was opened for — left it 3/3 green, exit 0. The scan requires create( immediately followed by {, so hoisting removes the site from the population without touching either arm. Same blind-to-a-variant-shape class as create() vs create(, which is what produced the miscount that started this row.

Two nets close it:

  • arm 3 refuses the shape instead of parsing it (the options must be inline), and
  • the call-site floor is now 8 — the measured population (seven suites + utils/testUtils.js), not a number three below it. The floor's only job is to notice the scan going blind, and at 5 it cannot: under the hoisted rewrite above the count fell 8 → 7 and stayed green.

Escapes are now caught twice, which the mutation ledger shows rather than asserts:

state result
hoisted rewrite, guard as shipped 2 failed — the floor and arm 3
hoisted rewrite, arm 3 removed 1 failed — the floor alone
hoisted rewrite, pre-fix guard (floor 5, two arms) 3 passed, exit 0 — the escape the hold reported, reproduced
clean tree 4 passed
synthetic file holding all three shapes (bare call, clean literal call, one hoisted) exactly 1 failure, arm 3 only — the arm discriminates

The call-site floor stays a literal — 8, not derived (Vera 74509). A derived floor counts the call sites the same scan can see, so it moves with a blindness loss and stops being able to detect one: the vacuity it exists to prevent, reintroduced through the back door. It is a ratchet, the same shape as routeRateLimitGuard.baseline.json (edited by the PR that changes the population); half (b) lowers the literal in the same commit that retires the call sites, so the diff makes the count visible to its reviewer. Withdrawn: my suggestion to derive it.

Arm 2's implemented predicate is deliberately stricter than its old name: it refuses any options object whose region lacks downloadDir, including one carrying only instance. That is the safe direction (arm 1 cannot see a version that is not written inline) and it is kept; the docblock and the test name now say what the code does.

optionRegion still takes the first } after the call's {, which is the binary option block in both the inline and multi-line forms (8/8 call sites). If a future call site puts another option first, the region would be wrong — say so and I'll make it structural.

@samxu01
samxu01 force-pushed the kai/task149-mongod-version-agreement branch from 0bd7403 to 4765293 Compare September 26, 2026 14:27
…che dir (TASK-149)

Measured at f5fd55b: eight call sites pass `binary` options to
`MongoMemoryServer.create(`. Four pinned mongod 7.0.11 and two pinned 7.0.14
without `downloadDir`, against the harness's MONGO_BINARY_VERSION = '7.0.14' —
three versions in one run, none of the mismatches pointing at the shared cache.

Why the mismatch is the one that can download in the wrong window: globalSetup
warms the harness binary once, before any worker exists, and assigns
process.env.MONGOMS_DOWNLOAD_DIR so per-file create() inherits it. An explicit
`version` option beats the env MONGOMS_VERSION, and the candidate filename is
version-scoped (mongod-<arch>-<dist>-<version>), so a mismatched pin misses the
warm-up, falls past every candidate on a cold runner, and reaches
MongoBinary.download() inside the parallel worker phase — the download-lock
window globalSetup exists to serialise. generateOptions folds the env
DOWNLOAD_DIR into downloadDir, so all four 7.0.11 suites contend on one lock
path: <pinned dir>/7.0.11.lock. No workflow caches a binary: MONGOMS/
mongodb-binaries has zero references under .github/workflows, and the only
`cache:` key in the test job is setup-node's ~/.npm. (The comment this PR
corrects claimed the pinned dir "survives across test runs in CI cache".)

Half (a) of TASK-149 only — the version/cache-dir split. Half (b), moving the
27 suites that own a create() onto testUtils.setupMongoDb, is not in this PR.

- 6 suites: version + downloadDir now come from mongoBinaryConfig's constants
- new guard mongodBinaryPinAgreement.test.js, three arms: no version string
  literal at a call site; every options object passed to a call site names
  downloadDir; and the options must be written INLINE — a hoisted variable is
  refused rather than parsed, because arms 1 and 2 read the literal text after
  the call and a hoisted call site leaves the population silently (Vera's hold
  on this PR demonstrated the escape: 7.0.11 + no downloadDir behind a variable
  stayed green). The call-site floor is the measured population (8), not a
  number three below it: at 5 the same escape kept it green. If half (b)
  removes call sites, the floor moves with the change.
- the two precedences are opposite, and the docblock says so: an explicit
  `version` option BEATS the env (`getEnsuredOptions`), so the version agreement
  is what closes the worker-phase lock; the env `DOWNLOAD_DIR` BEATS the option,
  so the explicit downloadDir is inert on a normal unit run and decides the path
  only where the env is absent — `globalSetup` returns early under
  INTEGRATION_TEST=true before exporting either variable. Arm 1 guards the unit
  path, arm 2 the integration path. Nobody should read arm 2 as the fix.
- one comment corrected in mongoBinaryConfig.js

Node 22: 7 suites / 57 tests pass. Guard reddens under: literal reinstated (M1),
downloadDir dropped (M2), scan root emptied (M3), and the hoisted escape (M4 —
2 failed: the floor and arm 3); with arm 3 removed the floor still catches it,
and with the pre-fix guard (floor 5, two arms) the escape is green, exactly as
the hold reported. A synthetic file holding a bare create(), a clean literal
create({...}) and one hoisted site flags exactly one offender, so the arm
discriminates. The pre-existing eslint parsing error in
agentEnsembleService.test.js (line 274 on f5fd55b, 276 after this diff) is
untouched.
@samxu01
samxu01 force-pushed the kai/task149-mongod-version-agreement branch 3 times, most recently from ca8598c to c14cc61 Compare September 26, 2026 14:33
@lilyshen0722
lilyshen0722 added this pull request to the merge queue Sep 26, 2026
Merged via the queue into main with commit cb20121 Sep 26, 2026
24 of 36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant