The best way to control Claude Code, Codex and more from any computer or phone
fix(web): a question answer restored with undo is no longer sent twice (#35574)
Co-authored-by: ixmxvii-hash <235766220+ixmxvii-hash@users.noreply.github.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Logan committed
Nov 12, 2026 at 17:46 UTC
4cfcfaf2fd7aaceffeac5f786ced5b9b4c7f1cef
4 files changed
+136
-8
apps/web/src/components/chat/ComposerPendingUserInputPanel.navigation.browser.test.tsx
+32
-8
@@ -17,7 +17,7 @@ vi.mock(import("../ChatMarkdown"), async (importOriginal) => ({
17
}));
18
19
import { act, fakeTimers, render } from "../../../test/render";
20
-import { useComposerDraftStore } from "../../composerDraftStore";
20
+import { DraftId, useComposerDraftStore } from "../../composerDraftStore";
21
import { makeThreadProjectionFixture } from "../../test-fixtures";
22
import { toastManager } from "../ui/toast";
23
import { usePendingThreadRequests } from "./usePendingThreadRequests";
@@ -80,9 +80,10 @@ interface ProbeProps {
80
environmentId: EnvironmentId;
81
projection: OrchestrationV2ThreadProjection;
82
loading?: boolean;
83
+ draftTarget?: DraftId;
84
}
85
85
-function useProbeRequests({ environmentId, projection, loading = false }: ProbeProps) {
86
+function useProbeRequests({ environmentId, projection, loading = false, draftTarget }: ProbeProps) {
87
const promptRef = useRef("");
88
const composerRef = useRef(null);
89
return usePendingThreadRequests({
@@ -90,7 +91,7 @@ function useProbeRequests({ environmentId, projection, loading = false }: ProbeP
91
activeThreadId: projection.thread.id,
92
serverProjection: loading ? null : projection,
93
supportsQuestionAttachments: false,
93
- composerDraftTarget: { environmentId, threadId: projection.thread.id },
94
+ composerDraftTarget: draftTarget ?? { environmentId, threadId: projection.thread.id },
95
setComposerDraftPrompt: (target, prompt) =>
96
useComposerDraftStore.getState().setPrompt(target, prompt),
97
promptRef,
@@ -150,12 +151,10 @@ it("tells the user when an option click moves their typed answer into the hidden
151
const draftTarget = { environmentId, threadId: threadRef.threadId };
152
const draftPrompt = () => useComposerDraftStore.getState().getComposerDraft(draftTarget)?.prompt;
153
using toast = vi.spyOn(toastManager, "add");
153
- await using _screen = await render(
154
- <PendingRequestsProbe
155
- environmentId={environmentId}
156
- projection={questionProjection(threadRef.threadId)}
157
- />,
154
+ const probe = (threadId: ThreadId) => (
155
+ <PendingRequestsProbe environmentId={environmentId} projection={questionProjection(threadId)} />
156
);
157
+ await using screen = await render(probe(threadRef.threadId));
158
159
await act(() => requests.onSelectActivePendingUserInputOption("question-0", "Incremental"));
160
expect(toast).not.toHaveBeenCalled();
@@ -179,6 +178,31 @@ it("tells the user when an option click moves their typed answer into the hidden
178
customAnswer: "second try",
179
});
180
expect(toast).toHaveBeenCalledTimes(1);
181
+
182
+ // The hook outlives a thread switch; text set aside in another thread is in another draft.
183
+ await screen.rerender(probe(ThreadId.make("other-thread")));
184
+ await act(() => requests.onChangeActivePendingUserInputCustomAnswer("question-0", "elsewhere"));
185
+ await act(() => requests.onSelectActivePendingUserInputOption("question-0", "Incremental"));
186
+ await screen.rerender(probe(threadRef.threadId));
187
+
188
+ // regression: #35205 undo puts the set-aside text back, so the draft must not keep a copy to resend
189
+ await act(() => requests.onChangeActivePendingUserInputCustomAnswer("question-0", "first try"));
190
+ expect(draftPrompt() ?? "").toBe("");
191
+
192
+ // Text set aside into a queued-message edit draft never withdraws from the thread draft.
193
+ await screen.rerender(
194
+ <PendingRequestsProbe
195
+ environmentId={environmentId}
196
+ projection={questionProjection(threadRef.threadId)}
197
+ draftTarget={DraftId.make("queued-edit")}
198
+ />,
199
+ );
200
+ await act(() => requests.onChangeActivePendingUserInputCustomAnswer("question-0", "foo"));
201
+ await act(() => requests.onSelectActivePendingUserInputOption("question-0", "Incremental"));
202
+ await screen.rerender(probe(threadRef.threadId));
203
+ useComposerDraftStore.getState().setPrompt(draftTarget, "foo");
204
+ await act(() => requests.onChangeActivePendingUserInputCustomAnswer("question-0", "foo"));
205
+ expect(draftPrompt()).toBe("foo");
206
});
207
208
function QuestionPositionProbe(props: ProbeProps) {
apps/web/src/components/chat/usePendingThreadRequests.ts
+44
@@ -31,6 +31,7 @@ import {
31
} from "@t3tools/client-runtime/state/pending-user-input";
32
import {
33
carryDisplacedCustomAnswerIntoPrompt,
34
+ withdrawDisplacedCustomAnswerFromPrompt,
35
derivePendingUserInputProgress,
36
projectPendingUserInputAnswer,
37
threadContextsReferencedByAnswer,
@@ -63,6 +64,7 @@ function makePendingCustomAnswerSetter(input: {
64
readonly activePendingUserInput: PendingUserInput | null;
65
readonly activePendingRequestKey: string;
66
readonly writePrompt: (prompt: string) => void;
67
+ readonly withdrawRestoredAnswer: (questionId: string, value: string) => void;
68
readonly setPendingUserInputAnswersByRequestId: (
69
update: SetStateAction<Record<string, Record<string, PendingUserInputDraftAnswer>>>,
70
) => void;
@@ -77,6 +79,7 @@ function makePendingCustomAnswerSetter(input: {
79
return;
80
}
81
input.writePrompt(value);
82
+ input.withdrawRestoredAnswer(questionId, value);
83
input.setPendingUserInputAnswersByRequestId((existing) => ({
84
...existing,
85
[activePendingRequestKey]: {
@@ -124,6 +127,15 @@ export function usePendingThreadRequests(input: {
127
reportFailure: false,
128
});
129
const userInputResponsesInFlight = useRef(new Set<string>());
130
+ // In the order the text went into the thread draft, which is how a copy is found again.
131
+ const displacedAnswers = useRef<
132
+ Array<{ readonly draft: string; readonly key: string; readonly text: string }>
133
+ >([]);
134
+ // This hook outlives a thread switch, and each thread has its own draft.
135
+ const displacedDraftKey =
136
+ typeof composerDraftTarget === "string"
137
+ ? composerDraftTarget
138
+ : `${composerDraftTarget.environmentId}:${composerDraftTarget.threadId}`;
139
const [respondingRequestIds, setRespondingRequestIds] = useState<RuntimeRequestId[]>([]);
140
const [respondingUserInputRequestIds, setRespondingUserInputRequestIds] = useState<
141
RuntimeRequestId[]
@@ -389,6 +401,11 @@ export function usePendingThreadRequests(input: {
401
const nextPrompt = carryDisplacedCustomAnswerIntoPrompt(currentPrompt, displacedAnswer);
402
if (nextPrompt !== currentPrompt) {
403
setComposerDraftPrompt(composerDraftTarget, nextPrompt);
404
+ displacedAnswers.current.push({
405
+ draft: displacedDraftKey,
406
+ key: `${activePendingRequestKey}:${questionId}`,
407
+ text: displacedAnswer ?? "",
408
+ });
409
toastManager.add({
410
type: "info",
411
title: "Your typed text was set aside",
@@ -444,10 +461,37 @@ export function usePendingThreadRequests(input: {
461
const writePrompt = useStableCallback((prompt: string) => {
462
promptRef.current = prompt;
463
});
464
+ // Undo can put set-aside text back into the answer; left in the draft too, it is sent twice.
465
+ const withdrawRestoredAnswer = useStableCallback((questionId: string, value: string) => {
466
+ const key = `${activePendingRequestKey}:${questionId}`;
467
+ const entries = displacedAnswers.current;
468
+ const index = entries.findLastIndex(
469
+ (entry) =>
470
+ entry.draft === displacedDraftKey &&
471
+ entry.key === key &&
472
+ entry.text.trim() === value.trim(),
473
+ );
474
+ const displaced = entries[index];
475
+ if (displaced === undefined) return;
476
+ const later = entries
477
+ .slice(index + 1)
478
+ .filter((entry) => entry.draft === displaced.draft)
479
+ .map((entry) => entry.text);
480
+ displacedAnswers.current = entries.filter((entry) => entry !== displaced);
481
+ const currentPrompt =
482
+ useComposerDraftStore.getState().getComposerDraft(composerDraftTarget)?.prompt ?? "";
483
+ const nextPrompt = withdrawDisplacedCustomAnswerFromPrompt(
484
+ currentPrompt,
485
+ displaced.text,
486
+ later,
487
+ );
488
+ if (nextPrompt !== currentPrompt) setComposerDraftPrompt(composerDraftTarget, nextPrompt);
489
+ });
490
const onChangeActivePendingUserInputCustomAnswer = makePendingCustomAnswerSetter({
491
activePendingUserInput,
492
activePendingRequestKey,
493
writePrompt,
494
+ withdrawRestoredAnswer,
495
setPendingUserInputAnswersByRequestId,
496
});
497
apps/web/src/pendingUserInput.test.ts
+38
@@ -9,6 +9,7 @@ import {
9
derivePendingUserInputProgress,
10
projectPendingUserInputAnswer,
11
threadContextsReferencedByAnswer,
12
+ withdrawDisplacedCustomAnswerFromPrompt,
13
} from "./pendingUserInput";
14
15
describe("projectPendingUserInputAnswer", () => {
@@ -161,3 +162,40 @@ describe("carryDisplacedCustomAnswerIntoPrompt", () => {
162
);
163
});
164
});
165
+
166
+describe("withdrawDisplacedCustomAnswerFromPrompt", () => {
167
+ // regression: #35205 an answer restored with undo stayed in the thread draft and was sent twice
168
+ it("takes set-aside text back out of the thread draft and leaves the rest", () => {
169
+ const setAside = carryDisplacedCustomAnswerIntoPrompt("", "first answer ");
170
+ expect(withdrawDisplacedCustomAnswerFromPrompt(setAside, "first answer ")).toBe("");
171
+
172
+ const afterDraft = carryDisplacedCustomAnswerIntoPrompt("draft", "first answer");
173
+ expect(withdrawDisplacedCustomAnswerFromPrompt(afterDraft, "first answer")).toBe("draft");
174
+
175
+ const beforeSecond = carryDisplacedCustomAnswerIntoPrompt(setAside, "second answer");
176
+ expect(
177
+ withdrawDisplacedCustomAnswerFromPrompt(beforeSecond, "first answer", ["second answer"]),
178
+ ).toBe("second answer");
179
+
180
+ const between = carryDisplacedCustomAnswerIntoPrompt(afterDraft, "second answer");
181
+ expect(
182
+ withdrawDisplacedCustomAnswerFromPrompt(between, "first answer", ["second answer"]),
183
+ ).toBe("draft\n\nsecond answer");
184
+ });
185
+
186
+ it("removes its own copy, not equal text inside a later set-aside answer", () => {
187
+ const first = carryDisplacedCustomAnswerIntoPrompt("", "foo");
188
+ const both = carryDisplacedCustomAnswerIntoPrompt(first, "bar\n\nfoo");
189
+ expect(withdrawDisplacedCustomAnswerFromPrompt(both, "foo", ["bar\n\nfoo"])).toBe("bar\n\nfoo");
190
+ });
191
+
192
+ it("leaves a thread draft that no longer holds the set-aside text where it was put", () => {
193
+ expect(withdrawDisplacedCustomAnswerFromPrompt("first answer, edited", "first answer")).toBe(
194
+ "first answer, edited",
195
+ );
196
+ expect(withdrawDisplacedCustomAnswerFromPrompt("my first answer", "first answer")).toBe(
197
+ "my first answer",
198
+ );
199
+ expect(withdrawDisplacedCustomAnswerFromPrompt("draft", " ")).toBe("draft");
200
+ });
201
+});
apps/web/src/pendingUserInput.ts
+22
@@ -72,6 +72,28 @@ export function carryDisplacedCustomAnswerIntoPrompt(
72
return `${prompt.trimEnd()}${DISPLACED_ANSWER_SEPARATOR}${displaced}`;
73
}
74
75
+/**
76
+ * Takes set-aside answer text back out of the thread draft once it is the answer again.
77
+ * `laterDisplaced` is what was set aside after it, in order: together they locate this copy,
78
+ * so equal text inside another answer or the user's own draft is never the one removed.
79
+ */
80
+export function withdrawDisplacedCustomAnswerFromPrompt(
81
+ prompt: string,
82
+ displaced: string,
83
+ laterDisplaced: ReadonlyArray<string> = [],
84
+): string {
85
+ const text = displaced.trim();
86
+ if (text.length === 0) return prompt;
87
+ const later = laterDisplaced.map((entry) => entry.trim()).filter((entry) => entry.length > 0);
88
+ const suffix = [text, ...later].join(DISPLACED_ANSWER_SEPARATOR);
89
+ if (!prompt.endsWith(suffix)) return prompt;
90
+ const before = prompt.slice(0, prompt.length - suffix.length);
91
+ const after = later.join(DISPLACED_ANSWER_SEPARATOR);
92
+ if (before.length === 0) return after;
93
+ if (!before.endsWith(DISPLACED_ANSWER_SEPARATOR)) return prompt;
94
+ return after.length === 0 ? before.slice(0, -DISPLACED_ANSWER_SEPARATOR.length) : before + after;
95
+}
96
+
97
function countAnsweredPendingUserInputQuestions(
98
questions: ReadonlyArray<UserInputQuestion>,
99
draftAnswers: Record<string, PendingUserInputDraftAnswer>,