Skip to content

NDC's commit ignores the mv result, so a failed truncate still rewrites the day stamp and logs a success #243

Closed

Description

@fdaviddpt

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 in now.md is 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_FILE is overwritten with today's date, for a now.md that 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 when NDC_KEPT is 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)"
fi

The 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

  1. added a commit that references this issue on Aug 2, 2026
    64cea0c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions