Repository navigation
Description
Code-read finding, main @ 2bb0d4f
scripts/save-session.sh ~603-629, inside the backgrounded NDC subshell, which runs under set +e:
if tail -c +$(( NDC_SRC_BYTES + 1 )) "$MEMORY_FILE" > "$NDC_TAIL" 2>/dev/null; then
NDC_KEPT=$(wc -c < "$NDC_TAIL" | tr -d ' ')
mv "$NDC_TAIL" "$MEMORY_FILE"
...
if [ "$NDC_KEPT" -gt 0 ]; then
printf '%s\n' "$(_remember_date +%Y-%m-%d)" > "$NOW_DAY_FILE" 2>/dev/null || true
else
rm -f "$NOW_DAY_FILE"
fi
[ "$NDC_KEPT" -gt 0 ] && log "ndc" "kept ${NDC_KEPT}b appended during compression"The mv result is never read. tail's is — carefully, with a whole else branch explaining why now.md is left untouched on failure (#173's defect (c)). The mv immediately after it has no such branch, and set +e means a failure does not stop the block either.
What happens when it fails
mv can fail for the ordinary reasons: ENOSPC on the destination, a read-only or full volume, EPERM, an unlink race on the temp. When it does, now.md still holds every byte it had, including the span that was just appended to today-*.md. But the code proceeds as though the commit happened:
NDC_KEPT == 0(the common case — nothing was appended during compression) →rm -f "$NOW_DAY_FILE". The NDC files compressed content by run date — after-midnight compression lands prior-day entries in the new day's today-*.md #141 day marker for content that is still entirely innow.mdis deleted. The next round re-stamps that content with whatever day it runs on, so a session that crossed midnight files yesterday's entries under today.NDC_KEPT > 0→NOW_DAY_FILEis overwritten with today's date, for anow.mdthat still contains the pre-compression content the old stamp described. Same misfiling, one round earlier.- Either way the log says
kept ${NDC_KEPT}b appended during compression, or says nothing at all whenNDC_KEPTis 0. Nothing anywhere records that the truncate did not happen.
So the outcome is: memory is not lost, it is misfiled, and the log reads like a clean run. That is the same signature as #142 and #225 — written to disk, then in the wrong place, with nothing logged — and it is the one class this file has otherwise been hardened against three times.
The tail-failure branch already documents the right answer for this exact situation ("whatever tail could not read is safer sitting in now.md than erased", and today-*.md may hold a duplicate). A failed mv lands in precisely that state and should take precisely that branch.
Fix
if mv "$NDC_TAIL" "$MEMORY_FILE"; then
...existing day-stamp + kept log...
else
rm -f "$NDC_TAIL"
log "ndc" "ERROR: mv failed, now.md left untouched and the day stamp not changed (today-${NDC_DAY}.md may now hold a duplicate of this span)"
fiThe day stamp must stay untouched in the failure arm — writing or deleting it is the actual damage here, not the failed mv.
Reachable in a test by shimming mv to exit non-zero for the tail path only, the same way tests/test_staging_lock.py shims wc. Assert now-day is unchanged and the log carries the failure.
Found while building the save.lock hold-time recorder for #226; related but distinct from #242, which is about where the temp file lives.
Activity
fix(ndc): commit now.md by rename and gate the day marker on it (Digi…