Skip to content

Commit 2113265

Browse files
committed
fix(web): offer Stop while a session is still starting
A session stuck in starting stays in the connecting phase, so the composer and the interrupt shortcut never offered Stop. Interrupt that session the same way as a running turn. A ready session stays non-interruptible. A failed turn start is already settled as a session error, and an interrupt is not turned into that failure. Co-authored-by: maco <[email protected]>
1 parent d15210c commit 2113265

4 files changed

Lines changed: 138 additions & 2 deletions

File tree

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

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ import {
3939
buildLoadingThreadFromShell,
4040
buildRunningThreadTurnInterruptInput,
4141
buildThreadTurnInterruptInput,
42+
canInterruptThreadSession,
43+
composerPrimaryActionIsStop,
4244
createLocalDispatchSnapshot,
4345
deriveComposerSendState,
4446
deriveLockedProvider,
@@ -2430,3 +2432,82 @@ describe("worktree setup visibility", () => {
24302432
});
24312433
});
24322434
});
2435+
describe("canInterruptThreadSession", () => {
2436+
it("offers Stop while the session is still starting", () => {
2437+
const session = { ...readySession, status: "starting" as const };
2438+
expect(canInterruptThreadSession("connecting", session)).toBe(true);
2439+
expect(buildRunningThreadTurnInterruptInput(makeThread({ session }), "connecting")).toEqual({
2440+
threadId,
2441+
});
2442+
});
2443+
2444+
it("does not offer Stop for a ready session, including one with only a queued message", () => {
2445+
expect(canInterruptThreadSession("ready", readySession)).toBe(false);
2446+
expect(canInterruptThreadSession("connecting", readySession)).toBe(false);
2447+
expect(canInterruptThreadSession("disconnected", null)).toBe(false);
2448+
expect(
2449+
buildRunningThreadTurnInterruptInput(makeThread({ session: readySession }), "ready"),
2450+
).toBeNull();
2451+
});
2452+
2453+
it("keeps Stop for a running turn", () => {
2454+
const activeTurnId = TurnId.make("turn-running");
2455+
const session = { ...readySession, status: "running" as const, activeTurnId };
2456+
expect(canInterruptThreadSession("running", session)).toBe(true);
2457+
expect(buildRunningThreadTurnInterruptInput(makeThread({ session }), "running")).toEqual({
2458+
threadId,
2459+
turnId: activeTurnId,
2460+
});
2461+
});
2462+
});
2463+
2464+
describe("composerPrimaryActionIsStop", () => {
2465+
it("replaces the connecting composer with Stop before the provider turn starts", () => {
2466+
expect(
2467+
composerPrimaryActionIsStop({
2468+
isRunning: false,
2469+
canInterrupt: true,
2470+
hasSendableContent: false,
2471+
isEditingQueuedMessage: false,
2472+
}),
2473+
).toBe(true);
2474+
expect(
2475+
composerPrimaryActionIsStop({
2476+
isRunning: false,
2477+
canInterrupt: true,
2478+
hasSendableContent: true,
2479+
isEditingQueuedMessage: false,
2480+
}),
2481+
).toBe(true);
2482+
});
2483+
2484+
it("keeps the send button beside Stop once a running turn has a draft", () => {
2485+
expect(
2486+
composerPrimaryActionIsStop({
2487+
isRunning: true,
2488+
canInterrupt: true,
2489+
hasSendableContent: true,
2490+
isEditingQueuedMessage: false,
2491+
}),
2492+
).toBe(false);
2493+
expect(
2494+
composerPrimaryActionIsStop({
2495+
isRunning: true,
2496+
canInterrupt: true,
2497+
hasSendableContent: false,
2498+
isEditingQueuedMessage: false,
2499+
}),
2500+
).toBe(true);
2501+
});
2502+
2503+
it("does not replace an in-progress queued-message edit", () => {
2504+
expect(
2505+
composerPrimaryActionIsStop({
2506+
isRunning: false,
2507+
canInterrupt: true,
2508+
hasSendableContent: false,
2509+
isEditingQueuedMessage: true,
2510+
}),
2511+
).toBe(false);
2512+
});
2513+
});

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,11 +646,43 @@ export function getAntigravitySendBlockReason(
646646
return null;
647647
}
648648

