Skip to content

fix: stop .trash/ clobbering notes, and archive applying before it validates - #162

Merged
navbytes merged 2 commits into
mainfrom
claude/fix-trash-clobber-and-partial-archive
Jul 28, 2026
Merged

navbytes merged 2 commits into
mainfrom
claude/fix-trash-clobber-and-partial-archive

Conversation

@navbytes

Copy link
Copy Markdown
Owner

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. TrashNote overwrote existing .trash/ entries — unrecoverable

internal/mutate/rename.go renamed straight onto .trash/<rel with / → _>, and os.Rename overwrites its destination.

Two collisions are reachable in ordinary use:

  • Same rel trashed twice — create → delete → recreate → delete
  • Distinct notes that flatten to one name — a/b.md and a_b.md both become a_b.md

This is unrecoverable, not merely annoying: .trash/ is the only recovery path, because TrashNote is deliberately not a journaled undo transaction (its own comment says so). And nt gc --yes trashes in a loop while printing collected N note(s) → .trash/ (recoverable) — so one gc over 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:

trash lost "VERSION ONE" — .trash/ holds 1 file(s): map[FLATTENED:true]
trash lost "VERSION TWO" — .trash/ holds 1 file(s): map[FLATTENED:true]

Fix: destination names are claimed with O_CREATE|O_EXCL before 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: TrashNote was at 0.0% in internal/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. archiveNotes wrote before it validated

internal/cli/maintenance.go resolved and saved inside one loop, returning on the first unresolvable handle. So:

$ nt archive "Alpha Note" "Bravo Note" no-such-note
nt: archive: no note "no-such-note"     # exit 1

…left Alpha and Bravo archived — out of nt index, nt search and nt recall — while the only output was an error. The archived 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.

cmdRm already 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"). archiveNotes never got the same treatment. It does now.

A mid-loop Save failure 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

…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
navbytes marked this pull request as ready for review July 28, 2026 14:15
@navbytes
navbytes enabled auto-merge (squash) July 28, 2026 14:16
@navbytes
navbytes merged commit 9f66b68 into main Jul 28, 2026
10 checks passed
@navbytes
navbytes deleted the claude/fix-trash-clobber-and-partial-archive branch July 28, 2026 14:18
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.

2 participants