Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(editor): record undo history only after a write lands, and force the call site to say

EtienneLescot90459e240466
diff --git a/electron/edit-menu.test.ts b/electron/edit-menu.test.ts
new file mode 100644
index 000000000..47ff19121
--- /dev/null
+++ b/electron/edit-menu.test.ts
@@ -0,0 +1,73 @@
+// Regression cover for the macOS half of #433.
+//
+// The Edit menu used to carry `role: "undo"` / `role: "redo"` with
+// `registerAccelerator: false`. That field is documented `@platform linux,win32`,
+// so on darwin it does nothing at all: AppKit still matches the menu's Cmd+Z key
+// equivalent inside `-[NSApplication sendEvent:]`, before the key event reaches
+// the web contents, and the editor's own keydown handler never runs.
+//
+// These tests pin the shape that actually reaches the renderer on every platform:
+// an explicit accelerator and a click that dispatches to the editor.
+
+import { describe, expect, it, vi } from "vitest";
+import { buildEditMenuSubmenu, type EditorUndoRedoChannel } from "./edit-menu";
+
+function build() {
+ const dispatch = vi.fn<(channel: EditorUndoRedoChannel) => void>();
+ const items = buildEditMenuSubmenu({
+ label: (_key, fallback) => fallback,
+ dispatch,
+ });
+ return { items, dispatch };
+}
+
+describe("buildEditMenuSubmenu", () => {
+ it("owns Cmd+Z itself instead of leaning on registerAccelerator", () => {
+ const { items } = build();
+ const undoItem = items.find((i) => i.label === "Undo");
+
+ expect(undoItem?.accelerator).toBe("CmdOrCtrl+Z");
+ // The two things that made the previous version a no-op on macOS.
+ expect(undoItem?.role).toBeUndefined();
+ expect(undoItem?.registerAccelerator).toBeUndefined();
+ });
+
+ it("owns Shift+Cmd+Z for redo on the same terms", () => {
+ const { items } = build();
+ const redoItem = items.find((i) => i.label === "Redo");
+
+ expect(redoItem?.accelerator).toBe("Shift+CmdOrCtrl+Z");
+ expect(redoItem?.role).toBeUndefined();
+ expect(redoItem?.registerAccelerator).toBeUndefined();
+ });
+
+ it("routes both to the editor renderer, which owns the document's undo stack", () => {
+ // `webContents.undo()` -- what the roles ran -- is the WEB EDITING undo. It does
+ // nothing outside a focused text field, so on macOS Cmd+Z was swallowed by a menu
+ // item that could not have serviced it anyway.
+ const { items, dispatch } = build();
+
+ items
+ .find((i) => i.label === "Undo")
+ ?.click?.(
+ // The click signature carries a menu item, a window and the event; none of
+ // them are read here.
+ undefined as never,
+ undefined as never,
+ undefined as never,
+ );
+ expect(dispatch).toHaveBeenCalledWith("menu-undo");
+
+ items
+ .find((i) => i.label === "Redo")
+ ?.click?.(undefined as never, undefined as never, undefined as never);
+ expect(dispatch).toHaveBeenCalledWith("menu-redo");
+ });
+
+ it("leaves the clipboard items as roles", () => {
+ // They act on the focused text selection, which is exactly what the roles do --
+ // and nothing in the editor shadows them.
+ const { items } = build();
+ expect(items.map((i) => i.role).filter(Boolean)).toEqual(["cut", "copy", "paste", "selectAll"]);
+ });
+});
diff --git a/electron/edit-menu.ts b/electron/edit-menu.ts
new file mode 100644
index 000000000..69e798a2b
--- /dev/null
+++ b/electron/edit-menu.ts
@@ -0,0 +1,62 @@
+// The application menu's Edit submenu, split out of `main.ts` so it can be tested
+// (same shape as `about.ts`).
+//
+// Undo/Redo are deliberately NOT `role: "undo"` / `role: "redo"`.
+//
+// Those roles run `webContents.undo()`, the WEB EDITING undo, which does nothing
+// at all outside a focused text field. On macOS their Cmd+Z key equivalent is
+// matched by AppKit inside `-[NSApplication sendEvent:]`, BEFORE the key event
+// reaches the web contents, so the editor's own document-level handler
+// (`useUndoRedoShortcuts`) never sees the keydown and Ctrl+Z silently did
+// nothing (#433).
+//
+// `registerAccelerator: false` does not fix that. Electron annotates the field
+// `@platform linux,win32` (see `MenuItemConstructorOptions` in electron.d.ts),
+// so on darwin it is ignored outright and the menu keeps the key equivalent.
+// And on Windows and Linux the roles were never the problem: menu accelerators
+// there are dispatched from the unhandled-keyboard-event path, i.e. AFTER the
+// renderer, which the renderer's own `preventDefault()` already suppresses.
+//
+// So the items own the accelerator on every platform and forward to the editor
+// renderer, which applies exactly the rule its keydown path applies: a focused
+// text field gets the browser's text undo, anything else gets the document undo.
+// `dispatch` is what falls back to `webContents.undo()` when the focused window
+// is not the editor at all.
+
+import type { MenuItemConstructorOptions } from "electron";
+
+/** IPC channels the Edit menu forwards to the editor renderer. */
+export type EditorUndoRedoChannel = "menu-undo" | "menu-redo";
+
+export interface EditMenuOptions {
+ /** Localised label for `key`, falling back to `fallback` when untranslated. */
+ label: (key: string, fallback: string) => string;
+ /** Route an undo/redo request to whichever window should service it. */
+ dispatch: (channel: EditorUndoRedoChannel) => void;
+}
+
+export function buildEditMenuSubmenu({
+ label,
+ dispatch,
+}: EditMenuOptions): MenuItemConstructorOptions[] {
+ return [
+ {
+ label: label("actions.undo", "Undo"),
+ accelerator: "CmdOrCtrl+Z",
+ click: () => dispatch("menu-undo"),
+ },
+ {
+ label: label("actions.redo", "Redo"),
+ accelerator: "Shift+CmdOrCtrl+Z",
+ click: () => dispatch("menu-redo"),
+ },
+ { type: "separator" },
+ // The clipboard roles keep theirs: they act on the focused text selection,
+ // which is precisely what `webContents.cut/copy/paste` do, and the editor has
+ // no document-level meaning for them to shadow.
+ { role: "cut", label: label("actions.cut", "Cut") },
+ { role: "copy", label: label("actions.copy", "Copy") },
+ { role: "paste", label: label("actions.paste", "Paste") },
+ { role: "selectAll", label: label("actions.selectAll", "Select All") },
+ ];
+}
diff --git a/electron/electron-env.d.ts b/electron/electron-env.d.ts
index 89494dd0d..4eb288e14 100644
--- a/electron/electron-env.d.ts
+++ b/electron/electron-env.d.ts
@@ -383,6 +383,10 @@ interface Window {
onMenuLoadProject: (callback: () => void) => () => void;
onMenuSaveProject: (callback: () => void) => () => void;
onMenuSaveProjectAs: (callback: () => void) => () => void;
+ /** Edit > Undo / Redo. On macOS the menu is the only route Cmd+Z has to the
+ * renderer at all — see `electron/edit-menu.ts`. */
+ onMenuUndo: (callback: () => void) => () => void;
+ onMenuRedo: (callback: () => void) => () => void;
quitApp: () => void;
setTitleBarOverlay: (color: string, symbolColor: string) => void;
getPlatform: () => string;
diff --git a/electron/main.ts b/electron/main.ts
index b02d50246..5a755b824 100644
--- a/electron/main.ts
+++ b/electron/main.ts
@@ -33,6 +33,7 @@ import {
import { parseCliArgs } from "./cli/args";
import { runCli } from "./cli/cliMain";
import { isDiagnosticModeEnabled, mainLogBuffer } from "./diagnostics/main-log-buffer";
+import { buildEditMenuSubmenu, type EditorUndoRedoChannel } from "./edit-menu";
import {
loadAndRegisterGlobalShortcut,
registerOpenAppShortcut,
@@ -189,6 +190,25 @@ function sendEditorMenuAction(
targetWindow.webContents.send(channel);
}
+/**
+ * Route the Edit menu's Undo/Redo to whoever should service it.
+ *
+ * Unlike `sendEditorMenuAction` this never CREATES an editor window: Cmd+Z is not
+ * a request to open the editor. And when the focused window is not the editor --
+ * the launch window, the notes window -- the web-editing undo the `undo` role used
+ * to provide is the right one after all, so fall through to it.
+ */
+function sendEditorUndoRedo(channel: EditorUndoRedoChannel) {
+ const targetWindow = BrowserWindow.getFocusedWindow() ?? mainWindow;
+ if (!targetWindow || targetWindow.isDestroyed()) return;
+ if (!isEditorWindow(targetWindow)) {
+ if (channel === "menu-undo") targetWindow.webContents.undo();
+ else targetWindow.webContents.redo();
+ return;
+ }
+ targetWindow.webContents.send(channel);
+}
+
function setupApplicationMenu() {
const isMac = process.platform === "darwin";
const template: Electron.MenuItemConstructorOptions[] = [];
@@ -274,32 +294,11 @@ function setupApplicationMenu() {
},
{
label: mainT("common", "actions.edit") || "Edit",
- submenu: [
- // `registerAccelerator: false` on both: the roles run `webContents.undo()`,
- // the WEB EDITING undo, which does nothing outside a focused text field —
- // and registering their accelerators lets the native menu eat Ctrl+Z /
- // Ctrl+Shift+Z before the renderer's document-level handler
- // (`useUndoRedoShortcuts`) ever sees the keydown. The items stay, greyed
- // shortcut text and all, so text fields keep their menu entries.
- {
- role: "undo",
- label: mainT("common", "actions.undo") || "Undo",
- registerAccelerator: false,
- },
- {
- role: "redo",
- label: mainT("common", "actions.redo") || "Redo",
- registerAccelerator: false,
- },
- { type: "separator" },
- { role: "cut", label: mainT("common", "actions.cut") || "Cut" },
- { role: "copy", label: mainT("common", "actions.copy") || "Copy" },
- { role: "paste", label: mainT("common", "actions.paste") || "Paste" },
- {
- role: "selectAll",
- label: mainT("common", "actions.selectAll") || "Select All",
- },
- ],
+ // Built in `edit-menu.ts` — read its header for why Undo/Redo are not roles.
+ submenu: buildEditMenuSubmenu({
+ label: (key, fallback) => mainT("common", key) || fallback,
+ dispatch: sendEditorUndoRedo,
+ }),
},
{
label: mainT("common", "actions.view") || "View",
diff --git a/electron/preload.ts b/electron/preload.ts
index 2705e4ba0..6aff16407 100644
--- a/electron/preload.ts
+++ b/electron/preload.ts
@@ -354,6 +354,20 @@ contextBridge.exposeInMainWorld("electronAPI", {
ipcRenderer.on("menu-save-project-as", listener);
return () => ipcRenderer.removeListener("menu-save-project-as", listener);
},
+ // The Edit menu's Undo/Redo. On macOS this is the ONLY way Cmd+Z reaches the
+ // renderer: AppKit matches the menu's key equivalent before the key event is
+ // delivered to the web contents, so the document-level keydown handler never
+ // runs. See `electron/edit-menu.ts`.
+ onMenuUndo: (callback: () => void) => {
+ const listener = () => callback();
+ ipcRenderer.on("menu-undo", listener);
+ return () => ipcRenderer.removeListener("menu-undo", listener);
+ },
+ onMenuRedo: (callback: () => void) => {
+ const listener = () => callback();
+ ipcRenderer.on("menu-redo", listener);
+ return () => ipcRenderer.removeListener("menu-redo", listener);
+ },
quitApp: () => {
ipcRenderer.send("app-quit");
},
diff --git a/src/components/ai-edition/CaptionsPane.tsx b/src/components/ai-edition/CaptionsPane.tsx
index 2bcd8c5ab..983852f1d 100644
--- a/src/components/ai-edition/CaptionsPane.tsx
+++ b/src/components/ai-edition/CaptionsPane.tsx
@@ -181,10 +181,13 @@ export function CaptionsPane() {
const clearLegacyCaptionAnnotations = async () => {
const doc = useProjectStore.getState().document;
if (!doc) return;
- await saveDocument({
- ...doc,
- annotations: doc.annotations.filter((a) => a.annotationSource !== "auto-caption"),
- });
+ await saveDocument(
+ {
+ ...doc,
+ annotations: doc.annotations.filter((a) => a.annotationSource !== "auto-caption"),
+ },
+ { history: true },
+ );
};
return (
diff --git a/src/components/ai-edition/NewEditorShell.tsx b/src/components/ai-edition/NewEditorShell.tsx
index 1d650d5cf..fd8607cd4 100644
--- a/src/components/ai-edition/NewEditorShell.tsx
+++ b/src/components/ai-edition/NewEditorShell.tsx
@@ -165,7 +165,7 @@ export function NewEditorShell() {
// edit the user just undid came back. `history: false` is load-bearing — a
// recording save here would push the restored document straight back onto the
// stack and clear the redo the undo had just created.
- useUndoRedoShortcuts(() => {
+ const { runUndo, runRedo } = useUndoRedoShortcuts(() => {
const doc = useProjectStore.getState().document;
if (doc) void useProjectStore.getState().saveDocument(doc, { history: false });
});
@@ -317,7 +317,7 @@ export function NewEditorShell() {
const doc = useProjectStore.getState().document;
// The store already toasted the reason; answering false is what keeps the
// window open on top of it.
- if (doc) return await saveDocument(doc);
+ if (doc) return await saveDocument(doc, { history: true });
return true;
});
@@ -651,15 +651,20 @@ export function NewEditorShell() {
const handleSave = useCallback(async () => {
const doc = useProjectStore.getState().document;
if (!doc) return;
- if (await saveDocument(doc)) toast.success("Project saved");
+ if (await saveDocument(doc, { history: true })) toast.success("Project saved");
}, [saveDocument]);
// Native File menu (electron/main.ts) → v4 actions. The menu is shown via
// Menu.setApplicationMenu and dispatches these IPC events; the old editor
// listened to them, but the v4 shell replaced it, leaving the File items
// dead. Wire them to the same handlers the top-bar buttons use so the
- // File/Edit/View menu bar works again (Edit/View items use Electron roles).
+ // File/Edit/View menu bar works again (the View items still use Electron roles).
// The v4 editor has no separate "Save As" location, so it maps to Save.
+ //
+ // Edit > Undo/Redo are here too, and not roles: on macOS the menu's Cmd+Z key
+ // equivalent is matched by AppKit before the key event reaches the renderer, so
+ // this subscription is the ONLY thing that makes Ctrl+Z work there. See
+ // `electron/edit-menu.ts`.
useEffect(() => {
const api = window.electronAPI;
if (!api) return;
@@ -668,18 +673,20 @@ export function NewEditorShell() {
api.onMenuLoadProject?.(() => setOpenProjectOpen(true)),
api.onMenuSaveProject?.(() => void handleSave()),
api.onMenuSaveProjectAs?.(() => void handleSave()),
+ api.onMenuUndo?.(runUndo),
+ api.onMenuRedo?.(runRedo),
];
return () => {
for (const unsub of unsubscribers) unsub?.();
};
- }, [handleSave]);
+ }, [handleSave, runUndo, runRedo]);
const handleRenameProject = useCallback(
async (title: string) => {
const doc = useProjectStore.getState().document;
if (!doc) return;
if (title === doc.project.title) return;
- await saveDocument({ ...doc, project: { ...doc.project, title } });
+ await saveDocument({ ...doc, project: { ...doc.project, title } }, { history: true });
},
[saveDocument],
);
@@ -703,7 +710,7 @@ export function NewEditorShell() {
// A failed save cancels the action that prompted this dialog. The store has
// already said why -- which is what the bare `catch {}` here used to swallow,
// leaving the window refusing to close with nothing on screen explaining it.
- if (doc && !(await saveDocument(doc))) {
+ if (doc && !(await saveDocument(doc, { history: true }))) {
resolve("cancel");
return;
}
@@ -797,24 +804,33 @@ export function NewEditorShell() {
);
if (snapshot.kind === "zoom") {
- await saveDocument({
- ...doc,
- zoomRanges: [...doc.zoomRanges, ...anchored] as typeof doc.zoomRanges,
- });
+ await saveDocument(
+ {
+ ...doc,
+ zoomRanges: [...doc.zoomRanges, ...anchored] as typeof doc.zoomRanges,
+ },
+ { history: true },
+ );
} else if (snapshot.kind === "annotation") {
- await saveDocument({
- ...doc,
- annotations: [...doc.annotations, ...anchored] as typeof doc.annotations,
- });
+ await saveDocument(
+ {
+ ...doc,
+ annotations: [...doc.annotations, ...anchored] as typeof doc.annotations,
+ },
+ { history: true },
+ );
} else {
// speed and cameraFullscreen are both plain spans on legacyEditor.
const key = snapshot.kind === "speed" ? "speedRegions" : "cameraFullscreenRegions";
const legacy = (doc.legacyEditor as Record<string, unknown>) ?? {};
const prev = (legacy[key] as unknown[]) ?? [];
- await saveDocument({
- ...doc,
- legacyEditor: { ...legacy, [key]: [...prev, ...anchored] },
- });
+ await saveDocument(
+ {
+ ...doc,
+ legacyEditor: { ...legacy, [key]: [...prev, ...anchored] },
+ },
+ { history: true },
+ );
}
toast.success("Region pasted");
// `tl` belongs here now that the trim branch calls tl.addTrim: useTimeline
@@ -889,7 +905,7 @@ export function NewEditorShell() {
if (choice === "save") {
const doc = useProjectStore.getState().document;
// Stay put if the save did not land -- the store has already said why.
- if (doc && !(await saveDocument(doc))) return;
+ if (doc && !(await saveDocument(doc, { history: true }))) return;
}
setNewProjectOpen(true);
})();
@@ -903,7 +919,7 @@ export function NewEditorShell() {
if (choice === "save") {
const doc = useProjectStore.getState().document;
// Stay put if the save did not land -- the store has already said why.
- if (doc && !(await saveDocument(doc))) return;
+ if (doc && !(await saveDocument(doc, { history: true }))) return;
}
setOpenProjectOpen(true);
})();
diff --git a/src/lib/ai-edition/store/agentDocumentApply.test.ts b/src/lib/ai-edition/store/agentDocumentApply.test.ts
index 18e95f723..a7b3fe698 100644
--- a/src/lib/ai-edition/store/agentDocumentApply.test.ts
+++ b/src/lib/ai-edition/store/agentDocumentApply.test.ts
@@ -3,6 +3,8 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
import { createEmptyDocument } from "../schema";
import { applyAgentDocumentIfCurrent, runAgentTurn } from "./agentDocumentApply";
import { useProjectStore } from "./projectStore";
+import { clearHistory, redo, undo } from "./undo";
+import { future, past } from "./undoStack";
const saveMock = vi.hoisted(() => vi.fn());
@@ -15,6 +17,7 @@ vi.mock("@/native/client", () => ({
describe("applyAgentDocumentIfCurrent", () => {
beforeEach(() => {
useProjectStore.getState().clear();
+ clearHistory();
saveMock.mockReset();
});
@@ -40,10 +43,13 @@ describe("applyAgentDocumentIfCurrent", () => {
project: { ...before.project, title: "Agent edit" },
};
useProjectStore.setState({ projectId: "project_1", document: before, revision: 4 });
- useProjectStore.getState().setDocument({
- ...before,
- project: { ...before.project, title: "Manual edit" },
- });
+ useProjectStore.getState().setDocument(
+ {
+ ...before,
+ project: { ...before.project, title: "Manual edit" },
+ },
+ { history: true },
+ );
await expect(applyAgentDocumentIfCurrent(agentResult, 4)).resolves.toBe("conflict");
@@ -70,6 +76,55 @@ describe("applyAgentDocumentIfCurrent", () => {
expect(useProjectStore.getState().dirty).toBe(false);
});
+ it("leaves no undo step behind a rejected agent edit", async () => {
+ // The rollback is a `setState` by design, so it cannot pop an entry the apply
+ // already pushed. Recording on the optimistic `setDocument` therefore left a
+ // phantom Ctrl+Z step for an edit that never reached disk -- and `pushHistory`
+ // had cleared `future` on the way in, so the user's redo went with it.
+ const before = createEmptyDocument({ projectId: "project_1", title: "Before" });
+ useProjectStore.setState({ projectId: "project_1", document: before, revision: 4 });
+
+ // A real edit and an undo first, so there is a redo entry to lose.
+ saveMock.mockResolvedValueOnce({
+ success: true,
+ document: { ...before, project: { ...before.project, title: "User edit" } },
+ });
+ await useProjectStore
+ .getState()
+ .saveDocument(
+ { ...before, project: { ...before.project, title: "User edit" } },
+ { history: true },
+ );
+ expect(undo()).toBe(true);
+ expect(past).toHaveLength(0);
+ expect(future).toHaveLength(1);
+
+ saveMock.mockResolvedValue({ success: false, error: "EACCES" });
+ const agentResult = { ...before, project: { ...before.project, title: "Agent edit" } };
+ await expect(applyAgentDocumentIfCurrent(agentResult)).resolves.toBe("save-failed");
+
+ expect(past).toHaveLength(0);
+ expect(future).toHaveLength(1);
+ expect(redo()).toBe(true);
+ expect(useProjectStore.getState().document?.project.title).toBe("User edit");
+ });
+
+ it("records exactly one undo step for an agent edit that lands", async () => {
+ // Two writes, one step: the optimistic `setDocument` opts out and the save
+ // names the pre-agent document as its base. Losing the step altogether would
+ // be the same class of bug in the other direction.
+ const before = createEmptyDocument({ projectId: "project_1", title: "Before" });
+ const agentResult = { ...before, project: { ...before.project, title: "Agent edit" } };
+ useProjectStore.setState({ projectId: "project_1", document: before, revision: 4 });
+ saveMock.mockImplementation(async (document) => ({ success: true, document }));
+
+ await expect(applyAgentDocumentIfCurrent(agentResult, 4)).resolves.toBe("applied");
+
+ expect(past).toHaveLength(1);
+ expect(undo()).toBe(true);
+ expect(useProjectStore.getState().document?.project.title).toBe("Before");
+ });
+
it("still rejects when the agent hands back something that is not a document", async () => {
// The one throw left on this path, and the reason the caller keeps a try/catch.
const before = createEmptyDocument({ projectId: "project_1", title: "Before" });
@@ -99,6 +154,7 @@ describe("applyAgentDocumentIfCurrent", () => {
describe("runAgentTurn", () => {
beforeEach(() => {
useProjectStore.getState().clear();
+ clearHistory();
saveMock.mockReset();
});
@@ -112,10 +168,13 @@ describe("runAgentTurn", () => {
const { result, applyDocument } = await runAgentTurn(async (documentSnapshot) => {
// A background transcription landing mid-turn, which is the common case.
- useProjectStore.getState().setDocument({
- ...before,
- project: { ...before.project, title: "Manual edit" },
- });
+ useProjectStore.getState().setDocument(
+ {
+ ...before,
+ project: { ...before.project, title: "Manual edit" },
+ },
+ { history: true },
+ );
return {
document: { ...documentSnapshot, project: { ...before.project, title: "Agent edit" } },
};
@@ -133,10 +192,13 @@ describe("runAgentTurn", () => {
saveMock.mockImplementation(async (document) => ({ success: true, document }));
const { applyDocument } = await runAgentTurn(async () => {
- useProjectStore.getState().setDocument({
- ...before,
- project: { ...before.project, title: "Manual edit" },
- });
+ useProjectStore.getState().setDocument(
+ {
+ ...before,
+ project: { ...before.project, title: "Manual edit" },
+ },
+ { history: true },
+ );
return { document: { ...before, project: { ...before.project, title: "Agent edit" } } };
});
diff --git a/src/lib/ai-edition/store/agentDocumentApply.ts b/src/lib/ai-edition/store/agentDocumentApply.ts
index 52b2c1b1a..6b0715fd5 100644
--- a/src/lib/ai-edition/store/agentDocumentApply.ts
+++ b/src/lib/ai-edition/store/agentDocumentApply.ts
@@ -25,19 +25,27 @@ export async function applyAgentDocumentIfCurrent(
const previous = store.document;
const previousDirty = store.dirty;
// Both calls, not just the save. `setDocument` puts the agent's document on screen
- // NOW, without waiting on the disk round-trip `saveDocument` awaits. Both record the
- // outgoing document on the undo stack, and recording it twice is not a risk: the
- // second sees the document the first already installed and skips.
- store.setDocument(parsed);
- if (await store.saveDocument(parsed)) return "applied";
+ // NOW, without waiting on the disk round-trip `saveDocument` awaits.
+ //
+ // Only the SAVE records history, and `historyBase` is what makes that possible: by
+ // the time it runs, the store already holds the agent's document, so its own idea
+ // of "previous" is useless. Recording on the `setDocument` instead put the entry on
+ // the stack before the write was known to have landed — and the rollback below, a
+ // `setState` by design, could not take it back off. A rejected agent edit left a
+ // phantom Ctrl+Z step and a cleared `future` for something that never happened.
+ store.setDocument(parsed, { history: false });
+ if (await store.saveDocument(parsed, { history: true, historyBase: previous })) {
+ return "applied";
+ }
// The edits are on screen by now. Leaving them there while the caller says they
// were not applied tells the user two opposite things at once, and worse: `dirty`
// is set, so the next unrelated save would quietly persist the document we just
// said was rejected.
//
- // Restored through `setState` rather than `setDocument`, so the rejected document
- // does not land on the undo stack. `revision` keeps the bump: it did move, and
+ // Restored through `setState` rather than `setDocument`, so the restore itself is
+ // not recorded — and there is nothing on the stack to take back off either, because
+ // the write above records only on success. `revision` keeps the bump: it did move, and
// leaving it forward makes any in-flight guard read "conflict", which is the safe
// direction to be wrong in.
//
diff --git a/src/lib/ai-edition/store/projectStore.test.ts b/src/lib/ai-edition/store/projectStore.test.ts
index 7435eb733..9ad09572e 100644
--- a/src/lib/ai-edition/store/projectStore.test.ts
+++ b/src/lib/ai-edition/store/projectStore.test.ts
@@ -312,7 +312,9 @@ describe("useProjectStore", () => {
bridgeMocks.save.mockResolvedValue({ success: false, error: "EACCES" });
const edited = { ...sampleDoc, project: { ...sampleDoc.project, title: "Edited" } };
- await expect(useProjectStore.getState().saveDocument(edited)).resolves.toBe(false);
+ await expect(
+ useProjectStore.getState().saveDocument(edited, { history: true }),
+ ).resolves.toBe(false);
expect(toastMocks.error).toHaveBeenCalledWith("Failed to save project", {
description: "EACCES",
@@ -329,7 +331,9 @@ describe("useProjectStore", () => {
useProjectStore.setState({ projectId: "proj_test", document: sampleDoc, dirty: true });
bridgeMocks.save.mockRejectedValue(new Error("bridge is gone"));
- await expect(useProjectStore.getState().saveDocument(sampleDoc)).resolves.toBe(false);
+ await expect(
+ useProjectStore.getState().saveDocument(sampleDoc, { history: true }),
+ ).resolves.toBe(false);
expect(toastMocks.error).toHaveBeenCalledWith("Failed to save project", {
description: "bridge is gone",
});
@@ -340,7 +344,9 @@ describe("useProjectStore", () => {
useProjectStore.setState({ projectId: "proj_test", document: sampleDoc, dirty: true });
bridgeMocks.save.mockResolvedValue({ success: true, document: saved });
- await expect(useProjectStore.getState().saveDocument(saved)).resolves.toBe(true);
+ await expect(useProjectStore.getState().saveDocument(saved, { history: true })).resolves.toBe(
+ true,
+ );
expect(toastMocks.error).not.toHaveBeenCalled();
const state = useProjectStore.getState();
diff --git a/src/lib/ai-edition/store/projectStore.ts b/src/lib/ai-edition/store/projectStore.ts
index 85245f595..a5432e784 100644
--- a/src/lib/ai-edition/store/projectStore.ts
+++ b/src/lib/ai-edition/store/projectStore.ts
@@ -20,15 +20,38 @@ export type ProjectStatus = "idle" | "loading" | "ready" | "error";
export interface DocumentWriteOptions {
/**
- * Record the outgoing document on the undo stack. Defaults to `true`: a write
- * is a user edit unless the caller says otherwise.
+ * Record the outgoing document on the undo stack.
*
- * Pass `false` for writes the user never asked for -- probe backfills,
- * transcripts arriving from a background job, the restore an undo itself
- * persists. Those must not become Ctrl+Z steps, and a persist that re-recorded
- * the document it just restored would undo the undo.
+ * REQUIRED, and deliberately not defaulted either way. A default is a decision
+ * nobody makes: defaulting to `true` let `probeAndCorrectClip` push a background
+ * probe's document onto the stack simply by not mentioning it, so the first
+ * Ctrl+Z after a drop snapped the clip back to its 60s placeholder instead of
+ * removing it -- and defaulting to `false` would recreate #433 itself the next
+ * time somebody added an edit. Making it a compile error to omit is the only
+ * version a new call site cannot get wrong by saying nothing.
+ *
+ * `true` for anything the user did. `false` for writes they never asked for:
+ * probe backfills, transcripts arriving from a background job, the camera
+ * auto-link, the optimistic half of an optimistic-write-then-save pair, and the
+ * persist an undo itself triggers (which would otherwise undo the undo).
+ */
+ history: boolean;
+ /**
+ * The document Ctrl+Z should return to, when it is NOT the one the store holds
+ * at the moment of the write.
+ *
+ * A live drag writes every pointermove straight into the store with
+ * `history: false`, so by the time the pointerup commit runs, the "previous"
+ * document the store holds is the dragged one -- recording that would make the
+ * gesture un-undoable. The commit passes the PRE-DRAG document here instead, and
+ * because `saveDocument` records only after the write succeeded, a failed commit
+ * records nothing at all and its rollback has nothing to pop.
+ *
+ * `null` means "there is no state to go back to" and records nothing. Omitting
+ * the field falls back to the store's current document, which is what an
+ * ordinary edit wants.
*/
- history?: boolean;
+ historyBase?: AxcutDocument | null;
}
export interface ProjectState {
@@ -64,8 +87,8 @@ export interface ProjectState {
* an unhandled rejection in the renderer with no toast, no log and no clue --
* change a caption font on a read-only project and the edit was simply gone.
*/
- saveDocument: (document: AxcutDocument, opts?: DocumentWriteOptions) => Promise<boolean>;
- setDocument: (document: AxcutDocument, opts?: DocumentWriteOptions) => void;
+ saveDocument: (document: AxcutDocument, opts: DocumentWriteOptions) => Promise<boolean>;
+ setDocument: (document: AxcutDocument, opts: DocumentWriteOptions) => void;
replaceTimeline: (intervals: Interval[], reason: string) => Promise<void>;
restoreFullTimeline: () => Promise<void>;
setSourceDuration: (sec: number) => void;
@@ -80,10 +103,19 @@ function parseDocument(value: unknown): AxcutDocument {
}
/**
- * Record `prev` as the state Ctrl+Z returns to, unless the caller opted out or
- * this write is not a change (`commit`-style re-saves hand back the very object
- * the store already holds, and a live drag's `setLive` then `commit` pair would
- * otherwise take two undos to reverse one gesture).
+ * The document a write should record as the state Ctrl+Z returns to. Read
+ * SYNCHRONOUSLY, before any await: after one, `get().document` is whatever the
+ * write installed.
+ */
+function historyBaseFor(opts: DocumentWriteOptions, current: AxcutDocument | null) {
+ return opts.historyBase !== undefined ? opts.historyBase : current;
+}
+
+/**
+ * Push `prev` onto the undo stack, unless the caller opted out or this write is
+ * not a change (`commit`-style re-saves hand back the very object the store
+ * already holds, and an optimistic `setDocument` then `saveDocument` pair would
+ * otherwise take two undos to reverse one edit).
*
* Synchronous on purpose -- see the header of `undoStack.ts` for what the
* deferred `import("./undo")` this replaced did to redo.
@@ -91,9 +123,9 @@ function parseDocument(value: unknown): AxcutDocument {
function recordHistory(
prev: AxcutDocument | null,
next: AxcutDocument,
- opts?: DocumentWriteOptions,
+ opts: DocumentWriteOptions,
) {
- if (opts?.history === false) return;
+ if (!opts.history) return;
if (!prev || prev === next) return;
pushHistory({ projectId: prev.project.id, doc: structuredClone(prev) });
}
@@ -276,13 +308,13 @@ export const useProjectStore = create<ProjectState>((set, get) => ({
},
async saveDocument(document, opts) {
- // Snapshot BEFORE the await, while `get().document` is still the pre-edit one.
+ // Read BEFORE the await, while `get().document` is still the pre-edit one.
// This is where undo history actually comes from: the editor writes through
// `saveDocument` for every user edit -- add a region, delete one, rename the
// project, every timeline op -- and `setDocument` is reserved for the handful
// of live/optimistic paths. Recording only in `setDocument` left `past` empty
// for everything the user does, so Ctrl+Z was a no-op (#433).
- recordHistory(get().document, document, opts);
+ const base = historyBaseFor(opts, get().document);
try {
const result = await nativeBridgeClient.aiEdition.save(document);
if (!result.success || !result.document) {
@@ -295,6 +327,14 @@ export const useProjectStore = create<ProjectState>((set, get) => ({
dirty: false,
lastSavedAt: new Date(),
});
+ // Recorded HERE, below the write, and not above it. `saveDocument` resolves
+ // false on a handled failure (a read-only project) and callers read that as
+ // "the edit did not happen". Recording first left `past` holding a snapshot
+ // identical to the live document and `future` wiped, so the next Ctrl+Z
+ // visibly did nothing and redo was gone -- #433's own symptom, re-created by
+ // the fix for it. Nothing between the `set` above and this line awaits, so no
+ // undo can observe the half-applied state.
+ recordHistory(base, document, opts);
return true;
} catch (error) {
// Logged as well as toasted: a toast is gone in five seconds, and "my edit
@@ -312,7 +352,9 @@ export const useProjectStore = create<ProjectState>((set, get) => ({
},
setDocument(document, opts) {
- recordHistory(get().document, document, opts);
+ // No await to sit above: this write cannot fail, so recording it up front is
+ // the same thing as recording it afterwards.
+ recordHistory(historyBaseFor(opts, get().document), document, opts);
set({
document,
revision: get().revision + 1,
@@ -324,14 +366,14 @@ export const useProjectStore = create<ProjectState>((set, get) => ({
const doc = get().document;
if (!doc) throw new Error("No project loaded");
const next = replaceTimelineOp(doc, intervals, reason);
- await get().saveDocument(next);
+ await get().saveDocument(next, { history: true });
},
async restoreFullTimeline() {
const doc = get().document;
if (!doc) throw new Error("No project loaded");
const next = restoreFullTimelineOp(doc);
- await get().saveDocument(next);
+ await get().saveDocument(next, { history: true });
},
setSourceDuration(sec) {
diff --git a/src/lib/ai-edition/store/undo.test.ts b/src/lib/ai-edition/store/undo.test.ts
index 4666a2b56..b85d805b6 100644
--- a/src/lib/ai-edition/store/undo.test.ts
+++ b/src/lib/ai-edition/store/undo.test.ts
@@ -6,10 +6,11 @@
// editor actually makes (add a region, delete one, rename the project) goes
// through `saveDocument`.
+import { act, renderHook } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { createEmptyDocument } from "../schema";
import { useProjectStore } from "./projectStore";
-import { clearHistory, redo, undo } from "./undo";
+import { clearHistory, redo, undo, useUndoRedoShortcuts } from "./undo";
import { future, past } from "./undoStack";
const saveMock = vi.hoisted(() => vi.fn());
@@ -45,7 +46,7 @@ describe("undo/redo", () => {
it("records a saveDocument edit and reverts it", async () => {
// The path every region add / delete / rename takes. It recorded nothing
// before this fix, so `past` stayed empty and `undo()` returned false.
- await useProjectStore.getState().saveDocument(titled("Renamed"));
+ await useProjectStore.getState().saveDocument(titled("Renamed"), { history: true });
expect(currentTitle()).toBe("Renamed");
expect(past).toHaveLength(1);
@@ -55,7 +56,7 @@ describe("undo/redo", () => {
it("reapplies the edit on redo", async () => {
const renamed = titled("Renamed");
- await useProjectStore.getState().saveDocument(renamed);
+ await useProjectStore.getState().saveDocument(renamed, { history: true });
expect(undo()).toBe(true);
expect(currentTitle()).toBe("Original");
@@ -69,7 +70,7 @@ describe("undo/redo", () => {
// after the re-entrancy guard had already been re-armed. The pre-undo document
// landed on `past` and `pushHistory` cleared `future`, so one Ctrl+Z turned the
// history into an A/B toggle with redo permanently gone.
- await useProjectStore.getState().saveDocument(titled("Renamed"));
+ await useProjectStore.getState().saveDocument(titled("Renamed"), { history: true });
expect(past).toHaveLength(1);
expect(undo()).toBe(true);
@@ -83,8 +84,8 @@ describe("undo/redo", () => {
it("walks back more than one level", async () => {
// The microtask race silently capped this at one: the second Ctrl+Z replayed
// the document the first had just pushed back on, so undo oscillated.
- await useProjectStore.getState().saveDocument(titled("Second"));
- await useProjectStore.getState().saveDocument(titled("Third"));
+ await useProjectStore.getState().saveDocument(titled("Second"), { history: true });
+ await useProjectStore.getState().saveDocument(titled("Third"), { history: true });
expect(currentTitle()).toBe("Third");
expect(undo()).toBe(true);
@@ -114,7 +115,7 @@ describe("undo/redo", () => {
// What `NewEditorShell` does in `useUndoRedoShortcuts`'s callback. Without
// `history: false` there, the persist re-records the restored document and
// wipes the redo the undo just created.
- await useProjectStore.getState().saveDocument(titled("Renamed"));
+ await useProjectStore.getState().saveDocument(titled("Renamed"), { history: true });
expect(undo()).toBe(true);
const restored = useProjectStore.getState().document;
@@ -127,18 +128,18 @@ describe("undo/redo", () => {
});
it("drops the redo stack once a new edit lands on an undone document", async () => {
- await useProjectStore.getState().saveDocument(titled("Renamed"));
+ await useProjectStore.getState().saveDocument(titled("Renamed"), { history: true });
expect(undo()).toBe(true);
expect(future).toHaveLength(1);
- await useProjectStore.getState().saveDocument(titled("Different branch"));
+ await useProjectStore.getState().saveDocument(titled("Different branch"), { history: true });
expect(future).toHaveLength(0);
expect(redo()).toBe(false);
});
it("refuses to restore a snapshot from another project", async () => {
- await useProjectStore.getState().saveDocument(titled("Renamed"));
+ await useProjectStore.getState().saveDocument(titled("Renamed"), { history: true });
useProjectStore.setState({ projectId: "project_2" });
expect(undo()).toBe(false);
@@ -147,7 +148,7 @@ describe("undo/redo", () => {
});
it("marks the document dirty so the restore can be persisted", async () => {
- await useProjectStore.getState().saveDocument(titled("Renamed"));
+ await useProjectStore.getState().saveDocument(titled("Renamed"), { history: true });
expect(useProjectStore.getState().dirty).toBe(false);
const revisionBefore = useProjectStore.getState().revision;
@@ -157,4 +158,126 @@ describe("undo/redo", () => {
// Consumers repaint off `document`; `revision` is what the agent-apply guard reads.
expect(useProjectStore.getState().revision).toBe(revisionBefore + 1);
});
+
+ describe("a write that failed", () => {
+ // `saveDocument` resolves false on a handled failure -- a read-only project --
+ // and every caller reads that as "the edit did not happen". Recording history
+ // ABOVE the await recorded it anyway, and nothing ever took it back off.
+
+ it("records no undo step, so Ctrl+Z is not left doing nothing", async () => {
+ // The exact symptom #433 was filed for, re-created by the first fix for it:
+ // `past` held a snapshot identical to the document on screen, so the next
+ // Ctrl+Z restored what was already there.
+ saveMock.mockResolvedValueOnce({ success: false, error: "EACCES" });
+
+ expect(
+ await useProjectStore.getState().saveDocument(titled("Renamed"), { history: true }),
+ ).toBe(false);
+
+ expect(currentTitle()).toBe("Original");
+ expect(past).toHaveLength(0);
+ expect(undo()).toBe(false);
+ });
+
+ it("leaves the redo stack alone", async () => {
+ // Worse than a wasted step: `pushHistory` clears `future` on its way past, so
+ // a failed write destroyed a redo the user had already earned.
+ await useProjectStore.getState().saveDocument(titled("Renamed"), { history: true });
+ expect(undo()).toBe(true);
+ expect(future).toHaveLength(1);
+
+ saveMock.mockResolvedValueOnce({ success: false, error: "EACCES" });
+ expect(
+ await useProjectStore.getState().saveDocument(titled("Rejected"), { history: true }),
+ ).toBe(false);
+
+ expect(future).toHaveLength(1);
+ expect(redo()).toBe(true);
+ expect(currentTitle()).toBe("Renamed");
+ });
+
+ it("records the step once a retry lands", async () => {
+ // Not recording is only correct if the record still happens when the write
+ // eventually succeeds -- otherwise the fix trades one dead Ctrl+Z for another.
+ saveMock.mockResolvedValueOnce({ success: false, error: "EACCES" });
+ await useProjectStore.getState().saveDocument(titled("Renamed"), { history: true });
+
+ await useProjectStore.getState().saveDocument(titled("Renamed"), { history: true });
+
+ expect(currentTitle()).toBe("Renamed");
+ expect(past).toHaveLength(1);
+ expect(undo()).toBe(true);
+ expect(currentTitle()).toBe("Original");
+ });
+ });
+
+ it("records the pre-drag document a commit names, not the one on screen", async () => {
+ // `historyBase`. A live drag writes every pointermove into the store with
+ // `history: false`, so by commit time the store's own "previous" document is the
+ // dragged one -- recording that would leave the gesture un-undoable.
+ const dragged = titled("Dragged");
+ const preDrag = useProjectStore.getState().document;
+ useProjectStore.getState().setDocument(dragged, { history: false });
+ expect(past).toHaveLength(0);
+
+ await useProjectStore.getState().saveDocument(dragged, { history: true, historyBase: preDrag });
+
+ expect(past).toHaveLength(1);
+ expect(undo()).toBe(true);
+ expect(currentTitle()).toBe("Original");
+ });
+});
+
+describe("the Edit menu's undo/redo route", () => {
+ // On macOS the native menu is the ONLY path Cmd+Z has to the renderer: AppKit
+ // matches the menu item's key equivalent before the key event reaches the web
+ // contents, so the keydown listener never runs. `electron/main.ts` therefore
+ // forwards `menu-undo` / `menu-redo` over IPC, and `NewEditorShell` wires them to
+ // these handlers. The Electron half is pinned in `electron/edit-menu.test.ts`.
+ beforeEach(() => {
+ useProjectStore.getState().clear();
+ clearHistory();
+ saveMock.mockReset();
+ saveMock.mockImplementation(async (document: unknown) => ({ success: true, document }));
+ useProjectStore.setState({ projectId: PROJECT_ID, document: titled("Original") });
+ window.document.body.innerHTML = "";
+ });
+
+ it("undoes the document and persists the restore", async () => {
+ const persist = vi.fn();
+ const { result } = renderHook(() => useUndoRedoShortcuts(persist));
+ await useProjectStore.getState().saveDocument(titled("Renamed"), { history: true });
+
+ act(() => {
+ result.current.runUndo();
+ });
+
+ expect(currentTitle()).toBe("Original");
+ expect(persist).toHaveBeenCalledOnce();
+
+ act(() => {
+ result.current.runRedo();
+ });
+ expect(currentTitle()).toBe("Renamed");
+ });
+
+ it("leaves a focused text field to the browser's own text undo", () => {
+ // Same rule the keydown path applies, and the one thing the `undo` role was
+ // still good for.
+ const persist = vi.fn();
+ const { result } = renderHook(() => useUndoRedoShortcuts(persist));
+ past.push({ projectId: PROJECT_ID, doc: titled("Older") });
+
+ const input = window.document.createElement("input");
+ window.document.body.appendChild(input);
+ input.focus();
+
+ act(() => {
+ result.current.runUndo();
+ });
+
+ expect(currentTitle()).toBe("Original");
+ expect(past).toHaveLength(1);
+ expect(persist).not.toHaveBeenCalled();
+ });
});
diff --git a/src/lib/ai-edition/store/undo.ts b/src/lib/ai-edition/store/undo.ts
index 875faaaaa..3c1800f9f 100644
--- a/src/lib/ai-edition/store/undo.ts
+++ b/src/lib/ai-edition/store/undo.ts
@@ -11,7 +11,7 @@
// all a repaint needs — the timeline, preview and shell all subscribe to
// `s.document`.
-import { useEffect, useRef } from "react";
+import { useCallback, useEffect, useRef } from "react";
import { isModalOpen } from "../modalGuard";
import type { AxcutDocument } from "../schema";
import { useProjectStore } from "./projectStore";
@@ -58,13 +58,36 @@ export function redo(): boolean {
return true;
}
-export function useUndoRedoShortcuts(onAfter: () => void) {
+/**
+ * Whether the document undo must keep its hands off: inside a text field the
+ * browser's own text undo is the one the user means. The keydown path checks the
+ * event target, the menu path checks `activeElement` — same rule, two entry
+ * points, so it lives in one function.
+ */
+function isTextEditingTarget(node: EventTarget | null): boolean {
+ if (node instanceof HTMLTextAreaElement || node instanceof HTMLInputElement) return true;
+ return node instanceof HTMLElement && node.isContentEditable;
+}
+
+export interface UndoRedoHandlers {
+ /**
+ * Undo, applying the same text-field rule the keyboard path applies.
+ *
+ * Wired to the native Edit menu, which on macOS is the ONLY route Cmd+Z has:
+ * AppKit matches the menu's key equivalent before the key event reaches the web
+ * contents, so the keydown listener below never runs there. See
+ * `electron/edit-menu.ts`.
+ */
+ runUndo: () => void;
+ runRedo: () => void;
+}
+
+export function useUndoRedoShortcuts(onAfter: () => void): UndoRedoHandlers {
const onAfterRef = useRef(onAfter);
onAfterRef.current = onAfter;
useEffect(() => {
const onKey = (e: KeyboardEvent) => {
- if (e.target instanceof HTMLTextAreaElement || e.target instanceof HTMLInputElement) return;
- if (e.target instanceof HTMLElement && e.target.isContentEditable) return;
+ if (isTextEditingTarget(e.target)) return;
// `NewEditorShell` hands Ctrl+Z / Ctrl+Y to this listener instead of handling them,
// so its modal guard never runs for them: without this one, undo kept rewriting the
// document under every open modal, including the ones the shell does suppress.
@@ -91,4 +114,26 @@ export function useUndoRedoShortcuts(onAfter: () => void) {
window.addEventListener("keydown", onKey);
return () => window.removeEventListener("keydown", onKey);
}, []);
+
+ // `execCommand` is the only handle the renderer has on the browser's text undo,
+ // and it is what the `undo` menu ROLE reached through `webContents.undo()`. It is
+ // deprecated and absent under jsdom, hence the optional call: losing text undo in
+ // a field is survivable, a TypeError from a menu click is not.
+ const runUndo = useCallback(() => {
+ if (isTextEditingTarget(window.document.activeElement)) {
+ window.document.execCommand?.("undo");
+ return;
+ }
+ if (undo()) onAfterRef.current();
+ }, []);
+
+ const runRedo = useCallback(() => {
+ if (isTextEditingTarget(window.document.activeElement)) {
+ window.document.execCommand?.("redo");
+ return;
+ }
+ if (redo()) onAfterRef.current();
+ }, []);
+
+ return { runUndo, runRedo };
}
diff --git a/src/lib/ai-edition/store/useCaptions.ts b/src/lib/ai-edition/store/useCaptions.ts
index dabf3f8d9..b7e05c3e6 100644
--- a/src/lib/ai-edition/store/useCaptions.ts
+++ b/src/lib/ai-edition/store/useCaptions.ts
@@ -65,23 +65,29 @@ export function useCaptions(): UseCaptionsResult {
const doc = useProjectStore.getState().document;
if (!doc) return;
const next = patchCaptionSettings(doc, patch);
- setDocument(next);
- await saveDocument(next);
+ // The optimistic write is not the edit — the save is. Only the one that can
+ // fail records, and it names `doc` as what Ctrl+Z returns to because by then
+ // the store already holds `next`.
+ setDocument(next, { history: false });
+ await saveDocument(next, { history: true, historyBase: doc });
},
[setDocument, saveDocument],
);
// See `useEditorSettings.setLive`: one undo step per slider drag, not one per
// pointer move. `liveDocRef` holds what this hook last wrote, so only a write
- // landing on someone else's document opens a new history entry.
+ // landing on someone else's document opens a new history entry -- and that entry
+ // is `liveBaseRef`, handed to the commit rather than recorded on the spot.
const liveDocRef = useRef<AxcutDocument | null>(null);
+ const liveBaseRef = useRef<AxcutDocument | null>(null);
const setLive = useCallback(
(patch: CaptionSettingsPatch) => {
const doc = useProjectStore.getState().document;
if (!doc) return;
const next = patchCaptionSettings(doc, patch);
- setDocument(next, { history: liveDocRef.current !== doc });
+ if (liveDocRef.current !== doc) liveBaseRef.current = doc;
+ setDocument(next, { history: false });
liveDocRef.current = next;
},
[setDocument],
@@ -90,7 +96,10 @@ export function useCaptions(): UseCaptionsResult {
const commit = useCallback(async () => {
const doc = useProjectStore.getState().document;
if (!doc) return;
- await saveDocument(doc);
+ const base = liveBaseRef.current;
+ liveBaseRef.current = null;
+ liveDocRef.current = null;
+ await saveDocument(doc, { history: true, historyBase: base });
}, [saveDocument]);
const saveTranslation = useCallback<UseCaptionsResult["saveTranslation"]>(
@@ -98,8 +107,8 @@ export function useCaptions(): UseCaptionsResult {
const doc = useProjectStore.getState().document;
if (!doc) return;
const next = putCaptionTranslation(doc, input);
- setDocument(next);
- await saveDocument(next);
+ setDocument(next, { history: false });
+ await saveDocument(next, { history: true, historyBase: doc });
},
[setDocument, saveDocument],
);
@@ -115,8 +124,8 @@ export function useCaptions(): UseCaptionsResult {
getCaptionSettings(cleared).language === language
? patchCaptionSettings(cleared, { language: null })
: cleared;
- setDocument(next);
- await saveDocument(next);
+ setDocument(next, { history: false });
+ await saveDocument(next, { history: true, historyBase: doc });
},
[setDocument, saveDocument],
);
diff --git a/src/lib/ai-edition/store/useEditorSettings.ts b/src/lib/ai-edition/store/useEditorSettings.ts
index 9af58dd79..032ec8230 100644
--- a/src/lib/ai-edition/store/useEditorSettings.ts
+++ b/src/lib/ai-edition/store/useEditorSettings.ts
@@ -48,8 +48,11 @@ export function useEditorSettings(): UseEditorSettingsResult {
const doc = useProjectStore.getState().document;
if (!doc) return;
const next = patchEditorSettings(doc, patch);
- setDocument(next);
- await saveDocument(next);
+ // The optimistic write is not the edit — the save is. Only the one that can
+ // fail records, and it names `doc` as what Ctrl+Z returns to because by then
+ // the store already holds `next`.
+ setDocument(next, { history: false });
+ await saveDocument(next, { history: true, historyBase: doc });
},
[setDocument, saveDocument],
);
@@ -57,15 +60,19 @@ export function useEditorSettings(): UseEditorSettingsResult {
// The document this hook's own last `setLive` produced. A slider drag fires one
// `setLive` per pointer move, and recording each of them buried the real history
// under sixty one-pixel steps; only the first write of a drag — the one editing a
- // document this callback did not produce — is a state worth returning to.
+ // document this callback did not produce — is a state worth returning to. It is
+ // held in `liveBaseRef` and recorded by `commit`, not here: a snapshot pushed
+ // mid-drag is on the stack whether or not the commit that follows ever lands.
const liveDocRef = useRef<AxcutDocument | null>(null);
+ const liveBaseRef = useRef<AxcutDocument | null>(null);
const setLive = useCallback(
(patch: EditorSettingsPatch) => {
const doc = useProjectStore.getState().document;
if (!doc) return;
const next = patchEditorSettings(doc, patch);
- setDocument(next, { history: liveDocRef.current !== doc });
+ if (liveDocRef.current !== doc) liveBaseRef.current = doc;
+ setDocument(next, { history: false });
liveDocRef.current = next;
},
[setDocument],
@@ -74,7 +81,10 @@ export function useEditorSettings(): UseEditorSettingsResult {
const commit = useCallback(async () => {
const doc = useProjectStore.getState().document;
if (!doc) return;
- await saveDocument(doc);
+ const base = liveBaseRef.current;
+ liveBaseRef.current = null;
+ liveDocRef.current = null;
+ await saveDocument(doc, { history: true, historyBase: base });
}, [saveDocument]);
return { settings, hasDocument, set, setLive, commit };
diff --git a/src/lib/ai-edition/store/useSequentialTimelineOps.test.ts b/src/lib/ai-edition/store/useSequentialTimelineOps.test.ts
index 13b097255..a72b146bd 100644
--- a/src/lib/ai-edition/store/useSequentialTimelineOps.test.ts
+++ b/src/lib/ai-edition/store/useSequentialTimelineOps.test.ts
@@ -74,7 +74,7 @@ describe("useSequentialTimelineOps", () => {
// Mirror the real store: write the saved doc back so the next
// call in the queue sees the latest committed state, and report
// success the way `projectStore.saveDocument` does.
- useProjectStore.getState().setDocument(doc);
+ useProjectStore.getState().setDocument(doc, { history: true });
callOrder.push(doc.timeline.trimRanges[0]?.startSec.toString() ?? "empty");
return true;
});
@@ -128,7 +128,7 @@ describe("useSequentialTimelineOps", () => {
.fn<(doc: AxcutDocument) => Promise<boolean>>()
.mockResolvedValueOnce(false)
.mockImplementationOnce(async (doc) => {
- useProjectStore.getState().setDocument(doc);
+ useProjectStore.getState().setDocument(doc, { history: true });
return true;
});
@@ -162,7 +162,7 @@ describe("useSequentialTimelineOps", () => {
expect(secondResult).not.toBeNull();
// The queue survived the first failure — both saves were attempted.
expect(saveDocument).toHaveBeenCalledTimes(2);
- expect(saveDocument).toHaveBeenNthCalledWith(2, secondResult);
+ expect(saveDocument).toHaveBeenNthCalledWith(2, secondResult, { history: true });
});
it("runs an enqueued write after the op ahead of it has committed", async () => {
@@ -170,7 +170,7 @@ describe("useSequentialTimelineOps", () => {
useProjectStore.setState({ document: seed });
const saveDocument = vi.fn(async (doc: AxcutDocument) => {
- useProjectStore.getState().setDocument(doc);
+ useProjectStore.getState().setDocument(doc, { history: true });
return true;
});
@@ -203,7 +203,7 @@ describe("useSequentialTimelineOps", () => {
useProjectStore.setState({ document: seed });
const saveDocument = vi.fn(async (doc: AxcutDocument) => {
- useProjectStore.getState().setDocument(doc);
+ useProjectStore.getState().setDocument(doc, { history: true });
return true;
});
@@ -261,7 +261,7 @@ describe("useSequentialTimelineOps", () => {
useProjectStore.setState({ document: seed });
const saveDocument = vi.fn(async (doc: AxcutDocument) => {
- useProjectStore.getState().setDocument(doc);
+ useProjectStore.getState().setDocument(doc, { history: true });
return true;
});
diff --git a/src/lib/ai-edition/store/useSequentialTimelineOps.ts b/src/lib/ai-edition/store/useSequentialTimelineOps.ts
index b77756bf4..45742754e 100644
--- a/src/lib/ai-edition/store/useSequentialTimelineOps.ts
+++ b/src/lib/ai-edition/store/useSequentialTimelineOps.ts
@@ -30,7 +30,7 @@
import { useCallback, useRef } from "react";
import type { AxcutTimelineOperation } from "@/lib/ai-edition/document/operations";
import type { AxcutDocument } from "@/lib/ai-edition/schema";
-import { useProjectStore } from "./projectStore";
+import { type DocumentWriteOptions, useProjectStore } from "./projectStore";
export interface SequentialTimelineOps {
/**
@@ -63,8 +63,12 @@ export function useSequentialTimelineOps(options: {
/** Used only when the project store has no document yet. */
fallbackDocument: AxcutDocument | null;
/** Persist a document, resolving false if the write failed (already reported).
- * The hook awaits this before unblocking the queue. */
- saveDocument: (doc: AxcutDocument) => Promise<boolean>;
+ * The hook awaits this before unblocking the queue.
+ *
+ * Typed with the store's own options parameter rather than a one-argument
+ * narrowing of it: every queued op here is a user edit, and the hook has to be
+ * able to say so. */
+ saveDocument: (doc: AxcutDocument, opts: DocumentWriteOptions) => Promise<boolean>;
}): SequentialTimelineOps {
const { fallbackDocument, saveDocument } = options;
const saveQueueRef = useRef<Promise<unknown>>(Promise.resolve());
@@ -94,7 +98,7 @@ export function useSequentialTimelineOps(options: {
const doc = useProjectStore.getState().document ?? fallbackDocument;
if (!doc) return null;
const applied = applyTimelineOperation(doc, op);
- return (await saveDocument(applied.document)) ? applied.document : null;
+ return (await saveDocument(applied.document, { history: true })) ? applied.document : null;
}),
[enqueue, fallbackDocument, saveDocument],
);
diff --git a/src/lib/ai-edition/store/useTimeline.test.ts b/src/lib/ai-edition/store/useTimeline.test.ts
index 939fc7c9d..08ab1c0da 100644
--- a/src/lib/ai-edition/store/useTimeline.test.ts
+++ b/src/lib/ai-edition/store/useTimeline.test.ts
@@ -5,6 +5,8 @@ import { I18nProvider } from "@/contexts/I18nContext";
import type { AxcutDocument } from "../schema";
import { axcutSchemaVersion } from "../schema";
import { useProjectStore } from "./projectStore";
+import { clearHistory, redo, undo } from "./undo";
+import { future, past } from "./undoStack";
import { useTimeline } from "./useTimeline";
/**
@@ -898,3 +900,195 @@ describe("useTimeline save failures", () => {
expect(useProjectStore.getState().document?.zoomRanges).toHaveLength(0);
});
});
+
+describe("useTimeline undo history", () => {
+ // `addAsset` (electron/ai-edition/document-service.ts) never writes `durationSec`,
+ // so EVERY freshly imported asset lands at the 60s placeholder and fires the
+ // background probe. Anything the probe records is therefore on the undo stack of
+ // every single drop, which is what makes these two the common case and not an
+ // edge one.
+ const unprobed = {
+ id: "asset_3",
+ kind: "video" as const,
+ label: "fresh-import.mp4",
+ originalPath: "/tmp/fresh-import.mp4",
+ durationSec: undefined,
+ // No `video` either: that is what `addAsset` produces, and it is what makes
+ // `probeAndCorrectClip` save even once the clip it came for is gone.
+ cameraTrack: null,
+ };
+
+ const docWithZoom: AxcutDocument = {
+ ...sampleDoc,
+ zoomRanges: [
+ {
+ id: "zoom_a",
+ startMs: 1000,
+ endMs: 3000,
+ depth: 3,
+ focus: { cx: 0.5, cy: 0.5 },
+ focusMode: "manual",
+ clipId: "clip_a",
+ sourceStartSec: 1,
+ sourceEndSec: 3,
+ },
+ ],
+ };
+
+ beforeEach(() => {
+ useProjectStore.getState().clear();
+ clearHistory();
+ for (const mock of Object.values(bridgeMocks)) mock.mockReset();
+ probeVideoDurationMock.mockReset();
+ probeVideoDimensionsMock.mockResolvedValue({ width: 1920, height: 1080 });
+ bridgeMocks.save.mockImplementation(async (doc: typeof sampleDoc) => ({
+ success: true,
+ document: doc,
+ }));
+ });
+
+ afterEach(() => {
+ vi.clearAllMocks();
+ });
+
+ function seed(document: AxcutDocument) {
+ useProjectStore.setState({
+ projectId: "proj_test",
+ document,
+ revision: 1,
+ status: "ready",
+ error: null,
+ });
+ }
+
+ it("keeps the background duration probe off the undo stack", async () => {
+ // Without `{ history: false }` on `probeAndCorrectClip`'s save, dropping a clip
+ // left `past` = [beforeDrop, dropWithPlaceholderClip]: the first Ctrl+Z snapped
+ // the clip back to a 60s placeholder instead of removing it.
+ seed({ ...sampleDoc, assets: [...sampleDoc.assets, unprobed] });
+ probeVideoDurationMock.mockResolvedValue(5);
+ const { result } = renderTimeline();
+
+ await act(async () => {
+ await result.current.insertClipAt("asset_3", 1);
+ });
+ await waitFor(() => {
+ const inserted = useProjectStore
+ .getState()
+ .document?.timeline.clips.find((c) => c.assetId === "asset_3");
+ expect(inserted?.sourceEndSec).toBe(5);
+ });
+
+ // One step for the drop the user made, none for the probe that corrected it.
+ expect(past).toHaveLength(1);
+ act(() => {
+ expect(undo()).toBe(true);
+ });
+ expect(useProjectStore.getState().document?.timeline.clips).toHaveLength(1);
+ });
+
+ it("does not let a probe landing after an undo destroy the redo", async () => {
+ // The probe is detached from the drop, so it can resolve at any point — including
+ // after the user has already pressed Ctrl+Z. A recording save there ran
+ // `pushHistory`, which clears `future` on its way past: redo was gone, wiped by a
+ // write the user never made and never saw.
+ seed({ ...sampleDoc, assets: [...sampleDoc.assets, unprobed] });
+ let landProbe!: (durationSec: number) => void;
+ probeVideoDurationMock.mockReturnValue(
+ new Promise<number>((resolvePromise) => {
+ landProbe = resolvePromise;
+ }),
+ );
+ const { result } = renderTimeline();
+
+ await act(async () => {
+ await result.current.insertClipAt("asset_3", 1);
+ });
+ expect(past).toHaveLength(1);
+
+ act(() => {
+ expect(undo()).toBe(true);
+ });
+ expect(useProjectStore.getState().document?.timeline.clips).toHaveLength(1);
+ expect(future).toHaveLength(1);
+
+ await act(async () => {
+ landProbe(5);
+ // The clip is gone, so only the dimensions half of the probe still has
+ // anything to write — and that is exactly the write that used to be recorded.
+ await new Promise((resolveTick) => setTimeout(resolveTick, 0));
+ });
+
+ expect(past).toHaveLength(0);
+ expect(future).toHaveLength(1);
+ act(() => {
+ expect(redo()).toBe(true);
+ });
+ expect(useProjectStore.getState().document?.timeline.clips).toHaveLength(2);
+ });
+
+ it("leaves no undo step behind a focus drag whose commit failed", async () => {
+ // The drag used to push its pre-drag document from the FIRST `setDocument`. When
+ // the commit then failed, `commitZoomFocus` restored that same document through
+ // `setState` — which cannot pop the entry. `past` was left holding a snapshot
+ // identical to what was on screen (a Ctrl+Z that visibly does nothing) and
+ // `future` had already been wiped by the push.
+ seed(docWithZoom);
+ const { result } = renderTimeline();
+
+ // An edit and an undo, so there is a redo entry to lose.
+ await act(async () => {
+ await result.current.updateZoomDepth("zoom_a", 5);
+ });
+ act(() => {
+ expect(undo()).toBe(true);
+ });
+ expect(past).toHaveLength(0);
+ expect(future).toHaveLength(1);
+
+ const beforeDrag = useProjectStore.getState().document;
+ bridgeMocks.save.mockResolvedValue({ success: false, error: "disk full" });
+ act(() => {
+ result.current.updateZoomFocusLive("zoom_a", { cx: 0.2, cy: 0.3 });
+ result.current.updateZoomFocusLive("zoom_a", { cx: 0.25, cy: 0.35 });
+ });
+ await act(async () => {
+ await result.current.commitZoomFocus();
+ });
+
+ expect(useProjectStore.getState().document).toBe(beforeDrag);
+ expect(past).toHaveLength(0);
+ expect(future).toHaveLength(1);
+ act(() => {
+ expect(redo()).toBe(true);
+ });
+ expect(useProjectStore.getState().document?.zoomRanges[0].depth).toBe(5);
+ });
+
+ it("records one undo step for a whole focus drag once it commits", async () => {
+ // The other half of the same change: moving the record to the commit must not
+ // lose it. Sixty pointermoves, one Ctrl+Z, back to where the drag started.
+ seed(docWithZoom);
+ const { result } = renderTimeline();
+
+ act(() => {
+ for (let i = 0; i < 60; i++) {
+ result.current.updateZoomFocusLive("zoom_a", { cx: 0.2 + i / 1000, cy: 0.3 });
+ }
+ });
+ expect(past).toHaveLength(0);
+
+ await act(async () => {
+ await result.current.commitZoomFocus();
+ });
+
+ expect(past).toHaveLength(1);
+ act(() => {
+ expect(undo()).toBe(true);
+ });
+ expect(useProjectStore.getState().document?.zoomRanges[0].focus).toEqual({
+ cx: 0.5,
+ cy: 0.5,
+ });
+ });
+});
diff --git a/src/lib/ai-edition/store/useTimeline.ts b/src/lib/ai-edition/store/useTimeline.ts
index c1fb293bf..859adbfe9 100644
--- a/src/lib/ai-edition/store/useTimeline.ts
+++ b/src/lib/ai-edition/store/useTimeline.ts
@@ -241,7 +241,7 @@ export function useTimeline() {
...document,
zoomRanges: [...document.zoomRanges, ...anchored] as AxcutDocument["zoomRanges"],
};
- await saveDocument(next);
+ await saveDocument(next, { history: true });
},
[document, saveDocument],
);
@@ -273,7 +273,7 @@ export function useTimeline() {
...document,
zoomRanges: [...document.zoomRanges, ...anchored] as AxcutDocument["zoomRanges"],
};
- if (!(await saveDocument(next))) return 0;
+ if (!(await saveDocument(next, { history: true }))) return 0;
return suggestions.length;
},
[document, saveDocument],
@@ -314,7 +314,7 @@ export function useTimeline() {
],
},
};
- await saveDocument(next);
+ await saveDocument(next, { history: true });
},
[document, saveDocument],
);
@@ -361,7 +361,7 @@ export function useTimeline() {
...created,
] as unknown as AxcutDocument["annotations"],
};
- if (!(await saveDocument(next))) return;
+ if (!(await saveDocument(next, { history: true }))) return;
// Select the freshly added annotation so its inspector opens and it shows a
// selection box on the canvas, ready to be retyped over.
const newId = created[0]?.id ?? ann.id;
@@ -394,7 +394,7 @@ export function useTimeline() {
],
},
};
- await saveDocument(next);
+ await saveDocument(next, { history: true });
},
[document, saveDocument],
);
@@ -433,7 +433,7 @@ export function useTimeline() {
],
},
};
- await saveDocument(next);
+ await saveDocument(next, { history: true });
},
[document, saveDocument],
);
@@ -469,7 +469,7 @@ export function useTimeline() {
),
},
};
- await saveDocument(nextDoc);
+ await saveDocument(nextDoc, { history: true });
},
[document, saveDocument],
);
@@ -512,10 +512,13 @@ export function useTimeline() {
origin: prev?.origin ?? ("user" as const),
};
});
- await saveDocument({
- ...doc,
- timeline: { ...doc.timeline, trimRanges: [...others, ...rebuilt] },
- });
+ await saveDocument(
+ {
+ ...doc,
+ timeline: { ...doc.timeline, trimRanges: [...others, ...rebuilt] },
+ },
+ { history: true },
+ );
},
[saveDocument],
);
@@ -540,7 +543,7 @@ export function useTimeline() {
() => createId("zoom"),
) as AxcutDocument["zoomRanges"],
};
- await saveDocument(next);
+ await saveDocument(next, { history: true });
},
[document, saveDocument],
);
@@ -555,18 +558,20 @@ export function useTimeline() {
const doc = useProjectStore.getState().document;
if (!doc) return;
// The first live write of a drag is the one editing a document this callback
- // did not itself produce — so it is the pre-drag state, and the only one worth
- // recording. Without the flag a pointermove-frequency drag pushed ~60 snapshots
- // a second and evicted the real history behind it.
- const dragStart = zoomFocusLiveRef.current !== doc;
- if (dragStart) zoomFocusRollbackRef.current = doc;
+ // did not itself produce, so it is the pre-drag state — the one thing worth
+ // returning to. It is remembered, not recorded: `commitZoomFocus` hands it to
+ // `saveDocument` as `historyBase`, so the whole gesture becomes ONE undo step
+ // and only once the write landed. Recording it here instead left the entry
+ // behind when the commit failed and rolled the document back to that very
+ // document — a Ctrl+Z that visibly did nothing, with `future` already wiped.
+ if (zoomFocusLiveRef.current !== doc) zoomFocusRollbackRef.current = doc;
const next: AxcutDocument = {
...doc,
zoomRanges: patchPillById(doc.zoomRanges, id, {
focus: { cx: finiteFraction(focus.cx), cy: finiteFraction(focus.cy) },
}) as AxcutDocument["zoomRanges"],
};
- setDocument(next, { history: dragStart });
+ setDocument(next, { history: false });
zoomFocusLiveRef.current = next;
},
[setDocument],
@@ -578,7 +583,10 @@ export function useTimeline() {
const rollback = zoomFocusRollbackRef.current;
zoomFocusRollbackRef.current = null;
zoomFocusLiveRef.current = null;
- if (!(await saveDocument(doc)) && rollback) {
+ // `historyBase: rollback` — the pre-drag document, not the one the store holds
+ // (that is the dragged one, written live). `null` when no live write happened,
+ // which records nothing, which is right: nothing changed.
+ if (!(await saveDocument(doc, { history: true, historyBase: rollback })) && rollback) {
useProjectStore.setState((state) =>
// `dirty` is deliberately NOT cleared. The rollback target is the last document
// this drag started from, which is not the same as the last SAVED one: with two
@@ -602,7 +610,7 @@ export function useTimeline() {
depth,
}) as AxcutDocument["zoomRanges"],
};
- await saveDocument(next);
+ await saveDocument(next, { history: true });
},
[document, saveDocument],
);
@@ -621,7 +629,7 @@ export function useTimeline() {
rotationPreset,
}) as AxcutDocument["zoomRanges"],
};
- await saveDocument(next);
+ await saveDocument(next, { history: true });
},
[document, saveDocument],
);
@@ -643,7 +651,7 @@ export function useTimeline() {
focusMode,
}) as AxcutDocument["zoomRanges"],
};
- await saveDocument(next);
+ await saveDocument(next, { history: true });
},
[document, saveDocument],
);
@@ -664,7 +672,7 @@ export function useTimeline() {
() => createId("ann"),
),
};
- await saveDocument(next);
+ await saveDocument(next, { history: true });
},
[document, saveDocument],
);
@@ -676,15 +684,14 @@ export function useTimeline() {
(id: string, patch: Partial<AxcutDocument["annotations"][number]>) => {
const doc = useProjectStore.getState().document;
if (!doc) return;
- // One undo step per drag, not per pointermove — same reasoning as
- // `updateZoomFocusLive` above.
- const dragStart = annotationLiveRef.current !== doc;
- if (dragStart) annotationRollbackRef.current = doc;
+ // One undo step per drag, recorded by the commit once it lands — same
+ // reasoning, and the same failed-commit hole, as `updateZoomFocusLive` above.
+ if (annotationLiveRef.current !== doc) annotationRollbackRef.current = doc;
const next: AxcutDocument = {
...doc,
annotations: patchPillById(doc.annotations, id, patch),
};
- setDocument(next, { history: dragStart });
+ setDocument(next, { history: false });
annotationLiveRef.current = next;
},
[setDocument],
@@ -696,7 +703,9 @@ export function useTimeline() {
const rollback = annotationRollbackRef.current;
annotationRollbackRef.current = null;
annotationLiveRef.current = null;
- if (!(await saveDocument(doc)) && rollback) {
+ // See `commitZoomFocus`: the pre-drag document is the undo target, and it is
+ // recorded only if this write succeeds.
+ if (!(await saveDocument(doc, { history: true, historyBase: rollback })) && rollback) {
useProjectStore.setState((state) =>
// `dirty` is deliberately NOT cleared. The rollback target is the last document
// this drag started from, which is not the same as the last SAVED one: with two
@@ -734,7 +743,7 @@ export function useTimeline() {
),
},
};
- await saveDocument(next);
+ await saveDocument(next, { history: true });
},
[document, saveDocument],
);
@@ -764,7 +773,7 @@ export function useTimeline() {
),
},
};
- await saveDocument(next);
+ await saveDocument(next, { history: true });
},
[document, saveDocument],
);
@@ -786,7 +795,7 @@ export function useTimeline() {
speedRegions: patchPillById(prev, id, { speed }),
},
};
- await saveDocument(next);
+ await saveDocument(next, { history: true });
},
[document, saveDocument],
);
@@ -795,7 +804,8 @@ export function useTimeline() {
async (kind: RegionKind, id: string) => {
if (!document) return;
// One shared mutator with the agent's removeTrim / removeModifier tools.
- if (!(await saveDocument(removeRegionInDocument(document, kind, id)))) return;
+ if (!(await saveDocument(removeRegionInDocument(document, kind, id), { history: true })))
+ return;
if (selection?.id === id) setSelection(null);
setMultiSelection((prev) => prev.filter((h) => h.id !== id));
},
@@ -846,7 +856,7 @@ export function useTimeline() {
? { ...legacy, speedRegions: prevSpeed, cameraFullscreenRegions: prevCameraFullscreen }
: document.legacyEditor,
};
- if (!(await saveDocument(next))) return;
+ if (!(await saveDocument(next, { history: true }))) return;
setSelection(null);
setMultiSelection([]);
},
@@ -931,7 +941,7 @@ export function useTimeline() {
),
},
};
- await saveDocument(next);
+ await saveDocument(next, { history: true });
},
[saveDocument],
);
@@ -996,11 +1006,20 @@ export function useTimeline() {
...(needsDims ? { video: { codec: "unknown", fps: 0, ...a.video, ...probedDims } } : {}),
};
});
- await state.saveDocument({
- ...doc,
- assets: nextAssets,
- timeline: { ...doc.timeline, clips: nextClips },
- });
+ // `history: false`. Nothing about this write is a user action: `addAsset` never
+ // populates `durationSec`, so EVERY freshly imported asset lands at the 60s
+ // placeholder and fires this probe. Recording it put a placeholder-length clip
+ // on the undo stack a beat after the drop, so the first Ctrl+Z snapped the clip
+ // back to 60s instead of removing it — and a probe resolving after the user
+ // had already undone wiped `future`, destroying redo from a background write.
+ await state.saveDocument(
+ {
+ ...doc,
+ assets: nextAssets,
+ timeline: { ...doc.timeline, clips: nextClips },
+ },
+ { history: false },
+ );
},
[],
);
@@ -1047,7 +1066,7 @@ export function useTimeline() {
timeline: { ...currentDoc.timeline, clips: newClips },
};
const finalDoc = rederiveRegionMs(next, newClips);
- if (!(await saveDocument(finalDoc))) return;
+ if (!(await saveDocument(finalDoc, { history: true }))) return;
setClipSelection(newClip.id);
// If we used the placeholder, kick off the probe in the background.
@@ -1076,7 +1095,7 @@ export function useTimeline() {
async (clipId: string, toIndex: number) => {
if (!document) return;
if (!document.timeline.clips.some((c) => c.id === clipId)) return;
- await saveDocument(moveClipInDocument(document, clipId, toIndex));
+ await saveDocument(moveClipInDocument(document, clipId, toIndex), { history: true });
},
[document, saveDocument],
);
@@ -1092,7 +1111,7 @@ export function useTimeline() {
// original, so its index in the result is the original's index + 1.
const insertedIndex = document.timeline.clips.findIndex((c) => c.id === clipId) + 1;
const next = duplicateClipInDocument(document, clipId, "user", "Duplicated clip");
- if (!(await saveDocument(next))) return;
+ if (!(await saveDocument(next, { history: true }))) return;
setClipSelection(next.timeline.clips[insertedIndex]?.id ?? null);
},
[document, saveDocument],
@@ -1102,7 +1121,7 @@ export function useTimeline() {
async (clipId: string) => {
if (!document) return;
// One shared mutator with the agent's removeClip tool: reflow survivors + rederive pills.
- if (!(await saveDocument(removeClipInDocument(document, clipId)))) return;
+ if (!(await saveDocument(removeClipInDocument(document, clipId), { history: true }))) return;
if (clipSelection === clipId) setClipSelection(null);
},
[document, clipSelection, saveDocument],