649+
/**
650+
* Stop is offered for a running turn, and while the session is still starting.
651+
* Starting stays in the connecting phase, but `thread.turn.interrupt` already
652+
* accepts that session. A ready or idle session is not interruptible, including
653+
* one that only has a queued message.
654+
*/
655+
export function canInterruptThreadSession(
656+
phase: SessionPhase,
657+
session: Thread["session"] | null | undefined,
658+
): boolean {
659+
if (phase === "running" && session?.status === "running") return true;
660+
return phase === "connecting" && session?.status === "starting";
661+
}
662+
663+
/**
664+
* Stop is the only primary control while a session can be interrupted and has
665+
* not started provider work, including when the draft already has text. A
666+
* running turn keeps the send button beside Stop once there is something to
667+
* send. Editing a queued message is left alone.
668+
*/
669+
export function composerPrimaryActionIsStop(input: {
670+
readonly isRunning: boolean;
671+
readonly canInterrupt: boolean;
672+
readonly hasSendableContent: boolean;
673+
readonly isEditingQueuedMessage: boolean;
674+
}): boolean {
675+
if (input.isEditingQueuedMessage) return false;
676+
if (input.isRunning) return !input.hasSendableContent;
677+
return input.canInterrupt;
678+
}
679+
680+
/** Interrupt target for a running turn or a session that is still starting. */
649681
export function buildRunningThreadTurnInterruptInput(
650682
thread: Pick<Thread, "id" | "session"> | null | undefined,
651683
phase: SessionPhase,
652684
): { threadId: ThreadId; turnId?: TurnId } | null {
653-
if (phase !== "running" || thread?.session?.status !== "running") {
685+
if (thread == null || !canInterruptThreadSession(phase, thread.session)) {
654686
return null;
655687
}
656688
return buildThreadTurnInterruptInput(thread);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ import {
8484
deriveComposerSendState,
8585
getAntigravitySendBlockReason,
8686
readFileAsDataUrl,
87+
canInterruptThreadSession,
8788
resolveComposerInteractionMode,
8889
resolveComposerProviderSelection,
8990
threadShellHasStarted,
@@ -1198,6 +1199,7 @@ const ComposerFooterPrimaryActions = memo(function ComposerFooterPrimaryActions(
11981199
isComplete: boolean;
11991200
} | null;
12001201
isRunning: boolean;
1202+
canInterrupt: boolean;
12011203
showPlanFollowUpPrompt: boolean;
12021204
promptHasText: boolean;
12031205
isSendBusy: boolean;
@@ -1230,6 +1232,7 @@ const ComposerFooterPrimaryActions = memo(function ComposerFooterPrimaryActions(
12301232
compact={props.compact}
12311233
pendingAction={props.pendingAction}
12321234
isRunning={props.isRunning}
1235+
canInterrupt={props.canInterrupt}
12331236
showPlanFollowUpPrompt={props.showPlanFollowUpPrompt}
12341237
promptHasText={props.promptHasText}
12351238
isSendBusy={props.isSendBusy}
@@ -7021,6 +7024,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
70217024
activeThreadModelDisplayName={activeThreadModelDisplayName}
70227025
pendingAction={pendingPrimaryAction}
70237026
isRunning={phase === "running"}
7027+
canInterrupt={canInterruptThreadSession(phase, activeThread?.session)}
70247028
showPlanFollowUpPrompt={
70257029
pendingUserInputs.length === 0 && showPlanFollowUpPrompt
70267030
}

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Button } from "../ui/button";
77
import { Menu, MenuItem, MenuPopup, MenuTrigger } from "../ui/menu";
88
import { Spinner } from "../ui/spinner";
99
import { composerFloatingLayerProps } from "./composerEventScope";
10+
import { composerPrimaryActionIsStop } from "../ChatView.logic";
1011

1112
interface PendingActionState {
1213
questionIndex: number;
@@ -20,6 +21,11 @@ interface ComposerPrimaryActionsProps {
2021
compact: boolean;
2122
pendingAction: PendingActionState | null;
2223
isRunning: boolean;
24+
/**
25+
* A session that is still starting stays in the connecting phase, but Stop
26+
* must still be offered. Defaults to `isRunning` for callers that only know that.
27+
*/
28+
canInterrupt?: boolean;
2329
showPlanFollowUpPrompt: boolean;
2430
promptHasText: boolean;
2531
isSendBusy: boolean;
@@ -65,6 +71,7 @@ export const ComposerPrimaryActions = memo(function ComposerPrimaryActions({
6571
compact,
6672
pendingAction,
6773
isRunning,
74+
canInterrupt,
6875
showPlanFollowUpPrompt,
6976
promptHasText,
7077
isSendBusy,
@@ -82,6 +89,7 @@ export const ComposerPrimaryActions = memo(function ComposerPrimaryActions({
8289
? { onPointerDown: preventPointerFocus }
8390
: undefined;
8491
const environmentIdentificationMode = useEnvironmentIdentificationMode();
92+
const stopAvailable = canInterrupt ?? isRunning;
8593
const isSendDisabled = sendDisabledReason !== null;
8694
const stageBackdropVariant = useSidebarStageBackdropVariant(
8795
environmentIdentificationMode === "artwork",
@@ -111,7 +119,7 @@ export const ComposerPrimaryActions = memo(function ComposerPrimaryActions({
111119
if (pendingAction) {
112120
return (
113121
<div className={cn("flex items-center justify-end", compact ? "gap-1.5" : "gap-2")}>
114-
{isRunning ? renderStopGenerationButton(true) : null}
122+
{stopAvailable ? renderStopGenerationButton(true) : null}
115123
{pendingAction.questionIndex > 0 ? (
116124
compact ? (
117125
<Button
@@ -265,6 +273,17 @@ export const ComposerPrimaryActions = memo(function ComposerPrimaryActions({
265273
</button>
266274
);
267275

276+
if (
277+
composerPrimaryActionIsStop({
278+
isRunning,
279+
canInterrupt: stopAvailable,
280+
hasSendableContent,
281+
isEditingQueuedMessage: false,
282+
})
283+
) {
284+
return renderStopGenerationButton(false);
285+
}
286+
268287
if (!isRunning) {
269288
return sendButton;
270289
}

0 commit comments

Comments
 (0)