Skip to content

NDC race: background subshell truncates now.md up to 180s after snapshotting it — saves landing in the window are silently lost #142

Closed

Description

@bonyohana

Code-read finding (0.8.3; re-verified on main @ 7db7866 — not yet observed as a confirmed loss in my logs, but the window is real and the default cooldowns make it reachable)

Mechanism

In scripts/save-session.sh, the NDC prompt snapshots now.md synchronously (line 231, build-ndc-prompt), then the compression runs in a background subshell with a 180s model-call timeout, and only afterwards truncates:

  • line 231: snapshot into $NDC_PROMPT
  • line ~237: call-haiku ... 180 (in the backgrounded subshell)
  • line 248: : > "$MEMORY_FILE"

So the truncation can land up to ~180s after the snapshot. The save cooldown defaults to 120s (scripts/save-session.sh:114), i.e. a different session's save (or a --force save; the lock is released when the parent exits, and the parent exits without waiting on the subshell) can append a fresh entry to now.md inside the window. That entry was never in the snapshot, but : > erases it — never compressed into today-*.md, never seen again. No log line records the discrepancy.

Suggested fix

Truncate-by-content, not blindly: have the subshell replace now.md only up to what it snapshotted — e.g. snapshot to a byte offset (NDC_SRC_BYTES is already computed at line ~229!) and, at completion, atomically rewrite now.md with whatever lies beyond that offset instead of : >:

tail -c +$((NDC_SRC_BYTES + 1)) "$MEMORY_FILE" > "$MEMORY_FILE.tmp" && mv "$MEMORY_FILE.tmp" "$MEMORY_FILE"

(mv-over is atomic on the same filesystem; worst case under a concurrent append is the pre-existing race, no worse.) That preserves any entry appended during the compression window at zero cost.

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