Skip to content

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

fix(composer): track the submit epoch without a renderer-owned ref

liuzixiang487b6fca87c8
diff --git a/apps/desktop/src/features/chat/composer/hooks/useComposerSubmit.ts b/apps/desktop/src/features/chat/composer/hooks/useComposerSubmit.ts
index 329222c01..83cb9ac78 100644
--- a/apps/desktop/src/features/chat/composer/hooks/useComposerSubmit.ts
+++ b/apps/desktop/src/features/chat/composer/hooks/useComposerSubmit.ts
@@ -1,4 +1,3 @@
-import { useRef } from "react";
import type { TFunction } from "i18next";
import {
serializeComposerFileReferences,
@@ -50,6 +49,15 @@ export type ComposerSubmitController = { submit: (steering?: boolean) => Promise
* dependency so command dispatch and optimistic draft clearing remain
* 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,
@@ -68,10 +76,6 @@ 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
@@ -183,7 +187,8 @@ export function useComposerSubmit({
return;
}
draft.clearDraftForKey(submittedDraftKey, submittedDraftRevision, submittedDraft);
- const submitEpoch = ++latestSubmitRef.current;
+ const submitEpoch = (latestSubmitEpochs.get(submittedDraftKey) ?? 0) + 1;
+ latestSubmitEpochs.set(submittedDraftKey, submitEpoch);
const accepted = steering
? await steerPrompt(inlineContent, submittedDraft)
: await sendPrompt(
@@ -193,7 +198,7 @@ export function useComposerSubmit({
captureAcceptedSession,
);
if (accepted) remember();
- else if (submitEpoch === latestSubmitRef.current)
+ else if (latestSubmitEpochs.get(submittedDraftKey) === submitEpoch)
draft.restoreDraftForKey(submittedDraftKey, submittedDraft);
};
diff --git a/apps/desktop/test/composer-input-history.test.mjs b/apps/desktop/test/composer-input-history.test.mjs
index de1f32fd0..4ecb231f9 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\) remember\(\);\s*else if \(submitEpoch === latestSubmitRef\.current\)\s*draft\.restoreDraftForKey\(submittedDraftKey, submittedDraft\);/,
+ /if \(accepted\) remember\(\);\s*else if \(latestSubmitEpochs\.get\(submittedDraftKey\) === submitEpoch\)\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 6da1dbe57..80fe7e8cf 100644
--- a/apps/desktop/test/composer-send-state.test.mjs
+++ b/apps/desktop/test/composer-send-state.test.mjs
@@ -250,14 +250,14 @@ 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 = ++latestSubmitRef.current;\n const accepted = steering");
+ 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");
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, /const submitEpoch = \+\+latestSubmitRef\.current;/);
+ assert.match(submit, /latestSubmitEpochs\.set\(submittedDraftKey, submitEpoch\);/);
assert.match(
submit,
- /if \(accepted\) remember\(\);\s*else if \(submitEpoch === latestSubmitRef\.current\)\s*draft\.restoreDraftForKey\(submittedDraftKey, submittedDraft\);/,
+ /if \(accepted\) remember\(\);\s*else if \(latestSubmitEpochs\.get\(submittedDraftKey\) === submitEpoch\)\s*draft\.restoreDraftForKey\(submittedDraftKey, submittedDraft\);/,
);
assert.doesNotMatch(submit, /if \(!accepted\) draft\.restoreDraftForKey/);
assert.doesNotMatch(submit, /if \(accepted\) draft\.clearDraftForKey\(submittedDraftKey, submittedDraftRevision, submittedDraft\);\s*\};/);
@@ -290,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 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\);/,
+ /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\);/,
);
assert.match(store, /draft\?: ComposerDraftSnapshot/);
const sendPrompt = queueSlice.slice(
@@ -316,15 +316,15 @@ test("a late rejection of an older submit never restores its draft (#1809)", ()
// 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 epochAt = submit.indexOf("const submitEpoch = (latestSubmitEpochs.get(submittedDraftKey) ?? 0) + 1;");
const awaitAt = submit.indexOf("await steerPrompt(inlineContent, submittedDraft)");
- const restoreAt = submit.indexOf("submitEpoch === latestSubmitRef.current");
+ 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 \(submitEpoch === latestSubmitRef\.current\)/);
+ assert.match(submit, /else if \(latestSubmitEpochs\.get\(submittedDraftKey\) === submitEpoch\)/);
assert.doesNotMatch(submit, /if \(!accepted\)/);
});