Skip to content

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

fix(composer): drop stale failure restores from superseded submits

liuzixiang0eef9df3ad30
diff --git a/apps/desktop/src/features/chat/composer/hooks/useComposerSubmit.ts b/apps/desktop/src/features/chat/composer/hooks/useComposerSubmit.ts
index 0d5de2050..329222c01 100644
--- a/apps/desktop/src/features/chat/composer/hooks/useComposerSubmit.ts
+++ b/apps/desktop/src/features/chat/composer/hooks/useComposerSubmit.ts
@@ -1,3 +1,4 @@
+import { useRef } from "react";
import type { TFunction } from "i18next";
import {
serializeComposerFileReferences,
@@ -67,6 +68,10 @@ export function useComposerSubmit({
recordHistory,
draft,
}: UseComposerSubmitOptions): ComposerSubmitController {
+ // One failure may restore its draft only while it is still the newest
+ // submit: a late rejection of an older send must never paint that older
+ // message, with its attachments, back into the composer (#1809).
+ const latestSubmitRef = useRef(0);
const submit = async (steering = false) => {
const rawText = draft.ref.current ? readEditorValue(draft.ref.current) : value;
// An image chip keeps its place in the prompt: main resolves the `@path` it
@@ -178,6 +183,7 @@ export function useComposerSubmit({
return;
}
draft.clearDraftForKey(submittedDraftKey, submittedDraftRevision, submittedDraft);
+ const submitEpoch = ++latestSubmitRef.current;
const accepted = steering
? await steerPrompt(inlineContent, submittedDraft)
: await sendPrompt(
@@ -186,8 +192,9 @@ export function useComposerSubmit({
activeSessionId ?? undefined,
captureAcceptedSession,
);
- if (!accepted) draft.restoreDraftForKey(submittedDraftKey, submittedDraft);
- else remember();
+ if (accepted) remember();
+ else if (submitEpoch === latestSubmitRef.current)
+ 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.mjs
index 1b225d262..de1f32fd0 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 global
assert.match(source, /if \(acceptedSessionId\) recordHistory\?\.\(submittedDraft, acceptedSessionId\);/);
assert.match(
source,
- /if \(!accepted\) draft\.restoreDraftForKey\(submittedDraftKey, submittedDraft\);\s*else remember\(\);/,
+ /if \(accepted\) remember\(\);\s*else if \(submitEpoch === latestSubmitRef\.current\)\s*draft\.restoreDraftForKey\(submittedDraftKey, submittedDraft\);/,
);
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.mjs
index dad176cd3..6da1dbe57 100644
--- a/apps/desktop/test/composer-send-state.test.mjs
+++ b/apps/desktop/test/composer-send-state.test.mjs
@@ -250,9 +250,16 @@ 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 accepted = steering");
+ const clearAt = submit.indexOf("draft.clearDraftForKey(submittedDraftKey, submittedDraftRevision, submittedDraft);\n const submitEpoch = ++latestSubmitRef.current;\n const accepted = steering");
assert.ok(clearAt > 0, "draft must be cleared before awaiting sendPrompt");
- assert.match(submit, /if \(!accepted\) draft\.restoreDraftForKey\(submittedDraftKey, submittedDraft\);/);
+ // 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, /const submitEpoch = \+\+latestSubmitRef\.current;/);
+ assert.match(
+ submit,
+ /if \(accepted\) remember\(\);\s*else if \(submitEpoch === latestSubmitRef\.current\)\s*draft\.restoreDraftForKey\(submittedDraftKey, submittedDraft\);/,
+ );
+ assert.doesNotMatch(submit, /if \(!accepted\) draft\.restoreDraftForKey/);
assert.doesNotMatch(submit, /if \(accepted\) draft\.clearDraftForKey\(submittedDraftKey, submittedDraftRevision, submittedDraft\);\s*\};/);
const restore = draftHook.match(
/const restoreDraftForKey = \(key: string, snapshot: ComposerDraftSnapshot\) => \{[\s\S]*?\n \};/,
@@ -283,7 +290,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 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\) draft\.restoreDraftForKey\(submittedDraftKey, submittedDraft\);/,
+ /const submittedDraftRevision = draft\.draftRevision\(submittedDraftKey\);\s*const submittedDraft = draft\.draftSnapshot\(rawText\);[\s\S]*?draft\.clearDraftForKey\(submittedDraftKey, submittedDraftRevision, submittedDraft\);\s*const submitEpoch = \+\+latestSubmitRef\.current;\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 \(submitEpoch === latestSubmitRef\.current\)\s*draft\.restoreDraftForKey\(submittedDraftKey, submittedDraft\);/,
);
assert.match(store, /draft\?: ComposerDraftSnapshot/);
const sendPrompt = queueSlice.slice(
@@ -301,6 +308,26 @@ 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)", () => {
+ 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 = ++latestSubmitRef.current;");
+ const awaitAt = submit.indexOf("await steerPrompt(inlineContent, submittedDraft)");
+ const restoreAt = submit.indexOf("submitEpoch === latestSubmitRef.current");
+ 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 \(submitEpoch === latestSubmitRef\.current\)/);
+ assert.doesNotMatch(submit, /if \(!accepted\)/);
+});
+
test("draft attachment routing keeps image chips structured and file chips textual", () => {
const helperSource = storeHelpers.match(
/(?:export\s+)?function promptAttachmentsFromDraft\([\s\S]*?\n\}\n\n(?:export\s+)?function promptAttachmentsFromMessage/,