Skip to content

fix: session-start-hook.sh no longer prunes a live delivery record on an unreadable clock - #418

Merged
fdaviddpt merged 3 commits into
mainfrom
fix/402
Aug 28, 2026
Merged

fdaviddpt merged 3 commits into
mainfrom
fix/402

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

Closes #402

What was wrong

The #393 grace-window sweep in scripts/session-start-hook.sh reads a delivery record's own mtime and the current clock (_remember_date +%s) to decide whether an absent transcript means a session is over. The mtime read already had a could-not-tell guard (-z then continue, meaning "keep"). The clock read did not: an empty or non-numeric _remember_date +%s was coerced to 0, and the -gt 0 gate on it then silently fell through to rm -f -- deleting a live session's delivery record, the exact case the grace window exists to protect.

The judgement call

The issue offered two shapes: give _remember_now its own -z-then-continue guard, or drop the -gt 0 gates entirely. I chose a third, narrower option: fold the guard into a single case on _remember_now that continues on either an empty read (clock command failed) or a non-numeric one (clock command exited 0 but printed garbage) -- both are could-not-tell in the same way. A plain -z-only guard, mirroring the mtime read as it stood, would not have caught the non-numeric case; dropping the -gt 0 gates entirely would have removed a check that costs nothing and is not implicated in this defect. A readable clock's behavior is unchanged.

Reachability

The issue was honest that it had not established this is reachable inside session-start-hook.sh specifically (only that _remember_date returning empty is a shape this codebase has met, elsewhere, via a different mechanism). I re-checked and could not establish reachability here either: _remember_date_builtin_ok in lib-clock.sh refuses the bash builtin for %s unconditionally, so this call always shells out to the real date binary, and no concrete failure mode for a plain date +%s on this call path was found. The fix stands as defense-in-depth, consistent with the file's existing #332 convention of guarding every arithmetic sink regardless of whether a failure has been observed there.

Self-review found a sibling gap

The spawned reviewer found that the mtime read's own existing guard had the identical problem one level down: it caught a fully-empty stat read via -z, but still coerced a non-empty, non-numeric one to 0 before the same -gt 0 gate -- so a garbage stat output could reach that gate as a confirmed, comparable age and be pruned on a healthy clock. Fixed in a second commit in this same branch, with its own must-not-prune test (TestUnreadableMtimeGarbageDuringPruneSweep), reproduced red by temporarily reverting only that hunk before restoring the fix.

Tests

tests/test_delivery_record_pruning_373.py gained two test classes, each pairing a must-not-prune case (fake date/stat returning empty or garbage output for exactly the call this sweep makes, deferring to the real binary otherwise) with the existing must-fire control (an old record with a healthy clock is still pruned) already in the file, so a harness that always "keeps" would be caught. pytest tests/test_delivery_record_pruning_373.py -q -> 10 passed.

Docs

No README change -- the fix restores an already-documented invariant (keep, never guess-delete) rather than changing any user-observable behavior. A changelog fragment (changelog.d/402.fixed.md) covers both commits.

…ve delivery record when the clock is unreadable

_remember_now (the "is this record inside GRACE_MIN" clock read) coerced
an empty or non-numeric _remember_date +%s to 0 and gated on -gt 0, so a
failed clock read fell through to rm -f -- the opposite of what the
mtime read three lines above already does for the same failure shape.
Give _remember_now the identical could-not-tell-means-keep treatment.

Fixes #402

Co-Authored-By: Max <noreply>
#402's clock fix just closed

Found in self-review of the #402 fix: _remember_stale_mtime's existing
guard caught a fully-empty stat read but still coerced a non-empty,
non-numeric one to 0 before the -gt 0 gate, letting a garbage stat
output be treated as a confirmed zero-age record and pruned on a
healthy clock -- the same could-not-tell-means-keep violation #402
fixes for the clock read, one guard away. Folded into #402's own
changelog fragment since it is the same class, same file, same commit.

Refs #402

Co-Authored-By: Max <noreply>
… force it defensively

CI's test_no_test_fakes_the_clock_on_path_without_disabling_the_builtin
guard (tests/test_prompt_hook_spawns.py) flagged
test_delivery_record_pruning_373.py's fake `date` for not mentioning
REMEMBER_NO_PRINTF_T: on bash >= 4.2 lib-clock.sh can answer from a bash
builtin instead of forking `date`, so a fake `date` on PATH can go
unused there.

Re-derived rather than trusting either side: `_remember_date +%s` is not
one of the formats that can reach the builtin -- `_remember_date_builtin_ok`
in lib-clock.sh refuses `%s` unconditionally -- confirmed by reading the
case pattern and by forcing _REMEMBER_PRINTF_T=1 locally (simulating
bash >= 4.2) and observing the fake `date` still gets called for `+%s`
while a non-%s format attempts the builtin instead. So the two new
must-not-prune tests were not vacuous on bash >= 4.2: the guard's own
textual scan just cannot see that, by design (it flags any file that
shims `date` without naming the hatch, not just the ones that need it).

Documented the reasoning in _fake_date_bin's docstring, and set
REMEMBER_NO_PRINTF_T=1 in both call sites anyway, so these tests do not
keep depending on the %s carve-out remaining true forever.

Refs #402

Co-Authored-By: Max <noreply>
@fdaviddpt
fdaviddpt merged commit cbf37d7 into main Aug 28, 2026
24 of 25 checks passed
@fdaviddpt
fdaviddpt deleted the fix/402 branch August 28, 2026 23:39
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.

The #393 grace window degrades could-not-tell to prune when the clock is unreadable, the opposite of what the code two lines above it does

1 participant