Skip to content

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

Closed

Description

@abogartz

Symptom

test/repokey-path.test.ts (#92) fails depending on which test file ran before it. Two tests:

  • a session dir containing a space stamps repo= as an unquoted hash, repoPath stays raw
  • rows written before the hash (repo= was the path) stay visible through repoPath

Minimal repro (macOS and Linux, Bun 1.4.3) — order is the whole bug:

bun test ./test/update-targeting.test.ts ./test/repokey-path.test.ts
Expected to contain: "repo=39fb4455edb0"
Received: "ts=… type=learning scope=s content="the build runs from a folder with a space" repo=7f336c009f10 repoPath=/tmp/osi-upd-H5OLSA/repo"
(fail) … a session dir containing a space stamps repo= as an unquoted hash, repoPath stays raw
(fail) … rows written before the hash (repo= was the path) stay visible through repoPath
 19 pass / 2 fail

Reproduced on origin/main at 9caed07 (0.14.12). The same two files in the same order pass on the pre-#133 revision cf9b73c (21 pass / 0 fail), so this is a regression introduced by #133, not a pre-existing flake.

Root cause

#133's entry binding made session identity sticky, and test/update-targeting.test.ts never releases it:

  1. It setCurrent({ sessionID: "upd", … }) (test/update-targeting.test.ts:62) and loads the plugin with liveRoot = <osi-upd-*/repo>.
  2. Dispatching a tool now runs toolIds(context) → bindSessionProvisional(sid, instanceDir) (src/index.ts:361-363), so bySession["upd"] → <osi-upd-*/repo> is registered. Before fix(security): session-scoped reads, evidence credential scrub, viewer origin preservation (#127 #128 #129) #133 only host identity events populated that map, which is why the test never leaked.
  3. Its afterEach (test/update-targeting.test.ts:47-51) does not call resetState() or resetSessionScopes(), so both the ambient sid "upd" and its binding survive into the next test file (module-global bySession).
  4. test/repokey-path.test.ts's beforeEach only calls setRepoScope(null) (test/repokey-path.test.ts:31-36). It has no session reset, and it relies on the ambient module scope for both directions:
    • write: appendMemory resolves repoScopeFor(input.sessionID ?? getCurrent().sid) (src/memory.ts:273) → the stale "upd" entry outranks the setRepoScope(resolveRepo(projDir)) the test just set → the row is stamped with the dead osi-upd-* repo;
    • read: relevantKnowledge → ambient reader → same stale scope → the legacy row is filtered out, so the hit string comes back empty.

This is the same class as the two hygiene findings already fixed on #133 (injection-framing, negative-eval) — update-targeting.test.ts is the third instance and was missed. It passed CI on #133's own head, and a full local bun test, purely because of file ordering.

Verified fix (2 lines + 2 lines, defense in depth)

# test/update-targeting.test.ts
- import { setRepoScope } from "../src/repokey.ts";
+ import { setRepoScope, resetSessionScopes } from "../src/repokey.ts";
  afterEach(() => {
+   resetState();
+   resetSessionScopes();
    delete process.env.OPENCODE_MEMORY_DIR;

# test/repokey-path.test.ts   (relies on ambient module scope; should assert it owns it)
  beforeEach(() => {
    …
    setRepoScope(null);
+   resetState();
+   resetSessionScopes();

Confirmed on 9caed07: the failing order goes 19 pass/2 fail → 21 pass / 0 fail; the reverse order stays green; full bun test ./test/ stays 578 pass / 12 skip / 0 fail.

Two things worth deciding beyond the hygiene patch

  1. Order-independence of the gate command. This is the third occurrence of the same leak. A single global reset in a shared test setup (a bunfig.toml preload or a test/support/* helper that every file imports) would stop the whack-a-mole; per-file patches keep depending on someone remembering.
  2. Should a provisional binding outrank an explicitly set module scope? setRepoScope(x) followed by a write currently resolves through repoScopeFor(ambientSid), which honors a provisional guess (src/repokey.ts:143-149). Product-wise "session identity wins" is the intended rule, but a stale provisional guess silently defeating an explicit scope assignment is surprising to any caller — including out-of-plugin ones. Worth an explicit decision (and a doc line) rather than an accident of lookup order.

Blast radius

Activity

  1. added
    bug
    on Oct 28, 2026
  2. WhollyMindless commented on Oct 28, 2026

    @WhollyMindless
    Contributor

    Independent Windows confirmation for this issue: on #135's da64f63, Windows / Bun 1.3.10 reproduces the same ordered pair:

    bun test ./test/update-targeting.test.ts ./test/repokey-path.test.ts

    Unmodified: 19 pass / 2 fail, with the wrong osi-upd-*/repo stamp and missing legacy-row recall. With reset hygiene in both files: 21 pass / 0 fail, and type-checking passes. The Linux CI failure is on Bun 1.4.4, so this is separate from the earlier Windows export/version-skew failures.

    The complete tested patch is already on #135. It also clears session bindings in update-targeting's setup so the reused upd id cannot retain a previous test's workspace, and resets state/session scopes in the consumer's setup and teardown. No product code or assertions changed; no branch push from this review.

    My recommendation: land the test-isolation fix on main, then update #135 and rerun its gates. A shared test reset is a sensible durable follow-up, with this ordered pair retained as a validation case. Keep the provisional/session-vs-module precedence decision separate: the hygiene repair should preserve the session-isolation behavior we just verified, rather than changing production identity resolution to accommodate leaked test state.

  3. added a commit that references this issue on Oct 28, 2026
  4. abogartz-opencode-self-improvement commented on Oct 28, 2026

    @abogartz-opencode-self-improvement

    🎉 This issue has been resolved in version 0.14.13 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

  5. added a commit that references this issue on Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugreleased

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions