Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(composer): clear undo history at workspace transitions

yuxinoc737087f863e
diff --git a/apps/desktop/src/features/chat/composer/hooks/useComposerDraft.ts b/apps/desktop/src/features/chat/composer/hooks/useComposerDraft.ts
index 44dfe6ab5..39069fd6b 100644
--- a/apps/desktop/src/features/chat/composer/hooks/useComposerDraft.ts
+++ b/apps/desktop/src/features/chat/composer/hooks/useComposerDraft.ts
@@ -168,7 +168,13 @@ export function useComposerDraft({
const deletedReferencesRef = useRef(new Map<string, ComposerFileReference>());
useEffect(() => {
deletedReferencesRef.current.clear();
- }, [draftKey, workspacePath]);
+ // Observe every transition, including A -> B -> A in one React batch.
+ return useAppStore.subscribe((state, previous) => {
+ if ((state.workspace?.path ?? "") !== (previous.workspace?.path ?? "")) {
+ deletedReferencesRef.current.clear();
+ }
+ });
+ }, [draftKey]);
const reconcileEditorReferences = (text: string) => {
const current = fileReferencesRef.current;
diff --git a/scripts/e2e/composer-paste.tsx b/scripts/e2e/composer-paste.tsx
index fc6338d93..93ecee6c7 100644
--- a/scripts/e2e/composer-paste.tsx
+++ b/scripts/e2e/composer-paste.tsx
@@ -216,47 +216,6 @@ globalThis.composerPasteProbe = async () => {
assert(controller.fileReferences.length === 1 && controller.fileReferences[0].path === references[1].path,
"changing workspace must preserve scratch references");
- // Native undo must restore reference metadata as well as the visible chip.
- await reset("inspect \uE050 please", 8, 9);
- const undoReference = createFileReference("src/main.ts", "main.ts", "paste-a", { token: "\uE050" });
- flushSync(() => controller.applyEditorDraft("inspect \uE050 please", [undoReference], 9));
- await new Promise(requestAnimationFrame);
- const undoEditor = controller.ref.current!;
- undoEditor.focus();
- select(undoEditor, 8, 9);
- assert(document.execCommand("delete"), "native chip deletion unavailable");
- await new Promise(requestAnimationFrame);
- assert(document.execCommand("undo"), "native chip undo unavailable");
- await new Promise(requestAnimationFrame);
- assert(controller.fileReferences.some(r => r.path === "src/main.ts"), "Undo restored the chip without its file reference metadata");
- assert(serializeInlineComposerFileReferences(readEditorValue(undoEditor), controller.activeFileReferences) === "inspect @src/main.ts please",
- "undo must restore the path used by submission");
- assert(document.execCommand("redo"), "native chip redo unavailable");
- await new Promise(requestAnimationFrame);
- assert(controller.fileReferences.length === 0, "redo retained a deleted attachment");
- assert(document.execCommand("undo"), "second native chip undo unavailable");
- await new Promise(requestAnimationFrame);
- assert(controller.fileReferences.length === 1, "repeated undo lost the attachment");
- render("paste-b");
- await new Promise(requestAnimationFrame);
- assert(controller.fileReferences.length === 0, "undo metadata leaked into another chat");
- render("paste-a");
- await new Promise(requestAnimationFrame);
- assert(controller.fileReferences.some(r => r.path === "src/main.ts"),
- "the restored reference did not survive a chat round-trip");
- const restoredEditor = controller.ref.current!;
- restoredEditor.focus();
- select(restoredEditor, 8, 9);
- assert(document.execCommand("delete"), "second chip deletion unavailable");
- await new Promise(requestAnimationFrame);
- assert(document.execCommand("insertText", false, "\uE050"), "private-use text insertion unavailable");
- await new Promise(requestAnimationFrame);
- assert(controller.fileReferences.length === 0,
- "typing a removed chip's token must not resurrect an attachment");
-
- flushSync(() => root.render(null));
- resetComposerDraftCache();
-
// Keep the source attachment snapshot when a paste finishes in another session.
await reset("keep \uE010 ", 7, 7);
const originalReference = createFileReference("/scratch/paste-a/original.txt", "original.txt", "paste-a", { token: "\uE010", kind: "file" });
@@ -746,6 +705,47 @@ globalThis.composerPasteProbe = async () => {
await new Promise(requestAnimationFrame);
assert(readEditorValue(controller.ref.current!) === "retry draft" && controller.fileReferences[0]?.path === imageReference.path,
"fast rejection before React commits must restore the text and attachments");
+ // Native undo must restore reference metadata as well as the visible chip.
+ await reset("inspect \uE050 please", 8, 9);
+ const undoReference = createFileReference("src/main.ts", "main.ts", "paste-a", { token: "\uE050" });
+ flushSync(() => controller.applyEditorDraft("inspect \uE050 please", [undoReference], 9));
+ await new Promise(requestAnimationFrame);
+ const undoEditor = controller.ref.current!;
+ undoEditor.focus();
+ select(undoEditor, 8, 9);
+ assert(document.execCommand("delete"), "native chip deletion unavailable");
+ await new Promise(requestAnimationFrame);
+ assert(document.execCommand("undo"), "native chip undo unavailable");
+ await new Promise(requestAnimationFrame);
+ assert(controller.fileReferences.some(r => r.path === "src/main.ts"), "Undo restored the chip without its file reference metadata");
+ assert(serializeInlineComposerFileReferences(readEditorValue(undoEditor), controller.activeFileReferences) === "inspect @src/main.ts please",
+ "undo must restore the path used by submission");
+ assert(document.execCommand("redo"), "native chip redo unavailable");
+ await new Promise(requestAnimationFrame);
+ assert(controller.fileReferences.length === 0, "redo retained a deleted attachment");
+ assert(document.execCommand("undo"), "second native chip undo unavailable");
+ await new Promise(requestAnimationFrame);
+ assert(controller.fileReferences.length === 1, "repeated undo lost the attachment");
+ render("paste-b");
+ await new Promise(requestAnimationFrame);
+ assert(controller.fileReferences.length === 0, "undo metadata leaked into another chat");
+ render("paste-a");
+ await new Promise(requestAnimationFrame);
+ assert(controller.fileReferences.some(r => r.path === "src/main.ts"),
+ "the restored reference did not survive a chat round-trip");
+ const restoredEditor = controller.ref.current!;
+ restoredEditor.focus();
+ select(restoredEditor, 8, 9);
+ assert(document.execCommand("delete"), "second chip deletion unavailable");
+ await new Promise(requestAnimationFrame);
+ assert(document.execCommand("insertText", false, "\uE050"), "private-use text insertion unavailable");
+ await new Promise(requestAnimationFrame);
+ assert(controller.fileReferences.length === 0,
+ "typing a removed chip's token must not resurrect an attachment");
+
+ flushSync(() => root.render(null));
+ resetComposerDraftCache();
+
await verifyComposerSubmission(imageReference.path, i18n);
return {
ok: true,