Skip to content

Commit 2da282f

Browse files
fix(mobile): resume continues a usage-limited thread before its queue
Mobile's Resume only sent queue.resume, which the server rejects while a usage limit holds the queue, and the failure was only logged. Share the web's resumable-run rule as deriveResumableRunId, send the manual continuation first on mobile, and show failures in a dialog. Fixes #27187 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent dd14848 commit 2da282f

5 files changed

Lines changed: 84 additions & 24 deletions

File tree

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

Lines changed: 46 additions & 4 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,11 @@ 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+
type AtomCommandResult,
62+
} from "@t3tools/client-runtime/state/runtime";
5763
import { readEnvironmentScope, useEnvironmentScope } from "../../state/session";
5864
import { useSelectedThreadDetailState } from "../../state/use-thread-detail";
5965
import { useThreadSelection } from "../../state/use-thread-selection";
@@ -386,6 +392,7 @@ function ThreadRouteContent(
386392
const requests = useSelectedThreadRequests();
387393
const interruptThreadTurn = useAtomCommand(threadEnvironment.interruptTurn, "thread interrupt");
388394
const resumeThreadQueue = useAtomCommand(threadEnvironment.resumeThreadQueue, "resume queue");
395+
const continueThreadRun = useAtomCommand(threadEnvironment.startTurn, "continue thread");
389396
const loadEarlierHistory = useAtomCommand(threadEnvironment.loadEarlierHistory, {
390397
label: "load earlier thread history",
391398
reportFailure: false,
@@ -689,10 +696,45 @@ function ThreadRouteContent(
689696

690697
const handleResumeQueue = async () => {
691698
if (!selectedThread) return;
692-
await resumeThreadQueue({
693-
environmentId: selectedThread.environmentId,
694-
input: { threadId: selectedThread.id },
695-
});
699+
const failed = (result: AtomCommandResult<unknown, unknown>) => {
700+
if (result._tag !== "Failure") return false;
701+
if (!isAtomCommandInterrupted(result)) {
702+
const error = squashAtomCommandFailure(result);
703+
Dialog.alert(
704+
"Could not resume thread",
705+
error instanceof Error ? error.message : "Try again.",
706+
);
707+
}
708+
return true;
709+
};
710+
// The server holds the queue behind a stopped or usage-limited run until that run is continued.
711+
if (composer.resumableRunId !== null) {
712+
const continued = await continueThreadRun({
713+
environmentId: selectedThread.environmentId,
714+
input: {
715+
creationSource: "mobile",
716+
threadId: selectedThread.id,
717+
manualContinuationOfRunId: composer.resumableRunId,
718+
...(composer.modelSelection ? { modelSelection: composer.modelSelection } : {}),
719+
message: {
720+
messageId: MessageId.make(uuidv4()),
721+
role: "user",
722+
text: "Continue where you left off.",
723+
attachments: [],
724+
},
725+
runtimeMode: composer.runtimeMode ?? selectedThread.runtimeMode,
726+
interactionMode: composer.interactionMode ?? selectedThread.interactionMode,
727+
dispatchMode: "start",
728+
},
729+
});
730+
if (failed(continued)) return;
731+
}
732+
failed(
733+
await resumeThreadQueue({
734+
environmentId: selectedThread.environmentId,
735+
input: { threadId: selectedThread.id },
736+
}),
737+
);
696738
};
697739

698740
const handleOpenTerminal = (nextTerminalId?: string | null) => {

‎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,
@@ -359,6 +360,9 @@ export function useThreadComposerState() {
359360
// A stop or a restart holds the queue, completion wakes included, until it
360361
// is resumed; the queue sheet lists only the user's own messages.
361362
const queueHeld = queueWorkflow?.isHeld === true;
363+
const resumableRunId = selectedThreadProjection
364+
? deriveResumableRunId(selectedThreadProjection.projection, selectedThreadRuntime)
365+
: null;
362366

363367
const runlessWorkStartedAt = selectedThreadProjection
364368
? deriveRunlessWorkStartedAt(selectedThreadProjection.projection)
@@ -743,6 +747,7 @@ export function useThreadComposerState() {
743747
followUpBehavior,
744748
canSteerActiveTurn,
745749
queueHeld,
750+
resumableRunId,
746751
queuedRunEdit,
747752
isSavingQueuedEdit,
748753
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";
@@ -76,6 +72,7 @@ import {
7672
import {
7773
deriveProviderSubagentStatus,
7874
deriveReportedModelSelection,
75+
deriveResumableRunId,
7976
formatModelSelectionEffort,
8077
deriveRunlessWorkStartedAt,
8178
deriveThreadActivityRun,
@@ -814,16 +811,10 @@ export default function ChatView(props: ChatViewProps) {
814811
isServerThread &&
815812
serverProjection?.runs.some((run) => run.status === "queued" && run.queueHeld === true) ===
816813
true;
817-
const resumableRunId = (() => {
818-
if (!isServerThread || serverProjection === null) return null;
819-
const run = latestExecutedRun(serverProjection.runs);
820-
if (run?.status === "interrupted") return run.id;
821-
return run?.status === "failed" &&
822-
serverRuntime?.lastErrorClass === "usage_limit" &&
823-
latestRootProviderFailure(run, serverProjection.turnItems)?.class === "usage_limit"
824-
? run.id
825-
: null;
826-
})();
814+
const resumableRunId =
815+
!isServerThread || serverProjection === null
816+
? null
817+
: deriveResumableRunId(serverProjection, serverRuntime);
827818
const parentSubagentThreadId =
828819
activeThread?.lineage.relationshipToParent === "subagent"
829820
? activeThread.lineage.parentThreadId

‎packages/client-runtime/src/state/threadExecution.test.ts‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
deriveReportedModelSelection,
2323
deriveLatestThreadRun,
2424
deriveProviderSubagentStatus,
25+
deriveResumableRunId,
2526
deriveRunlessWorkStartedAt,
2627
deriveThreadActivityRun,
2728
deriveThreadRuntime,
@@ -151,6 +152,7 @@ describe("thread execution presentation", () => {
151152
lastError: "Plan limit reached",
152153
lastErrorClass: "usage_limit",
153154
});
155+
expect(deriveResumableRunId(projection, deriveThreadRuntime(projection))).toBe(failed.id);
154156
const cancelledQueued = {
155157
...run("cancelled-queued", 3, "cancelled"),
156158
startedAt: null,
@@ -162,12 +164,15 @@ describe("thread execution presentation", () => {
162164
expect(
163165
deriveThreadRuntime({ ...projection, runs: [failed, queued, cancelledQueued] }),
164166
).toMatchObject({ status: "failed", lastErrorClass: "usage_limit" });
165-
expect(
166-
deriveThreadRuntime({
167-
...projection,
168-
turnItems: [{ ...item, failure: { ...item.failure, class: "provider_error" as const } }],
169-
}),
170-
).toMatchObject({ status: "queued", lastErrorClass: null });
167+
const providerError = {
168+
...projection,
169+
turnItems: [{ ...item, failure: { ...item.failure, class: "provider_error" as const } }],
170+
};
171+
expect(deriveThreadRuntime(providerError)).toMatchObject({
172+
status: "queued",
173+
lastErrorClass: null,
174+
});
175+
expect(deriveResumableRunId(providerError, deriveThreadRuntime(providerError))).toBeNull();
171176
});
172177

173178
it("keeps live activity attached to an executing run when a newer run is queued", () => {
@@ -228,6 +233,7 @@ describe("thread execution presentation", () => {
228233
status: "interrupted",
229234
activeRunId: null,
230235
});
236+
expect(deriveResumableRunId(projection, deriveThreadRuntime(projection))).toBe(interrupted.id);
231237

232238
const resumed = { ...projection, runs: [interrupted, { ...held, queueHeld: false }] };
233239
expect(deriveThreadRuntime(resumed)).toMatchObject({ status: "queued" });

‎packages/client-runtime/src/state/threadExecution.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
latestExecutedRun,
23
latestRootProviderFailure,
34
latestUnheldRun,
45
threadErrorSummary,
@@ -16,6 +17,7 @@ import {
1617
type OrchestrationV2ExecutionNode,
1718
type OrchestrationV2ThreadProjection,
1819
orchestrationV2RunWorkStartedAt,
20+
type RunId,
1921
type ThreadId,
2022
} from "@t3tools/contracts";
2123
import {
@@ -209,6 +211,20 @@ export function formatProviderSubagentStatus(
209211
return live ? `${label} ${elapsed}` : `${label} in ${elapsed}`;
210212
}
211213

214+
/** The run a manual continuation resumes; the server holds the queue until it does. */
215+
export function deriveResumableRunId(
216+
projection: Pick<ThreadStructure, "runs" | "turnItems">,
217+
runtime: Pick<ThreadStructureRuntime, "lastErrorClass"> | null,
218+
): RunId | null {
219+
const run = latestExecutedRun(projection.runs);
220+
if (run?.status === "interrupted") return run.id;
221+
return run?.status === "failed" &&
222+
runtime?.lastErrorClass === "usage_limit" &&
223+
latestRootProviderFailure(run, projection.turnItems)?.class === "usage_limit"
224+
? run.id
225+
: null;
226+
}
227+
212228
export function deriveThreadRuntime(
213229
projection: ThreadStructure & Pick<OrchestrationV2ThreadProjection, "updatedAt">,
214230
): ThreadRuntimeSummary | null {

0 commit comments

Comments
 (0)