Skip to content

fix(post-tool): take the session id from stdin, not the newest transcript (#212) - #215

Merged
fdaviddpt merged 2 commits into
mainfrom
fix/212-session-id-from-stdin
Jul 28, 2026
Merged

fdaviddpt merged 2 commits into
mainfrom
fix/212-session-id-from-stdin

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

Closes #212.

What LATEST_JSONL turned out to want

It had two consumers asking different questions, and the variable answered a third: "which transcript was written to last".

Use What it actually wants Resolution
SESSION_ID → capture-alive.d/<id> marker, legacy slot the session this invocation is for stdin session_id
CURRENT_LINES → DELTA = CURRENT_LINES - LAST_LINE the transcript of that same session — LAST_LINE comes from pipeline.shell read-position "$LAST_SAVE_FILE" "$SESSION_ID", i.e. a position keyed by the id (#140) the same session's transcript
SESSION_ID → save-session.sh "$SESSION_ID" ditto ditto
the ls -t lookup itself "is there anything in this project to save at all" — genuinely not per-session; it also drives the slug-mismatch notice (#144) left as-is, and now also serves as the fallback

So there was no use that wanted "newest regardless of session" once the id changed — measuring one session's transcript against another session's saved position is the same defect as the marker, just arriving as a spurious fork or a session that silently never saves. Both now resolve from one place, $SESSION_DIR/$STDIN_SESSION_ID.jsonl, so they cannot disagree. The mtime pick stays, unchanged, underneath.

Degrade, never die

The fallback is the point of the change, not a footnote — this repo has twice traded a correctness fix for a silent total outage (#204, #129), and misattributing a tool call is a strictly smaller harm than capturing nothing. Falls back to the pre-#212 behaviour, unchanged, on: no stdin, empty stdin, malformed stdin, no session_id field, an empty id, an id that is not a safe path component, and an id naming a session with no transcript in this project. Each is a separate parametrised case, and each asserts capture still happened (marker written, save forked) rather than merely that the hook exited 0.

Reading stdin at all is the other outage risk, since this fires on every tool call — a blocking read is a hung agent, not a slow one. A tty stdin (hand invocation from a shell) is not read at all; the read is capped by read -t 1 (bash 3.2 has no sub-second -t). Both are pinned by tests that drive the hook with a pty and with a PIPE the parent never writes to or closes.

Two more constraints from the issue:

  • Path-component validation kept, and moved earlier: the stdin id faces the same case ''|.|..|*[!A-Za-z0-9._-]* guard the basename-derived id has always faced, at the point of entry rather than the point of use, because it is now also used to build a filesystem path. Pinned with a ../../..-style id that would land a marker outside the store.
  • Nothing else in the hook chain consumed stdin (checked session-start-hook.sh, user-prompt-hook.sh, bootstrap-dirs.sh, log.sh:dispatch, hooks.d/). This hook does now, so the raw payload is re-exported as REMEMBER_HOOK_STDIN — a hooks.d/after_post_tool script would otherwise find EOF where a payload used to be. The shipped tree has no after_post_tool hooks, but installs can.

The extractor is deliberately narrow shell rather than a python3/jq fork per tool call: the key must be followed by nothing but whitespace and a colon before the value's opening quote, so a session_id occurring inside tool_input is not mistaken for the field — and the result is treated as a heuristic, gated by both the charset guard and the transcript-existence check before anything acts on it.

Tests — red first

tests/test_post_tool_session_id.py, 15 tests. Red on origin/main (7eb94d5):

FAILED test_marker_lands_under_the_invoking_session_not_the_newest_transcript
FAILED test_the_line_count_measures_the_invoking_sessions_transcript
FAILED test_a_save_still_fires_for_the_invoking_session
FAILED test_the_saved_position_is_read_for_the_invoking_session
4 failed, 11 passed

The 11 that passed red are the fallback and no-hang pins — they encode today's behaviour on purpose, so they pass before and must keep passing after. The 4 that failed are the central pin from the issue: two transcripts in one project, the older belonging to the invoking session, asserting the marker, the line count, the fork decision and the position lookup all follow the invoking session.

Green after: 15 passed. Full suite: 945 passed, 41 skipped (6m47s, coverage 93.98%). Pushed with --no-verify after running the suite locally — the pre-push hook's output kills the rtk wrapper with SIGPIPE (141), which is not a test failure.

Not polling CI.

vsajan added 2 commits July 28, 2026 13:15
…ript (#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>
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>
@fdaviddpt
fdaviddpt merged commit 01c9565 into main Jul 28, 2026
12 checks passed
@fdaviddpt
fdaviddpt deleted the fix/212-session-id-from-stdin 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.

post-tool-hook.sh infers SESSION_ID from the newest .jsonl by mtime, so under concurrent sessions it can vouch for the wrong one

2 participants