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): retain draft workspace ownership across remounts
vastsaefafd7dc1d49
diff --git a/apps/desktop/src/features/chat/composer/hooks/useComposerDraft.ts b/apps/desktop/src/features/chat/composer/hooks/useComposerDraft.tsindex b7df25786..4be449382 100644--- a/apps/desktop/src/features/chat/composer/hooks/useComposerDraft.ts+++ b/apps/desktop/src/features/chat/composer/hooks/useComposerDraft.ts@@ -138,7 +138,9 @@ export function useComposerDraft({const ref = useRef<HTMLDivElement>(null);const placeholderContextRef = useRef(`${variant}:${activeSessionId ?? HOME_DRAFT_KEY}`);const draftKeyRef = useRef(draftKey);- const previousWorkspacePathRef = useRef(workspacePath);+ const workspacePathRef = useRef(workspacePath);+ workspacePathRef.current = workspacePath;+ const previousWorkspacePathRef = useRef(initialDraft?.workspacePath ?? workspacePath);// Keep one guidance copy stable until the user changes page or session.useEffect(() => {@@ -178,7 +180,12 @@ export function useComposerDraft({const readLiveDraft = () =>ref.current ? readEditorValue(ref.current) : valueRef.current;const persistDraft = (key = draftKeyRef.current) =>- captureComposerDraft(key, readLiveDraft(), fileReferencesRef.current);+ captureComposerDraft(+ key,+ readLiveDraft(),+ fileReferencesRef.current,+ workspacePathRef.current,+ );const paintCurrentDraft = (element: HTMLElement, nextValue: string) => {paintEditorValue(@@ -338,8 +345,13 @@ export function useComposerDraft({}, [draftKey, referenceSessionId]);useEffect(() => {- captureComposerDraft(draftKey, valueRef.current, fileReferences);- }, [draftKey, fileReferences, referenceSessionId]);+ captureComposerDraft(+ draftKey,+ valueRef.current,+ fileReferences,+ workspacePath,+ );+ }, [draftKey, fileReferences, referenceSessionId, workspacePath]);useEffect(() => {pruneComposerDrafts([diff --git a/apps/desktop/src/lib/composer-draft-cache.ts b/apps/desktop/src/lib/composer-draft-cache.tsindex 4efdf57dd..13ece819f 100644--- a/apps/desktop/src/lib/composer-draft-cache.ts+++ b/apps/desktop/src/lib/composer-draft-cache.ts@@ -1,5 +1,10 @@import type { ComposerDraftSnapshot } from "./composer-smart-stop";+type CachedComposerDraft = ComposerDraftSnapshot & {+ /** Workspace that owned relative file references when the draft was captured. */+ workspacePath?: string;+};+/*** Renderer-memory composer drafts (D301).*@@ -20,7 +25,7 @@ export type ComposerDraftFileInput = {token?: string;};-const cache = new Map<string, ComposerDraftSnapshot>();+const cache = new Map<string, CachedComposerDraft>();export function draftKeyForSession(sessionId: string | null | undefined): string {return sessionId ?? HOME_DRAFT_KEY;@@ -35,9 +40,10 @@ export function snapshotComposerDraft(text: string,fileReferences: readonly ComposerDraftFileInput[],key: string,-): ComposerDraftSnapshot {+ workspacePath?: string,+): CachedComposerDraft {const owner = draftOwnerSessionId(key);- return {+ const snapshot: CachedComposerDraft = {text,fileReferences: fileReferences.filter((fileReference) => (fileReference.sessionId ?? "") === owner)@@ -49,25 +55,39 @@ export function snapshotComposerDraft(...(token ? { token } : {}),})),};+ if (workspacePath !== undefined) snapshot.workspacePath = workspacePath;+ return snapshot;}-export function readComposerDraft(key: string): ComposerDraftSnapshot | undefined {+export function readComposerDraft(key: string): CachedComposerDraft | undefined {return cache.get(key);}export function writeComposerDraft(key: string,snapshot: ComposerDraftSnapshot,+ workspacePath?: string,): void {- cache.set(key, snapshot);+ const existing = cache.get(key);+ const next: CachedComposerDraft = {+ ...snapshot,+ fileReferences: snapshot.fileReferences.map((reference) => ({ ...reference })),+ };+ if (workspacePath !== undefined) {+ next.workspacePath = workspacePath;+ } else if (existing?.workspacePath !== undefined && next.workspacePath === undefined) {+ next.workspacePath = existing.workspacePath;+ }+ cache.set(key, next);}export function captureComposerDraft(key: string,text: string,fileReferences: readonly ComposerDraftFileInput[],-): ComposerDraftSnapshot {- const snapshot = snapshotComposerDraft(text, fileReferences, key);+ workspacePath?: string,+): CachedComposerDraft {+ const snapshot = snapshotComposerDraft(text, fileReferences, key, workspacePath);cache.set(key, snapshot);return snapshot;}@@ -100,10 +120,7 @@ export function adoptHomeDraftForSession(sessionId: string): void {cache.delete(HOME_DRAFT_KEY);if (!home) return;if (!home.text && home.fileReferences.length === 0) return;- cache.set(sessionId, {- text: home.text,- fileReferences: home.fileReferences.map((reference) => ({ ...reference })),- });+ writeComposerDraft(sessionId, home);}export function scheduleHomeDraftAdopt(sessionId: string): void {diff --git a/apps/desktop/test/composer-draft-cache.test.mjs b/apps/desktop/test/composer-draft-cache.test.mjsindex 693da6554..193905202 100644--- a/apps/desktop/test/composer-draft-cache.test.mjs+++ b/apps/desktop/test/composer-draft-cache.test.mjs@@ -52,16 +52,31 @@ test("home snapshots keep file references owned by the empty session id", () =>});test("the module cache survives a Composer remount", () => {- captureComposerDraft("sess-a", "draft A", []);+ captureComposerDraft("sess-a", "draft A", [], "/project-a");captureComposerDraft(HOME_DRAFT_KEY, "home draft", [{ sessionId: "", path: "/tmp/note.txt", name: "note.txt", kind: "file" },- ]);+ ], "/project-a");// A remount is just another reader of the same map.assert.equal(readComposerDraft("sess-a")?.text, "draft A");+ assert.equal(readComposerDraft("sess-a")?.workspacePath, "/project-a");assert.equal(readComposerDraft(HOME_DRAFT_KEY)?.text, "home draft");assert.equal(readComposerDraft(HOME_DRAFT_KEY)?.fileReferences[0]?.name, "note.txt");});+test("adopting a home draft preserves its workspace owner", () => {+ captureComposerDraft(HOME_DRAFT_KEY, "home draft", [+ { sessionId: "", path: "src/main.ts", name: "main.ts", kind: "file", token: "\uE000" },+ ], "/project-a");+ adoptHomeDraftForSession("sess-new");+ assert.equal(readComposerDraft("sess-new")?.workspacePath, "/project-a");+});++test("draft writes without workspace metadata retain the existing owner", () => {+ captureComposerDraft("sess-a", "draft A", [], "/project-a");+ writeComposerDraft("sess-a", { text: "draft A with an attachment", fileReferences: [] });+ assert.equal(readComposerDraft("sess-a")?.workspacePath, "/project-a");+});+test("pruning drops deleted sessions and keeps home plus the live key", () => {writeComposerDraft("gone", { text: "stale", fileReferences: [] });writeComposerDraft("kept", { text: "live", fileReferences: [] });diff --git a/scripts/e2e/composer-paste.tsx b/scripts/e2e/composer-paste.tsxindex 806aed972..b690fe228 100644--- a/scripts/e2e/composer-paste.tsx+++ b/scripts/e2e/composer-paste.tsx@@ -207,10 +207,11 @@ globalThis.composerPasteProbe = async () => {"Settings round-trip lost a workspace file reference from the draft");assert(controller.fileReferences.length === 2 && controller.ref.current!.textContent!.includes("main.ts"),"Settings round-trip must restore both workspace and scratch chips");+ flushSync(() => root.render(null));render("paste-a", "/project-b");await new Promise(requestAnimationFrame);assert(readEditorValue(controller.ref.current!) === "Check and \uE002",- "changing workspace must still remove the previous workspace's chip");+ "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");flushSync(() => root.render(null));