Skip to content

fix: NDC must drop only the bytes it compressed, not the whole file (#142) - #151

Merged
fdaviddpt merged 1 commit into
mainfrom
fix/142-ndc-truncate-race
Jul 25, 2026
Merged

fdaviddpt merged 1 commit into
mainfrom
fix/142-ndc-truncate-race

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

Closes #142. Silent, unlogged data loss.

The window

save-session.sh snapshots now.md (NDC_SRC_BYTES, then build-ndc-prompt), runs compression in a backgrounded ( … ) &, and truncated with : > when it finished — up to 180s later.

By that point every gate that could have serialized this is gone:

  • the parent's cleanup() EXIT trap removes the save lock, and the script ends without waiting for the disowned subshell
  • the save cooldown marker is written at extract time, before the 10-30s Haiku call, so the next save is eligible ~120s after extract start — comfortably inside a 180s NDC timeout
  • post-tool-hook.sh only checks the parent PID, which is already dead
  • --force bypasses the cooldown entirely

So a newer save appends an entry, and the truncate erases it. Its position was already advanced at the append, so the content is unrecoverable, and nothing is logged — the memory is just missing.

The fix

Keep everything past the snapshot offset (tail -c +$((NDC_SRC_BYTES+1)) + mv) and log how many bytes were preserved. NDC_SRC_BYTES was already in scope — it was only feeding a percentage in a log line.

Tests

tests/test_ndc_truncate_race.py appends to now.md from inside the stubbed NDC call, which is precisely the window. Fails on main, passes here. A second test pins that the fix didn't become a no-op: the compressed span still leaves now.md and lands in today-*.md.

528 passed.

@fdaviddpt
fdaviddpt force-pushed the fix/142-ndc-truncate-race branch from 270e44e to c455005 Compare July 25, 2026 15:14
…142)

The compression step snapshots now.md, hands it to a Haiku call allowed up to
180s, then truncated the file with `: >`. Every gate that could have serialized
that is already released by then: the parent's EXIT trap removes the save lock
and the script ends without waiting for the disowned subshell, and the save
cooldown marker is written at extract time — before the 10-30s summarization —
so the next save becomes eligible well inside the NDC window. post-tool-hook
only checks the parent PID, which is dead.

So a newer save legitimately appends an entry, and the truncate erases it. That
entry's position was already advanced at the append, so the content is
unrecoverable, and nothing is logged: the memory is simply not there.

Keep everything past the snapshot offset instead, and log the bytes preserved.
NDC_SRC_BYTES was already in scope — it was only being used for a percentage in
a log line.

The regression test appends to now.md from inside the stubbed NDC call, which is
exactly the window, and fails on main.

Co-Authored-By: Max <noreply>
@fdaviddpt
fdaviddpt force-pushed the fix/142-ndc-truncate-race branch from c455005 to e6906ef Compare July 25, 2026 15:17
@fdaviddpt
fdaviddpt merged commit 8fea7fc into main Jul 25, 2026
12 checks passed
@fdaviddpt
fdaviddpt deleted the fix/142-ndc-truncate-race branch July 25, 2026 15:20
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.

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

1 participant