Repository navigation
fix(undo): restore only store snapshots and keep deleted rows deleted - #63
Conversation
memory_undo accepted any path. Any *.logfmt file became active memory, a live day file could be restored over itself, and a source named `<n>-blocklist.logfmt` or `<n>-canonical.logfmt` overwrote the reserved file, so export followed by undo could un-block a poisoned memory. Separately, check and merge mode re-added rows that were deliberately deleted, such as a forgotten leaked secret. - Only `<stamp>-YYYY-MM-DD.logfmt` files inside the store's snapshots directory are accepted, the same grammar as the viewer's restore (abogartz#21). Paths are realpath-checked: no `..`, symlink or junction escapes, and the target must be a regular day file, never a symlink or reserved file. The lock and the write still use the lexical path every other writer uses. - A snapshot row that has a deletions.logfmt audit and is absent from the live file stays deleted in check and merge mode. The audit is read inside the store lock. force=true is the explicit recovery path, and the tool reports how many rows it left out. - The list only shows valid snapshot names. Co-Authored-By: Claude Opus 5.5 <[email protected]>
abogartz
left a comment
There was a problem hiding this comment.
Approve — every scenario reproduced, the containment is the same grammar the viewer has used since #21, and the default-mode resurrection fix is the important part. One calibration request for the body, two nits.
What I reproduced on 0.8.1 (main @ 4643ec4), then re-ran against your branch
| Scenario | main | this PR |
|---|---|---|
memory_undo on </tmp>/attacker.logfmt |
written to <root>/attacker.logfmt, and memoryFiles() globs *.logfmt → became an active memory row |
invalid snapshot name |
memory_undo on a live day file |
2026-10-24.logfmt → ^\d+- strips 2026- → target 10-24.logfmt → row count went 3 → 6, every row duplicated and both files read as memory |
invalid snapshot name |
forget then undo in default (check) mode |
deleted row came back | stays deleted, count reported |
same, merge=true |
came back | stays deleted |
same, force=true |
came back | comes back (explicit path, unchanged) |
snapshots/<name> and ./<name> forms, and <root>/2026-10-24.logfmt via .. |
accepted; the last one silently produced the duplication above | rejected with a clear message |
symlink placed at snapshots/99-<day>.logfmt |
followed | snapshot is outside the snapshots directory |
Also verified: a restore on a store with no deletions.logfmt does not throw (readLines tolerates the missing file), and the viewer's restore is unaffected because it calls restoreSnapshot(..., "force"). 296 pass / 4 skip / 0 fail; 7 of your tests fail against main's source; tsc clean; biome warnings identical to main.
Please correct in the body (severity calibration)
The two reserved-file items — crafting <stamp>-blocklist.logfmt, and the memory_export → memory_undo chain — are refused by the default check mode in my repro (Undo refused: 0 row(s) added and 1 changed…). They need merge=true or force=true. With force=true they are exactly as bad as you describe: I replaced blocklist.logfmt with export JSONL and a row a human had blocked as a leaked secret became active again, with the block audit destroyed.
Please mark those two as force/merge-gated. It matters: the forget→undo resurrection is reachable in default mode, which is the strongest claim in the PR and shouldn't be diluted by two claims that need an explicit opt-in mode.
Nits
SNAPSHOT_NAME = /^(\d+)-(\d{4}-\d{2}-\d{2}\.logfmt)$/accepts impossible dates —123-2026-13-45.logfmtpasses and would restore to a new, glob-picked-up2026-13-45.logfmt. Names can only be minted bysnapshotFiles()today, so it is not reachable, but validating a real calendar date costs nothing.listSnapshots()now hides any pre-existing snapshot whose name doesn't match the grammar (e.g. an old<stamp>-canonical.logfmt). Intentional, but it silently shrinks the list for someone mid-recovery — worth a line in the tool description or the changelog note.
Design opinion
The deletions.logfmt filter changes undo's contract from "this exact state" to "this state, minus what a human deliberately deleted since", keyed on a store-global append-only audit. I think that is the right trade for a memory store whose default mode is meant to be safe, the live.has(key) escape correctly keeps a re-written row, and reporting deletedOmitted instead of silently dropping is the right touch. The one thing to keep an eye on is that the audit is read in full under the store lock on every non-force restore; fine at current scale, worth revisiting if it ever grows past a few thousand lines.
|
🎉 This PR is included in version 0.8.4 🎉 The release is available on: Your semantic-release bot 📦🚀 |
|
Thanks for merging, and for the thorough repro. Your nits crossed with the merge, so they're in #76: snapshot names must carry a real calendar date ( Noted on the full read of |
Review follow-up for #63: snapshot names must carry a real calendar date (UTC round-trip, leap years included), and the tool description and README now say that non-standard files in snapshots/ are not listed or restorable. Co-Authored-By: Claude Opus 5.5 <[email protected]>
memory_undoaccepts any path, the same class of problem as the viewer restore path in #21. Separately, its default mode brings back rows that were deliberately deleted. I found both while reviewing 0.8.0. #60's store lock doesn't change either one, and this PR is built on top of it.The problem. All of these reproduced on 0.8.0.
In the default
checkmode:memory_forget(or a viewer delete) removes a row, for example a leaked secret,memory_undoon the pre-forget snapshot puts it back. The same happens withmerge=true.*.logfmtfile anywhere on disk can be "restored" and becomes active memory..../2026-10-23.logfmt) duplicates its rows.Only with
merge=trueorforce=true(the defaultcheckmode refuses these, as your repro showed):<n>-blocklist.logfmtor<n>-canonical.logfmtoverwrites the reserved file, because the target is the basename with the stamp stripped.memory_exportto a.logfmtpath, thenmemory_undoon it withforce=true, overwritesblocklist.logfmtand un-blocks a memory a human had blocked as a leaked secret, destroying the block audit. Neither step asks the host for permission.Change (
src/undo.ts):<stamp>-YYYY-MM-DD.logfmtfiles inside the store'ssnapshots/directory are accepted. That's the same grammar the viewer restore uses since viewer /api/restore joins raw body.name — files outside the store can be promoted into memory #21. Both a bare name from the list and its full path work.snapshots/and the source are realpath-checked, so.., symlinks and Windows junctions can't escape. The target is derived from the snapshot name, must be a regular day file, and is never a symlink or a reserved file.deletions.logfmtaudit and is missing from the live file stays deleted incheckandmergemode, and the tool reports how many it left out. The audit is read inside the store lock. It doesn't depend on timing, which matters becauseprunenow logs the deletion before it takes the snapshot.force=trueremains the explicit way to get rows back, and its safety snapshot from fix: serialize store writes across concurrent sessions #60 is unchanged./varvs/private/varon macOS).memory_undo listonly shows valid snapshot names. The tool and schema descriptions are updated, so it no longer advertises "or a full path" to anywhere.The blocklist already wins at load time, so restoring a raw blocked row keeps it inactive. The real hole was overwriting
blocklist.logfmtitself.Tests (
test/undo.test.ts):..path and a path outsidesnapshots/are rejected.-blocklist/-canonicalnames can't overwrite reserved files.With the source change reverted, the new tests fail.
Checks (Linux, Bun 1.4.4, on 0.8.1):
bun test ./test/292 pass, 4 skip, 0 fail, including your newconcurrency.test.ts.bun run test:e2eagainst opencode 2.0.26: 9 pass.tsc --noEmitis clean, andbiome lintexits 0 with warnings unchanged frommain. On macOS,/var→/private/varis handled because every side is realpath'd before comparing.Follow-up: your two nits (calendar-date validation, documenting the name filter) are in #76.
🤖 Generated with Claude Code