Skip to content

Commit 049244a

Browse files
committed
fix(server): background wakes wait out a usage limit instead of failing
When a provider hits its usage limit, the thread's run fails. Background work that finishes afterwards (subagents, monitors, watched commands) wakes the thread with a queued message. With no run active, the orchestrator started that message immediately, so every wake failed against the same limit and its result reached a turn that never ran. A queued message now waits behind the limited run, which is the rule startNextQueuedRun already applies to queued runs on a limited provider. Continuing the thread drains it as before. Model: Claude Opus 5.5 (claude-opus-5-5) · Harness: Claude Code in T3 Code
1 parent 60b35a1 commit 049244a

2 files changed

Lines changed: 108 additions & 1 deletion

File tree

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5237,7 +5237,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
52375237
projection.thread.worktreePath === null
52385238
? latestRun
52395239
: undefined;
5240-
const queueBehindRun = activeRun ?? worktreeMissingRun;
5240+
const usageLimitedRun =
5241+
activeRun === undefined &&
5242+
dispatchMode.type === "queue_after_active" &&
5243+
usageLimitHoldsQueue(projection, modelSelection.instanceId)
5244+
? (latestRun ?? undefined)
5245+
: undefined;
5246+
const queueBehindRun = activeRun ?? worktreeMissingRun ?? usageLimitedRun;
52415247
const pendingMergeBackTransfers = pendingMergeBackTransfersForThread(projection);
52425248
const startsOrQueuesRun =
52435249
dispatchMode.type === "defer_start" ||

‎apps/server/src/orchestration-v2/runtimeLayer.test.ts‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4202,6 +4202,107 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
42024202
}),
42034203
);
42044204

4205+
it.effect("queues a background wake behind a usage limit instead of starting it", () =>
4206+
Effect.gen(function* () {
4207+
const orchestrator = yield* Orchestrator.OrchestratorV2;
4208+
const eventSink = yield* EventSink.EventSinkV2;
4209+
const threadId = ThreadId.make("runtime-layer-limited-wake");
4210+
yield* orchestrator.dispatch({
4211+
type: "thread.create",
4212+
commandId: CommandId.make(`${threadId}:create`),
4213+
threadId,
4214+
createdBy: "user",
4215+
creationSource: "web",
4216+
projectId: ProjectId.make(`${threadId}:project`),
4217+
title: "Limited wake",
4218+
modelSelection,
4219+
runtimeMode: "full-access",
4220+
interactionMode: "default",
4221+
branch: null,
4222+
worktreePath: process.cwd(),
4223+
});
4224+
yield* orchestrator.dispatch({
4225+
type: "message.dispatch",
4226+
commandId: CommandId.make(`${threadId}:prompt`),
4227+
threadId,
4228+
messageId: MessageId.make(`${threadId}:prompt`),
4229+
text: "Prompt",
4230+
attachments: [],
4231+
createdBy: "user",
4232+
creationSource: "web",
4233+
dispatchMode: { type: "start_immediately" },
4234+
});
4235+
const [limited] = (yield* orchestrator.getThreadProjection(threadId)).runs;
4236+
assert.isDefined(limited);
4237+
const now = yield* DateTime.now;
4238+
yield* eventSink.write({
4239+
events: [
4240+
{
4241+
id: EventId.make(`${threadId}:error`),
4242+
type: "turn-item.updated",
4243+
threadId,
4244+
runId: limited.id,
4245+
nodeId: limited.rootNodeId!,
4246+
providerInstanceId: limited.providerInstanceId,
4247+
occurredAt: now,
4248+
payload: {
4249+
id: TurnItemId.make(`${threadId}:error`),
4250+
type: "error",
4251+
threadId,
4252+
runId: limited.id,
4253+
nodeId: limited.rootNodeId!,
4254+
providerThreadId: limited.providerThreadId,
4255+
providerTurnId: null,
4256+
nativeItemRef: null,
4257+
parentItemId: null,
4258+
ordinal: 2,
4259+
status: "failed",
4260+
title: "Usage limit reached",
4261+
startedAt: now,
4262+
completedAt: now,
4263+
updatedAt: now,
4264+
failure: {
4265+
class: "usage_limit",
4266+
message: "Usage limit reached",
4267+
code: "usage_limit",
4268+
retryable: null,
4269+
},
4270+
},
4271+
},
4272+
{
4273+
id: EventId.make(`${threadId}:failed`),
4274+
type: "run.updated",
4275+
threadId,
4276+
runId: limited.id,
4277+
providerInstanceId: limited.providerInstanceId,
4278+
occurredAt: now,
4279+
payload: { ...limited, status: "failed", completedAt: now },
4280+
},
4281+
],
4282+
});
4283+
4284+
yield* orchestrator.dispatch({
4285+
type: "message.dispatch",
4286+
commandId: CommandId.make(`${threadId}:wake`),
4287+
threadId,
4288+
messageId: MessageId.make(`${threadId}:wake`),
4289+
text: "Subagent finished",
4290+
attachments: [],
4291+
createdBy: "agent",
4292+
creationSource: "provider",
4293+
notification: {
4294+
source: { kind: "background_task" },
4295+
outcome: "completed",
4296+
summary: "Subagent finished",
4297+
},
4298+
dispatchMode: { type: "queue_after_active" },
4299+
});
4300+
const { runs } = yield* orchestrator.getThreadProjection(threadId);
4301+
const wake = runs.find((run) => run.userMessageId === `${threadId}:wake`);
4302+
assert.equal(wake?.status, "queued");
4303+
}),
4304+
);
4305+
42054306
it.effect.each([
42064307
["server", modelSelection],
42074308
["provider", modelSelection],

0 commit comments

Comments
 (0)