Skip to content

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

fix(chat): preserve queued attachments after restart (#955)

Yuxino4d231d76aea1
diff --git a/apps/desktop/src/stores/slices/queue-slice.ts b/apps/desktop/src/stores/slices/queue-slice.ts
index cee3f62de..be5990fba 100644
--- a/apps/desktop/src/stores/slices/queue-slice.ts
+++ b/apps/desktop/src/stores/slices/queue-slice.ts
@@ -1,4 +1,5 @@
import i18n from "i18next";
+import { formatFileInsert } from "@pi-desktop/shared";
import type {
AgentQueueChangedEvent,
AgentPromptAttachment,
@@ -86,7 +87,14 @@ export function createQueueSlice({
content: entry.content,
draft: queuedDrafts.get(entry.id) ?? {
text: entry.content,
- fileReferences: [],
+ fileReferences: (entry.attachments ?? []).map((attachment) => {
+ const token = formatFileInsert(attachment.path, "file").trim();
+ return {
+ ...attachment,
+ // Keep restored inline files removable with their visible @path.
+ ...(attachment.kind === "file" && entry.content.includes(token) ? { token } : {}),
+ };
+ }),
},
createdAt: Date.parse(entry.createdAt) || Date.now(),
// The Host owns ordering and priority: entries arrive in delivery order.
diff --git a/apps/desktop/test/queue-pending-actions.test.mjs b/apps/desktop/test/queue-pending-actions.test.mjs
index 2724d9235..5eec111cb 100644
--- a/apps/desktop/test/queue-pending-actions.test.mjs
+++ b/apps/desktop/test/queue-pending-actions.test.mjs
@@ -119,3 +119,64 @@ test("pending queue actions stay locked until admission succeeds", async (t) =>
await server.close();
}
});
+
+test("editing a restored queue preserves attachments when resubmitted", async (t) => {
+ const server = await createServer({
+ root: fileURLToPath(new URL("..", import.meta.url)),
+ configFile: false,
+ server: { middlewareMode: true, hmr: false, ws: false },
+ appType: "custom",
+ optimizeDeps: { noDiscovery: true, include: [] },
+ });
+ const previousWindow = globalThis.window;
+ try {
+ const { createQueueSlice } = await server.ssrLoadModule("/src/stores/slices/queue-slice.ts");
+ const image = { path: "/scratch/icon.png", name: "icon.png", kind: "image", mimeType: "image/png" };
+ const file = { path: "/scratch/notes.txt", name: "notes.txt", kind: "file", mimeType: "text/plain" };
+ for (const [name, content, attachments] of [
+ ["image and file", "Review @/scratch/notes.txt with the image", [image, file]],
+ ["image only", "", [image]],
+ ["text only", "Review the plan", undefined],
+ ]) {
+ await t.test(name, async () => {
+ let entries = [{ id: "restored-turn", sessionId: "session-a", content, attachments,
+ position: 1, createdAt: "2026-09-23T00:00:00Z" }];
+ let submitted;
+ globalThis.window = { piDesktop: { invoke: async (channel, payload) => {
+ if (channel === IPC.invoke.agentQueueList) return { ok: true, data: { entries } };
+ if (channel === IPC.invoke.agentQueueRemove) {
+ entries = entries.filter((entry) => entry.id !== payload.turnId);
+ return { ok: true, data: {} };
+ }
+ if (channel === IPC.invoke.agentQueuePush) {
+ submitted = payload;
+ const entry = { ...payload, id: "resubmitted-turn", position: 1, createdAt: "2026-09-23T00:00:01Z" };
+ entries = [entry];
+ return { ok: true, data: entry };
+ }
+ throw new Error(`Unexpected IPC: ${channel}`);
+ } } };
+ let state = { activeSessionId: "session-a", queuedPrompts: {}, showToast: assert.fail };
+ const slice = createQueueSlice({
+ get: () => state,
+ set: (patch) => { state = { ...state, ...(typeof patch === "function" ? patch(state) : patch) }; },
+ promptAttachmentsFromDraft: (references) => references.map(({ path, name, kind, mimeType }) => ({ path, name, kind, mimeType })),
+ });
+ Object.assign(state, slice);
+ await slice.refreshQueuedPrompts("session-a");
+ slice.editQueuedPrompt("restored-turn");
+ assert.deepEqual(entries, [], "editing removes the original queued entry");
+ assert.deepEqual(state.composerPrefill, {
+ sessionId: "session-a", text: content, fileReferences: (attachments ?? []).map((attachment) =>
+ attachment.kind === "file" ? { ...attachment, token: "@/scratch/notes.txt" } : attachment),
+ }, "restored draft retains the Host's text and attachments");
+ assert.equal(await slice.enqueuePrompt(state.composerPrefill.text, state.composerPrefill), true);
+ assert.equal(submitted.content, content, "resubmitting must not rewrite serialized file references");
+ assert.deepEqual(submitted.attachments ?? [], attachments ?? [], "resubmitting must keep the same attachments");
+ });
+ }
+ } finally {
+ globalThis.window = previousWindow;
+ await server.close();
+ }
+});
diff --git a/docs/spec/04-ux/08-component-spec.md b/docs/spec/04-ux/08-component-spec.md
index 59468e0b7..f915ee19e 100644
--- a/docs/spec/04-ux/08-component-spec.md
+++ b/docs/spec/04-ux/08-component-spec.md
@@ -2783,6 +2783,8 @@ reasoning-level control.
Host's durable `position`; at the waiting-block boundary it is a no-op and
never crosses into the promoted block. Edit removes the row and returns its
captured draft — text plus inline file-reference chips — to the composer;
+ after an app restart, when that in-memory draft is unavailable, restore the
+ Host-stored content and image/file attachments without rewriting the content;
while the input is non-empty (or holds attachments) the action is refused with
a toast and nothing changes. Remove drops the row immediately.
- Send now: promotes the row to the end of the session's priority block, so a
diff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.md
index 379076d85..05a488d4a 100644
--- a/docs/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/spec/06-delivery/04-e2e-test-plan.md
@@ -1592,13 +1592,16 @@ identify the platform validation still needed.
- **Steps**: 1) Type a draft, then choose edit on the queued row. 2) Clear the
composer and choose edit again. 3) Confirm the queue no longer lists the row
and the composer holds the row's text and its file-reference chip. 4) Send it
- and compare the transcript with the original queued prompt.
+ and compare the transcript with the original queued prompt. 5) Queue an image
+ with a file, quit the app normally, reopen it, and edit the restored row.
+ Repeat with an image-only row and a text-only row; resend each edited draft.
- **Expected**: With a non-empty input (or an attachment chip) the edit is
refused with a visible message and the row stays queued. With an empty input
the row leaves the queue, the Host no longer lists it, and the composer holds
the exact text plus the original file-reference chip — not the
serialized prompt the Host received. Re-sending produces the same prompt as the
- queued row would have.
+ queued row would have. After restart, Host-stored content and all attachments
+ survive editing and resubmission even though the in-memory draft is gone.
- **Specs linked**: `04-ux/08-component-spec.md` (§11), ADR 0265
- **Acceptance**: C (chat, stream)
- **Milestone**: M6+
diff --git a/docs/zh-CN/spec/04-ux/08-component-spec.md b/docs/zh-CN/spec/04-ux/08-component-spec.md
index 3d7c53042..1082a9ace 100644
--- a/docs/zh-CN/spec/04-ux/08-component-spec.md
+++ b/docs/zh-CN/spec/04-ux/08-component-spec.md
@@ -1997,6 +1997,7 @@ MainChat 底部的输入区域,用于撰写和发送提示。支持多行输
- 排队行:内容,然后依次是上移、下移、立即发送、编辑、删除。上移/下移与该行相邻的
等待条目互换,并镜像 Host 持久的 `position`;在等待区块边界处是空操作,绝不会跨入
优先区块。编辑会移除该行,并把捕获的草稿——文本加内联文件引用芯片——回填到输入框;
+ 应用重启后若内存草稿已不存在,则恢复 Host 保存的正文及图片/文件附件,不改写正文;
输入框非空(或带有附件)时该操作被拒绝并提示,且不改变任何内容。删除立即移除该行。
- 立即发送:把该行提升到会话优先区块的末尾,因此第二次点击排在上一次之后,而不是
顶掉它。随后请求 `agent/stop`,当前回复/工具批次正常结束后释放优先区块,并先于
diff --git a/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md b/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md
index 6e4f39577..2953c5522 100644
--- a/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md
@@ -8017,9 +8017,12 @@ runner 会在运行时的隔离临时目录中生成六个插件形态 fixture
2. 清空输入框,再次点击编辑。
3. 确认队列中不再有该行,且输入框持有该行的文本与其文件引用芯片。
4. 发送它,并与原本排队时的提示对比转录。
+ 5. 将图片和文件加入队列,正常退出并重新打开应用,再编辑恢复的队列行。
+ 同时覆盖仅图片和仅文字的消息,并分别重新发送。
- **预期**:输入框非空(或带有附件芯片)时编辑被拒绝并给出可见提示,该行仍留在队列中。
输入框为空时该行离开队列、Host 也不再列出它,输入框得到完全相同的文本与原始文件引用
- 芯片——而不是被剥离标注的内联内容。重新发送产生的提示与排队时一致。
+ 芯片——而不是被剥离标注的内联内容。重新发送产生的提示与排队时一致。重启后即使内存
+ 草稿已不存在,Host 保存的正文及全部附件也应在编辑和重新发送后保持完整。
- **链接规格**:`04-ux/08-component-spec.md`(§11)、ADR 0265
- **验收**:C(对话与流)
- **里程碑**:M6+
diff --git a/scripts/e2e/composer-submission.tsx b/scripts/e2e/composer-submission.tsx
index b3c6e6192..569aea502 100644
--- a/scripts/e2e/composer-submission.tsx
+++ b/scripts/e2e/composer-submission.tsx
@@ -74,6 +74,37 @@ export async function verifyComposerSubmission(imagePath: string, i18n: i18n) {
flushSync(() => last.querySelector<HTMLButtonElement>(".composer-image-attachment-remove")!.click());
assert(tray.children.length === 19 && !sendButton().disabled, "scrolled attachment removal must preserve other images");
+ // A restarted renderer has only the Host queue entry, not the cached draft.
+ const originalRemoveQueuedPrompt = api.removeQueuedPrompt;
+ api.removeQueuedPrompt = async () => undefined;
+ try {
+ prefill("", []);
+ await painted();
+ flushSync(() => useAppStore.getState().applyQueueChanged({
+ sessionId,
+ entries: [{
+ id: "restored-attachment-entry", sessionId,
+ content: "Review @/scratch/notes.txt",
+ attachments: [attachment, { path: "/scratch/notes.txt", name: "notes.txt", kind: "file", mimeType: "text/plain" }],
+ position: 1, createdAt: "2026-01-01T00:00:00Z",
+ }],
+ }));
+ host.querySelector<HTMLButtonElement>(".composer-queued-prompt-edit")!.click();
+ await painted();
+ assert(editor().textContent === "Review @/scratch/notes.txt" && host.querySelectorAll(".composer-image-attachment").length === 1,
+ "editing a restored queue entry must recover text and image attachments");
+ editor().textContent = "Review";
+ flushSync(() => editor().dispatchEvent(new InputEvent("input", { bubbles: true, inputType: "deleteContentBackward" })));
+ await painted();
+ sendButton().click();
+ await painted();
+ const restoredSubmission = sent.at(-1);
+ assert(restoredSubmission?.content === "Review" && restoredSubmission.draft?.fileReferences.length === 1 && restoredSubmission.draft.fileReferences[0].path === imagePath,
+ "deleting a restored inline file must remove that attachment while preserving the image");
+ } finally {
+ api.removeQueuedPrompt = originalRemoveQueuedPrompt;
+ }
+
// Issue #795: a command source that cannot be read must refuse the
// submission, instead of handing `/compact` to the model as prompt text.
const toasts: string[] = [];