Skip to content

Commit 20beef9

Browse files
ixmxvii-hashclaude
andcommitted
fix(web): withdraw only the set-aside copy, not equal text in a later answer
The helper matched on text alone, so restoring one answer could remove the same words from the end of a later set-aside answer. Set-aside text is now tracked in the order it entered the draft, and a copy is removed only where that order places it. Co-Authored-By: Claude Mythos 5.2 <[email protected]>
1 parent 5a423ee commit 20beef9

3 files changed

Lines changed: 53 additions & 25 deletions

File tree

‎apps/web/src/components/chat/usePendingThreadRequests.ts‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ export function usePendingThreadRequests(input: {
127127
reportFailure: false,
128128
});
129129
const userInputResponsesInFlight = useRef(new Set<string>());
130-
const displacedAnswers = useRef(new Map<string, string>());
130+
// In the order the text went into the thread draft, which is how a copy is found again.
131+
const displacedAnswers = useRef<Array<{ readonly key: string; readonly text: string }>>([]);
131132
const [respondingRequestIds, setRespondingRequestIds] = useState<RuntimeRequestId[]>([]);
132133
const [respondingUserInputRequestIds, setRespondingUserInputRequestIds] = useState<
133134
RuntimeRequestId[]
@@ -393,10 +394,10 @@ export function usePendingThreadRequests(input: {
393394
const nextPrompt = carryDisplacedCustomAnswerIntoPrompt(currentPrompt, displacedAnswer);
394395
if (nextPrompt !== currentPrompt) {
395396
setComposerDraftPrompt(composerDraftTarget, nextPrompt);
396-
displacedAnswers.current.set(
397-
`${activePendingRequestKey}:${questionId}`,
398-
displacedAnswer ?? "",
399-
);
397+
displacedAnswers.current.push({
398+
key: `${activePendingRequestKey}:${questionId}`,
399+
text: displacedAnswer ?? "",
400+
});
400401
toastManager.add({
401402
type: "info",
402403
title: "Your typed text was set aside",
@@ -455,12 +456,21 @@ export function usePendingThreadRequests(input: {
455456
// Undo can put set-aside text back into the answer; left in the draft too, it is sent twice.
456457
const withdrawRestoredAnswer = useStableCallback((questionId: string, value: string) => {
457458
const key = `${activePendingRequestKey}:${questionId}`;
458-
const displaced = displacedAnswers.current.get(key);
459-
if (displaced === undefined || displaced.trim() !== value.trim()) return;
460-
displacedAnswers.current.delete(key);
459+
const entries = displacedAnswers.current;
460+
const index = entries.findLastIndex(
461+
(entry) => entry.key === key && entry.text.trim() === value.trim(),
462+
);
463+
const displaced = entries[index];
464+
if (displaced === undefined) return;
465+
const later = entries.slice(index + 1).map((entry) => entry.text);
466+
displacedAnswers.current = entries.filter((entry) => entry !== displaced);
461467
const currentPrompt =
462468
useComposerDraftStore.getState().getComposerDraft(composerDraftTarget)?.prompt ?? "";
463-
const nextPrompt = withdrawDisplacedCustomAnswerFromPrompt(currentPrompt, displaced);
469+
const nextPrompt = withdrawDisplacedCustomAnswerFromPrompt(
470+
currentPrompt,
471+
displaced.text,
472+
later,
473+
);
464474
if (nextPrompt !== currentPrompt) setComposerDraftPrompt(composerDraftTarget, nextPrompt);
465475
});
466476
const onChangeActivePendingUserInputCustomAnswer = makePendingCustomAnswerSetter({

‎apps/web/src/pendingUserInput.test.ts‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,20 +173,29 @@ describe("withdrawDisplacedCustomAnswerFromPrompt", () => {
173173
expect(withdrawDisplacedCustomAnswerFromPrompt(afterDraft, "first answer")).toBe("draft");
174174

175175
const beforeSecond = carryDisplacedCustomAnswerIntoPrompt(setAside, "second answer");
176-
expect(withdrawDisplacedCustomAnswerFromPrompt(beforeSecond, "first answer")).toBe(
177-
"second answer",
178-
);
176+
expect(
177+
withdrawDisplacedCustomAnswerFromPrompt(beforeSecond, "first answer", ["second answer"]),
178+
).toBe("second answer");
179179

180180
const between = carryDisplacedCustomAnswerIntoPrompt(afterDraft, "second answer");
181-
expect(withdrawDisplacedCustomAnswerFromPrompt(between, "first answer")).toBe(
182-
"draft\n\nsecond answer",
183-
);
181+
expect(
182+
withdrawDisplacedCustomAnswerFromPrompt(between, "first answer", ["second answer"]),
183+
).toBe("draft\n\nsecond answer");
184184
});
185185

186-
it("leaves a thread draft that no longer holds the set-aside text", () => {
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", () => {
187193
expect(withdrawDisplacedCustomAnswerFromPrompt("first answer, edited", "first answer")).toBe(
188194
"first answer, edited",
189195
);
196+
expect(withdrawDisplacedCustomAnswerFromPrompt("my first answer", "first answer")).toBe(
197+
"my first answer",
198+
);
190199
expect(withdrawDisplacedCustomAnswerFromPrompt("draft", " ")).toBe("draft");
191200
});
192201
});

‎apps/web/src/pendingUserInput.ts‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,26 @@ export function carryDisplacedCustomAnswerIntoPrompt(
7272
return `${prompt.trimEnd()}${DISPLACED_ANSWER_SEPARATOR}${displaced}`;
7373
}
7474

75-
/** Takes set-aside answer text back out of the thread draft once it is the answer again. */
76-
export function withdrawDisplacedCustomAnswerFromPrompt(prompt: string, displaced: string): string {
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 {
7785
const text = displaced.trim();
7886
if (text.length === 0) return prompt;
79-
if (prompt === text) return "";
80-
const tail = `${DISPLACED_ANSWER_SEPARATOR}${text}`;
81-
if (prompt.endsWith(tail)) return prompt.slice(0, -tail.length);
82-
const head = `${text}${DISPLACED_ANSWER_SEPARATOR}`;
83-
if (prompt.startsWith(head)) return prompt.slice(head.length);
84-
const middle = prompt.indexOf(`${tail}${DISPLACED_ANSWER_SEPARATOR}`);
85-
return middle === -1 ? prompt : prompt.slice(0, middle) + prompt.slice(middle + tail.length);
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;
8695
}
8796

8897
function countAnsweredPendingUserInputQuestions(

0 commit comments

Comments
 (0)