Skip to content

fix(save-session): a failed last-entry read must not read as "(no previous entry)" (#251) - #275

Merged
fdaviddpt merged 1 commit into
mainfrom
fix/251-tail-failure-overwrites
Jul 31, 2026
Merged

fdaviddpt merged 1 commit into
mainfrom
fix/251-tail-failure-overwrites

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

What

save-session.sh Step 2 built the summarizer's dedup context with

[ -n "$LAST_LINE" ] && tail -n +"$LAST_LINE" "$MEMORY_FILE" > "$TMP" || echo "(no previous entry)" > "$TMP"

A && B || C is not if/else. When the guard holds and tail fails, C runs as well and its > overwrites whatever B wrote. A read failure was therefore rendered as a factual claim about now.md — rc 0, nothing logged, no marker.

Reproduced before fixing, with a tail shim that fails only that one invocation: now.md held a real entry and the prompt said there was none.

Why it matters

prompts/save-session.prompt.txt gives the previous entry to Haiku for two decisions — do not repeat it, and return SKIP when the span is the same work. A false empty removes both anchors at once, so the model writes the previous span back into now.md as new. That is the duplication #142/#224/#250 close from the other end, reached through the prompt rather than through the file.

Three states, not two

state sent to the summarizer
no ## header yet (or no now.md) (no previous entry) — unchanged
now.md exists but is not readable (previous entry unavailable — …) + ERROR log
the read itself failed (previous entry unavailable — …) + ERROR log

The marker is written for a model, not a person: it says the field is missing rather than empty, and states the consequence (earlier work may already be recorded) so a failed read is not treated as a fresh buffer. It deliberately does not push toward SKIP — nudging a model to skip on missing context spends a real span to protect the prompt.

The failure chosen

The save still completes, rc 0. Failing here would trade the loud bug for the quiet one: an entry written without dedup context is visible in now.md and recoverable; a span that never reaches memory is neither. Same trade the NDC tail arm at Step 8 already makes.

What else that block did wrong

grep -n '^## ' … | tail -1 | cut -d: -f1 returned cut's exit status, and cut succeeds on empty input — so grep failing on an unreadable file was indistinguishable from grep finding no header. The same false empty, one line earlier, and it would have survived a fix aimed only at the &&/||. grep now runs for its own status (1 = no match, >1 = error) and the last line is taken with parameter expansion, removing tail and cut from that path rather than checking them.

Correction to the issue's second half

The issue and the brief describe > truncating before tail writes as a second defect. It is not reachable: $TMP_LAST_ENTRY comes straight from mktemp and is always empty, so the truncate destroys nothing. The partial-write concern is real but its only cause is C overwriting B — one defect, not two. A test pins the partial case anyway, since the fix's handling of it is a deliberate choice (discard the partial, send the marker).

Tests

tests/test_last_entry_read_failure_251.py — 7 tests, RED before the fix on the 4 that assert the new behaviour (the 3 guards for the legitimately-empty states passed before and after, which is the point of having them). Uses the repo's existing PATH-shim idiom from test_ndc_tail_failure_no_truncate.py, narrowed to tail -n +N <now.md> so tail -1 elsewhere still runs the real binary; command -p without exec for the dash-on-Ubuntu reason documented there.

STUB_LAST_ENTRY_SNAPSHOT added to the shared build-prompt stub — env-gated, no effect on existing tests. It is the only way to observe the last-entry file as the summarizer receives it; the script's cleanup trap unlinks it.

Full suite: 1223 passed, 42 skipped locally (bash 3.2, macOS). bash -n and shellcheck clean on the edited region.

Not in this PR

Two more A && B || C instances in scripts/log.sh (268, 332), noted in the issue comment. There the branches concatenate, so the caller gets the value followed by the default — a different and arguably worse consequence, and they are config readers with their own callers. Left for the sweep the comment proposes; claude-supertool#665 (shellcheck SC2015) would catch all three at edit time.

Closes #251

…s entry)"

`A && B || C` is not if/else: when `tail` failed, the fallback ran too and
its `>` overwrote the partial read, so a read error reached the summarizer
as a factual claim about now.md — rc 0, nothing logged.

Three states now, not two: no header yet, now.md unreadable, read failed.
Only the first renders as "(no previous entry)"; the other two send an
explicit unavailable marker and log an ERROR. The save still completes —
losing a span to protect the prompt is the wrong direction.

The `grep | tail | cut` pipeline above it reported cut's status, which
succeeds on empty input, so a grep error was indistinguishable from no
match. grep is checked on its own; tail and cut are gone from that path.

Closes #251

Co-Authored-By: Max <noreply>
@fdaviddpt
fdaviddpt force-pushed the fix/251-tail-failure-overwrites branch from 714ee79 to 419b110 Compare July 31, 2026 16:27
@fdaviddpt
fdaviddpt merged commit b632877 into main Jul 31, 2026
12 checks passed
@fdaviddpt
fdaviddpt deleted the fix/251-tail-failure-overwrites branch July 31, 2026 16:37
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.

A failed tail renders as "(no previous entry)" — save-session.sh:211 overwrites its own output on error

1 participant