Skip to content

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

fix(composer): preserve workspace references across remounts

yuxino6eadcfc3389e
diff --git a/apps/desktop/src/features/chat/composer/hooks/useComposerDraft.ts b/apps/desktop/src/features/chat/composer/hooks/useComposerDraft.ts
index 8cc9878c6..b7df25786 100644
--- a/apps/desktop/src/features/chat/composer/hooks/useComposerDraft.ts
+++ b/apps/desktop/src/features/chat/composer/hooks/useComposerDraft.ts
@@ -138,6 +138,7 @@ export function useComposerDraft({
const ref = useRef<HTMLDivElement>(null);
const placeholderContextRef = useRef(`${variant}:${activeSessionId ?? HOME_DRAFT_KEY}`);
const draftKeyRef = useRef(draftKey);
+ const previousWorkspacePathRef = useRef(workspacePath);
// Keep one guidance copy stable until the user changes page or session.
useEffect(() => {
@@ -385,6 +386,10 @@ export function useComposerDraft({
}, []);
useEffect(() => {
+ // A remount restores this workspace's draft; only a real workspace change
+ // invalidates its relative file references.
+ if (previousWorkspacePathRef.current === workspacePath) return;
+ previousWorkspacePathRef.current = workspacePath;
const current = fileReferencesRef.current;
const kept = current.filter((fileReference) =>
isPersistedScratchReference(fileReference.path),
diff --git a/docs/spec/04-ux/08-component-spec.md b/docs/spec/04-ux/08-component-spec.md
index e45b35364..b2cf5d729 100644
--- a/docs/spec/04-ux/08-component-spec.md
+++ b/docs/spec/04-ux/08-component-spec.md
@@ -2721,7 +2721,10 @@ reasoning-level control.
session (D301). The cache is module-scoped, not instance state, so a remount
— empty-home ↔ docked, chat ↔ Settings/Plugins/other pages, or the window
hiding and showing — restores the same slot. Switching sessions saves the
- source draft and restores the target draft; an uncached target and every
+ source draft and restores the target draft. Restoring a composer in the same
+ workspace must retain relative `@` file references as well as absolute scratch
+ attachments; workspace-reference cleanup runs only when the workspace changes.
+ An uncached target and every
newly created session start empty. The no-active-session home composer has
its own slot. A successful send clears only the submitting session's slot,
including when navigation occurs while the request is in flight, and
diff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.md
index bb5ad75e3..5563f0757 100644
--- a/docs/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/spec/06-delivery/04-e2e-test-plan.md
@@ -1235,7 +1235,10 @@ identify the platform validation still needed.
and inspect the composer. 3) Type a different prompt in B, then switch back
to A. 4) Create a new session and inspect its composer. 5) Return to B and
then delete B; revisit the remaining sessions and the home composer if it is
- available. 6) Type in A, open Settings (or Plugins), then return to chat.
+ available. 6) Type in A, add a workspace file through `@` and a pasted scratch
+ file, open Settings (or Plugins), then return to chat. Confirm both file chips
+ and the surrounding text remain. Change the workspace of the same draft and
+ confirm only its workspace-relative reference is removed.
7) Hide the app window and show it again with an unsent draft in A.
- **Expected**: B initially shows an empty composer, A restores its original
unsent prompt, and the new session starts empty rather than inheriting A or
@@ -1250,7 +1253,9 @@ identify the platform validation still needed.
- **Acceptance**: C (session isolation and composer input)
- **Milestone**: M2
- **Status**: Source-level regression covered
- (`composer-draft-cache.test.mjs`); full UI scenario Draft
+ (`composer-draft-cache.test.mjs`); real React remount and workspace-change
+ coverage in `scripts/e2e/composer-paste.tsx` (`pnpm test:e2e:composer-paste`);
+ full UI scenario Draft
#### E2E-011d: New task creates an immediate durable empty slot
diff --git a/scripts/e2e/composer-paste.tsx b/scripts/e2e/composer-paste.tsx
index 122b55a71..de3867c8f 100644
--- a/scripts/e2e/composer-paste.tsx
+++ b/scripts/e2e/composer-paste.tsx
@@ -45,11 +45,11 @@ let controller: ComposerDraftController;
let pastePending: Promise<unknown> | undefined;
let submitted = 0;
let rejectSubmission: () => Promise<void>;
-function Fixture({ sessionId, t }: { sessionId: string; t: TFunction }) {
+function Fixture({ sessionId, t, workspacePath }: { sessionId: string; t: TFunction; workspacePath: string }) {
const draft = useComposerDraft({
variant: "docked",
activeSessionId: sessionId,
- workspacePath: "",
+ workspacePath,
sessions,
composerPrefill: null,
clearComposerPrefill: noop,
@@ -120,10 +120,10 @@ globalThis.composerPasteProbe = async () => {
onUncaughtError: (error) => errors.push(error),
});
let key = 0;
- const render = (sessionId = "paste-a") => {
+ const render = (sessionId = "paste-a", workspacePath = "") => {
useAppStore.setState({ activeSessionId: sessionId });
flushSync(() =>
- root.render(<I18nextProvider i18n={i18n}><Fixture key={key} sessionId={sessionId} t={i18n.t} /></I18nextProvider>),
+ root.render(<I18nextProvider i18n={i18n}><Fixture key={key} sessionId={sessionId} t={i18n.t} workspacePath={workspacePath} /></I18nextProvider>),
);
assert(
errors.length === 0,
@@ -190,6 +190,32 @@ globalThis.composerPasteProbe = async () => {
selection.addRange(range);
};
try {
+ // Settings replaces ChatSurface, then remounts the composer in the same
+ // workspace. Exercise the real draft hook and DOM across that lifecycle.
+ render("paste-a", "/project-a");
+ const referenceDraft = "Check \uE001 and \uE002";
+ const references = [
+ createFileReference("src/main.ts", "main.ts", "paste-a", { token: "\uE001" }),
+ createFileReference("/scratch/paste-a/notes.txt", "notes.txt", "paste-a", { token: "\uE002" }),
+ ];
+ flushSync(() => controller.applyEditorDraft(referenceDraft, references, referenceDraft.length));
+ await new Promise(requestAnimationFrame);
+ flushSync(() => root.render(null));
+ render("paste-a", "/project-a");
+ await new Promise(requestAnimationFrame);
+ assert(readEditorValue(controller.ref.current!) === referenceDraft,
+ "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");
+ 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");
+ assert(controller.fileReferences.length === 1 && controller.fileReferences[0].path === references[1].path,
+ "changing workspace must preserve scratch references");
+ flushSync(() => root.render(null));
+ resetComposerDraftCache();
+
const nativeFiles = Array.from(
(document.getElementById("native-files") as HTMLInputElement).files!,
);
@@ -665,6 +691,7 @@ globalThis.composerPasteProbe = async () => {
imageZoomFocusAndRecovery: true,
nativeMultipleFiles: true,
selectionAndSessionDrafts: true,
+ workspaceReferencesAcrossRemount: true,
};
} finally {
flushSync(() => root.unmount());