Skip to content

Fix: NDC commit race where a stale offset survives the size check (#614) - #616

Merged
fdaviddpt merged 3 commits into
mainfrom
fix/614
Sep 7, 2026
Merged

fdaviddpt merged 3 commits into
mainfrom
fix/614

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

Closes #614.

What was actually wrong

The reporter's own suspected cause (a hook blind-truncating now.md) is not what is happening -- confirmed by reading scripts/save-session.sh in full. The ordinary append path (a cat old > tmp; append entry; mv tmp -> now.md sequence under save.lock) never truncates; that class of bug (#142) stays closed.

The real gap is narrower and lives entirely inside NDC (Now-Day Compression), the backgrounded subshell that snapshots now.md's byte length before an up-to-180s Haiku call, then later re-acquires the save lock and commits by tail -c +(snapshot+1) of whatever now.md holds at commit time. The existing staleness guard only checked NDC_LIVE_BYTES < NDC_SRC_BYTES (skip if the live file shrank below the snapshot). It never considered a replacement that is the same size or larger -- which is exactly what happens when a second, overlapping NDC round's own commit lands first (its own tail, written over now.md by its own mv). The first-to-commit round's replacement can easily be >= a later round's now-stale snapshot size, which passes the size check and lets tail -c +N slice into content that round's own Haiku call never summarized -- lost from now.md and never written to any today-*.md either.

Two overlapping NDC rounds require the shared hour-long cooldown marker to be legitimately re-passed while an earlier round is still alive -- not reachable via two ordinary sibling-worktree sessions saving close together under default config, but reachable via e.g. a laptop sleep/wake spanning the cooldown window, or the marker's own documented self-heal path recurring.

Reproduction

Confirmed red before the fix with a targeted scratch repro (using the existing STUB_REPLACE_DURING_NDC harness hook, already built for "now.md replaced mid-compression -- a rotation, or another NDC round that committed its own tail first"): a replacement longer than the snapshot but otherwise unrelated to it loses its own first bytes, present nowhere -- not in now.md, not in any today-*.md. Turned into a tracked regression test, tests/test_ndc_commit_lock.py::TestNdcCommitLock::test_commit_skips_when_now_md_was_replaced_by_a_later_committed_round, red before the fix (AssertionError), green after, alongside the other 27 NDC-adjacent tests (all still green).

The fix

Adds a small monotonic generation counter file, .remember/tmp/ndc-generation: read under the save lock at snapshot time, re-checked under the same lock at commit time, mismatch skips the commit exactly like the pre-existing size-based skip, bumped by 1 on every successful commit. This closes the exact window the byte-count-only check missed -- size alone cannot distinguish a legitimate append from a same-or-larger replacement, but a counter bumped only by an actual commit can.

Self-review

Spawned Explore and oss:auditor concurrently against the first commit; both independently flagged the same real defect from different angles (a swallowed write-failure on the generation-bump, silently reopening the exact race being fixed for that one round -- below-bar consideration weighed and rejected since it is directly load-bearing for the fix's own guarantee, so it was fixed rather than filed), and Explore additionally caught a leading-zero octal-parsing bug in the new arithmetic, matching a precedent this same file already fixed once (record_summary_failure()'s 10#$_prev_count, #332). Both fixed in a second commit; all 28 NDC-adjacent tests re-ran green.

Neither commit required test_command's full suite -- run narrowly (pytest tests/test_ndc_*.py -q, 131 tests including every save_session/ndc-keyed test), per the developer brief's own guidance; CI is the gate for the full matrix.

🤖 Generated with Claude Code

[AI-generated]

fdaviddpt and others added 3 commits September 7, 2026 04:10
…check (#614)

Two overlapping NDC compression rounds can happen when a second round's
hour-long cooldown gate opens while the first round's Haiku call, or its own
lock-reacquisition wait, is still in flight -- for example across a laptop
sleep/wake spanning the cooldown. The existing staleness check
(`NDC_LIVE_BYTES < NDC_SRC_BYTES`) only catches a now.md replacement that ends
up SHORTER than the round's own pre-Haiku snapshot. It cannot tell a
legitimate append from a REPLACEMENT that happens to still be at least as
long -- exactly what another round's own commit produces (its own tail,
written over now.md by its own `mv`). When that happens, the
second-committing round's own `tail -c +N` offset no longer describes any
real boundary in the live file, and silently slices into bytes its own Haiku
call never summarized -- content that then exists nowhere, not in now.md, not
in any today-*.md.

Adds a small monotonic generation counter
(`.remember/tmp/ndc-generation`), read at snapshot time and re-checked, under
the same save lock the size check already uses, before a commit is allowed to
land. Any mismatch means a commit has landed since this round's snapshot was
taken, and the round is skipped the same way the size check already skips a
shrunk file, rather than acting on an offset that no longer means anything.

A prior triage pass on #614 could not confirm the reporter's own suspected
cause (a hook blind-truncating now.md) -- the append path never truncates,
and this race is unrelated to that guess. It was reproduced directly: a test
harness stub simulates "another NDC round already committed its own tail
first" with a replacement at least as long as the stale snapshot, which the
byte-count check alone lets through and the fix now catches.

tests/test_ndc_commit_lock.py adds the regression test (confirmed red before
the fix, green after). tests/test_save_session_gates.py's STUB_REPLACE_DURING_NDC
now also bumps the generation counter, since that is what a real committed
round would have done.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01P9WexZb39yUjQgJ53Zmf1c
…write failure

Two findings from the self-review pass, both confirmed real and both narrow
enough to fix in place:

1. The generation-bump arithmetic (`$(( NDC_SRC_GEN + 1 ))`) read the counter
   as bash arithmetic without a base-10 prefix, so a leading-zero value
   ("08"/"09") would be parsed as invalid octal and abort that statement.
   record_summary_failure() hit this exact shape earlier (#332, its own
   `10#$_prev_count`) -- applied the same fix here.

2. A failed write of the bumped generation was swallowed by a bare `|| true`,
   with no log line. Since the `mv` that commits now.md already lands before
   this write runs, a failure here leaves the on-disk generation at its
   PRE-commit value while a real commit has already happened -- silently
   reopening the exact #614 window the generation check exists to close, for
   any other round that snapshotted the same starting generation. Now logged
   the same way the sibling `mv` failure a few lines above already is.

Both findings came from the two self-review spawns (Explore + oss:auditor)
against the #614 commit; neither required changing the fix's shape or
touching the tests, which all still pass unmodified.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01P9WexZb39yUjQgJ53Zmf1c
tests/test_arith_base_lint_332.py's scanner works over raw file text, not
comment-stripped source, so any literal `$(( VAR + op ))`-shaped substring
anywhere in the file is a candidate site -- including inside a comment
quoting the pre-fix, unguarded form as an illustrative example. The second
#614 commit's own explanatory comment did exactly that (`` `$(( NDC_SRC_GEN +
1 ))` would abort... ``), which is prose, not code, but indistinguishable
from a real finding to a lint with no notion of `#`.

Reworded to describe the same failure without reproducing the exact
unguarded `$(( VAR + 1 ))` shape verbatim. The actual code five lines below
was already correctly guarded with `10#$NDC_SRC_GEN`; only the comment
tripped the gate. Confirmed locally: test_arith_base_lint_332.py now passes
(10 passed), and the rest of the NDC-adjacent suite is unaffected (38 passed
total across the NDC suite plus this lint).

Reported by another lane after PR #616 (fix/614) hit this on the
windows-latest 3.11/3.12 CI legs; not platform-specific, reproduced directly
here on macOS.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01P9WexZb39yUjQgJ53Zmf1c
@fdaviddpt
fdaviddpt merged commit cdeb42d into main Sep 7, 2026
16 checks passed
@fdaviddpt
fdaviddpt deleted the fix/614 branch September 7, 2026 02:42
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.

now.md gets silently truncated (not appended) when multiple sessions share the same REMEMBER_DIR

1 participant