Skip to content

fix: quarantine, not lose, a span behind an unrecognised-envelope save (#450) - #457

Merged
fdaviddpt merged 1 commit into
mainfrom
fix/450
Aug 29, 2026
Merged

fdaviddpt merged 1 commit into
mainfrom
fix/450

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

What

scripts/save-session.sh advanced the saved read position past a span it never actually read whenever ExtractResult.envelope came back "unrecognised" -- a genuinely quiet 0-exchange session and a 0-exchange failure to read were saved the same way, so once a later build could finally parse that envelope (as #443 did for Codex), the span was already behind the marker and did not come back.

The design call (this was most of the issue)

Not advancing has its own failure mode: #147, where a permanently-unreadable session re-triggers the save path forever and never clears its cooldown. So the saved position still advances unconditionally on an "unrecognised" envelope -- that is what keeps #147's loop closed -- but the span is now also quarantined, in a new sidecar (unread-envelope.json, next to last-save.json, same directory, same bounded/evicted shape as the existing _POSITION_SLOTS position store) at the earliest line not yet actually read.

The next extraction for that session consults the quarantine and, when one exists, resumes from the quarantine point instead of the (already-advanced) saved position -- so a build that can now parse the envelope actually re-reads the missed span. pipeline.shell.cmd_save_position gained two new optional arguments, envelope and skip_lines: passing envelope="unrecognised" marks/keeps the quarantine (set-if-absent, so the point never creeps forward across repeated failed runs); passing any other envelope clears it, because a save with a recognised envelope means the span has now actually been read; and envelope=None (every existing caller, including the plain pipeline.shell save-position last.json id 99 form) leaves any quarantine another, envelope-aware caller set completely untouched -- so an old-style call can never silently erase it.

The three-reasons-for-zero question the issue raised: a genuinely quiet session (envelope recognised, 0 exchanges) is unaffected and still just advances, never quarantined -- pinned by a must-fire positive control in the new tests. A file this build's envelope sniffing cannot classify at all (sniff_file_envelope never finds a parseable first line, or finds one it does not recognise) is the case this quarantines. Corrupt/truncated JSON within an otherwise-recognised envelope was left alone: extract_messages already skips unparseable lines one at a time and keeps going, so a torn tail of an otherwise-normal Claude-Code or Codex transcript is not conflated with the unrecognised-envelope case at all -- it was never advancing past content it could parse.

Tests

tests/test_unread_envelope_450.py -- red (ImportError against the pre-fix tree) then green, six tests: the core quarantine-then-recover round trip, a must-fire positive control (quiet + recognised envelope advances and is never quarantined), quarantine monotonicity (earliest point wins), back-compat (envelope=None cannot erase another caller's quarantine), and eviction parity with the existing position-store bound. tests/test_shell.py updated for the two new optional save-position CLI arguments (one pre-existing assertion had to change to match the new default kwargs; a second test added for the populated-argument case).

Full suite: 1834 passed, 43 skipped (platform-gated, expected), 0 failed.

Closes #450.

Deferred, not fixed here

The self-review auditor found that read_unread_envelope collapses "the quarantine sidecar is corrupt or unreadable" and "nothing was ever quarantined for this session" into the same empty-dict return -- a torn write to unread-envelope.json (crash mid-write, disk fault) would silently re-lose an already-quarantined span, the same failure class this diff exists to close. This is real and reachable, but fixing it honestly needs a design decision (how to plumb a distinct "quarantine unreadable" signal through cmd_extract's existing KEY=VALUE shell bridge and into a bash-visible log line, given this python module's own stderr is discarded in the normal backgrounded save path) and would meaningfully widen this diff rather than ride along inside it. Reported for filing, not fixed here -- see the accompanying report's adjacent item.

#450)

`save-session.sh` advanced the saved position past a span it never actually
read whenever ExtractResult.envelope came back "unrecognised" -- a 0-exchange
quiet session and a 0-exchange failure-to-read were saved identically, so a
later build that could finally parse the envelope (as #443 did for Codex)
found the span already behind the marker.

The position still advances unconditionally on an unrecognised envelope --
leaving it in place re-triggers the save path forever (#147) -- but the span
is now also quarantined in a sidecar next to last-save.json
(pipeline.extract.mark_unread_envelope / read_unread_envelope), at the
earliest point not yet actually read. The next extraction for that session
resumes from the quarantine point instead of the advanced position, and
cmd_save_position clears the entry once a save with a recognised envelope
lands. `save-position` gains two optional, backward-compatible arguments
(envelope, skip_lines); omitted, they leave any existing quarantine alone.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@fdaviddpt
fdaviddpt merged commit 8b8745e into main Aug 29, 2026
13 checks passed
@fdaviddpt
fdaviddpt deleted the fix/450 branch August 29, 2026 10:12
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.

save-session.sh advances the read position past lines it failed to read, so a later fix cannot recover them

1 participant