Skip to content

Fix: distinguish absent from unreadable NDC generation marker (#619) - #624

Merged
fdaviddpt merged 2 commits into
mainfrom
fix/619
Sep 7, 2026
Merged

fdaviddpt merged 2 commits into
mainfrom
fix/619

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

Closes #619.

save-session.sh's NDC generation guard (added by #614) read the generation counter (NDC_GEN_FILE) at two sites with cat "$NDC_GEN_FILE" 2>/dev/null || echo 0, which collapses "the file does not exist yet" (legitimately generation 0) and "the read failed" (a permission or I/O error, or a reader racing the truncating write that bumps the counter) into the identical fallback value 0. When both reads fail the same way, NDC_LIVE_GEN equals NDC_SRC_GEN, the mismatch check the guard exists to enforce never fires, and the commit proceeds on the byte-size check alone -- silently restoring the pre-#614 behaviour whose failure mode is content that then exists nowhere.

What changed

Adds ndc_read_gen(), used at both read sites, which echoes 0 only when the marker was never created (-e and, after a review follow-up, -L, so a dangling symlink is not mistaken for a plain absent path) and the literal string unreadable when it exists but a read of it fails or produces non-numeric content -- a value a real generation number can never equal. A new elif branch in the commit gate treats unreadable from either read as a reason to skip the commit and log why, ordered before the existing generation-mismatch check (load-bearing: an unreadable == unreadable string match would otherwise fall through to arithmetic on a non-numeric value and abort the script).

TDD

Red, before the fix (right assertion, right reason -- the marker exists as a directory so every read of it fails, and the guard silently let the commit through anyway):

AssertionError: the generation marker exists but neither read of it could succeed -- this round cannot tell whether another round committed since its snapshot, and must not assume it did not just because a failed read and an absent file happen to produce the same fallback value
1 failed, 1 passed, 4 deselected in 3.63s

Green after:

7 passed in 15.54s

Paired with a positive control (test_commit_proceeds_when_generation_marker_is_absent): a marker that was never created is legitimately generation 0 and must still commit.

On the brief's invited pushback

The brief flagged this finding as AI-generated and reasoned-not-observed, and asked that tests/test_save_session_gates.py:111 be read first to confirm the collapse actually reaches production behaviour before building around it. Read it: that line does drive the generation file (via STUB_REPLACE_DURING_NDC), but only ever writes a valid numeric generation -- it never exercises a read failure, so the described collapse was real and genuinely unguarded. No pushback is warranted; the finding held up.

Self-review

Two agents (Explore and oss:auditor) reviewed the first commit independently. The auditor came back clean across all four classes (silent absence, guard reachability, untrusted-input boundary, platform band), with one filing suggestion. The reviewer raised four findings:

  1. Fixed -- [ ! -e "$NDC_GEN_FILE" ] alone treats a dangling symlink the same as a plain absent path (both fail -e), reopening the same absent-vs-unreadable ambiguity one layer down. Added a -L check, pinned by a new red/green test (test_commit_skips_when_generation_marker_is_a_broken_symlink).
  2. Fixed -- the helper's scratch variable wasn't declared local, unlike this file's other helper function. Harmless today (every call site is inside $( ), which forks a subshell) but a latent trap; added local.
  3. report-for-filing -- if the marker becomes durably unreadable outside the transient race this fix targets (e.g. left empty by a crash, or replaced by a directory by external tooling), every future NDC round skips forever with no operator-facing hint in the log that deleting the marker resets it to generation 0. A reasonable fail-safe trade (skip forever beats losing data), but a new failure mode this fix introduces for the durable case, previously self-healing by accident under the old "collapse to 0" behaviour. Needs a design decision (bounded retry? richer log message naming the remedy?) rather than a one-line fix, so it is not folded into this PR.
  4. report-for-filing (also independently raised by the auditor as a filing suggestion) -- the identical cat FILE 2>/dev/null || echo 0 collapsing pattern is still present, unfixed, at two other sites in the same file: COOLDOWN_MARKER (scripts/save-session.sh:239) and NDC_MARKER (scripts/save-session.sh:851). Lower stakes than the generation-marker case -- a failed read there makes the cooldown look expired, i.e. fails open to running NDC/summarization more often than intended (extra Haiku calls), not to silent data loss -- and out of save-session.sh NDC generation guard (#614) collapses "file absent" and "read failed" to the same value #619's stated scope, but the same code smell and immediately adjacent.

Scope notes

  • README.md (this repo's only docs_targets entry) needed no change: purely an internal bugfix, no user-facing behaviour or interface changed.
  • No repo diagnostic/convention file applies: nothing moved, no file added or removed, no CLAUDE.md/pyproject.toml change.
  • The narrowed test run (pytest tests/test_ndc_commit_lock.py) trips a pre-existing 80% coverage-gate failure unrelated to this diff -- confirmed identical against the pre-save-session.sh NDC generation guard (#614) collapses "file absent" and "read failed" to the same value #619 baseline. Full test_command was not run locally per policy; CI is the merge gate.

🤖 Generated with Claude Code

[AI-generated]

fdaviddpt and others added 2 commits September 7, 2026 06:37
save-session.sh's NDC generation guard (#614) read NDC_GEN_FILE at two sites
with `cat "$NDC_GEN_FILE" 2>/dev/null || echo 0`, which collapses "the file
does not exist yet" (legitimately generation 0) and "the read failed" (a
permission or I/O error, or a reader racing the truncating write that bumps
the file) to the identical 0. When both reads fail the same way, NDC_LIVE_GEN
equals NDC_SRC_GEN, the mismatch check the guard exists to enforce never
fires, and the commit proceeds on the byte-size check alone -- silently
restoring the pre-#614 behaviour whose failure mode is content that then
exists nowhere.

Adds ndc_read_gen(), used at both sites, which echoes 0 only when the marker
was never created and the literal string "unreadable" when it exists but
could not be read -- a value a real generation number can never equal. A new
elif branch in the commit gate treats "unreadable" from either read as a
reason to skip the commit and log why, the same way a byte-size regression or
an explicit generation mismatch already does.

tests/test_ndc_commit_lock.py adds a positive control (marker genuinely
absent -- commit must still proceed) paired with the negative case (marker
exists as a directory, so every read of it fails -- commit must now be
skipped and logged; before this fix it silently proceeded, which is what the
new test caught red).

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

Self-review findings from the /oss:developer review pass on the #619 commit
(0530b9e):

`[ ! -e "$NDC_GEN_FILE" ]` follows a symlink, so it is false for both "no
path was ever created here" and "a symlink was created here and its target
is now gone" -- it cannot tell those apart, which reopens the exact
absent-vs-unreadable ambiguity #619 closes, one layer down. `ndc_read_gen()`
now also checks `-L`, so a dangling symlink falls through to the (failing)
`cat` and is classified "unreadable" rather than a fresh, legitimate 0. Pinned
by a new test (marker created as a symlink to a nonexistent target); red
before this change, green after.

Also scopes `_ndc_gen` with `local`, matching this file's other helper
(`keep_rejected_text()`) -- harmless today since every call site invokes
`ndc_read_gen` inside `$( )`, which already forks a subshell, but a latent
trap if that ever changes.

Two other findings from the same review pass are not folded into this
commit and are reported to the maintainer instead: a durable-corruption
skip-loop with no operator-facing recovery hint in the log line, and the
identical `cat FILE 2>/dev/null || echo 0` collapsing pattern still present,
unfixed, at two lower-stakes sites in this file (COOLDOWN_MARKER, NDC_MARKER)
-- both are scope/design questions rather than one-line fixes.

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

save-session.sh NDC generation guard (#614) collapses "file absent" and "read failed" to the same value

1 participant