Repository navigation
Description
Every writer of now.md takes a lock. No reader does.
save-session.sh:435-436 appends an entry in two operations:
echo "" >> "$MEMORY_FILE" 2>/dev/null || { log "write" "ERROR: cannot write now.md"; exit 1; }
cat "$HAIKU_TEXT_FILE" >> "$MEMORY_FILE"A separator, then the entry. Both under save.lock, which is correct — but the lock only excludes other writers. A reader landing between the two appends sees now.md ending in a blank line whose entry has not arrived yet; a reader landing inside the cat sees a partial entry.
And there is a reader that cannot take the lock at all. session-start-hook.sh sources exactly four libraries — detect-tools.sh, bootstrap-dirs.sh, log.sh, lib-env-cache.sh — and never lib-lock.sh, so lock_acquire is not merely uncalled there, it is undefined in that process. Line 385 is a bare cat "$MFILE" that injects now.md straight into the new session's context.
That was verified in the code, not inferred from a missing call site — an absence in a grep is not an absence in the code, which is this repo's own recurring lesson.
What it costs, and what it does not
This is a torn entry boundary, not loss: every byte is on disk, and the next read sees the file whole. The damage is that the session which read it mid-write starts with a truncated final entry and no way to know it — the same shape as #235, where a summary split across a consolidation boundary and the two halves were reassembled by nothing.
Scoped honestly, because the neighbouring issue is bigger and this one should not borrow its severity:
- This needs no second filesystem. Unlike NDC's commit
mvs across filesystems, so the atomicity #142's design rests on is not the atomicity it gets #242, it is reachable on any machine, on every save. - The window is small — two appends of a few kilobytes — and a session start has to land inside it.
- NDC's commit
mvs across filesystems, so the atomicity #142's design rests on is not the atomicity it gets #242's fix does not close this. That fix removes the partial file produced by a cross-device copy; it does not make an append atomic, because appends never were.
Judgment for whoever takes it
- Write the entry in one operation, or rename it into place. A single
catof separator-plus-entry closes the two-append window without any locking change, and the sibling-temp-then-rename pattern (lib-env-cache.sh:170-174,post-tool-hook.sh:260, and now the NDC commit via NDC's commitmvs across filesystems, so the atomicity #142's design rests on is not the atomicity it gets #242) closes it completely. The second is the repo's established answer and is already used three times. - Do not reach for a reader lock first.
session-start-hook.shdeliberately runs lean —user-prompt-hookspawns 27 processes to print a timestamp — 8.7s p50 blocking every prompt on Windows #227, post-tool-hook pays the same resolution prefix on every tool call #230 and Nested Haiku summarizer re-triggers the plugin's own hooks, creating a spurious$TMPDIR-slug memory directory #204 are all about spawn cost and blocking on that path, and making session start acquire a lock puts a hook on a user's prompt behind a file lock held by a process that calls Haiku. That trade is worse than the tear. - Decide whether a torn read is worth detecting at all. An entry that begins mid-sentence is visible to a human reading their own memory; the argument for fixing it is that nothing tells the session it happened. If the answer is "accept it", that belongs written down — an undocumented known-good-enough is indistinguishable from an unnoticed bug.
Provenance
Found by the agent implementing #242/#243, which narrowed its own claim twice before reporting: it initially framed this as making #242's copy window observable by a live process, then established that route is only reachable when NDC_KEPT > 0 and that #242's rename removes it entirely. What survives is the append race above, which it identified as the part actually worth filing. Verified independently by me: I read save-session.sh:432-437.
Activity
fix(tests): take the large entry off the exec boundary (#247)
Commit the now.md entry by rename, not two appends (#247) (#250)
Cache the injected context, flattened config and tool verdicts across…