Skip to content

fix(#116): capture gates and stamps the captured session, not the last setCurrent - #118

Merged
abogartz merged 2 commits into
abogartz:mainfrom
WhollyMindless:fix/capture-sid-threading
Oct 26, 2026
Merged

abogartz merged 2 commits into
abogartz:mainfrom
WhollyMindless:fix/capture-sid-threading

Conversation

@WhollyMindless

Copy link
Copy Markdown
Contributor

Fixes #116 — capture's repo stamp (and the whole gate evaluation) followed whichever session called setCurrent last, not the session being captured.

The race, in one paragraph

captureSession binds setCurrent({ sessionID: sid }) and then awaits — the context re-read and, decisively, generate.text. On a server hosting several workspaces, any other session's hook moves the module-global current during that await. Everything the capture then resolves follows the hijacker: gateContext() reads evidence/engagement/liveRoot for the wrong session, and appendMemory's choke point resolves the stamp via repoScopeFor(getCurrent().sid) — so a fact mined from repo A's transcript lands carrying repo B's repo/repoPath. Invisible to A; a quiet cross-repo leak into B. The setCurrent comment claimed the binding "so captures stamp (and gate against) that session's repo" — true only if nothing interleaves, which a single model call guarantees can happen.

The fix — thread the identity, don't read it back

  • GateContext.sessionID (gates.ts): known-session writers name their session. gateContext(sid) in index.ts binds liveRoot/evidence/engagement for that session; the getCurrent() fallback keeps the tool-hook paths bit-identical (they run inside their session's hook dispatch — threading them further needs per-callID plumbing, noted below).
  • AppendMemoryInput.sessionID (memory.ts): the single write choke point prefers the caller's session over the ambient current; unnamed writers (tools, viewer, restore) are unchanged.
  • captureSession threads its sid into runWriteGate. setCurrent stays for the synchronous stretches — it's just no longer load-bearing across the awaits.

The pin — deterministic, not timing-luck

test/capture-sid-race.test.ts interleaves via a new park seam in the harness's model stub (generate.park: the stub awaits a test-held promise inside the call — same precedent as #110's determinism rule, no sleeps). Sequence: compaction capture for session A parks mid-model-call → the test runs another session's setCurrent (session B, bound to a second workspace dir) → release → judge what landed.

  • Red (verified on 3589658): the capture blocks — the gate evaluated B's empty evidence — and while chasing the red we also caught the sibling shape with the seam un-parked: promoted but stamped with B's key (682ddf2210c0 where A's f677cf6e7244 was expected). Both shapes violate one contract, and the pin asserts the contract.
  • Both threading arms are individually kill-verified: mutating either the choke-point preference (input.sessionID ?? dropped) or the gateContext(sid) thread alone re-reddens the pin.
  • Control test pins the uninterrupted capture unchanged (row + own stamp + verdict evidence), and it caught one real thing while being written: the row lands a beat before the outcome evidence line — wait on the verdict record, not the row.

Suite 490 pass / 0 fail / 5 skip on Windows (matches your 490 from #113), tsc clean.

Not in this PR (deliberately)

The three tool-path runWriteGate sites (remember/update/import) still read gateContext() without a sid. They're the same disease one seam away — the tool hook dispatch carries sessionID, but the execute bodies get current via setCurrent from execute.before, so a per-callID thread (or reading the sid off the tool ctx) is the right home for it. Didn't want to widen this PR's blast radius on a path where the dispatch timing is much tighter than a model call; happy to follow up if you see a real window.

Assisted by Qwen3-flash-next

… the last setCurrent

captureSession binds setCurrent({sessionID: sid}) and then AWAITS - context
re-reads and, decisively, the generate.text model call. On a server hosting
several workspaces, any other session's hook moves the module-global current
during that await, and everything the capture then resolves follows the hijacker:
the gate judges its candidate against the other session's (empty) evidence, and
appendMemory's choke point stamps the row with the other session's repo. A fact
mined from repo A's transcript becomes repo B's memory - invisible to A, a
cross-repo leak into B.

Thread the identity instead of reading it back:
- GateContext.sessionID: known-session writers name their session; gateContext(sid)
  binds liveRoot/evidence/engagement for THAT session (getCurrent() fallback
  keeps the tool-hook paths bit-identical);
- AppendMemoryInput.sessionID: the choke point prefers it over the ambient
  current, unnamed writers (tools, viewer, restore) unchanged;
- captureSession threads its sid - setCurrent stays for the synchronous
  stretches, but it is no longer load-bearing across the awaits.

Pin (test/capture-sid-race.test.ts) interleaves deterministically via a new
harness park seam inside the model stub: red now (blocked under the hijacker's
evidence - and an unthreaded persistence variant stamps the wrong repo), green
here; each threading arm alone is kill-verified. Control test pins the happy
path unchanged. Suite 490/0/5 + tsc clean.

@abogartz abogartz left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

APPROVED at b40c03a — every body claim verified independently on macOS (Bun 1.4.3); all three threading arms re-killed by me, not taken on faith.

What I verified and how

  • The race is real and the fix lands where the contract says: gateContext(sid) binds liveRoot/evidenceCalls/hasGateCheck/hasEngagement for the named session; GateContext.sessionID flows into appendMemory's choke point (input.sessionID ?? getCurrent().sid); capture threads its sid. setCurrent stays for the sync stretches, no longer load-bearing across awaits — matches the body exactly.
  • Kill-verification, my own mutations (each restored after): (A) choke point back to repoScopeFor(getCurrent().sid) → red, row carries the hijacker's key (Expected 5b393bce5a35, Received 0baf7026d607); (B) appendMemory({...candidate}) dropping ctx.sessionID → red, wrong key again; (C) gateContext() instead of gateContext(sid) → red in the blocked shape (captured undefined — the gate judged the hijacker's empty evidence). Matches the two shapes the pin's comment describes.
  • Fallback is a true no-op for unnamed writers: GateContext.sessionID is undefined wherever gateContext() is called without an argument (tool hooks at index.ts:371/484/638, init.ts), so appendMemory's resolution is byte-identical to before; viewer/restore/import direct appends untouched.
  • Determinism of the pin: the park seam waits inside the model stub after logging the call — the interleave is causal, no sleeps on the critical path; hermetic temp root, resetSessionScopes() + resetCaptureCooldowns() in beforeEach (the #113 cross-file-state lesson applied). Control test pins the happy path and its completion wait is itself race-free.
  • Gates: full suite 498 / 0 / 11, tsc clean; CI all four green including test-windows (1m57s) on the rebased head — the earlier Windows red was the #117 flake, now fixed on main.

Non-blocking notes (follow-up material, not asks)

  1. Evidence sid= residue: record() still resolves the ambient current, so under exactly the hijack shape this PR fixes, the capture's auto_capture_* audit lines — and write_promoted from inside the gate — can carry the hijacker's sid= and land in its evidence stream. The durable artifact (the stamped row) is correct, so this is audit attribution only; same threading (an optional sessionID field on record) would close it.
  2. Tool-path windows: memory_remember/update/import keep ambient resolution — consciously deferred per the code comment ("needs per-callID plumbing"), and the windows are fs-sized rather than model-sized. Worth one tracking issue so #116's contract gets finished where it can be, same as this PR did for capture.
  3. Cosmetic: "AWITS" → "AWAITS" in the test header.

One commit, no scope creep: the pin's red legs are the issue's stated decision made concrete, and the fallback discipline means every non-capture writer is provably unchanged.

@abogartz
abogartz merged commit 3448ba0 into abogartz:main Oct 26, 2026
4 checks passed
@abogartz-opencode-self-improvement

Copy link
Copy Markdown

🎉 This PR is included in version 0.14.6 🎉

The release is available on:

Your semantic-release bot 📦🚀

abogartz pushed a commit that referenced this pull request Oct 26, 2026
#118 approval note 1. The durable rows got their session threading in #118;
the evidence lines the gate appends around them still read sid from ambient
getCurrent() - so under the exact interleave #118 pins, the paper trail an
operator greps first carries the HIJACKER's sid (write_promoted sid=ses-other
while the row itself is correctly repo A's). Same disease, audit limb.

- record() gains an explicit sessionID that outranks the ambient read; when
  the two disagree the ambient call belongs to the other session's flow, so
  it is dropped rather than borrowed (formatLine omits undefined).
  sessionID undefined (tool paths - #121) is a true no-op: the old read.
- runWriteGate threads ctx.sessionID through every audit line via rec/recBlocked
  helpers - all 19 sites, one threading seam, killable as one line.
- pins: the hijack test's write_promoted + chain-mates must carry ses-capture
  (red before: sid=ses-other, exactly his observed residue); the control pins
  threading behavior where ambient coincidentally matches; a direct-gate
  blocked-shape test covers the recBlocked arm with the ambient poisoned.
- header typo from the review: AWITS -> AWAITS (note 3).
abogartz added a commit that referenced this pull request Oct 26, 2026
fix(evidence): gate audit lines name the session the gate runs FOR, not the ambient current (#118 note 1)
abogartz-opencode-self-improvement Bot pushed a commit that referenced this pull request Oct 26, 2026
## [0.14.9](v0.14.8...v0.14.9) (2026-10-26)

### Bug Fixes

* **evidence:** gate-authored lines name the session the gate runs FOR ([392642f](392642f)), closes [#118](#118) [#118](#118) [#118](#118) [#121](#121)
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.

capture: sid is bound via setCurrent then awaited — write-time identity races the live session

2 participants