Skip to content

Commit cbb0417

Browse files
fix(mobile): resume continues a usage-limited thread before its queue (#27662)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent dc9b1a5 commit cbb0417

10 files changed

Lines changed: 264 additions & 31 deletions

File tree

‎apps/mobile/src/features/threads/ThreadComposer.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,7 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
11011101
) : showResumeAction ? (
11021102
<ComposerActionButton
11031103
size={usesLargeControls ? "large" : "regular"}
1104-
accessibilityLabel="Resume queue"
1104+
accessibilityLabel="Resume thread"
11051105
icon="play"
11061106
disabled={!props.canOperateThread || resumingQueue}
11071107
onPress={() => void handleResumeQueue()}
@@ -1170,7 +1170,7 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
11701170
) : showResumeAction && voicePresentation.showsSend ? (
11711171
<ComposerActionButton
11721172
size={usesLargeControls ? "large" : "regular"}
1173-
accessibilityLabel="Resume queue"
1173+
accessibilityLabel="Resume thread"
11741174
icon="play"
11751175
disabled={!props.canOperateThread || resumingQueue}
11761176
onPress={() => void handleResumeQueue()}

‎apps/mobile/src/features/threads/ThreadRouteScreen.tsx‎

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
AuthTerminalReadScope,
2121
DEFAULT_SERVER_SETTINGS,
2222
EnvironmentId,
23+
MessageId,
2324
ThreadId,
2425
type ProjectScript,
2526
} from "@t3tools/contracts";
@@ -54,6 +55,10 @@ import {
5455
} from "../../state/use-remote-environment-registry";
5556
import { useKnownTerminalSessions } from "../../state/use-terminal-session";
5657
import { uuidv4 } from "../../lib/uuid";
58+
import {
59+
isAtomCommandInterrupted,
60+
squashAtomCommandFailure,
61+
} from "@t3tools/client-runtime/state/runtime";
5762
import { readEnvironmentScope, useEnvironmentScope } from "../../state/session";
5863
import { useSelectedThreadDetailState } from "../../state/use-thread-detail";
5964
import { useThreadSelection } from "../../state/use-thread-selection";
@@ -101,6 +106,7 @@ import {
101106
threadRouteOpensDraft,
102107
} from "./thread-route-hydration";
103108
import { ThreadHeader } from "./ThreadHeader";
109+
import { resumeThread } from "./resume-thread";
104110
import {
105111
useAndroidWorkspaceBack,
106112
useWorkspaceBackNavigation,
@@ -379,7 +385,22 @@ function ThreadRouteContent(
379385
const gitActions = useSelectedThreadGitActions();
380386
const requests = useSelectedThreadRequests();
381387
const interruptThreadTurn = useAtomCommand(threadEnvironment.interruptTurn, "thread interrupt");
382-
const resumeThreadQueue = useAtomCommand(threadEnvironment.resumeThreadQueue, "resume queue");
388+
const resumeThreadQueue = useAtomCommand(threadEnvironment.resumeThreadQueue, {
389+
label: "resume queue",
390+
reportFailure: false,
391+
});
392+
const continueThreadRun = useAtomCommand(threadEnvironment.startTurn, {
393+
label: "continue thread",
394+
reportFailure: false,
395+
});
396+
const setThreadRuntimeMode = useAtomCommand(threadEnvironment.setRuntimeMode, {
397+
label: "set thread runtime mode",
398+
reportFailure: false,
399+
});
400+
const setThreadInteractionMode = useAtomCommand(threadEnvironment.setInteractionMode, {
401+
label: "set thread interaction mode",
402+
reportFailure: false,
403+
});
383404
const loadEarlierHistory = useAtomCommand(threadEnvironment.loadEarlierHistory, {
384405
label: "load earlier thread history",
385406
reportFailure: false,
@@ -683,10 +704,27 @@ function ThreadRouteContent(
683704

684705
const handleResumeQueue = async () => {
685706
if (!selectedThread) return;
686-
await resumeThreadQueue({
687-
environmentId: selectedThread.environmentId,
688-
input: { threadId: selectedThread.id },
707+
const result = await resumeThread({
708+
thread: selectedThread,
709+
resumableRunId: composer.resumableRunId,
710+
queueHeld: composer.queueHeld,
711+
runtimeMode: composer.runtimeMode ?? selectedThread.runtimeMode,
712+
interactionMode: composer.interactionMode ?? selectedThread.interactionMode,
713+
modelSelection: composer.modelSelection,
714+
messageId: MessageId.make(uuidv4()),
715+
createdAt: new Date().toISOString(),
716+
setRuntimeMode: setThreadRuntimeMode,
717+
setInteractionMode: setThreadInteractionMode,
718+
startTurn: continueThreadRun,
719+
resumeQueue: resumeThreadQueue,
689720
});
721+
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
722+
const error = squashAtomCommandFailure(result);
723+
Dialog.alert(
724+
"Could not resume thread",
725+
error instanceof Error ? error.message : "Try again.",
726+
);
727+
}
690728
};
691729

692730
const handleOpenTerminal = (nextTerminalId?: string | null) => {
@@ -1071,7 +1109,9 @@ function ThreadRouteContent(
10711109
onRemoveDraftImage={composer.onRemoveDraftImage}
10721110
serverConfig={serverConfig}
10731111
onStopThread={awaitingBootstrapTurn ? handleCancelWorktreeSetup : handleStopThread}
1074-
onResumeQueue={composer.queueHeld ? handleResumeQueue : null}
1112+
onResumeQueue={
1113+
composer.queueHeld || composer.resumableRunId !== null ? handleResumeQueue : null
1114+
}
10751115
onSendMessage={composer.onSendMessage}
10761116
onReconnectEnvironment={handleReconnectEnvironment}
10771117
canSwitchThreadProvider={composer.canSwitchThreadProvider}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { EnvironmentId, MessageId, RunId, ThreadId } from "@t3tools/contracts";
2+
import * as Cause from "effect/Cause";
3+
import { AsyncResult } from "effect/reactivity";
4+
import { describe, expect, it } from "vite-plus/test";
5+
6+
import { resumeThread } from "./resume-thread";
7+
8+
const runId = RunId.make("run-limited");
9+
const failure = AsyncResult.failure(Cause.fail(new Error("not connected")));
10+
11+
function setup(options: { readonly startTurnFails?: boolean } = {}) {
12+
const calls: Array<{ readonly command: string; readonly input: unknown }> = [];
13+
const record =
14+
(command: string, fails = false) =>
15+
async (request: { readonly input: unknown }) => {
16+
calls.push({ command, input: request.input });
17+
return fails ? failure : AsyncResult.success(undefined);
18+
};
19+
const base = {
20+
thread: {
21+
environmentId: EnvironmentId.make("env"),
22+
id: ThreadId.make("thread"),
23+
runtimeMode: "full-access",
24+
interactionMode: "default",
25+
},
26+
runtimeMode: "full-access",
27+
interactionMode: "default",
28+
modelSelection: null,
29+
messageId: MessageId.make("message"),
30+
createdAt: "2026-10-30T00:00:00.000Z",
31+
setRuntimeMode: record("setRuntimeMode"),
32+
setInteractionMode: record("setInteractionMode"),
33+
startTurn: record("startTurn", options.startTurnFails),
34+
resumeQueue: record("resumeQueue"),
35+
} as const;
36+
return { calls, base };
37+
}
38+
39+
describe("resumeThread", () => {
40+
it("continues the limited run before resuming its held queue", async () => {
41+
const { calls, base } = setup();
42+
const result = await resumeThread({ ...base, resumableRunId: runId, queueHeld: true });
43+
44+
expect(result._tag).toBe("Success");
45+
expect(calls.map((call) => call.command)).toEqual(["startTurn", "resumeQueue"]);
46+
expect(calls[0]?.input).toMatchObject({
47+
manualContinuationOfRunId: runId,
48+
dispatchMode: "start",
49+
});
50+
});
51+
52+
it("does not resume the queue when the continuation fails", async () => {
53+
const { calls, base } = setup({ startTurnFails: true });
54+
const result = await resumeThread({ ...base, resumableRunId: runId, queueHeld: true });
55+
56+
expect(result).toBe(failure);
57+
expect(calls.map((call) => call.command)).toEqual(["startTurn"]);
58+
});
59+
60+
it("only continues the run when nothing is queued", async () => {
61+
const { calls, base } = setup();
62+
await resumeThread({ ...base, resumableRunId: runId, queueHeld: false });
63+
64+
expect(calls.map((call) => call.command)).toEqual(["startTurn"]);
65+
});
66+
67+
it("saves changed modes to the thread before continuing", async () => {
68+
const { calls, base } = setup();
69+
await resumeThread({
70+
...base,
71+
runtimeMode: "approval-required",
72+
interactionMode: "plan",
73+
resumableRunId: runId,
74+
queueHeld: false,
75+
});
76+
77+
expect(calls.map((call) => call.command)).toEqual([
78+
"setRuntimeMode",
79+
"setInteractionMode",
80+
"startTurn",
81+
]);
82+
});
83+
84+
it("resumes a held queue directly when no run needs continuing", async () => {
85+
const { calls, base } = setup();
86+
await resumeThread({ ...base, resumableRunId: null, queueHeld: true });
87+
88+
expect(calls.map((call) => call.command)).toEqual(["resumeQueue"]);
89+
});
90+
});
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import type {
2+
ResumeThreadQueueInput,
3+
SetThreadInteractionModeInput,
4+
SetThreadRuntimeModeInput,
5+
StartThreadTurnInput,
6+
} from "@t3tools/client-runtime/operations";
7+
import type { AtomCommandResult } from "@t3tools/client-runtime/state/runtime";
8+
import type {
9+
EnvironmentId,
10+
MessageId,
11+
ModelSelection,
12+
ProviderInteractionMode,
13+
RunId,
14+
RuntimeMode,
15+
ThreadId,
16+
} from "@t3tools/contracts";
17+
18+
type Command<Input, E> = (request: {
19+
readonly environmentId: EnvironmentId;
20+
readonly input: Input;
21+
}) => Promise<AtomCommandResult<unknown, E>>;
22+
23+
/** Resolves to the first failed command's result, or the last successful one. */
24+
export async function resumeThread<E>(input: {
25+
readonly thread: {
26+
readonly environmentId: EnvironmentId;
27+
readonly id: ThreadId;
28+
readonly runtimeMode: RuntimeMode;
29+
readonly interactionMode: ProviderInteractionMode;
30+
};
31+
readonly resumableRunId: RunId | null;
32+
readonly queueHeld: boolean;
33+
readonly runtimeMode: RuntimeMode;
34+
readonly interactionMode: ProviderInteractionMode;
35+
readonly modelSelection: ModelSelection | null;
36+
readonly messageId: MessageId;
37+
readonly createdAt: string;
38+
readonly setRuntimeMode: Command<SetThreadRuntimeModeInput, E>;
39+
readonly setInteractionMode: Command<SetThreadInteractionModeInput, E>;
40+
readonly startTurn: Command<StartThreadTurnInput, E>;
41+
readonly resumeQueue: Command<ResumeThreadQueueInput, E>;
42+
}): Promise<AtomCommandResult<unknown, E>> {
43+
const { thread, runtimeMode, interactionMode, createdAt } = input;
44+
const environmentId = thread.environmentId;
45+
// The server holds the queue behind a stopped or usage-limited run until that run is continued.
46+
if (input.resumableRunId !== null) {
47+
// A turn on an existing thread runs with the thread's saved modes, not the turn's.
48+
if (runtimeMode !== thread.runtimeMode) {
49+
const saved = await input.setRuntimeMode({
50+
environmentId,
51+
input: { threadId: thread.id, runtimeMode, createdAt },
52+
});
53+
if (saved._tag === "Failure") return saved;
54+
}
55+
if (interactionMode !== thread.interactionMode) {
56+
const saved = await input.setInteractionMode({
57+
environmentId,
58+
input: { threadId: thread.id, interactionMode, createdAt },
59+
});
60+
if (saved._tag === "Failure") return saved;
61+
}
62+
const continued = await input.startTurn({
63+
environmentId,
64+
input: {
65+
creationSource: "mobile",
66+
threadId: thread.id,
67+
manualContinuationOfRunId: input.resumableRunId,
68+
...(input.modelSelection ? { modelSelection: input.modelSelection } : {}),
69+
message: {
70+
messageId: input.messageId,
71+
role: "user",
72+
text: "Continue where you left off.",
73+
attachments: [],
74+
},
75+
runtimeMode,
76+
interactionMode,
77+
dispatchMode: "start",
78+
},
79+
});
80+
if (continued._tag === "Failure" || !input.queueHeld) return continued;
81+
}
82+
return input.resumeQueue({ environmentId, input: { threadId: thread.id } });
83+
}

‎apps/mobile/src/state/use-thread-composer-state.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { ThreadCheckpointSummary } from "@t3tools/client-runtime/state/thre
88
import { resolvePendingHandoff } from "@t3tools/client-runtime/handoff";
99
import {
1010
deriveProviderSubagentStatus,
11+
deriveResumableRunId,
1112
deriveRunlessWorkStartedAt,
1213
deriveThreadActivityRun,
1314
deriveThreadRuntime,
@@ -361,6 +362,9 @@ export function useThreadComposerState() {
361362
// A stop or a restart holds the queue, completion wakes included, until it
362363
// is resumed; the queue sheet lists only the user's own messages.
363364
const queueHeld = queueWorkflow?.isHeld === true;
365+
const resumableRunId = selectedThreadProjection
366+
? deriveResumableRunId(selectedThreadProjection.projection, selectedThreadRuntime)
367+
: null;
364368

365369
const runlessWorkStartedAt = selectedThreadProjection
366370
? deriveRunlessWorkStartedAt(selectedThreadProjection.projection)
@@ -749,6 +753,7 @@ export function useThreadComposerState() {
749753
followUpBehavior,
750754
canSteerActiveTurn,
751755
queueHeld,
756+
resumableRunId,
752757
queuedRunEdit,
753758
isSavingQueuedEdit,
754759
cancelQueuedRunEdit,

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ import { RightPanelSurfaceContent } from "./chat/RightPanelSurfaceContent";
4242
import { usePendingThreadRequests } from "./chat/usePendingThreadRequests";
4343
import { isScratchProject } from "@t3tools/client-runtime/state/projects";
4444
import { visibleThreadPullRequests } from "@t3tools/shared/threadPullRequests";
45-
import {
46-
latestExecutedRun,
47-
latestRootProviderFailure,
48-
} from "@t3tools/shared/orchestrationV2ThreadError";
4945
import type { UsageLimitSourceSnapshots } from "@t3tools/contracts";
5046
import { feedbackBannerItem } from "./chat/ComposerFeedback";
5147
import { useUsageLimitsPanel } from "./chat/useUsageLimitsPanel";
@@ -77,6 +73,7 @@ import {
7773
import {
7874
deriveProviderSubagentStatus,
7975
deriveReportedModelSelection,
76+
deriveResumableRunId,
8077
formatModelSelectionEffort,
8178
deriveRunlessWorkStartedAt,
8279
deriveThreadActivityRun,
@@ -815,16 +812,10 @@ export default function ChatView(props: ChatViewProps) {
815812
isServerThread &&
816813
serverProjection?.runs.some((run) => run.status === "queued" && run.queueHeld === true) ===
817814
true;
818-
const resumableRunId = (() => {
819-
if (!isServerThread || serverProjection === null) return null;
820-
const run = latestExecutedRun(serverProjection.runs);
821-
if (run?.status === "interrupted") return run.id;
822-
return run?.status === "failed" &&
823-
serverRuntime?.lastErrorClass === "usage_limit" &&
824-
latestRootProviderFailure(run, serverProjection.turnItems)?.class === "usage_limit"
825-
? run.id
826-
: null;
827-
})();
815+
const resumableRunId =
816+
!isServerThread || serverProjection === null
817+
? null
818+
: deriveResumableRunId(serverProjection, serverRuntime);
828819
const parentSubagentThreadId =
829820
activeThread?.lineage.relationshipToParent === "subagent"
830821
? activeThread.lineage.parentThreadId

‎docs/user/composer.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,8 @@ images. **Send now** sends a message right away, into the running turn if there
213213
also edit or remove it.
214214

215215
If you stop the agent or the server restarts, queued messages keep their order and are held.
216-
Press **Resume** in an empty composer on web and desktop, or **Resume queue** on mobile, to
217-
continue. Sending a new message also resumes the queue, unless it steers a turn that is already
216+
Press **Resume** in an empty composer to continue. **Resume** also continues a stopped
217+
run or one that hit a usage limit, even with nothing queued. Sending a new message also resumes the queue, unless it steers a turn that is already
218218
running. You can edit or remove held messages without starting them.
219219

220220
The pencil on a queued message opens it in the composer for editing. The original message

‎docs/user/thread-sidebar.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,9 @@ from its context menu, or **Copy thread** from its long-press menu on mobile.
238238
**Limited** means the provider stopped on a usage or rate limit. The conversation
239239
keeps the provider's explanation. Retry after the limit resets, or switch to
240240
another provider instance.
241-
On web and desktop, press **Resume** in an empty composer to continue a limited
242-
or interrupted turn manually.
241+
Press **Resume** in an empty composer to continue a limited or interrupted turn
242+
manually. Do this when the provider reports no reset time, because nothing
243+
continues the turn on its own.
243244
Queued messages stay saved while the limit blocks the thread. They run after
244245
the continuation finishes. If the queue was held by a restart, resume it then.
245246

0 commit comments

Comments
 (0)