The best way to control Claude Code, Codex and more from any computer or phone
fix(server): attribute usage-limit auto-resume to T3 Code (#29838)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
shivam committed
Nov 5, 2026 at 07:02 UTC
90f0158e60b08946bcaae1df07109a893e7c36af
4 files changed
+121
-2
apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts
+107
@@ -27,6 +27,7 @@ import * as Fiber from "effect/Fiber";
27
import * as Layer from "effect/Layer";
28
import * as Stream from "effect/Stream";
29
import * as Tracer from "effect/Tracer";
30
+import * as TestClock from "effect/testing/TestClock";
31
32
import * as CheckpointStore from "../checkpointing/CheckpointStore.ts";
33
import * as ServerConfig from "../config.ts";
@@ -46,6 +47,7 @@ import * as Orchestrator from "./Orchestrator.ts";
47
import { continueRestartedRun } from "./RestartContinuation.ts";
48
import * as RuntimeLayer from "./runtimeLayer.ts";
49
import * as ThreadCommandExecutor from "./ThreadCommandExecutor.ts";
50
+import { limitRecoveryCommand } from "./UsageLimitRecoveryWorker.ts";
51
import * as ProviderTurnStartServiceTestkit from "./ProviderTurnStartService.testkit.ts";
52
import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
53
import * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests";
@@ -2398,6 +2400,111 @@ it.layer(Layer.merge(layerTest, ProviderContinuationRequests.layer))(
2400
}),
2401
);
2402
2403
+ it.effect("tracks a child's usage-limit auto-resume as a follow-up", () =>
2404
+ Effect.gen(function* () {
2405
+ const orchestrator = yield* Orchestrator.OrchestratorV2;
2406
+ const eventSink = yield* EventSink.EventSinkV2;
2407
+ const now = yield* DateTime.now;
2408
+ const parentThreadId = ThreadId.make("thread:limit-resume-parent");
2409
+ const projectId = ProjectId.make("project:limit-resume");
2410
+ const parentRunId = RunId.make("run:limit-resume-parent");
2411
+ const rootNodeId = NodeId.make("node:limit-resume-parent");
2412
+ yield* seedParentWithTerminalTask({
2413
+ threadId: parentThreadId,
2414
+ projectId,
2415
+ runId: parentRunId,
2416
+ rootNodeId,
2417
+ taskId: NodeId.make("node:limit-resume-seed"),
2418
+ deliveryState: "acknowledged",
2419
+ now,
2420
+ });
2421
+ const child = yield* seedRestartCancelledChild({
2422
+ parentThreadId,
2423
+ projectId,
2424
+ parentRunId,
2425
+ rootNodeId,
2426
+ name: "limit-resume-child",
2427
+ completionWake: "always",
2428
+ continuationPending: false,
2429
+ runStatus: "completed",
2430
+ now,
2431
+ });
2432
+ const childRun = (yield* orchestrator.getThreadProjection(child.childThreadId)).runs[0]!;
2433
+ yield* eventSink.write({
2434
+ commandId: reconcileCommandId("limit-resume-child-limited"),
2435
+ events: [
2436
+ {
2437
+ id: EventId.make("event:limit-resume-child-failed"),
2438
+ type: "run.updated",
2439
+ threadId: child.childThreadId,
2440
+ runId: childRun.id,
2441
+ occurredAt: now,
2442
+ payload: { ...childRun, status: "failed", completedAt: now },
2443
+ },
2444
+ {
2445
+ id: EventId.make("event:limit-resume-child-error"),
2446
+ type: "turn-item.updated",
2447
+ threadId: child.childThreadId,
2448
+ occurredAt: now,
2449
+ payload: {
2450
+ id: TurnItemId.make("turn-item:limit-resume-child-error"),
2451
+ type: "error",
2452
+ threadId: child.childThreadId,
2453
+ runId: childRun.id,
2454
+ nodeId: childRun.rootNodeId,
2455
+ providerThreadId: null,
2456
+ providerTurnId: null,
2457
+ nativeItemRef: null,
2458
+ parentItemId: null,
2459
+ ordinal: 1,
2460
+ status: "failed",
2461
+ title: "Usage limit reached",
2462
+ startedAt: now,
2463
+ completedAt: now,
2464
+ updatedAt: now,
2465
+ failure: {
2466
+ class: "usage_limit",
2467
+ message: "Plan limit reached.",
2468
+ code: "usageLimitExceeded",
2469
+ retryable: null,
2470
+ resetAt: DateTime.formatIso(DateTime.add(now, { minutes: 1 })),
2471
+ },
2472
+ },
2473
+ },
2474
+ ],
2475
+ });
2476
+ yield* orchestrator.recoverDelegatedTasks;
2477
+ const childShell = Effect.map(orchestrator.getShellSnapshot(), (snapshot) =>
2478
+ snapshot.threads.find((thread) => thread.id === child.childThreadId),
2479
+ );
2480
+ const arm = limitRecoveryCommand((yield* childShell)!, true, DateTime.toEpochMillis(now));
2481
+ assert(arm?.type === "thread.metadata.update" && arm.limitRecovery != null);
2482
+ yield* orchestrator.autoArmLimitRecovery({
2483
+ commandId: arm.commandId,
2484
+ threadId: arm.threadId,
2485
+ limitRecovery: arm.limitRecovery,
2486
+ });
2487
+ yield* TestClock.adjust("1 minute");
2488
+ const resume = limitRecoveryCommand(
2489
+ (yield* childShell)!,
2490
+ true,
2491
+ DateTime.toEpochMillis(yield* DateTime.now),
2492
+ );
2493
+ assert(resume?.type === "message.dispatch");
2494
+ yield* orchestrator.dispatch(resume);
2495
+
2496
+ const resumed = (yield* orchestrator.getThreadProjection(child.childThreadId)).runs.find(
2497
+ (run) => run.userMessageId === resume.messageId,
2498
+ );
2499
+ assert.isDefined(resumed?.delegatedTaskId);
2500
+ const followup = (yield* orchestrator.getThreadProjection(parentThreadId)).subagents.find(
2501
+ (task) => task.id === resumed?.delegatedTaskId,
2502
+ );
2503
+ assert.equal(followup?.childThreadId, child.childThreadId);
2504
+ assert.isNull(followup?.result);
2505
+ }),
2506
+ );
2507
+
2508
it.effect("serializes a recovered child answer with its parent and tracks the follow-up", () =>
2509
Effect.gen(function* () {
2510
const orchestrator = yield* Orchestrator.OrchestratorV2;
apps/server/src/orchestration-v2/Orchestrator.ts
+6
-1
@@ -701,7 +701,11 @@ function mayStartSubagentFollowup(
701
if (command.scheduledTaskId !== undefined || command.restartContinuationOfRunId !== undefined)
702
return false;
703
if (command.notification !== undefined) return command.notification.source.kind !== "monitor";
704
- return command.createdBy === "user" || command.creationSource === "mcp";
704
+ return (
705
+ command.createdBy === "user" ||
706
+ command.creationSource === "mcp" ||
707
+ command.usageLimitContinuationOfRunId !== undefined
708
+ );
709
}
710
711
/** Waiting runs only drain post-terminal output; their agent turn is over and still needs a wake. */
@@ -8041,6 +8045,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
8045
parentThreadId === null ||
8046
(command.notification === undefined &&
8047
command.createdBy !== "user" &&
8048
+ command.usageLimitContinuationOfRunId === undefined &&
8049
command.senderThreadId !== parentThreadId)
8050
)
8051
return;
apps/server/src/orchestration-v2/UsageLimitRecoveryWorker.ts
+1
-1
@@ -73,7 +73,7 @@ export function limitRecoveryCommand(
73
text: "Continue where you left off.",
74
attachments: [],
75
dispatchMode: { type: "start_immediately" },
76
- createdBy: "user",
76
+ createdBy: "agent",
77
creationSource: "server",
78
};
79
}
apps/server/src/orchestration-v2/runtimeLayer.test.ts
+7
@@ -8471,6 +8471,13 @@ it.layer(layerTest)("usage-limit recovery", (it) => {
8471
yield* orchestrator.dispatch(resume);
8472
}
8473
const after = yield* orchestrator.getThreadProjection(threadId);
8474
+ if (scenario === "resume") {
8475
+ assert.deepInclude(after.messages.at(-1), {
8476
+ text: "Continue where you left off.",
8477
+ createdBy: "agent",
8478
+ creationSource: "server",
8479
+ });
8480
+ }
8481
assert.lengthOf(
8482
after.runs,
8483
before.runs.length +