Skip to content

Fix consolidation retire overwriting a re-opened day's .done.md, and write down the cross-host lock contract (#509, #491) - #515

Merged
fdaviddpt merged 2 commits into
mainfrom
fix/509
Sep 3, 2026
Merged

fdaviddpt merged 2 commits into
mainfrom
fix/509

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

#509 -- consolidation retire overwrites an existing .done.md

run-consolidation.sh's retire loop turned a today-YYYY-MM-DD.md staging file into today-YYYY-MM-DD.done.md with a plain mv (or head -c ... > in the concurrent-append path). When a day was re-opened after already being retired once -- a session spanning midnight, or NDC re-creating the staging file for an already-retired day -- the second retirement silently truncate-overwrote the existing .done.md, destroying the first retired span's hourly-detail data with no log line. recent.md/archive.md are unaffected (they already merged both spans), so this was never memory-injection loss, but it destroyed the "searchable, not injected" hourly detail layer the README promises.

Fixed by appending to an existing .done.md instead of overwriting it, in both the plain-rename and the concurrent-append retire paths. Test-first: tests/test_consolidation_retire_reopened_day_509.py reproduces the overwrite (retire the same day twice, assert the first span survives), red before the fix and green after, plus a positive control (a day retired for the first time still works normally).

#491 -- cross-host locking contract, Claude Code + Codex sharing one store

scripts/lib-lock.sh and scripts/lib-staging-lock.sh were written and tested against a single host's process model and never mentioned Codex or "host" anywhere. Established (not assumed): the lock primitive is safe when Claude Code and Codex share one .remember/ store on one machine, because mkdir/kill -0 are OS-level process-table operations indifferent to which CLI spawned the contending process, and every lock is keyed by a fixed literal name or by day, never by session id. Both files now carry that contract as a header comment. tests/test_cross_host_lock_contract_491.py proves it under real concurrent writes from two simulated hosts (no lost/interleaved entries -- a genuine two-writer test, not one where the second writer never actually wrote), documents the one general, pre-existing kill -0 PID-reuse limitation this design inherits (not new, not worsened by a second host), and locks in via a grep contract that the lock/staging layer never assumes a session-ID shape.

Self-review

Spawned Explore and oss:auditor against the first commit. Both found a real duplication bug my first draft introduced: retire_prefix_into wrote the consumed prefix straight into .done.md before the paired tail extraction was known to succeed, so a tail failure's whole-file fallback re-committed the same prefix on top -- duplicating it unconditionally on the very first attempt, not only on a documented retry. Fixed in a follow-up commit by landing both extractions in temp files first and touching .done.md only once both are known good; reproduced red against the pre-restructure code with a tail-stub, green after. Also reworded two ERROR log lines and a stale comment that overclaimed a clean, safely-retriable state when cat (not atomic, unlike the mv it replaces here) can durably succeed while the trailing rm fails.

Below the bar, recorded here rather than filed: a persistent rm failure after a successful cat (a stuck permission/immutable-flag condition unlinking a retired staging file) still causes unbounded duplicate growth in .done.md across every subsequent run -- only its logging is now honest, not the underlying growth. A real fix needs a design decision (a commit marker, refusing a partial cat, or an atomicity redesign of the append path) beyond this issue's scope; the class is reachable in principle but requires a pathological, sustained filesystem-permission condition to trigger, which is why it stops here rather than becoming a filed issue.

Closes #509, closes #491.

fdaviddpt and others added 2 commits September 3, 2026 23:37
…write down the cross-host lock contract (#509, #491)

#509: a today-YYYY-MM-DD.md staging file re-created for a day already
retired -- a long-running session spanning midnight, or NDC re-opening a
retired day's staging file -- had its second retirement silently truncate
the existing .done.md over the previous span, in both the plain-rename and
the concurrent-append (head -c / tail-split) retire paths. Both now append
to an existing .done.md instead of overwriting it.

#491: established, rather than assumed, whether the lock primitive
(lib-lock.sh) and the staging lock (lib-staging-lock.sh) are safe when
Claude Code and Codex share one .remember/ store on one machine. They are,
because mkdir/kill -0 are OS-level process-table operations indifferent to
which CLI spawned the contending process, and every lock is keyed by a fixed
literal name or by day, never by session id. Both files now carry that
contract as a header comment, proved under real concurrent writes from two
simulated hosts in tests/test_cross_host_lock_contract_491.py, which also
documents the one general, pre-existing kill -0 PID-reuse limitation this
design inherits (not new, not worsened by a second host).

Co-Authored-By: Claude Sonnet 5 <[email protected]>
…ned, and be honest in the log when it can't tell (#509)

Two reviewers (Explore, oss:auditor) spawned against the first commit found:

- retire_prefix_into wrote the consumed prefix straight into staging_done
  before the tail extraction below it was known to succeed. When `tail`
  failed, the whole-file fallback re-committed the same prefix on top,
  duplicating it unconditionally on the very first attempt (not only on a
  retry, as the comment at the time claimed). Fixed by extracting both the
  prefix and the tail into fresh temp files first and touching staging_done
  only once both are known good -- reproduced red (a tail-stub forcing the
  failure) before the fix, green after, verified against the pre-fix
  commit and restored cleanly.
- retire_whole_into's cat+rm is not atomic like the mv it replaces: if cat
  durably appends but the trailing rm fails, the two "left in place" log
  lines asserted a clean, safely-retriable state that does not hold in that
  sub-case. Reworded to say the append may already have landed.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@fdaviddpt
fdaviddpt merged commit 8f97a08 into main Sep 3, 2026
13 checks passed
@fdaviddpt
fdaviddpt deleted the fix/509 branch September 3, 2026 23:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant