Skip to content

fix(capture-gap): store capture evidence per session, not in one slot - #211

Merged
fdaviddpt merged 1 commit into
mainfrom
fix/206-capture-gap-false-positive
Jul 28, 2026
Merged

fdaviddpt merged 1 commit into
mainfrom
fix/206-capture-gap-false-positive

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

The defect

session-start-hook.sh decided whether the previous session was captured by comparing tmp/capture-alive — a single, last-write-wins session id — against the second-newest .jsonl. 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, capture-alive already 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 for compact and fork.

Why not the fix the issue proposed

The issue proposed parsing the stdin JSON and running the check only for source in startup/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_seconds thresholds, captured whole by the next session's recovery block. save-session.sh's recovery path never touches capture-alive — only post-tool-hook.sh does — so the slot named an unrelated session while last-save.json recorded 72 of 72 lines saved. With the shipped thresholds that is the ordinary case for a short session, not an edge case.

A source filter 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:

  • resume does not reliably mint a new session id either, so the proposed allowlist would have kept the false positive on that path.
  • Selecting the previous transcript by session_id instead of "second-newest by mtime" — the issue's §2 — does not fix /clear on its own. Exclude the current session and you still get capture-alive == current id vs. a PREV_ID that 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-alive answers "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:

  1. the marker — PostToolUse ran for that session; written pre-throttle, so it means wired, not saved;
  2. the legacy single slot, kept as valid evidence;
  3. last-save.json's per-session record, via the session_was_saved helper 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:doctor behind 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-alive file and no capture-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 by test_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 a tmp/ dir nothing else sweeps.

Tests

Written first, run red, then implemented. Red, against the current script:

FAILED test_evidence_of_capture_survives_a_later_session_making_tool_calls
FAILED test_a_session_captured_only_by_recovery_is_not_flagged
FAILED test_the_same_gap_is_reported_once_not_on_every_restart
FAILED test_the_evidence_store_stays_bounded
4 failed, 10 passed

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 /clear guarantees), then SessionStart runs. 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_warns is 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-alive is 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.sh still derives SESSION_ID from the newest .jsonl by mtime rather than from the session_id on its stdin. Under two concurrent sessions in one project it can therefore write a marker naming the wrong session. Changing it means changing what LATEST_JSONL refers 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 at origin/main fails it identically, and this branch does not touch save-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.

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
fdaviddpt merged commit e14b244 into main Jul 28, 2026
12 checks passed
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]>
@fdaviddpt
fdaviddpt deleted the fix/206-capture-gap-false-positive branch September 26, 2026 20:46
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.

False "previous session was not captured" warning fires on every /clear

2 participants