Repository navigation
fix: stop .trash/ clobbering notes, and archive applying before it validates - #162
Merged
Merged
Conversation
…lidates Two data-integrity bugs found and reproduced during the multi-agent field test in #159. Both silently shrink the store while reporting nothing wrong. **TrashNote overwrote existing .trash/ entries.** It renamed straight onto .trash/<rel with / → _>, and os.Rename overwrites its destination. Two collisions are reachable in ordinary use: trashing the same rel twice (create → delete → recreate → delete), and two distinct notes that flatten to one name (a/b.md and a_b.md). .trash/ is the ONLY recovery path — TrashNote is deliberately not a journaled undo txn — so the overwritten copy was gone for good, and `nt gc --yes` trashes in a loop while printing "collected N note(s) → .trash/ (recoverable)". The new regression test reproduces it: under the old code 1 of 3 notes survived. Destination names are now claimed with O_CREATE|O_EXCL before the rename, so the rename can only land on a name nothing else holds — that also closes the stat-then-rename race rather than narrowing it. Collisions get -2, -3, … . **archiveNotes wrote before validating.** It resolved and saved inside one loop, returning on the first bad handle, so `nt archive good1 good2 typo` archived good1 and good2 — dropping them out of index, search and recall — while the only output was an error and a non-zero exit. Nobody re-checks a command that reported failure. cmdRm already resolves every handle up front for exactly this reason and says so in a comment (commands.go:1186); archive never got the same treatment. It does now. Both regression tests were confirmed to fail against the unfixed code before being committed. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FgqQf7Lvou5F5as9aNmagu
navbytes
marked this pull request as ready for review
July 28, 2026 14:15
…ber-and-partial-archive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Two data-integrity bugs found and reproduced during the multi-agent field test in #159. Both silently shrink the store while reporting nothing wrong.
Each regression test was confirmed to fail against the unfixed code before being committed.
1.
TrashNoteoverwrote existing.trash/entries — unrecoverableinternal/mutate/rename.gorenamed straight onto.trash/<rel with / → _>, andos.Renameoverwrites its destination.Two collisions are reachable in ordinary use:
a/b.mdanda_b.mdboth becomea_b.mdThis is unrecoverable, not merely annoying:
.trash/is the only recovery path, becauseTrashNoteis deliberately not a journaled undo transaction (its own comment says so). Andnt gc --yestrashes in a loop while printingcollected N note(s) → .trash/ (recoverable)— so onegcover a store with colliding flattened names could destroy several notes with no warning, no journal, and no undo.Severity is confirmed by the test against old code — 1 of 3 notes survived:
Fix: destination names are claimed with
O_CREATE|O_EXCLbefore the rename, so the rename can only land on a name nothing else holds. That closes the stat-then-rename race outright rather than narrowing it. Collisions get a-2,-3, … suffix before the extension.Coverage note:
TrashNotewas at 0.0% ininternal/mutate's own tests, and the only assertion anywhere (gc_test.go:80) counts files in.trash/— it would still have read 2 with a third note clobbered.2.
archiveNoteswrote before it validatedinternal/cli/maintenance.goresolved and saved inside one loop, returning on the first unresolvable handle. So:…left Alpha and Bravo archived — out of
nt index,nt searchandnt recall— while the only output was an error. Thearchived N note(s)summary never printed, so there was no signal at all. Nobody re-checks a command that reported failure, which makes this the memory layer quietly losing entries.cmdRmalready resolves every handle up front for exactly this reason, and says so (commands.go:1186-1189: "Resolve and classify EVERY handle up front … so a later invalid handle never leaves earlier notes trashed").archiveNotesnever got the same treatment. It does now.A mid-loop
Savefailure can still leave a partial application, but that requires an I/O error rather than a typo, and the count reports what did flip. Noted in a comment rather than papered over.Verification
go build,go vet ./...,go test ./...all clean. Both new tests verified red-then-green against the unfixed code.Generated by Claude Code