The best way to control Claude Code, Codex and more from any computer or phone
refactor(web): resuming an interrupted run lives in its own hook (#23087)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 21, 2026 at 14:25 UTC
5b715cc13cc5bcb3f82951be42888452c0f18557
3 files changed
+192
-100
apps/web/src/components/ChatView.tsx
+23
-5
@@ -15,6 +15,7 @@ import { useThreadLifecycle } from "./chat/useThreadLifecycle";
15
import { useRightPanelActions } from "./chat/useRightPanelActions";
16
import { type TerminalLaunchContext, useChatTerminalActions } from "./chat/useChatTerminalActions";
17
import { useComposerSend } from "./chat/useComposerSend";
18
+import { useComposerResume } from "./chat/useComposerResume";
19
import { ThreadFind, ThreadFindCanvas, type ThreadFindControls } from "./chat/ThreadFindProvider";
20
import { THREAD_FIND_BAR_RESERVED_HEIGHT } from "./chat/ThreadFindBar";
21
import { usageLimitRecoveryBannerItem } from "./chat/UsageLimitRecoveryBanner";
@@ -2923,7 +2924,28 @@ export default function ChatView(props: ChatViewProps) {
2924
setPendingServerThreadStartFromOriginByThreadId,
2925
scheduleComposerFocus,
2926
});
2926
- const { onSend, onResume, onImplementPlanInNewThread, isResuming } = useComposerSend({
2927
+ const { onResume, isResuming } = useComposerResume({
2928
+ environmentId,
2929
+ routeThreadKey,
2930
+ currentRouteThreadKeyRef,
2931
+ activeThread,
2932
+ resumableRunId,
2933
+ hasHeldQueuedRuns,
2934
+ isSendBusy,
2935
+ isConnecting,
2936
+ isRevertingCheckpoint,
2937
+ threadDetailLoading,
2938
+ activeEnvironmentUnavailable,
2939
+ runtimeMode,
2940
+ interactionMode,
2941
+ localCheckoutBranchMismatch,
2942
+ composerRef,
2943
+ persistThreadSettingsForNextTurn,
2944
+ clearUsageLimitsFor,
2945
+ scrollToEnd,
2946
+ setThreadError,
2947
+ });
2948
+ const { onSend, onImplementPlanInNewThread } = useComposerSend({
2949
environmentId,
2950
routeThreadKey,
2951
currentRouteThreadKeyRef,
@@ -2939,12 +2961,9 @@ export default function ChatView(props: ChatViewProps) {
2961
isServerThread,
2962
isLocalDraftThread,
2963
isGitRepo,
2942
- hasHeldQueuedRuns,
2943
- resumableRunId,
2964
phase,
2965
runtimeMode,
2966
defaultRuntimeMode,
2947
- interactionMode,
2967
sendEnvMode,
2968
startFromOrigin,
2969
localCheckoutBranchMismatch,
@@ -3000,7 +3019,6 @@ export default function ChatView(props: ChatViewProps) {
3019
setMultipleModelSelections,
3020
setThreadError,
3021
setKeepFullHistory,
3003
- scrollToEnd,
3022
anchorNewTurn,
3023
acknowledgeActiveThreadWoke,
3024
handleInteractionModeChange,
apps/web/src/components/chat/useComposerResume.ts
new
+167
@@ -0,0 +1,167 @@
1
+import {
2
+ type EnvironmentId,
3
+ type ProviderInteractionMode,
4
+ type RunId,
5
+ type RuntimeMode,
6
+} from "@t3tools/contracts";
7
+import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
8
+import {
9
+ isAtomCommandInterrupted,
10
+ squashAtomCommandFailure,
11
+ type AtomCommandResult,
12
+} from "@t3tools/client-runtime/state/runtime";
13
+import { useState } from "react";
14
+import { newMessageId } from "~/lib/utils";
15
+import type { ComposerHandleRef } from "../../composerHandleContext";
16
+import { threadEnvironment } from "../../state/threads";
17
+import { useOrchestrationCommand } from "../../state/use-orchestration-command";
18
+import type { makeThreadErrorSetter } from "../ChatView.logic";
19
+import { draftSendState } from "./chatViewSend";
20
+import type { useTimelineFollow } from "./useTimelineFollow";
21
+import type { useUsageLimitsPanel } from "./useUsageLimitsPanel";
22
+
23
+interface ComposerResumeInput {
24
+ readonly environmentId: EnvironmentId;
25
+ readonly routeThreadKey: string;
26
+ readonly currentRouteThreadKeyRef: { readonly current: string | null };
27
+ readonly activeThread: EnvironmentThreadShell | undefined;
28
+ readonly resumableRunId: RunId | null;
29
+ readonly hasHeldQueuedRuns: boolean;
30
+ readonly isSendBusy: boolean;
31
+ readonly isConnecting: boolean;
32
+ readonly isRevertingCheckpoint: boolean;
33
+ readonly threadDetailLoading: boolean;
34
+ readonly activeEnvironmentUnavailable: boolean;
35
+ readonly runtimeMode: RuntimeMode;
36
+ readonly interactionMode: ProviderInteractionMode;
37
+ readonly localCheckoutBranchMismatch: { readonly currentBranch: string } | null;
38
+ readonly composerRef: ComposerHandleRef;
39
+ readonly persistThreadSettingsForNextTurn: (input: {
40
+ threadId: EnvironmentThreadShell["id"];
41
+ createdAt: string;
42
+ branch?: string;
43
+ runtimeMode: RuntimeMode;
44
+ interactionMode: ProviderInteractionMode;
45
+ }) => Promise<AtomCommandResult<void, unknown>>;
46
+ readonly clearUsageLimitsFor: ReturnType<typeof useUsageLimitsPanel>["clearFor"];
47
+ readonly scrollToEnd: ReturnType<typeof useTimelineFollow>["scrollToEnd"];
48
+ readonly setThreadError: ReturnType<typeof makeThreadErrorSetter>;
49
+}
50
+
51
+/**
52
+ * Resumes an interrupted run ("Continue where you left off.") and/or releases the turns held
53
+ * in its queue. Shares the per-route send lock with the composer's send paths.
54
+ */
55
+export function useComposerResume(input: ComposerResumeInput) {
56
+ const {
57
+ environmentId,
58
+ routeThreadKey,
59
+ currentRouteThreadKeyRef,
60
+ activeThread,
61
+ resumableRunId,
62
+ hasHeldQueuedRuns,
63
+ isSendBusy,
64
+ isConnecting,
65
+ isRevertingCheckpoint,
66
+ threadDetailLoading,
67
+ activeEnvironmentUnavailable,
68
+ runtimeMode,
69
+ interactionMode,
70
+ localCheckoutBranchMismatch,
71
+ composerRef,
72
+ persistThreadSettingsForNextTurn,
73
+ clearUsageLimitsFor,
74
+ scrollToEnd,
75
+ setThreadError,
76
+ } = input;
77
+ const startThreadTurn = useOrchestrationCommand(threadEnvironment.startTurn, {
78
+ reportFailure: false,
79
+ });
80
+ const resumeThreadQueue = useOrchestrationCommand(threadEnvironment.resumeThreadQueue, {
81
+ reportFailure: false,
82
+ });
83
+ const [resumingThreadKeys, setResumingThreadKeys] = useState<ReadonlySet<string>>(
84
+ () => new Set(),
85
+ );
86
+ const isResuming = resumingThreadKeys.has(routeThreadKey);
87
+ const { sendInFlight: sendInFlightRef } = draftSendState(routeThreadKey);
88
+
89
+ const onResume = async () => {
90
+ if (
91
+ !activeThread ||
92
+ (resumableRunId === null && !hasHeldQueuedRuns) ||
93
+ isSendBusy ||
94
+ isResuming ||
95
+ isConnecting ||
96
+ isRevertingCheckpoint ||
97
+ threadDetailLoading ||
98
+ activeEnvironmentUnavailable ||
99
+ sendInFlightRef.current
100
+ ) {
101
+ return;
102
+ }
103
+ const threadId = activeThread.id;
104
+ sendInFlightRef.current = true;
105
+ setResumingThreadKeys((current) => new Set(current).add(routeThreadKey));
106
+ setThreadError(threadId, null);
107
+ // Commands settle into a result, so this always runs.
108
+ const resume = async () => {
109
+ if (resumableRunId === null) {
110
+ return resumeThreadQueue({ environmentId, input: { threadId } });
111
+ }
112
+ const createdAt = new Date().toISOString();
113
+ const settingsResult = await persistThreadSettingsForNextTurn({
114
+ threadId,
115
+ createdAt,
116
+ ...(localCheckoutBranchMismatch
117
+ ? { branch: localCheckoutBranchMismatch.currentBranch }
118
+ : {}),
119
+ runtimeMode,
120
+ interactionMode,
121
+ });
122
+ if (settingsResult._tag === "Failure") return settingsResult;
123
+ const modelSelection = composerRef.current?.getSendContext().selectedModelSelection;
124
+ const turnResult = await startThreadTurn({
125
+ environmentId,
126
+ input: {
127
+ threadId,
128
+ manualContinuationOfRunId: resumableRunId,
129
+ ...(modelSelection ? { modelSelection } : {}),
130
+ message: {
131
+ messageId: newMessageId(),
132
+ role: "user",
133
+ text: "Continue where you left off.",
134
+ attachments: [],
135
+ },
136
+ runtimeMode,
137
+ interactionMode,
138
+ dispatchMode: "start",
139
+ },
140
+ });
141
+ if (turnResult._tag === "Failure" || !hasHeldQueuedRuns) return turnResult;
142
+ clearUsageLimitsFor(routeThreadKey);
143
+ return resumeThreadQueue({ environmentId, input: { threadId } });
144
+ };
145
+ const result = await resume();
146
+ if (result._tag === "Failure") {
147
+ if (!isAtomCommandInterrupted(result)) {
148
+ const error = squashAtomCommandFailure(result);
149
+ setThreadError(
150
+ threadId,
151
+ error instanceof Error ? error.message : "Could not resume thread.",
152
+ );
153
+ }
154
+ } else {
155
+ clearUsageLimitsFor(routeThreadKey);
156
+ if (currentRouteThreadKeyRef.current === routeThreadKey) scrollToEnd();
157
+ }
158
+ sendInFlightRef.current = false;
159
+ setResumingThreadKeys((current) => {
160
+ const next = new Set(current);
161
+ next.delete(routeThreadKey);
162
+ return next;
163
+ });
164
+ };
165
+
166
+ return { onResume, isResuming };
167
+}
apps/web/src/components/chat/useComposerSend.ts
+2
-95
@@ -25,7 +25,6 @@ import {
25
type ModelSelection,
26
type PreviewAnnotationPayload,
27
type ThreadId,
28
- type RunId,
28
ProviderInteractionMode,
29
RuntimeMode,
30
} from "@t3tools/contracts";
@@ -44,7 +43,7 @@ import {
43
} from "@t3tools/client-runtime/environment";
44
import { createModelSelection } from "@t3tools/shared/model";
45
import { truncate } from "@t3tools/shared/String";
47
-import { type SetStateAction, useLayoutEffect, useRef, useState } from "react";
46
+import { type SetStateAction, useLayoutEffect, useRef } from "react";
47
import { flushSync } from "react-dom";
48
import { useNavigate } from "@tanstack/react-router";
49
import {
@@ -162,12 +161,9 @@ interface ComposerSendInput {
161
readonly isServerThread: boolean;
162
readonly isLocalDraftThread: boolean;
163
readonly isGitRepo: boolean;
165
- readonly hasHeldQueuedRuns: boolean;
166
- readonly resumableRunId: RunId | null;
164
readonly phase: SessionPhase;
165
readonly runtimeMode: RuntimeMode;
166
readonly defaultRuntimeMode: RuntimeMode;
170
- readonly interactionMode: ProviderInteractionMode;
167
readonly sendEnvMode: DraftThreadEnvMode;
168
readonly startFromOrigin: boolean;
169
readonly localCheckoutBranchMismatch: {
@@ -230,7 +226,6 @@ interface ComposerSendInput {
226
) => void;
227
readonly setThreadError: ReturnType<typeof makeThreadErrorSetter>;
228
readonly setKeepFullHistory: (threadKey: string, keep: boolean) => void;
233
- readonly scrollToEnd: TimelineFollow["scrollToEnd"];
229
readonly anchorNewTurn: TimelineFollow["anchorNewTurn"];
230
readonly acknowledgeActiveThreadWoke: () => void;
231
readonly handleInteractionModeChange: (mode: ProviderInteractionMode) => void;
@@ -267,12 +262,9 @@ export function useComposerSend(input: ComposerSendInput) {
262
isServerThread,
263
isLocalDraftThread,
264
isGitRepo,
270
- hasHeldQueuedRuns,
271
- resumableRunId,
265
phase,
266
runtimeMode,
267
defaultRuntimeMode,
275
- interactionMode,
268
sendEnvMode,
269
startFromOrigin,
270
localCheckoutBranchMismatch,
@@ -328,7 +320,6 @@ export function useComposerSend(input: ComposerSendInput) {
320
setMultipleModelSelections,
321
setThreadError,
322
setKeepFullHistory,
331
- scrollToEnd,
323
anchorNewTurn,
324
acknowledgeActiveThreadWoke,
325
handleInteractionModeChange,
@@ -342,9 +333,6 @@ export function useComposerSend(input: ComposerSendInput) {
333
const startThreadTurn = useOrchestrationCommand(threadEnvironment.startTurn, {
334
reportFailure: false,
335
});
345
- const resumeThreadQueue = useOrchestrationCommand(threadEnvironment.resumeThreadQueue, {
346
- reportFailure: false,
347
- });
336
const uploadThreadFeedback = useOrchestrationCommand(threadEnvironment.uploadFeedback, {
337
reportFailure: false,
338
});
@@ -365,10 +353,6 @@ export function useComposerSend(input: ComposerSendInput) {
353
const readMultipleModelSelections = () => appAtomRegistry.get(fanoutSelectionsAtom);
354
const { sendInFlight: sendInFlightRef, uncertainSubmissions: uncertainMultipleSubmissionsRef } =
355
draftSendState(routeThreadKey);
368
- const [resumingThreadKeys, setResumingThreadKeys] = useState<ReadonlySet<string>>(
369
- () => new Set(),
370
- );
371
- const isResuming = resumingThreadKeys.has(routeThreadKey);
356
const composerSendGenerationRef = useRef(0);
357
const environmentUnavailableSendToastSlotRef = useRef(0);
358
// A send awaits uploads before it declines, so it reads the approvals pending by then.
@@ -379,83 +363,6 @@ export function useComposerSend(input: ComposerSendInput) {
363
respondingRequestIdsRef.current = respondingRequestIds;
364
}, [pendingApprovals, respondingRequestIds]);
365
382
- const onResume = async () => {
383
- if (
384
- !activeThread ||
385
- (resumableRunId === null && !hasHeldQueuedRuns) ||
386
- isSendBusy ||
387
- isResuming ||
388
- isConnecting ||
389
- isRevertingCheckpoint ||
390
- threadDetailLoading ||
391
- activeEnvironmentUnavailable ||
392
- sendInFlightRef.current
393
- ) {
394
- return;
395
- }
396
- const threadId = activeThread.id;
397
- sendInFlightRef.current = true;
398
- setResumingThreadKeys((current) => new Set(current).add(routeThreadKey));
399
- setThreadError(threadId, null);
400
- // Commands settle into a result, so this always runs.
401
- const resume = async () => {
402
- if (resumableRunId === null) {
403
- return resumeThreadQueue({ environmentId, input: { threadId } });
404
- }
405
- const createdAt = new Date().toISOString();
406
- const settingsResult = await persistThreadSettingsForNextTurn({
407
- threadId,
408
- createdAt,
409
- ...(localCheckoutBranchMismatch
410
- ? { branch: localCheckoutBranchMismatch.currentBranch }
411
- : {}),
412
- runtimeMode,
413
- interactionMode,
414
- });
415
- if (settingsResult._tag === "Failure") return settingsResult;
416
- const modelSelection = composerRef.current?.getSendContext().selectedModelSelection;
417
- const turnResult = await startThreadTurn({
418
- environmentId,
419
- input: {
420
- threadId,
421
- manualContinuationOfRunId: resumableRunId,
422
- ...(modelSelection ? { modelSelection } : {}),
423
- message: {
424
- messageId: newMessageId(),
425
- role: "user",
426
- text: "Continue where you left off.",
427
- attachments: [],
428
- },
429
- runtimeMode,
430
- interactionMode,
431
- dispatchMode: "start",
432
- },
433
- });
434
- if (turnResult._tag === "Failure" || !hasHeldQueuedRuns) return turnResult;
435
- clearUsageLimitsFor(routeThreadKey);
436
- return resumeThreadQueue({ environmentId, input: { threadId } });
437
- };
438
- const result = await resume();
439
- if (result._tag === "Failure") {
440
- if (!isAtomCommandInterrupted(result)) {
441
- const error = squashAtomCommandFailure(result);
442
- setThreadError(
443
- threadId,
444
- error instanceof Error ? error.message : "Could not resume thread.",
445
- );
446
- }
447
- } else {
448
- clearUsageLimitsFor(routeThreadKey);
449
- if (currentRouteThreadKeyRef.current === routeThreadKey) scrollToEnd();
450
- }
451
- sendInFlightRef.current = false;
452
- setResumingThreadKeys((current) => {
453
- const next = new Set(current);
454
- next.delete(routeThreadKey);
455
- return next;
456
- });
457
- };
458
-
366
const onSend = async (
367
e?: { preventDefault: () => void },
368
dispatchMode: ComposerDispatchMode = "auto",
@@ -1912,5 +1819,5 @@ export function useComposerSend(input: ComposerSendInput) {
1819
finish();
1820
};
1821
1915
- return { onSend, onResume, onImplementPlanInNewThread, isResuming };
1822
+ return { onSend, onImplementPlanInNewThread };
1823
}