Skip to content

post-tool-hook.sh: keep pipeline.shell read-position off the hot path (#353) - #389

Merged
fdaviddpt merged 2 commits into
mainfrom
fix/353
Aug 28, 2026
Merged

fdaviddpt merged 2 commits into
mainfrom
fix/353

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

Closes #353

Part 2 of #350, split from the #352 latency fix so the sidecar's own misreports risk gets its own argument.

What changed

save-session.sh, via pipeline.shell save-position (pipeline/shell.py::cmd_save_position), now also writes a per-session, bash-read-able position sidecar under tmp/position.<session_id>. scripts/post-tool-hook.sh's per-tool-call hot path reads it with a bash read builtin (no spawn) instead of forking python3 -m pipeline.shell read-position on every warm tool call once a save has landed for that session.

The disagreement design (the actual point of #353 being split out)

The sidecar is written strictly after last-save.json is committed via os.replace, never before -- so a crash between the two writes can only leave the sidecar holding the previous position, never a value ahead of the truth. That ordering is what makes a sidecar value greater than the current run's own transcript line count self-evidently wrong (corruption or a session-id collision, never a legitimate crash window), so the hot path bounds the sidecar against that count and treats any excess -- or a non-numeric value -- as a loud, logged disagreement, falling back to the pre-existing read-position spawn rather than trusting it or defaulting to a silent 0.

An mtime-based staleness check (bash -nt) was considered and rejected: this repo's own tests/test_hot_path_cost_pin_330.py already documents -nt as one-second granularity on macOS system bash 3.2.57, and the sidecar write lands in the same wall-clock second as last-save.json far too often for that to be a reliable signal on the very runs this feature is meant to help. Full reasoning in the maintainer's notes for this branch.

Self-review

An Explore reviewer and oss:auditor both ran against the first commit. Two real gaps they found were fixed in a follow-up commit on this branch: sidecar files were not being removed when last-save.json's own bounded eviction (_POSITION_SLOTS, #140) dropped a session, which could let a stale-but-in-bounds sidecar be trusted after the authoritative store had forgotten that session -- cmd_save_position now removes an evicted session's sidecar in the same call, pinned by a new test; and the non-numeric-sidecar test was missing the log assertion its out-of-range sibling already had, so a regression dropping just that warning would have shipped green -- assertion added.

Below the bar, not filed: the auditor also flagged that the SESSION_ID case guard gating the sidecar path (scripts/post-tool-hook.sh:502-505) has no test exercising its rejection arm. Confirmed the only call path: SESSION_ID is always the basename of a transcript file the hook itself just found on disk, or the already-validated STDIN_SESSION_ID -- never raw untrusted text here -- and the identical shape already exists untested a few lines up in the same file. No reachable caller would exercise the rejection arm, so this is left as-is rather than filed.

Tests

tests/test_position_sidecar_353.py (new, 9 tests): the write-side ordering guarantee (reproduced by forcing the second os.replace to fail, simulating a crash between the two writes), the per-session isolation, the eviction cleanup, and the read-side hot path against the real hook binary -- an agreeing sidecar skips the spawn, a missing/non-numeric/out-of-range sidecar all fall back to read-position and (for the latter two) log a loud disagreement. Every "must not fire" assertion is paired with a "must fire" control in the same fixture.

Full suite: 1684 passed, 43 skipped, twice (once per commit on this branch).

…as landed (#353)

save-session.sh (via pipeline.shell save-position) now also writes a
per-session, bash-`read`-able position sidecar (tmp/position.<session_id>),
written strictly after last-save.json is committed so a crash between the
two writes can only leave it stale, never ahead of the truth. post-tool-hook.sh's
hot path bounds the sidecar's value against the transcript's own line count and
falls back to read-position -- loudly logging the disagreement, never silently
trusting a bad sidecar or defaulting to 0 -- on any mismatch.

Part 2 of #350, split from the #352 latency fix so the sidecar's own
misreports risk gets its own argument. Credit to @oguzhanpisgin, who reported
both halves in #350 with the measurement matrix.

Co-Authored-By: Max <noreply>
…rage

Self-review (Explore reviewer + oss:auditor) on the first #353 commit found
two real gaps in the sidecar mechanism itself:

- last-save.json evicts the oldest session past _POSITION_SLOTS (#140), but
  the sidecar mirror was never cleaned up on eviction -- a stale sidecar
  still <= the current transcript's line count would be silently TRUSTED by
  post-tool-hook.sh's bounds check, which has no way to see that
  last-save.json itself has forgotten the session. cmd_save_position now
  removes the evicted session's sidecar in the same call, pinned by a new
  test that reproduces the eviction and asserts the sidecar is gone.

- the non-numeric-sidecar test asserted the fallback spawn but never the
  loud-warning log line its sibling (out-of-range sidecar) already checked,
  so a regression that silently dropped just that warning would have shipped
  green. Added the missing assertion.

Also: the header docstring's Binaries/COST sections and the block heading
above the read-position block still described the pre-#353 shape (python3
spawned unconditionally once a save had landed) -- updated to describe the
sidecar as the common path and read-position as the fallback, marking the
projected spawn/latency drop as REASONED rather than re-measured.

Co-Authored-By: Max <noreply>
@fdaviddpt
fdaviddpt merged commit 250d11f into main Aug 28, 2026
13 checks passed
@fdaviddpt
fdaviddpt deleted the fix/353 branch August 28, 2026 08:05
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: keep pipeline.shell read-position off the hot path (part 2 of #350)

1 participant