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): release stale submit restore ownership
vastsa4f6340acf5a6
diff --git a/apps/desktop/src/features/chat/composer/hooks/submit-with-latest-draft-restore.d.mts b/apps/desktop/src/features/chat/composer/hooks/submit-with-latest-draft-restore.d.mtsnew file mode 100644index 000000000..b315ec933--- /dev/null+++ b/apps/desktop/src/features/chat/composer/hooks/submit-with-latest-draft-restore.d.mts@@ -0,0 +1,5 @@+export function submitWithLatestDraftRestore(+ draftKey: string,+ submit: () => Promise<boolean>,+ restoreDraft: () => void,+): Promise<boolean>;diff --git a/apps/desktop/src/features/chat/composer/hooks/submit-with-latest-draft-restore.mjs b/apps/desktop/src/features/chat/composer/hooks/submit-with-latest-draft-restore.mjsnew file mode 100644index 000000000..5149e415b--- /dev/null+++ b/apps/desktop/src/features/chat/composer/hooks/submit-with-latest-draft-restore.mjs@@ -0,0 +1,23 @@+const latestSubmissions = new Map();++/**+ * Restore a rejected draft only while its submission still owns that draft+ * slot. The unique token prevents older in-flight submissions from regaining+ * ownership after a newer submission has settled.+ *+ * @param {string} draftKey+ * @param {() => Promise<boolean>} submit+ * @param {() => void} restoreDraft+ * @returns {Promise<boolean>}+ */+export async function submitWithLatestDraftRestore(draftKey, submit, restoreDraft) {+ const token = Symbol();+ latestSubmissions.set(draftKey, token);+ try {+ const accepted = await submit();+ if (!accepted && latestSubmissions.get(draftKey) === token) restoreDraft();+ return accepted;+ } finally {+ if (latestSubmissions.get(draftKey) === token) latestSubmissions.delete(draftKey);+ }+}diff --git a/apps/desktop/src/features/chat/composer/hooks/useComposerSubmit.ts b/apps/desktop/src/features/chat/composer/hooks/useComposerSubmit.tsindex 83cb9ac78..07a1e503d 100644--- a/apps/desktop/src/features/chat/composer/hooks/useComposerSubmit.ts+++ b/apps/desktop/src/features/chat/composer/hooks/useComposerSubmit.ts@@ -14,6 +14,7 @@ import {import { readEditorValue, type ComposerFileReference } from "../editor";import type { ComposerDraftSnapshot } from "../../../../lib/composer-smart-stop";import type { ComposerDraftController } from "./useComposerDraft";+import { submitWithLatestDraftRestore } from "./submit-with-latest-draft-restore.mjs";type UseComposerSubmitOptions = {value: string;@@ -50,14 +51,6 @@ export type ComposerSubmitController = { submit: (steering?: boolean) => Promise* independent from editor rendering.*/-/**- * One failure may restore its draft only while it is still the newest submit- * for that slot: a late rejection of an older send must never paint that older- * message, with its attachments, back into the composer (#1809). Module scope- * keeps the hook callable outside a renderer, the way `Composer.tsx` drives it.- */-const latestSubmitEpochs = new Map<string, number>();-export function useComposerSubmit({value,draftKey,@@ -187,19 +180,19 @@ export function useComposerSubmit({return;}draft.clearDraftForKey(submittedDraftKey, submittedDraftRevision, submittedDraft);- const submitEpoch = (latestSubmitEpochs.get(submittedDraftKey) ?? 0) + 1;- latestSubmitEpochs.set(submittedDraftKey, submitEpoch);- const accepted = steering- ? await steerPrompt(inlineContent, submittedDraft)- : await sendPrompt(- inlineContent,- submittedDraft,- activeSessionId ?? undefined,- captureAcceptedSession,- );+ const accepted = await submitWithLatestDraftRestore(+ submittedDraftKey,+ () => steering+ ? steerPrompt(inlineContent, submittedDraft)+ : sendPrompt(+ inlineContent,+ submittedDraft,+ activeSessionId ?? undefined,+ captureAcceptedSession,+ ),+ () => draft.restoreDraftForKey(submittedDraftKey, submittedDraft),+ );if (accepted) remember();- else if (latestSubmitEpochs.get(submittedDraftKey) === submitEpoch)- draft.restoreDraftForKey(submittedDraftKey, submittedDraft);};return { submit };diff --git a/apps/desktop/test/composer-input-history.test.mjs b/apps/desktop/test/composer-input-history.test.mjsindex 4ecb231f9..d2b8bf83d 100644--- a/apps/desktop/test/composer-input-history.test.mjs+++ b/apps/desktop/test/composer-input-history.test.mjs@@ -274,7 +274,7 @@ test("accepted history uses sendPrompt's materialized session, not later globalassert.match(source, /if \(acceptedSessionId\) recordHistory\?\.\(submittedDraft, acceptedSessionId\);/);assert.match(source,- /if \(accepted\) remember\(\);\s*else if \(latestSubmitEpochs\.get\(submittedDraftKey\) === submitEpoch\)\s*draft\.restoreDraftForKey\(submittedDraftKey, submittedDraft\);/,+ /const accepted = await submitWithLatestDraftRestore\(\s*submittedDraftKey,[\s\S]*?\(\) => draft\.restoreDraftForKey\(submittedDraftKey, submittedDraft\),\s*\);\s*if \(accepted\) remember\(\);/,);const blocked = source.slice(source.indexOf('dispatch.action === "blocked"'),diff --git a/apps/desktop/test/composer-send-state.test.mjs b/apps/desktop/test/composer-send-state.test.mjsindex 80fe7e8cf..54222c237 100644--- a/apps/desktop/test/composer-send-state.test.mjs+++ b/apps/desktop/test/composer-send-state.test.mjs@@ -250,14 +250,11 @@ test("send clears the composer before the round trip and restores a rejected dra/if \(!steering && !modelReady\) \{\s*showToast\(t\("errors\.MODEL_NOT_CONFIGURED"\), \{ variant: "error" \}\);\s*return;\s*\}/,);// Optimistic clear, restore on rejection. The clear must precede the await.- const clearAt = submit.indexOf("draft.clearDraftForKey(submittedDraftKey, submittedDraftRevision, submittedDraft);\n const submitEpoch = (latestSubmitEpochs.get(submittedDraftKey) ?? 0) + 1;\n latestSubmitEpochs.set(submittedDraftKey, submitEpoch);\n const accepted = steering");+ const clearAt = submit.indexOf("draft.clearDraftForKey(submittedDraftKey, submittedDraftRevision, submittedDraft);\n const accepted = await submitWithLatestDraftRestore(");assert.ok(clearAt > 0, "draft must be cleared before awaiting sendPrompt");- // Only the newest submit may restore: a late rejection of an older send- // must not paint that older message over the composer (#1809).- assert.match(submit, /latestSubmitEpochs\.set\(submittedDraftKey, submitEpoch\);/);assert.match(submit,- /if \(accepted\) remember\(\);\s*else if \(latestSubmitEpochs\.get\(submittedDraftKey\) === submitEpoch\)\s*draft\.restoreDraftForKey\(submittedDraftKey, submittedDraft\);/,+ /submitWithLatestDraftRestore\(\s*submittedDraftKey,[\s\S]*?\(\) => draft\.restoreDraftForKey\(submittedDraftKey, submittedDraft\),\s*\);\s*if \(accepted\) remember\(\);/,);assert.doesNotMatch(submit, /if \(!accepted\) draft\.restoreDraftForKey/);assert.doesNotMatch(submit, /if \(accepted\) draft\.clearDraftForKey\(submittedDraftKey, submittedDraftRevision, submittedDraft\);\s*\};/);@@ -290,7 +287,7 @@ test("mode slash prefixes send the trailing prompt and retain failed drafts", ());assert.match(submit,- /const submittedDraftRevision = draft\.draftRevision\(submittedDraftKey\);\s*const submittedDraft = draft\.draftSnapshot\(rawText\);[\s\S]*?draft\.clearDraftForKey\(submittedDraftKey, submittedDraftRevision, submittedDraft\);\s*const submitEpoch = \(latestSubmitEpochs\.get\(submittedDraftKey\) \?\? 0\) \+ 1;\s*latestSubmitEpochs\.set\(submittedDraftKey, submitEpoch\);\s*const accepted = steering\s*\?\s*await steerPrompt\(inlineContent, submittedDraft\)\s*:\s*await sendPrompt\(\s*inlineContent,\s*submittedDraft,\s*activeSessionId \?\? undefined,\s*captureAcceptedSession,\s*\);\s*if \(accepted\) remember\(\);\s*else if \(latestSubmitEpochs\.get\(submittedDraftKey\) === submitEpoch\)\s*draft\.restoreDraftForKey\(submittedDraftKey, submittedDraft\);/,+ /const submittedDraftRevision = draft\.draftRevision\(submittedDraftKey\);\s*const submittedDraft = draft\.draftSnapshot\(rawText\);[\s\S]*?draft\.clearDraftForKey\(submittedDraftKey, submittedDraftRevision, submittedDraft\);\s*const accepted = await submitWithLatestDraftRestore\(\s*submittedDraftKey,\s*\(\) => steering\s*\?\s*steerPrompt\(inlineContent, submittedDraft\)\s*:\s*sendPrompt\(\s*inlineContent,\s*submittedDraft,\s*activeSessionId \?\? undefined,\s*captureAcceptedSession,\s*\),\s*\(\) => draft\.restoreDraftForKey\(submittedDraftKey, submittedDraft\),\s*\);\s*if \(accepted\) remember\(\);/,);assert.match(store, /draft\?: ComposerDraftSnapshot/);const sendPrompt = queueSlice.slice(@@ -308,24 +305,18 @@ test("mode slash prefixes send the trailing prompt and retain failed drafts", ());});-test("a late rejection of an older submit never restores its draft (#1809)", () => {+test("the latest-submit restore guard wraps the asynchronous send (#1809)", () => {const submit = submitHook.match(/const submit = async \(steering = false\) => \{[\s\S]*?\n \};/,)?.[0] ?? "";assert.ok(submit.length > 0, "composer submit implementation not found");- // The epoch is taken after the optimistic clear, so any submit that started- // later — including one whose own clear emptied the slot again — invalidates- // this failure's restore.- const epochAt = submit.indexOf("const submitEpoch = (latestSubmitEpochs.get(submittedDraftKey) ?? 0) + 1;");- const awaitAt = submit.indexOf("await steerPrompt(inlineContent, submittedDraft)");- const restoreAt = submit.indexOf("latestSubmitEpochs.get(submittedDraftKey) === submitEpoch");- assert.ok(epochAt > 0, "submit epoch must be captured");- assert.ok(awaitAt > epochAt, "epoch precedes the round trip");- assert.ok(restoreAt > awaitAt, "the restore checks the epoch after the await");- // The stale guard lives on the restore path only; a rejected newest submit- // still gets its draft back, and history still records accepted sends.- assert.match(submit, /else if \(latestSubmitEpochs\.get\(submittedDraftKey\) === submitEpoch\)/);- assert.doesNotMatch(submit, /if \(!accepted\)/);+ const clearAt = submit.indexOf("draft.clearDraftForKey(submittedDraftKey, submittedDraftRevision, submittedDraft);");+ const guardAt = submit.indexOf("submitWithLatestDraftRestore(", clearAt);+ const restoreAt = submit.indexOf("() => draft.restoreDraftForKey(submittedDraftKey, submittedDraft)", guardAt);+ assert.ok(clearAt >= 0, "the draft is cleared optimistically");+ assert.ok(guardAt > clearAt, "the ownership guard starts after the clear");+ assert.ok(restoreAt > guardAt, "the restore is owned by the guarded send");+ assert.match(submit, /if \(accepted\) remember\(\);/);});test("draft attachment routing keeps image chips structured and file chips textual", () => {diff --git a/apps/desktop/test/composer-submit-race.test.mjs b/apps/desktop/test/composer-submit-race.test.mjsnew file mode 100644index 000000000..9546de3d5--- /dev/null+++ b/apps/desktop/test/composer-submit-race.test.mjs@@ -0,0 +1,45 @@+import assert from "node:assert/strict";+import { test } from "node:test";+import { submitWithLatestDraftRestore } from "../src/features/chat/composer/hooks/submit-with-latest-draft-restore.mjs";++function deferred() {+ let resolve;+ const promise = new Promise((complete) => {+ resolve = complete;+ });+ return { promise, resolve };+}++test("a late rejection cannot restore over a newer accepted submission", async () => {+ const olderSend = deferred();+ const newerSend = deferred();+ const restored = [];++ const older = submitWithLatestDraftRestore("session-a", () => olderSend.promise, () => restored.push("older"));+ const newer = submitWithLatestDraftRestore("session-a", () => newerSend.promise, () => restored.push("newer"));++ newerSend.resolve(true);+ assert.equal(await newer, true);+ olderSend.resolve(false);+ assert.equal(await older, false);+ assert.deepEqual(restored, []);+});++test("an older pending send cannot regain ownership after a later slot reuse", async () => {+ const olderSend = deferred();+ const latestSend = deferred();+ const restored = [];++ const older = submitWithLatestDraftRestore("session-b", () => olderSend.promise, () => restored.push("older"));+ const latest = submitWithLatestDraftRestore("session-b", async () => true, () => restored.push("latest"));+ assert.equal(await latest, true);++ const reused = submitWithLatestDraftRestore("session-b", () => latestSend.promise, () => restored.push("reused"));+ olderSend.resolve(false);+ assert.equal(await older, false);+ assert.deepEqual(restored, []);++ latestSend.resolve(false);+ assert.equal(await reused, false);+ assert.deepEqual(restored, ["reused"]);+});diff --git a/docs/spec/04-ux/09-interaction-patterns.md b/docs/spec/04-ux/09-interaction-patterns.mdindex 47687e778..9498d8900 100644--- a/docs/spec/04-ux/09-interaction-patterns.md+++ b/docs/spec/04-ux/09-interaction-patterns.md@@ -402,7 +402,10 @@ may be retained while exactly one workspace supplies the visible shell context.has its own draft slot. Creating a new session does not copy another slot. Acompleted send clears only the draft belonging to the session that submittedit, even if the user switches sessions while the request is in flight;- deleted sessions cannot retain drafts.+ deleted sessions cannot retain drafts. A rejected submission restores its+ captured text and file references only while no later submission has taken+ ownership of that draft slot; an older late failure never replaces newer+ input.- The composer also recalls the accepted submissions of its own conversation,newest-first, with ArrowUp/ArrowDown (D632). ArrowUp starts browsing only whenthe draft is empty (no text and no references), and while browsing both keysdiff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.mdindex 483bbc539..54f78d31c 100644--- a/docs/spec/06-delivery/04-e2e-test-plan.md+++ b/docs/spec/06-delivery/04-e2e-test-plan.md@@ -2254,6 +2254,20 @@ identify the platform validation still needed.`pnpm test:e2e:composer-paste`; real desktop recording uses isolated data anda local model with a controlled response delay.+#### E2E-011c-2: Late send failures do not restore stale composer drafts++- **Preconditions**: Session A is idle; the two send results for its composer+ can be held and settled independently.+- **Steps**: Submit draft N, then submit draft N+1 before N settles. Accept N+1+ and reject N. Repeat with N+1 rejected after N remains pending.+- **Expected**: N's late rejection never restores its text or file references+ over N+1. If N+1 is rejected, only N+1's own snapshot is restored.+- **Specs linked**: `04-ux/09-interaction-patterns.md` (session-scoped drafts)+- **Acceptance**: C (composer input and session isolation)+- **Milestone**: M2+- **Status**: Async ownership behavior covered by+ `apps/desktop/test/composer-submit-race.test.mjs`; rendered UI scenario Draft+#### E2E-SESSION-reference-conversation-link: A pasted conversation link carries that conversation into the turn- **Preconditions**: Isolated desktop profile; one project with conversations A@@ -10298,7 +10312,7 @@ must keep splitting are covered by `markdown-blocks.test.mjs`.| A / C / F / Quality — Tray session navigation | E2E-TRAY-bounded-session-navigation || B — Model config | E2E-005, E2E-PROVIDER-custom-endpoint-no-api-key, E2E-006, E2E-007, E2E-038, E2E-050, E2E-052, E2E-055, E2E-066, E2E-080, E2E-082, E2E-102c, E2E-102d, E2E-102e, E2E-151, E2E-154, E2E-163, E2E-166, E2E-172, E2E-174, E2E-197, E2E-005G, E2E-005J, E2E-199, E2E-201, E2E-202, E2E-203, E2E-205, E2E-206, E2E-209, E2E-PLUGIN-provider-catalog-add-service || C — Conversation & stream | E2E-CHAT-long-history-stream-keeps-responsive, E2E-CHAT-running-status-survives-output-pauses, E2E-008, E2E-008d, E2E-008e, E2E-008a, E2E-009, E2E-010, E2E-011, E2E-011a, E2E-011b, E2E-011d, E2E-011e, E2E-011g, E2E-031, E2E-040, E2E-047, E2E-048, E2E-048A, E2E-049, E2E-052, E2E-053, E2E-054, E2E-055, E2E-059, E2E-059a, E2E-060c, E2E-060d, E2E-061, E2E-061a, E2E-062, E2E-064, E2E-065, E2E-068, E2E-071, E2E-073, E2E-074, E2E-075, E2E-081, E2E-083, E2E-084, E2E-086, E2E-087, E2E-088, E2E-088b, E2E-089, E2E-090, E2E-COMPOSER-narrow-controls, E2E-094, E2E-095, E2E-096, E2E-097, E2E-098, E2E-099, E2E-102, E2E-102a, E2E-102b, E2E-102c, E2E-102d, E2E-102g, E2E-106, E2E-109, E2E-111, E2E-114, E2E-116, E2E-117, E2E-118, E2E-119, E2E-120, E2E-121, E2E-218, E2E-259, E2E-219, E2E-AGENTS-001, E2E-142, E2E-144, E2E-145, E2E-146, E2E-146a, E2E-147, E2E-151, E2E-154, E2E-155, E2E-158, E2E-159, E2E-161, E2E-162, E2E-166, E2E-172, E2E-173, E2E-174, E2E-177, E2E-178, E2E-179, E2E-180, E2E-182, E2E-183, E2E-187, E2E-198, E2E-199, E2E-202, E2E-203, E2E-207, E2E-208, E2E-CHAT-content-width-handles, E2E-250, E2E-102i, E2E-PLUGIN-session-orchestrator-real-workers, E2E-SUBAGENT-settlement-updates-before-parent-poll, E2E-SUBAGENT-resume-a-settled-delegation |-| C — Conversation & stream (composer drafts) | E2E-011c, E2E-011c-1 |+| C — Conversation & stream (composer drafts) | E2E-011c, E2E-011c-1, E2E-011c-2 || D — Workspace | E2E-012, E2E-013, E2E-022B, E2E-024I, E2E-047, E2E-049, E2E-057, E2E-058, E2E-060, E2E-068, E2E-075, E2E-078, E2E-153, E2E-158, E2E-182, E2E-187, E2E-252 || C / D / Quality — Project session history resets after collapse | E2E-263 || D — Workspace (project ordering) | E2E-253 |diff --git a/docs/zh-CN/spec/04-ux/09-interaction-patterns.md b/docs/zh-CN/spec/04-ux/09-interaction-patterns.mdindex c91ff8930..9d548f198 100644--- a/docs/zh-CN/spec/04-ux/09-interaction-patterns.md+++ b/docs/zh-CN/spec/04-ux/09-interaction-patterns.md@@ -300,7 +300,8 @@挂载更长寿,因此切换会话、空首页 ↔ 停靠、聊天 ↔ 其他页面、或操作系统窗口时,都会保存/恢复来源文本和文件引用。未缓存的目标从空开始,首页输入框有自己的草稿槽位。创建新会话不会复制其他槽位。完成的发送只清除提交该请求的会话草稿,- 即使请求进行中用户切换了会话;已删除的会话不能保留草稿。+ 即使请求进行中用户切换了会话;已删除的会话不能保留草稿。提交被拒绝时,只有该草稿槽位+ 仍由这次提交拥有才恢复捕获的文本和文件引用;较早提交的迟到失败不能覆盖较新的输入。- 每个工具调用都会从原始持久性解析 `workspaceRoot`会话,而不是来自当前选定的项目选项卡。后台完成刷新匹配行而不重定向活动对话。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.mdindex f89516873..102c77a49 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@@ -850,6 +850,16 @@ task-candidate E2E 从请求工作树运行,但使用主工作区已经准备- **覆盖**:`scripts/e2e/composer-submission.tsx`,由`pnpm test:e2e:composer-paste` 执行;真实桌面录屏使用隔离数据,以及响应时间受控的本地模型。+#### E2E-011c-2:迟到的发送失败不会恢复旧草稿++- **前提条件**:会话 A 处于空闲状态;可分别暂停并结算该输入框的两次发送结果。+- **步骤**:提交草稿 N,在 N 结算前再提交 N+1。先接受 N+1,再拒绝 N。然后保持 N 未结算,重复测试并拒绝 N+1。+- **预期**:N 的迟到失败不会把 N 的文本或文件引用恢复到 N+1 上。如果 N+1 被拒绝,只恢复 N+1 自己的快照。+- **关联规范**:`04-ux/09-interaction-patterns.md`(按会话保存的草稿)+- **验收**:C(输入框和会话隔离)+- **里程碑**:M2+- **状态**:异步所有权行为由 `apps/desktop/test/composer-submit-race.test.mjs` 覆盖;完整 UI 场景仍为草稿+#### E2E-SESSION-reference-conversation-link:粘贴的会话链接把那段对话带进本轮- **先决条件**:隔离的桌面配置;一个项目内有会话 A 与 B,另一个项目内有会话 C;@@ -5846,7 +5856,7 @@ eleven-tool-round desktop paths are verified by| A — 应用程序启动 | E2E-001、E2E-002、E2E-003、E2E-004、E2E-067、E2E-076、E2E-079、E2E-092、E2E-097、E2E-143、E2E-150、E2E-168、E2E-204、E2E-217 || B——模型配置 | E2E-005、E2E-005G、E2E-006、E2E-007、E2E-038、E2E-050、E2E-052、E2E-055、E2E-066、E2E-080、E2E-082、E2E-151、E2E-005J、E2E-199、E2E-201、E2E-202、E2E-203、E2E-209、E2E-166 || C — 对话和直播 | E2E-CHAT-running-status-survives-output-pauses、E2E-008、E2E-008d、E2E-008a、E2E-009、E2E-010、E2E-011、E2E-011a、E2E-011b、E2E-031、E2E-040、E2E-047、E2E-048、E2E-048c、E2E-048A、E2E-049、E2E-052、 E2E-053、E2E-054、E2E-055、E2E-059、E2E-059a、E2E-060c、E2E-060d、E2E-061、E2E-061a、E2E-062、E2E-064、E2E-065、E2E-068、E2E-071、 E2E-073、E2E-074、E2E-075、E2E-081、E2E-083、E2E-084、E2E-086、E2E-087、E2E-088、E2E-088b、E2E-089、E2E-090、E2E-094、E2E-095、E2E-096、 E2E-097、E2E-098、E2E-099、E2E-102、E2E-102a、E2E-102b、E2E-106、E2E-109、E2E-111、E2E-114、E2E-116、E2E-117、E2E-118、E2E-119、 E2E-120、E2E-121、E2E-代理-001、E2E-142、E2E-144、E2E-145、E2E-146、E2E-147、E2E-151、E2E-199、E2E-250、E2E-166、E2E-SUBAGENT-resume-a-settled-delegation |-| C — 对话和直播(输入框草稿) | E2E-011c、E2E-011c-1 |+| C — 对话和直播(输入框草稿) | E2E-011c、E2E-011c-1、E2E-011c-2 || A / C / F / Quality — Tray session navigation | E2E-TRAY-bounded-session-navigation || D——工作区 | E2E-012、E2E-013、E2E-022B、E2E-024I、E2E-047、E2E-049、E2E-057、E2E-058、E2E-060、E2E-068、E2E-075、E2E-078、E2E-153 || D——工作区(项目排序) | E2E-253 |