Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
fix(composer): restore file references with native undo
yuxino41719e97c4f2
diff --git a/apps/desktop/src/features/chat/composer/hooks/useComposerDraft.ts b/apps/desktop/src/features/chat/composer/hooks/useComposerDraft.tsindex 4be449382..44dfe6ab5 100644--- a/apps/desktop/src/features/chat/composer/hooks/useComposerDraft.ts+++ b/apps/desktop/src/features/chat/composer/hooks/useComposerDraft.ts@@ -164,6 +164,32 @@ export function useComposerDraft({}return map;}, [activeFileReferences]);+ // Native undo restores chip DOM, but deletion has already removed its metadata.+ const deletedReferencesRef = useRef(new Map<string, ComposerFileReference>());+ useEffect(() => {+ deletedReferencesRef.current.clear();+ }, [draftKey, workspacePath]);++ const reconcileEditorReferences = (text: string) => {+ const current = fileReferencesRef.current;+ const next = current.filter((reference) => {+ if (!reference.token || text.includes(reference.token)) return true;+ deletedReferencesRef.current.set(reference.token, reference);+ return false;+ });+ // Only recover an actual restored chip, never a pasted private-use character.+ for (const chip of ref.current?.querySelectorAll<HTMLElement>(".composer-chip") ?? []) {+ const token = chip.dataset.token ?? "";+ const reference = deletedReferencesRef.current.get(token);+ if (!reference || !text.includes(token)) continue;+ if (!next.some((item) => item.token === token)) next.push(reference);+ deletedReferencesRef.current.delete(token);+ }+ if (next.length === current.length && next.every((reference, index) => reference === current[index])) return;+ fileReferencesRef.current = next;+ setFileReferences(next);+ };+const referenceByTokenRef = useRef(referenceByToken);referenceByTokenRef.current = referenceByToken;const imagePreview = useComposerImagePreview({ references: fileReferences, value, sessionId: referenceSessionId, editorRef: ref });@@ -273,13 +299,7 @@ export function useComposerDraft({}valueRef.current = nextValue;setValue(nextValue);- setFileReferences((current) => {- const next = current.filter(- (fileReference) =>- !fileReference.token || nextValue.includes(fileReference.token),- );- return next.length === current.length ? current : next;- });+ reconcileEditorReferences(nextValue);updateCursor(caret);return nextValue;};@@ -294,13 +314,7 @@ export function useComposerDraft({editorValueRef.current = nextValue;valueRef.current = nextValue;setValue(nextValue);- setFileReferences((current) => {- const next = current.filter(- (fileReference) =>- !fileReference.token || nextValue.includes(fileReference.token),- );- return next.length === current.length ? current : next;- });+ reconcileEditorReferences(nextValue);updateCursor(start);};@@ -557,6 +571,7 @@ export function useComposerDraft({deleteComposerDraft(key);const currentKey = draftKeyForSession(useAppStore.getState().activeSessionId);if (currentKey !== key) return;+ deletedReferencesRef.current.clear();valueRef.current = "";if (ref.current) paintCurrentDraft(ref.current, "");setValue("");diff --git a/docs/spec/04-ux/08-component-spec.md b/docs/spec/04-ux/08-component-spec.mdindex bec306927..65b153c7c 100644--- a/docs/spec/04-ux/08-component-spec.md+++ b/docs/spec/04-ux/08-component-spec.md@@ -3800,7 +3800,10 @@ remove a newly created BR only when removing that exact node makes the draftmatch the requested deletion. Never trim leading newlines or normalize all BRs.Remember proven placeholder nodes weakly so native redo cannot restore them.Explicit line breaks, IME composition, file references, and chip deletion retain-their normal behavior. The input owns and disposes the native event listeners.+their normal behavior. Native undo of chip deletion restores its file-reference metadata+as well as its DOM, so submission and draft caching retain the path. Deleted+reference history is local to the current draft/workspace and is cleared on send;+pasting a private-use character alone must not restore an attachment. The input owns and disposes the native event listeners.### Dialog long-text containmentdiff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.mdindex fe553e47d..04f4ab8b3 100644--- a/docs/spec/06-delivery/04-e2e-test-plan.md+++ b/docs/spec/06-delivery/04-e2e-test-plan.md@@ -1470,6 +1470,16 @@ identify the platform validation still needed.`session-switch-performance.test.mjs`); full UI scenario Draft+#### Composer file-reference undo regression++- Add a file reference to an unsent draft, select the chip, delete it, then+ undo and redo with the native editor shortcuts. Undo must restore both the+ chip and the file path used by submission; redo must remove both.+- Switch to another chat and back after undo: the other draft stays empty and+ the source retains the restored reference. Typing a removed chip's private-use+ token as plain text must not restore the attachment.+- Automated by `pnpm test:e2e:composer-paste` (`fileReferenceUndoRedo`).+### Conversation Top Bar#### E2E-087: Conversation top bar renders on the chat routediff --git a/scripts/e2e/composer-paste.tsx b/scripts/e2e/composer-paste.tsxindex b690fe228..fc6338d93 100644--- a/scripts/e2e/composer-paste.tsx+++ b/scripts/e2e/composer-paste.tsx@@ -1,3 +1,4 @@+import { serializeInlineComposerFileReferences } from "@pi-desktop/shared";import { useComposerSubmit } from "../../apps/desktop/src/features/chat/composer/hooks/useComposerSubmit";import { verifyComposerSubmission } from "./composer-submission";import { ComposerImageAttachments } from "../../apps/desktop/src/features/chat/composer/ComposerImageAttachments";@@ -214,6 +215,45 @@ globalThis.composerPasteProbe = async () => {"changing workspace while the composer is unmounted must remove the previous workspace's chip");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();@@ -712,6 +752,7 @@ globalThis.composerPasteProbe = async () => {fullComposerSubmissionAndOverflow: true,mixedShortText: true,multilineAndUndoRedo: true,+ fileReferenceUndoRedo: true,crossBreakAndChipSelection: true,mixedLongText: true,imageOnly: true,