Skip to content

Commit 151d90b

Browse files
voltcrashclaude
andcommitted
fix(server): queue resume uses the same usage-limit rule as the drain path
After a provider switch, the drain path would start a queued PR update on the new provider, but queue.resume still refused whenever any provider was usage-limited. Both paths now resolve the next queued run's provider with one helper and only hold the queue when that provider is the limited one. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 0c21f52 commit 151d90b

2 files changed

Lines changed: 95 additions & 51 deletions

File tree

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

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,37 @@ function nextQueuedRun(
617617
return queuedRunsInDeliveryOrder(projection)[0];
618618
}
619619

620+
function queuedRunModelSelection(
621+
projection: Pick<OrchestrationV2ThreadProjection, "thread" | "messages">,
622+
run: OrchestrationV2Run,
623+
): ModelSelection {
624+
const message = projection.messages.find((candidate) => candidate.id === run.userMessageId);
625+
// A PR update belongs to the thread's current agent. One queued before a
626+
// provider handoff must not switch the thread back to the old provider.
627+
return message?.notification?.source.kind === "monitor"
628+
? projection.thread.modelSelection
629+
: run.modelSelection;
630+
}
631+
632+
// The limit already stopped this provider. Starting the queue on it would send
633+
// every waiting message and drop it from the queue as each one fails.
634+
function usageLimitHoldsQueue(
635+
projection: Pick<OrchestrationV2ThreadProjection, "runs" | "turnItems" | "providerSessions">,
636+
providerInstanceId: ModelSelection["instanceId"],
637+
): boolean {
638+
const sessionError =
639+
projection.providerSessions
640+
.filter((session) => session.providerInstanceId === providerInstanceId)
641+
.toSorted(
642+
(left, right) =>
643+
DateTime.toEpochMillis(right.updatedAt) - DateTime.toEpochMillis(left.updatedAt),
644+
)[0]?.lastError ?? null;
645+
return (
646+
usageLimitBlockedRun(projection.runs, projection.turnItems, sessionError)
647+
?.providerInstanceId === providerInstanceId
648+
);
649+
}
650+
620651
function latestStableRun(
621652
projection: Pick<OrchestrationV2ThreadProjection, "runs">,
622653
): OrchestrationV2Run | null {
@@ -1302,31 +1333,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
13021333
const queuedMessage = projection.messages.find(
13031334
(candidate) => candidate.id === storedQueuedRun.userMessageId,
13041335
);
1305-
// A PR update belongs to the thread's current agent. One queued before a
1306-
// provider handoff must not switch the thread back to the old provider.
1307-
const followsThreadSelection = queuedMessage?.notification?.source.kind === "monitor";
1308-
const modelSelection = followsThreadSelection
1309-
? projection.thread.modelSelection
1310-
: storedQueuedRun.modelSelection;
1336+
const modelSelection = queuedRunModelSelection(projection, storedQueuedRun);
13111337
const queuedRun = {
13121338
...storedQueuedRun,
13131339
modelSelection,
13141340
providerInstanceId: modelSelection.instanceId,
13151341
};
1316-
1317-
// The limit already stopped this provider. Starting the queue would send
1318-
// every waiting message and drop it from the queue as each one fails.
1319-
const sessionError =
1320-
projection.providerSessions
1321-
.filter((session) => session.providerInstanceId === queuedRun.providerInstanceId)
1322-
.toSorted(
1323-
(left, right) =>
1324-
DateTime.toEpochMillis(right.updatedAt) - DateTime.toEpochMillis(left.updatedAt),
1325-
)[0]?.lastError ?? null;
1326-
if (
1327-
usageLimitBlockedRun(projection.runs, projection.turnItems, sessionError)
1328-
?.providerInstanceId === queuedRun.providerInstanceId
1329-
) {
1342+
if (usageLimitHoldsQueue(projection, queuedRun.providerInstanceId)) {
13301343
return;
13311344
}
13321345
// A message queued behind a worktree that was cancelled or never created
@@ -11097,8 +11110,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1109711110
case "queue.resume": {
1109811111
const projection = yield* loadProjectionForCommand(
1109911112
command,
11100-
["runs", "turnItems", "providerSessions"],
11101-
{ turnItemTypes: ["error"] },
11113+
["runs", "turnItems", "providerSessions", "messages"],
11114+
{ turnItemTypes: ["error"], messageRoles: ["user"] },
1110211115
);
1110311116
if (projection.thread.archivedAt !== null || projection.thread.deletedAt !== null) {
1110411117
return yield* new OrchestratorDispatchError({
@@ -11107,16 +11120,15 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1110711120
cause: `Thread ${command.threadId} is not active.`,
1110811121
});
1110911122
}
11110-
const sessionError =
11111-
projection.providerSessions
11112-
.filter(
11113-
(session) => session.providerInstanceId === projection.thread.providerInstanceId,
11114-
)
11115-
.toSorted(
11116-
(left, right) =>
11117-
DateTime.toEpochMillis(right.updatedAt) - DateTime.toEpochMillis(left.updatedAt),
11118-
)[0]?.lastError ?? null;
11119-
if (usageLimitBlockedRun(projection.runs, projection.turnItems, sessionError) !== null) {
11123+
const nextRun = nextQueuedRun(projection);
11124+
if (
11125+
usageLimitHoldsQueue(
11126+
projection,
11127+
nextRun === undefined
11128+
? projection.thread.providerInstanceId
11129+
: queuedRunModelSelection(projection, nextRun).instanceId,
11130+
)
11131+
) {
1112011132
return yield* new OrchestratorDispatchError({
1112111133
commandId: command.commandId,
1112211134
commandType: command.type,

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

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4522,13 +4522,13 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
45224522
}),
45234523
);
45244524

4525-
it.effect(
4526-
"starts a queued prompt for another provider with its own model after a usage limit",
4527-
() =>
4525+
it.effect.each(["user prompt", "PR update"] as const)(
4526+
"starts a queued %s on another provider after a usage limit",
4527+
(kind) =>
45284528
Effect.gen(function* () {
45294529
const orchestrator = yield* Orchestrator.OrchestratorV2;
45304530
const eventSink = yield* EventSink.EventSinkV2;
4531-
const threadId = ThreadId.make("runtime-layer-limited-queue-other-provider");
4531+
const threadId = ThreadId.make(`runtime-layer-limited-queue-other-provider:${kind}`);
45324532
const queuedSelection = { instanceId: alternateInstanceId, model: "gpt-5.5" };
45334533
yield* orchestrator.dispatch({
45344534
type: "thread.create",
@@ -4544,20 +4544,38 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
45444544
branch: null,
45454545
worktreePath: process.cwd(),
45464546
});
4547-
for (const [index, selection] of [modelSelection, queuedSelection].entries()) {
4548-
yield* orchestrator.dispatch({
4549-
type: "message.dispatch",
4550-
createdBy: "user",
4551-
creationSource: "web",
4552-
commandId: CommandId.make(`${threadId}:message:${index}`),
4553-
threadId,
4554-
messageId: MessageId.make(`${threadId}:message:${index}`),
4555-
text: index === 0 ? "Active" : "Queued",
4556-
attachments: [],
4557-
modelSelection: selection,
4558-
dispatchMode: { type: index === 0 ? "start_immediately" : "queue_after_active" },
4559-
});
4560-
}
4547+
yield* orchestrator.dispatch({
4548+
type: "message.dispatch",
4549+
createdBy: "user",
4550+
creationSource: "web",
4551+
commandId: CommandId.make(`${threadId}:message:active`),
4552+
threadId,
4553+
messageId: MessageId.make(`${threadId}:message:active`),
4554+
text: "Active",
4555+
attachments: [],
4556+
modelSelection,
4557+
dispatchMode: { type: "start_immediately" },
4558+
});
4559+
yield* orchestrator.dispatch({
4560+
type: "message.dispatch",
4561+
commandId: CommandId.make(`${threadId}:message:queued`),
4562+
threadId,
4563+
messageId: MessageId.make(`${threadId}:message:queued`),
4564+
text: "Queued",
4565+
attachments: [],
4566+
dispatchMode: { type: "queue_after_active" },
4567+
...(kind === "user prompt"
4568+
? { modelSelection: queuedSelection, createdBy: "user", creationSource: "web" }
4569+
: {
4570+
notification: {
4571+
source: { kind: "monitor" },
4572+
outcome: "updated",
4573+
summary: "PR updated",
4574+
},
4575+
createdBy: "agent",
4576+
creationSource: "server",
4577+
}),
4578+
});
45614579
const [activeRun, queuedRun] = (yield* orchestrator.getThreadProjection(threadId)).runs;
45624580
assert.isDefined(activeRun?.rootNodeId);
45634581
assert.isDefined(queuedRun);
@@ -4610,7 +4628,21 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
46104628
},
46114629
],
46124630
});
4613-
yield* orchestrator.resumeQueuedRuns;
4631+
if (kind === "user prompt") {
4632+
yield* orchestrator.resumeQueuedRuns;
4633+
} else {
4634+
yield* orchestrator.dispatch({
4635+
type: "thread.model-selection.set",
4636+
commandId: CommandId.make(`${threadId}:select`),
4637+
threadId,
4638+
modelSelection: queuedSelection,
4639+
});
4640+
yield* orchestrator.dispatch({
4641+
type: "queue.resume",
4642+
commandId: CommandId.make(`${threadId}:resume`),
4643+
threadId,
4644+
});
4645+
}
46144646

46154647
const after = yield* orchestrator.getThreadProjection(threadId);
46164648
const started = after.runs.find((run) => run.id === queuedRun.id);

0 commit comments

Comments
 (0)