Skip to content

test: release session identity between tests (#149) - #151

Merged
abogartz merged 2 commits into
mainfrom
fix/149-test-isolation
Oct 28, 2026
Merged

abogartz merged 2 commits into
mainfrom
fix/149-test-isolation

Conversation

@abogartz

Copy link
Copy Markdown
Owner

Closes #149. Unblocks #135, whose required test check went red the moment gh pr update-branch reordered its files onto post-#133 main.

Root cause

#127's entry binding made session identity sticky: dispatching a tool runs bindSessionProvisional(sid, instanceDir) at flow entry (src/index.ts:361-363), registering that sid → the instance's workspace in bySession. That map is a module global, so it outlives a test file.

test/update-targeting.test.ts sets an ambient session (setCurrent({sessionID:"upd"}), line 62) and dispatches tools against its own liveRoot, but its afterEach released neither the ambient state nor the binding. The next file to rely on the ambient module scope then breaks in both directions:

  • write — appendMemory resolves repoScopeFor(input.sessionID ?? getCurrent().sid) (src/memory.ts:273), so the stale "upd" entry outranks a freshly called setRepoScope();
  • read — readerForAmbient() resolves the same stale scope and filters the rows back out.

That is why test/repokey-path.test.ts (#92) failed with repo=7f336c009f10 repoPath=/tmp/osi-upd-H5OLSA/repo — a stamp from a temp directory that had already been deleted.

Changes

  1. test/update-targeting.test.ts — release the identity the file takes (resetState() + resetSessionScopes() in afterEach). It is the leaker.
  2. test/repokey-path.test.ts — own the ambient scope it asserts on (resetState() + resetSessionScopes() in beforeEach). It depends on module scope for both its stamp and its read, so it must not inherit it.
  3. bunfig.toml + test/preload-isolation.ts — the durable net: clear both identity registries after every test, so runner file order cannot change an outcome. This is the third instance of the same leak (injection-framing, negative-eval, update-targeting); per-file patches keep depending on someone remembering to add one.

Evidence

check result
bun test ./test/update-targeting.test.ts ./test/repokey-path.test.ts before 19 pass / 2 fail (on main 9caed07)
same pair after, and reversed 21 pass / 0 fail both orders
pre-#133 revision cf9b73c, same pair & order 21 pass / 0 fail (confirms #133 introduced it)
full bun test ./test/ 578 pass / 12 skip / 0 fail
full suite in reverse file order 0 fail, 5 consecutive runs (3 with preload, 2 after the commit below)
bun test ./e2e/ 11 pass / 0 fail
bun run gate (tsc → biome → unit → real-host BDD) all green

Second commit: a latent race I hit twice

While iterating I hit hits.test.ts › a re-ingested row cannot inherit the reinforcement… fail twice (once inside the pre-push gate). Root cause is pre-existing and independent of #149: appendMemory stamps ts from the wall clock at millisecond resolution and only bumps it when that ts is already in the day file — and this test deletes the store between its two writes, so nothing forces the stamp forward and both rows share a millisecond whenever the prune is fast enough. expect(fresh.ts).not.toBe(original.ts) was a coin flip.

It never reproduced in 20 solo runs or 6 full-suite runs on pristine main, so I am not claiming this branch caused it — but a gate that fails at random is worse than a gate with a known gap, so the second commit crosses the millisecond boundary before the second write. The assertion itself is unchanged; hits.test.ts is now 0 failures in 20 consecutive runs.

Notes

Dispatching a tool binds the calling session to the plugin instance's
workspace at flow entry (#127), and that session -> repository map is
module-global. A file that binds a session and never releases it silently
changes the NEXT file's write stamp and read scope, so the outcome depends
purely on runner file order.

- update-targeting: release the ambient session and its binding in afterEach
  (it is the leaker: setCurrent("upd") + a tool dispatch against liveRoot).
- repokey-path: own the ambient scope it asserts on, in beforeEach — both
  appendMemory's stamp and the ambient reader resolve the calling session
  first, so a leftover sid outranks its setRepoScope().
- bunfig preload: clear both identity registries after every test so file
  order cannot change the outcome; this is the third instance of the leak
  (injection-framing, negative-eval, update-targeting) and per-file patches
  keep depending on someone remembering.

Repro before: `bun test ./test/update-targeting.test.ts
./test/repokey-path.test.ts` -> 19 pass / 2 fail on main 9caed07; after:
21/0, and green in the reverse order too. Full suite 578 pass / 12 skip /
0 fail forward and in reverse file order (3 consecutive runs); e2e 11/0;
tsc and biome clean.
appendMemory stamps `ts` from the wall clock at millisecond resolution and
only bumps it when that ts is already present in the day file. The
re-ingested-row test deletes the store between its two writes, so nothing
forces the second stamp forward and both rows can share a millisecond —
`expect(fresh.ts).not.toBe(original.ts)` becomes a coin flip on how fast
the prune runs.

Observed twice while working on this branch (once inside the pre-push gate).
The race is pre-existing and independent of #149, but it makes the gate
untrustworthy for everyone. Cross the millisecond boundary before the second
write; the assertion itself is unchanged.

hits.test.ts 0 failures in 20 consecutive runs; full suite clean forward x3
and in reverse file order x2.
@abogartz-opencode-self-improvement

Copy link
Copy Markdown

🎉 This PR is included in version 0.14.13 🎉

The release is available on:

Your semantic-release bot 📦🚀

@abogartz
abogartz deleted the fix/149-test-isolation branch November 19, 2026 05:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test-suite isolation: #127 entry binding leaks a provisional session scope across test files (repokey-path #92 red in some orders)

1 participant