Skip to content

Commit cbc68c9

Browse files
voida6JustYanniccjuliusmarmingemaria-rcksclaude
authored
fix(web): Stop shows a pending state and repeat clicks no longer resume (#20809)
Co-authored-by: Yannic Charlon <[email protected]> Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Claude Sonnet 5.5 <[email protected]>
1 parent a74ee4c commit cbc68c9

4 files changed

Lines changed: 208 additions & 12 deletions

File tree

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3578,11 +3578,14 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
35783578
{composerControls}
35793579
</div>
35803580
<ComposerFooterPrimaryActions
3581+
// Stop state belongs to one thread; the composer stays mounted across threads.
3582+
key={`${environmentId}:${activeThreadId ?? "new"}`}
35813583
compact={isComposerPrimaryActionsCompact}
35823584
canOperateThread={canOperateThread}
35833585
pendingAction={pendingPrimaryAction}
35843586
isRunning={phase === "running"}
35853587
canInterrupt={canInterrupt}
3588+
runId={activeThread?.runtime?.activeRunId ?? null}
35863589
followUpBehavior={followUpBehavior}
35873590
canSteer={canSteer}
35883591
alternateShortcutLabel={shortcutLabelForCommand(

‎apps/web/src/components/chat/ComposerFooterControls.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ export const ComposerFooterPrimaryActions = memo(function ComposerFooterPrimaryA
199199
} | null;
200200
isRunning: boolean;
201201
canInterrupt: boolean;
202+
runId: string | null;
202203
followUpBehavior: "queue" | "steer";
203204
canSteer: boolean;
204205
alternateShortcutLabel: string | null;
@@ -226,6 +227,7 @@ export const ComposerFooterPrimaryActions = memo(function ComposerFooterPrimaryA
226227
pendingAction={props.pendingAction}
227228
isRunning={props.isRunning}
228229
canInterrupt={props.canInterrupt}
230+
runId={props.runId}
229231
followUpBehavior={props.followUpBehavior}
230232
canSteer={props.canSteer}
231233
alternateShortcutLabel={props.alternateShortcutLabel}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import "~/index.css";
2+
3+
import type { ComponentProps } from "react";
4+
import { expect, it, vi } from "vite-plus/test";
5+
import { act, fakeTimers, render } from "../../../test/render";
6+
7+
vi.mock("~/hooks/useSettings", () => ({
8+
useEnvironmentIdentificationMode: () => "none",
9+
useClientSettings: () => false,
10+
}));
11+
vi.mock("../SidebarStageBackdrop", () => ({
12+
StageBackdropButtonArt: () => null,
13+
useSidebarStageBackdropVariant: () => null,
14+
}));
15+
16+
import { ComposerPrimaryActions } from "./ComposerPrimaryActions";
17+
18+
function setup() {
19+
return {
20+
compact: true,
21+
canOperateThread: true,
22+
pendingAction: null,
23+
isRunning: true,
24+
canInterrupt: true,
25+
canResume: false,
26+
isSendBusy: false,
27+
sendDisabledReason: null,
28+
isEnvironmentUnavailable: false,
29+
isPreparingWorktree: false,
30+
hasSendableContent: false,
31+
onPreviousPendingQuestion: vi.fn(),
32+
onInterrupt: vi.fn(),
33+
onResume: vi.fn(),
34+
onSubmitMessage: vi.fn(),
35+
} satisfies ComponentProps<typeof ComposerPrimaryActions>;
36+
}
37+
38+
it.each([false, true])(
39+
"keeps repeat clicks away from Resume and Send (draft: %s)",
40+
async (draft) => {
41+
const props = setup();
42+
const container = document.createElement("div");
43+
Object.assign(container.style, { display: "flex", justifyContent: "flex-end", width: "400px" });
44+
await using screen = await render(<ComposerPrimaryActions {...props} />, { container });
45+
const stop = screen
46+
.getByRole("button", { name: "Stop generation" })
47+
.element() as HTMLButtonElement;
48+
const { x, y, width, height } = stop.getBoundingClientRect();
49+
const target = () =>
50+
document.elementFromPoint(x + width / 2, y + height / 2)?.closest("button");
51+
await act(() => stop.click());
52+
await act(() =>
53+
(
54+
screen.getByRole("button", { name: "Stopping", exact: true }).element() as HTMLButtonElement
55+
).click(),
56+
);
57+
await expect
58+
.element(screen.getByRole("button", { name: "Stopping", exact: true }))
59+
.toBeDisabled();
60+
expect(props.onInterrupt).toHaveBeenCalledTimes(1);
61+
await screen.rerender(
62+
<ComposerPrimaryActions
63+
{...props}
64+
isRunning={false}
65+
canInterrupt={false}
66+
canResume
67+
hasSendableContent={draft}
68+
/>,
69+
);
70+
await expect
71+
.element(screen.getByRole("button", { name: "Stopped", exact: true }))
72+
.toBeDisabled();
73+
expect(target()).toHaveAttribute("aria-label", "Stopped");
74+
await act(() => {
75+
for (let i = 0; i < 20; i++) target()?.click();
76+
});
77+
expect(props.onResume).not.toHaveBeenCalled();
78+
expect(props.onSubmitMessage).not.toHaveBeenCalled();
79+
const action = screen
80+
.getByRole("button", { name: draft ? "Submit message" : "Resume thread" })
81+
.element() as HTMLButtonElement;
82+
await act(() => action.click());
83+
expect(draft ? props.onSubmitMessage : props.onResume).toHaveBeenCalledTimes(1);
84+
await screen.rerender(<ComposerPrimaryActions {...props} />);
85+
await expect.element(screen.getByRole("button", { name: "Stop generation" })).toBeEnabled();
86+
},
87+
);
88+
89+
it("allows retry after ten seconds and protects a late stop", async () => {
90+
const props = setup();
91+
await using screen = await render(<ComposerPrimaryActions {...props} />);
92+
using _timers = fakeTimers();
93+
await act(() =>
94+
(
95+
screen.getByRole("button", { name: "Stop generation" }).element() as HTMLButtonElement
96+
).click(),
97+
);
98+
await act(() => vi.advanceTimersByTime(9_999));
99+
expect(screen.getByRole("button", { name: "Stopping", exact: true }).element()).toBeDisabled();
100+
await act(() => vi.advanceTimersByTime(1));
101+
await act(() =>
102+
(
103+
screen.getByRole("button", { name: "Stop generation" }).element() as HTMLButtonElement
104+
).click(),
105+
);
106+
await screen.rerender(
107+
<ComposerPrimaryActions {...props} isRunning={false} canInterrupt={false} canResume />,
108+
);
109+
expect(screen.getByRole("button", { name: "Stopped", exact: true }).element()).toBeDisabled();
110+
expect(props.onInterrupt).toHaveBeenCalledTimes(2);
111+
expect(props.onResume).not.toHaveBeenCalled();
112+
});
113+
114+
it("keeps Stop pending across a permission failure", async () => {
115+
const props = setup();
116+
await using screen = await render(<ComposerPrimaryActions {...props} />);
117+
await act(() =>
118+
(
119+
screen.getByRole("button", { name: "Stop generation" }).element() as HTMLButtonElement
120+
).click(),
121+
);
122+
await screen.rerender(
123+
<ComposerPrimaryActions {...props} canOperateThread={false} canInterrupt={false} />,
124+
);
125+
await screen.rerender(<ComposerPrimaryActions {...props} />);
126+
await expect
127+
.element(screen.getByRole("button", { name: "Stopping", exact: true }))
128+
.toBeDisabled();
129+
expect(props.onInterrupt).toHaveBeenCalledTimes(1);
130+
});
131+
132+
it("keeps a pending stop when the run id clears and resets it for a different run", async () => {
133+
const props = setup();
134+
await using screen = await render(<ComposerPrimaryActions {...props} runId="first" />);
135+
await act(() =>
136+
(
137+
screen.getByRole("button", { name: "Stop generation" }).element() as HTMLButtonElement
138+
).click(),
139+
);
140+
await screen.rerender(<ComposerPrimaryActions {...props} runId={null} />);
141+
expect(screen.getByRole("button", { name: "Stopping", exact: true }).element()).toBeDisabled();
142+
await screen.rerender(<ComposerPrimaryActions {...props} runId="second" />);
143+
expect(screen.getByRole("button", { name: "Stop generation" }).element()).toBeEnabled();
144+
});

‎apps/web/src/components/chat/ComposerPrimaryActions.tsx‎

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { memo, type MouseEventHandler, type PointerEventHandler, type ReactNode } from "react";
1+
import {
2+
memo,
3+
useEffect,
4+
useState,
5+
type MouseEventHandler,
6+
type PointerEventHandler,
7+
type ReactNode,
8+
} from "react";
29
import {
310
CheckIcon,
411
ChevronLeftIcon,
@@ -36,6 +43,7 @@ interface ComposerPrimaryActionsProps {
3643
pendingAction: PendingActionState | null;
3744
isRunning: boolean;
3845
canInterrupt: boolean;
46+
runId?: string | null;
3947
followUpBehavior?: "queue" | "steer";
4048
canSteer?: boolean;
4149
alternateShortcutLabel?: string | null;
@@ -76,6 +84,8 @@ const formatPendingPrimaryActionLabel = (input: {
7684
return input.questionIndex > 0 ? "Submit answers" : "Submit answer";
7785
};
7886

87+
const STOP_PENDING_MAX_MS = 10_000;
88+
7989
const messageActionPillClassName =
8090
"inline-flex shrink-0 cursor-pointer items-center justify-center gap-1.5 whitespace-nowrap rounded-full bg-message-action font-medium text-base text-message-action-foreground shadow-xs shadow-message-action/24 outline-none hover:bg-message-action-hover focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring focus-visible:ring-offset-1 focus-visible:ring-offset-background disabled:pointer-events-none disabled:opacity-64 disabled:shadow-none sm:text-sm";
8191

@@ -90,6 +100,7 @@ export const ComposerPrimaryActions = memo(function ComposerPrimaryActions({
90100
pendingAction,
91101
isRunning,
92102
canInterrupt,
103+
runId = null,
93104
followUpBehavior = "steer",
94105
canSteer = true,
95106
alternateShortcutLabel = null,
@@ -110,6 +121,26 @@ export const ComposerPrimaryActions = memo(function ComposerPrimaryActions({
110121
keepFullHistory = false,
111122
onToggleKeepFullHistory,
112123
}: ComposerPrimaryActionsProps) {
124+
const [stopPhase, setStopPhase] = useState<"pending" | "timedOut" | "landed" | null>(null);
125+
const [stopRunId, setStopRunId] = useState(runId);
126+
if (runId !== null && stopRunId !== runId) {
127+
setStopRunId(runId);
128+
setStopPhase(null);
129+
} else if (canOperateThread) {
130+
// Losing permission also clears canInterrupt; it does not acknowledge the stop.
131+
if ((stopPhase === "pending" || stopPhase === "timedOut") && !canInterrupt)
132+
setStopPhase("landed");
133+
else if (stopPhase === "landed" && canInterrupt) setStopPhase(null);
134+
}
135+
const isStopDisabled = stopPhase === "pending" || stopPhase === "landed";
136+
const stopLabel =
137+
stopPhase === "landed" ? "Stopped" : stopPhase === "pending" ? "Stopping" : "Stop generation";
138+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- timer that re-enables Stop if the interrupt never settles
139+
useEffect(() => {
140+
if (stopPhase !== "pending") return;
141+
const timeout = setTimeout(() => setStopPhase("timedOut"), STOP_PENDING_MAX_MS);
142+
return () => clearTimeout(timeout);
143+
}, [stopPhase]);
113144
const pointerFocusProps = preserveComposerFocusOnPointerDown
114145
? { onPointerDown: preventPointerFocus }
115146
: undefined;
@@ -134,30 +165,41 @@ export const ComposerPrimaryActions = memo(function ComposerPrimaryActions({
134165
<button
135166
type="button"
136167
className={cn(
137-
"flex shrink-0 cursor-pointer items-center justify-center rounded-full bg-destructive/90 text-white shadow-xs shadow-destructive/24 inset-shadow-control-highlight transition-colors duration-150 hover:bg-destructive active:inset-shadow-control-pressed active:shadow-none [&_svg]:pointer-events-none",
168+
"flex shrink-0 cursor-pointer items-center justify-center rounded-full bg-destructive/90 text-white shadow-xs shadow-destructive/24 inset-shadow-control-highlight transition-colors duration-150 hover:bg-destructive active:inset-shadow-control-pressed active:shadow-none disabled:cursor-default disabled:opacity-64 disabled:hover:bg-destructive/90 [&_svg]:pointer-events-none",
138169
insidePendingAction ? "size-8 sm:size-7" : "size-8 sm:h-8 sm:w-8",
139170
)}
140171
{...pointerFocusProps}
141-
disabled={!canOperateThread}
172+
disabled={!canOperateThread || isStopDisabled}
142173
onClick={() => {
143-
if (canOperateThread) onInterrupt();
174+
if (!canOperateThread || isStopDisabled) return;
175+
setStopPhase("pending");
176+
onInterrupt();
144177
}}
145-
aria-label="Stop generation"
178+
aria-label={stopLabel}
146179
/>
147180
}
148181
>
149182
<svg width="12" height="12" viewBox="0 0 12 12" fill="currentColor" aria-hidden="true">
150183
<rect x="2" y="2" width="8" height="8" rx="1.5" />
151184
</svg>
152185
</TooltipTrigger>
153-
<TooltipPopup>Interrupt</TooltipPopup>
186+
<TooltipPopup>
187+
{stopPhase === "pending" ? "Stopping…" : stopPhase === "landed" ? "Stopped" : "Interrupt"}
188+
</TooltipPopup>
154189
</Tooltip>
155190
);
156191

192+
// Keep the clicked target at the right edge even if typing changes the primary action.
193+
const withStopButton = (action: ReactNode) => (
194+
<>
195+
{action}
196+
{stopPhase === null ? null : renderStopGenerationButton(false)}
197+
</>
198+
);
199+
157200
if (pendingAction) {
158201
return (
159202
<div className={cn("flex items-center justify-end", compact ? "gap-1.5" : "gap-2")}>
160-
{canInterrupt ? renderStopGenerationButton(true) : null}
161203
{pendingAction.questionIndex > 0 ? (
162204
compact ? (
163205
<Button
@@ -205,11 +247,16 @@ export const ComposerPrimaryActions = memo(function ComposerPrimaryActions({
205247
questionIndex: pendingAction.questionIndex,
206248
})}
207249
</button>
250+
{canInterrupt || stopPhase !== null ? renderStopGenerationButton(true) : null}
208251
</div>
209252
);
210253
}
211254

212-
if (canInterrupt && !hasSendableContent && !isEditingQueuedMessage) {
255+
if (
256+
(canInterrupt || stopPhase === "pending" || stopPhase === "timedOut") &&
257+
!hasSendableContent &&
258+
!isEditingQueuedMessage
259+
) {
213260
return (
214261
<>
215262
{children}
@@ -310,15 +357,15 @@ export const ComposerPrimaryActions = memo(function ComposerPrimaryActions({
310357
</Tooltip>
311358
);
312359
if (compactTokens === null) {
313-
return (
360+
return withStopButton(
314361
<>
315362
{children}
316363
{submit}
317-
</>
364+
</>,
318365
);
319366
}
320367

321-
return (
368+
return withStopButton(
322369
<div data-chat-composer-compact-send="true" className="flex items-center gap-2">
323370
<Tooltip>
324371
<TooltipTrigger
@@ -351,6 +398,6 @@ export const ComposerPrimaryActions = memo(function ComposerPrimaryActions({
351398
</Tooltip>
352399
{children}
353400
{submit}
354-
</div>
401+
</div>,
355402
);
356403
});

0 commit comments

Comments
 (0)