Repository navigation
fix(capture-gap): store capture evidence per session, not in one slot - #211
Merged
Merged
Conversation
The #200 check compared `tmp/capture-alive` — a single, last-write-wins session id — against the second-newest transcript. That assumes SessionStart means a new session id was minted. `/clear` does not mint one. It fires SessionStart with source=clear while the transcript, the session id and the .jsonl all stay the same, so the current session is still the newest file and capture-alive already holds the current id (that session had been making tool calls for hours). The computed "previous session" is a genuinely older one, the ids cannot match, and the warning was structurally guaranteed regardless of capture health — once per /clear, forever. Same for compact and fork. The issue proposed gating on SessionStart's `source`. Not taken: ca-sringert reproduced the same warning with no same-session restart at all — a short session captured by the next session's recovery block, which never touches capture-alive, while last-save.json recorded it saved 72/72. A source filter silences the loudest instance and leaves the store unable to answer the question it is asked. So the store changed shape. capture-alive.d/<session-id> markers, membership-tested, with three independent sources accepted as evidence: the marker (PostToolUse ran — written pre-throttle, so it means wired, not saved), the legacy single slot (kept so the first run after an upgrade is not itself a false positive), and last-save.json via the session_was_saved helper now hoisted out of the recovery block so the two can no longer contradict each other about one session. Gaps are reported once per session id rather than re-announced on every restart. Weighted toward silence deliberately: a warning that cries wolf has no second chance to be believed, while a missed gap still has /remember:doctor behind it and the content itself still has the recovery block. The true positive is pinned — a previous session that ran tools and never fired PostToolUse still warns, with the store populated by an older session so the test cannot pass by the machinery merely being broken. Also closes the known limit recorded under 0.8.9: capture-alive being per project rather than per session, so two concurrent windows on one project could produce a spurious notice. Closes #206 Co-Authored-By: Max <noreply>
fdaviddpt
added a commit
that referenced
this pull request
Jul 28, 2026
…ript (#212) (#215) * fix(post-tool): take the session id from stdin, not the newest transcript (#212) post-tool-hook.sh derived the session it recorded for from `ls -t` over the project's transcripts, so with two sessions live in one project a tool call made by A could be recorded as B. Since #206/#211 that writes a persistent capture-evidence marker vouching for a session that did no work, which silences the gap warning for a session whose hooks really are dead. The id now comes from the `session_id` PostToolUse supplies on stdin, and the transcript that is measured is resolved from that same id: the line count is compared against a position keyed by it, so "newest file" was never what that use wanted either. Every way stdin can fail to answer — absent, empty, unparseable, no session_id, an unsafe path component, an id with no transcript here — falls back to the previous mtime behaviour unchanged. The read is never taken from a tty and is capped by `read -t 1`, because this fires on every tool call. The payload is re-exported as REMEMBER_HOOK_STDIN for after_post_tool hooks. Co-Authored-By: Max <noreply> * fix(tests): import pty inside the test that uses it Module-level `import pty` runs at collection time, before the file own skipif(win32) applies, so `termios` (Unix-only) failed the entire file on all four Windows legs instead of skipping it. Co-Authored-By: Max <noreply> --------- Co-authored-by: t <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The defect
session-start-hook.shdecided whether the previous session was captured by comparingtmp/capture-alive— a single, last-write-wins session id — against the second-newest.jsonl. That assumesSessionStartmeans a new session id was minted./cleardoes not mint one. It firesSessionStartwithsource=clearwhile the transcript, the session id and the.jsonlall stay the same. So the current session is still the newest file,capture-alivealready holds the current id (that session had been making tool calls for hours), and the computed "previous session" is a genuinely older one. The ids cannot match. The warning was structurally guaranteed regardless of capture health — once per/clear, forever. Same forcompactandfork.Why not the fix the issue proposed
The issue proposed parsing the stdin JSON and running the check only for
sourceinstartup/resume. I did not take it.@ca-sringert reproduced the same warning with no same-session restart involved: a short session that ended before crossing its own
delta_lines_trigger/save_secondsthresholds, captured whole by the next session's recovery block.save-session.sh's recovery path never touchescapture-alive— onlypost-tool-hook.shdoes — so the slot named an unrelated session whilelast-save.jsonrecorded 72 of 72 lines saved. With the shipped thresholds that is the ordinary case for a short session, not an edge case.A
sourcefilter silences the loudest instance and leaves the store still unable to answer the question it is asked. Worse, it would have made the store's defect harder to find, because the remaining reports would have looked like a different bug.Two further points against it, both worth recording:
resumedoes not reliably mint a new session id either, so the proposed allowlist would have kept the false positive on that path.session_idinstead of "second-newest by mtime" — the issue's §2 — does not fix/clearon its own. Exclude the current session and you still getcapture-alive== current id vs. aPREV_IDthat was never the slot's occupant. That the store or the trigger had to change is provable, and it is the store that is wrong.What changed
capture-aliveanswers "which session most recently made a tool call". The check asks "was session X captured". Those are different questions, and the answer for X is destroyed by the next writer.The store is now a per-session marker directory,
tmp/capture-alive.d/<session-id>, membership-tested. Three independent sources count as evidence, any one sufficing:last-save.json's per-session record, via thesession_was_savedhelper now hoisted out of the recovery block so the two can no longer drift and tell the user opposite things about one session.A gap is also reported once per session id rather than re-announced on every restart.
False positive vs. false negative
Weighted deliberately toward silence, because the two errors are not symmetric here.
A warning that cries wolf has no second chance to be believed, and this repo keeps clearing that exact failure class. A missed gap still has
/remember:doctorbehind it, and the content itself still has the recovery block directly above this check — which force-saves the previous session in the same hook run.But a detector that is silent when hooks really are dead is worse than none, so the true positive is pinned rather than assumed: a previous session that ran tools and never once fired PostToolUse — the #200 signature — still warns, and that test populates the store from an older session so it cannot pass by the machinery merely being broken.
Migration
Existing installs have a
capture-alivefile and nocapture-alive.d/. Keeping the legacy slot as valid evidence (source 2) is the migration: the first run after the upgrade behaves exactly as it did before, rather than reading its own empty store as a fault. Pinned bytest_the_legacy_single_slot_marker_still_counts_as_evidence, which asserts the pre-upgrade state explicitly.The store is pruned to the 200 newest markers on each
SessionStart— it lives in atmp/dir nothing else sweeps.Tests
Written first, run red, then implemented. Red, against the current script:
Green after:
14 passed.The central pin is deliberately store-agnostic and end-to-end — nothing writes a marker by hand. The previous session is captured by running the real PostToolUse hook, then the current session makes its own tool calls (the precondition
/clearguarantees), thenSessionStartruns. Under a single-slot store the second PostToolUse run destroys the first's evidence, which is the defect exactly.The two tests that pass in both directions are the regression pins, and that is their job:
test_a_real_capture_gap_still_warnsis what stops this fix degenerating into deleting the warning, and the migration test is what stops the upgrade being its own false positive.Also closes
The known limit recorded under
0.8.9— "capture-aliveis per project rather than per session, so two concurrent Claude Code windows on one project can produce a spurious notice". Per-session markers close it.Known residual, deliberately not fixed here
post-tool-hook.shstill derivesSESSION_IDfrom the newest.jsonlby mtime rather than from thesession_idon its stdin. Under two concurrent sessions in one project it can therefore write a marker naming the wrong session. Changing it means changing whatLATEST_JSONLrefers to for the line-count and throttle logic in the same script, which is a materially larger and riskier change than this fix, and it is not what #206 reports. Flagged rather than smuggled in.Suite
scripts/run-tests.sh: 906 pytest passed, 26/27 shell checks pass.The one failure is section 7,
save-session.sh --dry. It is pre-existing and environmental — a detached worktree atorigin/mainfails it identically, and this branch does not touchsave-session.sh. It needs a real transcript under~/.claude/projects/for the checkout's own path, which a fresh worktree does not have.Closes #206.