commit 4e0ca3ecb2cf12294c81dbed5dca5a34b789a424
parent aaa0ad6657c6e79dd20e73ccf1cf9391418c9c2e
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date: Tue, 10 Nov 2026 13:12:06 -0500
umtool: a redo refuses unless the draft is exactly as the undo left it (review blocker)
An undo now records, beside its undo-saved (.redo.json), a hash of the draft as it left it -- the manifest's bytes and
takes/draft.json (draftStateHash). A redo takes the newest undo not yet redone and refuses ("the draft changed since
the undo — nothing to redo") unless both still match: any write since -- a title, an edit under the terminal's
throttle, a saved version, the CLI or another process -- ends it, where before only a newer auto-before snapshot did
and such a write was silently discarded. Tests: a title after an undo, a throttled move after an undo, an older undo
after a newer one was redone, and a redo with nothing changed. The undo stack's undo-saved (with its pairing and
pointer) and redone-* are pruned to the newest 200 like the undo points.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Diffstat:
2 files changed, 146 insertions(+), 14 deletions(-)
diff --git a/umtool/lib/report/manifest.mjs b/umtool/lib/report/manifest.mjs
@@ -20,6 +20,7 @@
// 4. GUARD ON THE FILE'S OWN MTIME. A PUT carrying a stale token is a 409, not
// a silent overwrite -- somebody may have run `resolve-windows --write` in
// between, and losing that is losing human judgement.
+import { createHash } from "node:crypto";
import { statSync } from "node:fs";
import { copyFile, readFile, rename, rm, stat, writeFile } from "node:fs/promises";
import path from "node:path";
@@ -2153,16 +2154,28 @@ export async function autoSnapshot(dir, op, { now = Date.now(), every = undoEver
/**
* Keep the newest `keep` undo points (`auto-before-*` snapshots in
* revisions/), removing the older ones and their draft-pointer sidecars
- * (versions.mjs openVersion). Named snapshots, `undo-saved` and `undone-*`
- * are never touched. Returns the names removed.
+ * (versions.mjs openVersion) -- and, by the same rule, the newest `keep` of
+ * `undo-saved` (with its `.redo.json` pairing and `.draft.json`) and of
+ * `redone-*`. Named snapshots and `undone-*` are never touched. Returns the
+ * names removed.
*/
export async function pruneAutoSnapshots(dir, keep = AUTO_SNAPSHOT_KEEP) {
- const auto = (await listSnapshots(dir)).filter((x) => !x.legacy && x.label?.startsWith(AUTO_SNAPSHOT_PREFIX));
+ const all = (await listSnapshots(dir)).filter((x) => !x.legacy);
+ // Each kind the undo stack makes keeps its newest `keep`: the undo points, what an undo replaced (`undo-saved`,
+ // its redo pairing and draft pointer beside it), and what a redo used up (`redone-*`).
+ const kinds = [
+ (x) => x.label?.startsWith(AUTO_SNAPSHOT_PREFIX),
+ (x) => x.label === "undo-saved",
+ (x) => x.label?.startsWith("redone-"),
+ ];
const gone = [];
- for (const x of auto.slice(Math.max(0, keep))) {
- await rm(path.join(dir, x.rel), { force: true });
- await rm(path.join(dir, x.rel.replace(/\.manifest\.json$/, ".draft.json")), { force: true });
- gone.push(x.name);
+ for (const kind of kinds) {
+ for (const x of all.filter(kind).slice(Math.max(0, keep))) {
+ await rm(path.join(dir, x.rel), { force: true });
+ await rm(path.join(dir, x.rel.replace(/\.manifest\.json$/, ".draft.json")), { force: true });
+ await rm(path.join(dir, x.rel.replace(/\.manifest\.json$/, ".redo.json")), { force: true });
+ gone.push(x.name);
+ }
}
return gone;
}
@@ -4882,8 +4895,6 @@ export async function undoStructural(dir, { token = null } = {}) {
await rename(tmp, file);
const undone = target.rel.replace(`-${target.label}.manifest.json`, `-undone-${op}.manifest.json`);
await rename(path.join(dir, target.rel), path.join(dir, undone)).catch(() => {});
- // Which undo point this undo took back, beside the state it replaced: what a Redo pairs (redoStructural).
- if (saved) await writeFile(path.join(dir, saved.replace(/\.manifest\.json$/, ".redo.json")), JSON.stringify({ undone, op }) + "\n", "utf8").catch(() => {});
// An undo point that carries the DRAFT POINTER as it was (versions.mjs openVersion: `<snapshot>.draft.json`):
// the draft's parent goes back with the manifest, so "Undo open v1" leaves the draft named as it was.
const sidecar = path.join(dir, target.rel.replace(/\.manifest\.json$/, ".draft.json"));
@@ -4907,17 +4918,37 @@ export async function undoStructural(dir, { token = null } = {}) {
}
await rename(sidecar, path.join(dir, undone.replace(/\.manifest\.json$/, ".draft.json"))).catch(() => {});
}
+ // What a Redo pairs (redoStructural), beside the state this undo replaced: the undo point it took back, and the
+ // draft as this undo LEFT it (the manifest's bytes and the draft pointer's). A redo refuses unless both are
+ // still exactly that -- any writer since (a title, a throttled terminal edit, a saved version) ends it.
+ if (saved) await writeFile(path.join(dir, saved.replace(/\.manifest\.json$/, ".redo.json")), JSON.stringify({ undone, op, after: await draftStateHash(dir) }) + "\n", "utf8").catch(() => {});
return { restored: target.rel, op, saved, token: await manifestToken(dir), ...(draft !== undefined ? { draft } : {}) };
});
}
/**
+ * The draft's state, as one hash: the manifest's bytes and the draft pointer's (`takes/draft.json`, absent or not).
+ * What an undo records and a redo checks (redoStructural).
+ *
+ * @param {string} dir
+ */
+export async function draftStateHash(dir) {
+ const h = createHash("sha256");
+ h.update(await readFile(manifestFile(dir), "utf8"));
+ h.update("\0");
+ h.update(await readFile(path.join(dir, "takes", "draft.json"), "utf8").catch(() => "\u0001absent"));
+ return h.digest("hex");
+}
+
+/**
* Put back what the newest Undo took back (umtool redesign L7a: the workspace's Redo; `umtool timeline <p> redo`).
*
* An undo keeps the manifest it replaced as `undo-saved` and renames the undo point it restored `undone-<op>`
- * (undoStructural). A redo is possible while the newest of the `undo-saved` and `auto-before-*` snapshots is an
- * `undo-saved` -- no edit has been made since the undo (an edit takes an undo point, and the redo is gone, as in
- * every editor). It restores that `undo-saved` byte for byte, renames it `redone-<op>` (never used again), and
+ * (undoStructural), and beside it (`<undo-saved>.redo.json`) the undo point and a hash of the draft as the undo
+ * left it (draftStateHash: the manifest's bytes and takes/draft.json). A redo takes the newest undo not yet redone
+ * and refuses unless the draft is still exactly that -- ANY write since, whether or not it took an undo point
+ * (a title, a terminal edit under the throttle, a saved version, another process), ends the redo, as in every
+ * editor. It restores that `undo-saved` byte for byte, renames it `redone-<op>` (never used again), and
* renames the `undone-<op>` that undo took back (named beside it, `<undo-saved>.redo.json`) back to
* `auto-before-<op>`, so the next Undo takes the redone edit back again.
* Several undos redo in turn, newest first. The draft pointer an undo carried (`.draft.json`) comes back with it.
@@ -4930,10 +4961,24 @@ export async function redoStructural(dir, { token = null } = {}) {
const current = await manifestToken(dir);
if (token !== null && current !== token) throw new StaleToken(token, current);
const all = (await listSnapshots(dir)).filter((s) => !s.legacy);
- const head = all.find((s) => s.label === "undo-saved" || s.label?.startsWith(AUTO_SNAPSHOT_PREFIX));
- const pair = head?.label === "undo-saved" ? await readFile(path.join(dir, head.rel.replace(/\.manifest\.json$/, ".redo.json")), "utf8").then((t) => JSON.parse(t), () => null) : null;
+ const pairOf = (s) => readFile(path.join(dir, s.rel.replace(/\.manifest\.json$/, ".redo.json")), "utf8").then((t) => JSON.parse(t), () => null);
+ // The newest undo not yet redone (a redone one's pairing is removed).
+ let head = null;
+ let pair = null;
+ for (const s of all) {
+ if (s.label !== "undo-saved") continue;
+ const p = await pairOf(s);
+ if (p) {
+ head = s;
+ pair = p;
+ break;
+ }
+ }
const undone = pair && typeof pair.undone === "string" ? all.find((s) => s.rel === pair.undone && s.label?.startsWith("undone-")) : null;
if (!head || !undone) throw new Error("nothing to redo: no undo since the last edit");
+ // The draft exactly as that undo left it, or no redo: whatever wrote it since -- a write that took no undo point
+ // (a title, a terminal edit under the throttle, a saved version), the CLI, another process -- would be lost.
+ if (typeof pair.after !== "string" || pair.after !== (await draftStateHash(dir))) throw new Error("the draft changed since the undo — nothing to redo");
const op = undone.label.slice("undone-".length);
const text = await readFile(path.join(dir, head.rel), "utf8");
JSON.parse(text);
diff --git a/umtool/lib/report/structure.test.mjs b/umtool/lib/report/structure.test.mjs
@@ -22,6 +22,7 @@ import {
removeEntry,
removePost,
redoStructural,
+ updateClip,
setUndoEveryMs,
undoStructural,
updateFactcheck,
@@ -266,6 +267,92 @@ test("redoStructural puts back what each undo took back, newest first, and an ed
}
});
+test("redo refuses after ANY write since the undo -- one that took no undo point included -- and keeps it", async () => {
+ const pause = () => new Promise((r) => setTimeout(r, 25));
+ // (a) every edit its own undo point: move, undo, a title (no undo point), redo -> refused, the title kept.
+ {
+ const dir = await project();
+ setUndoEveryMs(0);
+ try {
+ await moveEntry(dir, "c03", 1);
+ await pause();
+ await undoStructural(dir);
+ await pause();
+ await updateClip(dir, "c02", { title: "A new title" });
+ const titled = await readRaw(dir);
+ await assert.rejects(redoStructural(dir), /the draft changed since the undo — nothing to redo/);
+ assert.equal(await readRaw(dir), titled, "the title is kept");
+ assert.equal((await read(dir)).timeline.find((e) => e.id === "c02").title, "A new title");
+ } finally {
+ setUndoEveryMs(null);
+ await rm(dir, { recursive: true });
+ }
+ }
+ // (b) the terminal's throttle: move, remove, undo, then a move within two minutes (no undo point) -> refused.
+ {
+ const dir = await project();
+ setUndoEveryMs(null);
+ try {
+ await moveEntry(dir, "c03", 1);
+ await pause();
+ await removeEntry(dir, "c01");
+ await pause();
+ await undoStructural(dir);
+ await pause();
+ const n = (await revisions(dir)).length;
+ await moveEntry(dir, "c02", 1);
+ assert.equal((await revisions(dir)).length, n, "the throttle took no undo point");
+ const moved = await readRaw(dir);
+ await assert.rejects(redoStructural(dir), /nothing to redo/);
+ assert.equal(await readRaw(dir), moved, "the move is kept");
+ } finally {
+ await rm(dir, { recursive: true });
+ }
+ }
+ // An older undo never becomes the head once a newer one is redone and the draft moved since.
+ {
+ const dir = await project();
+ setUndoEveryMs(0);
+ try {
+ await moveEntry(dir, "c03", 1);
+ await pause();
+ await removeEntry(dir, "c01");
+ await pause();
+ await undoStructural(dir);
+ await pause();
+ await undoStructural(dir);
+ await pause();
+ await redoStructural(dir); // the move again; the remove's undo is the head now
+ await pause();
+ await updateClip(dir, "c02", { title: "Since" });
+ const since = await readRaw(dir);
+ await assert.rejects(redoStructural(dir), /the draft changed since the undo/);
+ assert.equal(await readRaw(dir), since);
+ } finally {
+ setUndoEveryMs(null);
+ await rm(dir, { recursive: true });
+ }
+ }
+ // Nothing changed since the undo: the redo still works, byte for byte.
+ {
+ const dir = await project();
+ setUndoEveryMs(0);
+ try {
+ await updateClip(dir, "c02", { start: 31 });
+ const trimmed = await readRaw(dir);
+ await pause();
+ await undoStructural(dir);
+ await pause();
+ const r = await redoStructural(dir);
+ assert.equal(r.op, "trim");
+ assert.equal(await readRaw(dir), trimmed);
+ } finally {
+ setUndoEveryMs(null);
+ await rm(dir, { recursive: true });
+ }
+ }
+});
+
test("a manifest that already has a build problem can still be re-ordered", async () => {
const m = base();
m.posts[0].attachTo = "gone"; // already broken before the edit