Skip to content

Commit 5335e95

Browse files
committed
fix(server): a limited provider's queue stays held after another provider's turn succeeds
The usage-limit hold on queued messages read the thread's latest executed run. A successful turn on another provider became that run and released the hold, so queued messages for the limited provider started, failed on its limit, and were consumed. The hold now reads only the queued message's own provider runs. Fixes #21705
1 parent 758d773 commit 5335e95

2 files changed

Lines changed: 128 additions & 2 deletions

File tree

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -688,9 +688,14 @@ function usageLimitHoldsQueue(
688688
latestSession = session;
689689
}
690690
}
691+
// Only this provider's runs say whether its limit still applies. A later turn
692+
// on another provider does not clear it.
691693
return (
692-
usageLimitBlockedRun(projection.runs, projection.turnItems, latestSession?.lastError ?? null)
693-
?.providerInstanceId === providerInstanceId
694+
usageLimitBlockedRun(
695+
projection.runs.filter((run) => run.providerInstanceId === providerInstanceId),
696+
projection.turnItems,
697+
latestSession?.lastError ?? null,
698+
) !== null
694699
);
695700
}
696701

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

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5069,6 +5069,127 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
50695069
}),
50705070
);
50715071

5072+
it.effect("keeps a limited provider's queue held after another provider completes a turn", () =>
5073+
Effect.gen(function* () {
5074+
const orchestrator = yield* Orchestrator.OrchestratorV2;
5075+
const eventSink = yield* EventSink.EventSinkV2;
5076+
const threadId = ThreadId.make("runtime-layer-queue-limit-other-provider");
5077+
const otherSelection = { instanceId: alternateInstanceId, model: "gpt-5.5" };
5078+
yield* orchestrator.dispatch({
5079+
type: "thread.create",
5080+
commandId: CommandId.make(`${threadId}:create`),
5081+
threadId,
5082+
createdBy: "user",
5083+
creationSource: "web",
5084+
projectId: ProjectId.make(`${threadId}:project`),
5085+
title: "Limited queue",
5086+
modelSelection,
5087+
runtimeMode: "full-access",
5088+
interactionMode: "default",
5089+
branch: null,
5090+
worktreePath: process.cwd(),
5091+
});
5092+
const send = (
5093+
index: number,
5094+
selection: ModelSelection,
5095+
type: "start_immediately" | "queue_after_active",
5096+
) =>
5097+
orchestrator.dispatch({
5098+
type: "message.dispatch",
5099+
commandId: CommandId.make(`${threadId}:${index}`),
5100+
threadId,
5101+
messageId: MessageId.make(`${threadId}:${index}`),
5102+
text: `Prompt ${index}`,
5103+
attachments: [],
5104+
createdBy: "user",
5105+
creationSource: "web",
5106+
modelSelection: selection,
5107+
dispatchMode: { type },
5108+
});
5109+
yield* send(0, modelSelection, "start_immediately");
5110+
yield* send(1, modelSelection, "queue_after_active");
5111+
const [limited, queued] = (yield* orchestrator.getThreadProjection(threadId)).runs;
5112+
assert.isDefined(limited);
5113+
assert.isDefined(queued);
5114+
const now = yield* DateTime.now;
5115+
yield* eventSink.write({
5116+
events: [
5117+
{
5118+
id: EventId.make(`${threadId}:error`),
5119+
type: "turn-item.updated",
5120+
threadId,
5121+
runId: limited.id,
5122+
nodeId: limited.rootNodeId!,
5123+
providerInstanceId: limited.providerInstanceId,
5124+
occurredAt: now,
5125+
payload: {
5126+
id: TurnItemId.make(`${threadId}:error`),
5127+
type: "error",
5128+
threadId,
5129+
runId: limited.id,
5130+
nodeId: limited.rootNodeId!,
5131+
providerThreadId: limited.providerThreadId,
5132+
providerTurnId: null,
5133+
nativeItemRef: null,
5134+
parentItemId: null,
5135+
ordinal: 2,
5136+
status: "failed",
5137+
title: "Usage limit",
5138+
startedAt: now,
5139+
completedAt: now,
5140+
updatedAt: now,
5141+
failure: {
5142+
class: "usage_limit",
5143+
message: "Usage limit reached",
5144+
code: "usage_limit",
5145+
retryable: null,
5146+
resetAt: DateTime.formatIso(DateTime.add(now, { days: 5 })),
5147+
},
5148+
},
5149+
},
5150+
{
5151+
id: EventId.make(`${threadId}:failed`),
5152+
type: "run.updated",
5153+
threadId,
5154+
runId: limited.id,
5155+
providerInstanceId: limited.providerInstanceId,
5156+
occurredAt: now,
5157+
payload: { ...limited, status: "failed", completedAt: now },
5158+
},
5159+
],
5160+
});
5161+
yield* send(2, otherSelection, "start_immediately");
5162+
const other = (yield* orchestrator.getThreadProjection(threadId)).runs.at(-1);
5163+
assert.equal(other?.providerInstanceId, alternateInstanceId);
5164+
yield* eventSink.write({
5165+
events: [
5166+
{
5167+
id: EventId.make(`${threadId}:other-completed`),
5168+
type: "run.updated",
5169+
threadId,
5170+
runId: other!.id,
5171+
providerInstanceId: alternateInstanceId,
5172+
occurredAt: now,
5173+
payload: { ...other!, status: "completed", completedAt: now },
5174+
},
5175+
],
5176+
});
5177+
5178+
yield* orchestrator.resumeQueuedRuns;
5179+
5180+
const after = yield* orchestrator.getThreadProjection(threadId);
5181+
assert.equal(after.runs.find((run) => run.id === queued.id)?.status, "queued");
5182+
const blocked = yield* orchestrator
5183+
.dispatch({
5184+
type: "queue.resume",
5185+
commandId: CommandId.make(`${threadId}:blocked`),
5186+
threadId,
5187+
})
5188+
.pipe(Effect.flip);
5189+
assert.equal(blocked._tag, "OrchestratorDispatchError");
5190+
}),
5191+
);
5192+
50725193
it.effect.each([
50735194
["server", modelSelection],
50745195
["provider", modelSelection],

0 commit comments

Comments
 (0)