Skip to content

NDC's partial-truncate commit runs after the save lock is released, so a concurrent append can still be erased by the mv #223

Closed

Description

@fdaviddpt

Where this comes from

@Jutiphan's #173 named three residual defects in the NDC step. Two are now closed, by two different routes, and I want the accounting on the record because the third is still open and it is the one #173 called (b).

I verified each of these in scripts/save-session.sh on main just now rather than reasoning from the PR description.

(a) Unsynchronized snapshot — closed. NDC_SRC_BYTES=$(wc -c < "$MEMORY_FILE") and the build-ndc-prompt "$MEMORY_FILE" read both run in the parent, before the ( ... ) & at line 571, and therefore under the lock acquired at line 102. The byte count and the summarized content describe the same file.

(c) The tail-failure branch — closed, and closed well. Line ~541's else arm no longer falls back to : > "$MEMORY_FILE". now.md is left completely untouched, and the cost (a span that may be summarized twice into today-*.md) is spelled out in the comment along with why rolling back the TODAY_FILE append was rejected. That is the right trade: a duplicated summary is visible and recoverable, erased entries are neither.

(b) is still open

The commit itself is outside the lock:

NDC_TAIL=$(mktemp ...)
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"

All of that sits inside the subshell backgrounded at line 571. The parent's trap cleanup EXIT (line 87) calls lock_release and exits, so by the time tail runs, nothing holds the lock. The code's own comment at line ~505 states this as a fact — "by then the parent has released the save lock and exited" — and uses it to justify the offset arithmetic that replaced the blind truncate.

The offset arithmetic closes the 180s window. It does not close this one:

  1. tail -c +N reads to EOF and writes the remainder to $NDC_TAIL.
  2. Another save acquires the lock and appends an entry to now.md. It is entitled to — no lock is held here.
  3. mv "$NDC_TAIL" "$MEMORY_FILE" replaces the whole file with a copy that predates step 2.

The entry from step 2 is gone. Not truncated-and-recoverable: replaced, with its offset already advanced, and nothing logged — the same signature #142 was filed for, in a window of milliseconds instead of minutes.

Why it is worth closing anyway

Small window, permanent record, and the fix is now cheap in a way it was not when #173 was written. #182 landed scripts/lib-lock.sh, and lock_acquire/lock_release are already sourced in this file. The background subshell can re-acquire around the commit — read the current size, tail, and mv under the lock — which is roughly what #173 proposed, minus the flock dependency that blocked it on macOS.

Two things for whoever picks this up to decide rather than assume:

  • What should a failed re-acquire do? The parent uses timeout 0 and skips. Skipping here means leaving now.md whole, which lands in the same safe state as the (c) branch: a possible duplicate summary, no data loss. That seems right, but the duplicate then becomes routine rather than exceptional, so say so in the log line.
  • Re-read the size under the lock, or trust NDC_SRC_BYTES? The offset was computed before the Haiku call. It stays valid as the start of the kept range regardless, but confirming the file has not shrunk below it is the cheap guard against acting on a stale offset.

Credit

The diagnosis and the split into (a)/(b)/(c) are @Jutiphan's, from #173. The mkdir-based lock this can now use is #182, which existed because #173's flock approach forced the question. #173 stays open until this lands or its author closes it — the analysis in it is still the best write-up of this area.

Activity

  1. added a commit that references this issue on Jul 30, 2026
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