Skip to content

fix: sidecar hot-path trust check now actually consults last-save.json (#403) - #416

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

fdaviddpt merged 2 commits into
mainfrom
fix/403

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

Closes #403.

The #353 position sidecar's hot-path trust check only bounded a sidecar's value against the CURRENT run's own transcript line count -- never against last-save.json itself, even though the two log lines that fire on a disagreement both claimed the comparison consulted it. An orphaned sidecar (its session evicted from last-save.json, and the best-effort cleanup unlink in pipeline/shell.py's cmd_save_position failed) that still happened to fall inside that bound was silently trusted, resuming from a position the authoritative store no longer recognised and re-summarizing the same span.

scripts/post-tool-hook.sh now also requires the session id to be a key in last-save.json's own sessions map before trusting the sidecar, read via bash's fork-free $(< file) substitution (no subprocess) so the hot path this feature exists to keep cheap stays cheap -- checked against the pinned cost budgets in tests/test_hot_path_cost_pin_330.py, both of which still pass.

This makes an orphaned sidecar detectable regardless of why it survived eviction -- including the Windows case the issue names as reasoned-not-observed (os.remove against a file another process holds open raises PermissionError, an OSError subclass the cleanup loop swallows). The fix does not depend on which platform caused the failed unlink; it closes the gap at the read side instead.

Self-review found and fixed one real gap

Spawned review (Explore, correctness/cross-platform) returned NO FINDINGS. Spawned audit (oss:auditor) returned two findings against the first commit:

  • Confirmed and fixed (follow-up commit): the membership check did a substring match over the WHOLE raw file, not scoped to the sessions object. last-save.json's own schema always carries the fixed top-level keys session and line alongside sessions, so a session id literally equal to one of those field names would false-match even with an empty sessions map. Reproduced red, then scoped the match to the sessions object's own text and added a pinned regression test, green.
  • Argued down: a claim that $SESSION_ID could reach the case pattern unfiltered via the transcript-basename fallback. Traced: the actual gating filter (scripts/post-tool-hook.sh:521) tests the real, final $SESSION_ID (post-fallback-resolution) against [A-Za-z0-9._-]+, not a separate unfiltered variable, and the new code only runs once that filter already passed -- verified empirically that a session id containing a glob metacharacter never reaches the new block at all.

Testing

TDD: new tests in tests/test_position_sidecar_353.py (an orphaned-sidecar-rejected case, its positive control, and a fixed-field-name-collision case) were shown red before each corresponding fix and green after. Both hot-path cost-budget tests in tests/test_hot_path_cost_pin_330.py still pass, confirming the added check stays within the pinned per-tool-call cost budget. Full suite: 1729 passed, 43 skipped, no failures.

Docs

No user-facing behavior to document -- this is an internal hot-path correctness fix with no new flag, config or observable output. README.md (the repo's only docs_targets entry) does not describe the sidecar trust mechanism at all, and still doesn't need to. Changelog fragment: changelog.d/403.fixed.md.

…e session to still be in last-save.json (#403)

The two log lines that fire on a sidecar disagreement both claimed the
comparison consulted last-save.json; it only bounded the sidecar against
the current run's own transcript line count. An orphaned sidecar (its
session evicted from last-save.json, cleanup unlink best-effort and
failed) that still fell inside that bound was trusted, resuming from a
position the authoritative store no longer recognised and
re-summarizing the same span. The check now also requires the session
id to be a key in last-save.json's own sessions map, read via bash's
fork-free $(< file) substitution so the hot path stays inside its
pinned cost budget.

Co-Authored-By: Max <noreply>
Self-review (spawned oss:auditor) found the fix landed in the previous
commit did a substring match over the WHOLE raw file text, not just
last-save.json's "sessions" object. cmd_save_position's payload also
always carries the fixed top-level keys "session" and "line" -- so a
session id literally equal to one of THOSE field names would
false-match and be wrongly trusted even with an empty "sessions" map,
the exact failure #403 exists to close. Scope the match to the text
between the "sessions" key and its own closing brace (values in that
object are always bare integers, never braces, so the object's own
closing brace is unambiguous) before testing membership. Reproduced
red (session id "session", empty sessions map, false-trusted) then
green with a new pinned test; both hot-path cost-budget tests in
tests/test_hot_path_cost_pin_330.py still pass.

Co-Authored-By: Max <noreply>
@fdaviddpt
fdaviddpt merged commit a402ac1 into main Aug 28, 2026
13 checks passed
@fdaviddpt
fdaviddpt deleted the fix/403 branch August 28, 2026 22: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.

An orphaned position sidecar is trusted, because the hot path bounds it against the transcript rather than against last-save.json

1 participant