Skip to content

Commit 3f69542

Browse files
only21milclaudemaria-rcks
authored
fix(web): a failed plan follow-up no longer overwrites the next draft (#24684)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent b8badda commit 3f69542

3 files changed

Lines changed: 72 additions & 2 deletions

File tree

‎apps/web/src/components/ChatView.logic.test.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
buildExpiredTerminalContextToastCopy,
44
mergeDirectPreviewAnnotation,
55
planComposerTurn,
6+
restorePlanFollowUpComposer,
67
snapshotComposerSend,
78
withMessageContext,
89
deriveAvailableRevertTurnCounts,
@@ -1129,6 +1130,61 @@ describe("planComposerTurn", () => {
11291130
});
11301131
});
11311132

1133+
describe("restorePlanFollowUpComposer", () => {
1134+
// The draft is the failed follow-up's thread; the refs and cursor are whatever thread is on
1135+
// screen.
1136+
it.each([
1137+
{
1138+
name: "puts the follow-up back while its thread is open",
1139+
before: { draft: "", refs: "", cursor: null },
1140+
routeIsCurrent: true,
1141+
after: { draft: "Tighten step 2", refs: "Tighten step 2", cursor: "Tighten step 2" },
1142+
},
1143+
{
1144+
name: "keeps a draft typed while the follow-up was in flight",
1145+
before: { draft: "Next idea", refs: "Next idea", cursor: null },
1146+
routeIsCurrent: true,
1147+
after: { draft: "Next idea", refs: "Next idea", cursor: null },
1148+
},
1149+
{
1150+
name: "leaves another thread's composer alone after navigating away",
1151+
before: { draft: "", refs: "Other thread", cursor: null },
1152+
routeIsCurrent: false,
1153+
after: { draft: "Tighten step 2", refs: "Other thread", cursor: null },
1154+
},
1155+
])("$name", ({ before, routeIsCurrent, after }) => {
1156+
const composer: { draft: string; refs: string; cursor: string | null } = { ...before };
1157+
restorePlanFollowUpComposer({
1158+
snapshot: {
1159+
prompt: "Tighten step 2",
1160+
environmentContexts: [],
1161+
terminalContexts: [],
1162+
reviewComments: [],
1163+
previewAnnotations: [],
1164+
threadContexts: [],
1165+
},
1166+
draftHasUserContent: composer.draft.trim().length > 0,
1167+
routeIsCurrent,
1168+
writeComposerRefs: (snapshot) => {
1169+
composer.refs = snapshot.prompt;
1170+
},
1171+
writePrompt: (prompt) => {
1172+
composer.draft = prompt;
1173+
},
1174+
writeTerminalContexts: () => undefined,
1175+
writeReviewComments: () => undefined,
1176+
writePreviewAnnotations: () => undefined,
1177+
writeThreadContexts: () => undefined,
1178+
writeEnvironmentContexts: () => undefined,
1179+
resetCursor: ({ prompt }) => {
1180+
composer.cursor = prompt;
1181+
},
1182+
});
1183+
1184+
expect(composer).toEqual(after);
1185+
});
1186+
});
1187+
11321188
describe("withMessageContext", () => {
11331189
const context = { records: [] } as unknown as NonNullable<
11341190
Parameters<typeof withMessageContext>[0]["context"]

‎apps/web/src/components/ChatView.logic.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,8 +1180,13 @@ export interface PlanFollowUpComposerSnapshot {
11801180
readonly environmentContexts: ReadonlyArray<EnvironmentContextRecord>;
11811181
}
11821182

1183+
// A draft typed while the send was in flight is newer and stays. Composer refs and the cursor
1184+
// belong to the thread on screen, so they follow only while the follow-up's route is current.
11831185
export function restorePlanFollowUpComposer(input: {
11841186
readonly snapshot: PlanFollowUpComposerSnapshot;
1187+
readonly draftHasUserContent: boolean;
1188+
readonly routeIsCurrent: boolean;
1189+
readonly writeComposerRefs: (snapshot: PlanFollowUpComposerSnapshot) => void;
11851190
readonly writePrompt: (prompt: string) => void;
11861191
readonly writeTerminalContexts: (contexts: ReadonlyArray<TerminalContextDraft>) => void;
11871192
readonly writeReviewComments: (comments: ReadonlyArray<ReviewCommentContext>) => void;
@@ -1194,12 +1199,15 @@ export function restorePlanFollowUpComposer(input: {
11941199
detectTrigger: boolean;
11951200
}) => void;
11961201
}): void {
1202+
if (input.draftHasUserContent) return;
1203+
if (input.routeIsCurrent) input.writeComposerRefs(input.snapshot);
11971204
input.writePrompt(input.snapshot.prompt);
11981205
input.writeTerminalContexts(input.snapshot.terminalContexts);
11991206
input.writeReviewComments(input.snapshot.reviewComments);
12001207
input.writePreviewAnnotations(input.snapshot.previewAnnotations);
12011208
input.writeThreadContexts(input.snapshot.threadContexts);
12021209
input.writeEnvironmentContexts(input.snapshot.environmentContexts);
1210+
if (!input.routeIsCurrent) return;
12031211
input.resetCursor({
12041212
cursor: collapseExpandedComposerCursor(input.snapshot.prompt, input.snapshot.prompt.length),
12051213
prompt: input.snapshot.prompt,

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -759,8 +759,6 @@ export function useComposerSend(input: ComposerSendInput) {
759759
interactionMode: followUp.interactionMode,
760760
});
761761
if (!followUpSent) {
762-
promptRef.current = followUpPromptSnapshot;
763-
composerTerminalContextsRef.current = [...followUpTerminalContexts];
764762
restorePlanFollowUpComposer({
765763
snapshot: {
766764
prompt: followUpPromptSnapshot,
@@ -770,6 +768,14 @@ export function useComposerSend(input: ComposerSendInput) {
770768
threadContexts: followUpThreadContexts,
771769
environmentContexts: followUpEnvironmentContexts,
772770
},
771+
draftHasUserContent: composerDraftHasUserContent(
772+
useComposerDraftStore.getState().getComposerDraft(composerDraftTarget),
773+
),
774+
routeIsCurrent: currentRouteThreadKeyRef.current === routeThreadKey,
775+
writeComposerRefs: (snapshot) => {
776+
promptRef.current = snapshot.prompt;
777+
composerTerminalContextsRef.current = [...snapshot.terminalContexts];
778+
},
773779
writePrompt: (prompt) => setComposerDraftPrompt(composerDraftTarget, prompt),
774780
writeTerminalContexts: (contexts) =>
775781
setComposerDraftTerminalContexts(composerDraftTarget, [...contexts]),

0 commit comments

Comments
 (0)