Skip to content

Commit f01a444

Browse files
mkantautasMarve10s
authored andcommitted
fix(server): a limited provider's queue stays held through another provider's turn until its reset
A usage-limit failure held queued work only while it was the thread's latest run, so a turn that completed on another provider started the limited provider's queued messages against its limit. The hold now reads the queued provider's own latest run. It still holds when that run is the thread's latest, and otherwise holds only while the reported reset time is ahead, so a limit that has reset no longer pins messages queued for it.
1 parent 5070d6f commit f01a444

2 files changed

Lines changed: 51 additions & 32 deletions

File tree

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -783,9 +783,15 @@ function queuedRunModelSelection(
783783
: run.modelSelection;
784784
}
785785

786+
/**
787+
* The provider's latest run stopped on its usage limit, and either nothing
788+
* ran after it or the limit has not reset yet. A run that completed on
789+
* another provider since does not lift the limit.
790+
*/
786791
function usageLimitHoldsQueue(
787792
projection: Pick<OrchestrationV2ThreadProjection, "runs" | "turnItems" | "providerSessions">,
788793
providerInstanceId: ModelSelection["instanceId"],
794+
now: DateTime.Utc,
789795
): boolean {
790796
let latestSession: OrchestrationV2ThreadProjection["providerSessions"][number] | undefined;
791797
for (const session of projection.providerSessions) {
@@ -797,15 +803,15 @@ function usageLimitHoldsQueue(
797803
latestSession = session;
798804
}
799805
}
800-
// Only this provider's runs say whether its limit still applies. A later turn
801-
// on another provider does not clear it.
802-
return (
803-
usageLimitBlockedRun(
804-
projection.runs.filter((run) => run.providerInstanceId === providerInstanceId),
805-
projection.turnItems,
806-
latestSession?.lastError ?? null,
807-
) !== null
806+
const limited = usageLimitBlockedRun(
807+
projection.runs.filter((run) => run.providerInstanceId === providerInstanceId),
808+
projection.turnItems,
809+
latestSession?.lastError ?? null,
808810
);
811+
if (limited === null) return false;
812+
if (latestExecutedRun(projection.runs)?.id === limited.id) return true;
813+
const resetAt = latestRootProviderFailure(limited, projection.turnItems)?.resetAt;
814+
return resetAt != null && Date.parse(resetAt) > DateTime.toEpochMillis(now);
809815
}
810816

811817
function latestStableRun(
@@ -1536,7 +1542,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
15361542
providerInstanceId: modelSelection.instanceId,
15371543
};
15381544
// A limited provider would fail and consume every waiting message.
1539-
if (usageLimitHoldsQueue(projection, queuedRun.providerInstanceId)) {
1545+
if (usageLimitHoldsQueue(projection, queuedRun.providerInstanceId, yield* DateTime.now)) {
15401546
return;
15411547
}
15421548
// Failed worktree creation would run queued work in the project checkout; wait for the user.
@@ -12897,12 +12903,14 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1289712903
}
1289812904
}
1289912905
const nextRun = selectedRun ?? queuedRunsInDeliveryOrder(projection)[0];
12906+
const now = yield* DateTime.now;
1290012907
if (
1290112908
usageLimitHoldsQueue(
1290212909
projection,
1290312910
nextRun === undefined
1290412911
? projection.thread.providerInstanceId
1290512912
: queuedRunModelSelection(projection, nextRun).instanceId,
12913+
now,
1290612914
)
1290712915
) {
1290812916
return yield* new OrchestratorDispatchError({
@@ -12911,7 +12919,6 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1291112919
cause: "Continue the limited thread before resuming its queue.",
1291212920
});
1291312921
}
12914-
const now = yield* DateTime.now;
1291512922
const queued = projection.runs.filter((run) => run.status === "queued");
1291612923
for (const run of queued) {
1291712924
yield* emit(

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

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5191,45 +5191,45 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
51915191
}),
51925192
);
51935193

5194-
it.effect("keeps a limited provider's queue held after another provider completes a turn", () =>
5194+
it.effect("holds a limited provider's queue through another provider's turn until reset", () =>
51955195
Effect.gen(function* () {
51965196
const orchestrator = yield* Orchestrator.OrchestratorV2;
51975197
const eventSink = yield* EventSink.EventSinkV2;
5198-
const threadId = ThreadId.make("runtime-layer-queue-limit-other-provider");
5199-
const otherSelection = { instanceId: alternateInstanceId, model: "gpt-5.5" };
5198+
const threadId = ThreadId.make("runtime-layer-queue-limit-switch");
5199+
const switchedSelection = { instanceId: alternateInstanceId, model: "gpt-5.5" };
52005200
yield* orchestrator.dispatch({
52015201
type: "thread.create",
52025202
commandId: CommandId.make(`${threadId}:create`),
52035203
threadId,
52045204
createdBy: "user",
52055205
creationSource: "web",
52065206
projectId: ProjectId.make(`${threadId}:project`),
5207-
title: "Limited queue",
5207+
title: "Limit switch",
52085208
modelSelection,
52095209
runtimeMode: "full-access",
52105210
interactionMode: "default",
52115211
branch: null,
52125212
worktreePath: process.cwd(),
52135213
});
52145214
const send = (
5215-
index: number,
5216-
selection: ModelSelection,
5215+
name: string,
5216+
selection: typeof modelSelection | typeof switchedSelection,
52175217
type: "start_immediately" | "queue_after_active",
52185218
) =>
52195219
orchestrator.dispatch({
52205220
type: "message.dispatch",
5221-
commandId: CommandId.make(`${threadId}:${index}`),
5221+
commandId: CommandId.make(`${threadId}:${name}`),
52225222
threadId,
5223-
messageId: MessageId.make(`${threadId}:${index}`),
5224-
text: `Prompt ${index}`,
5223+
messageId: MessageId.make(`${threadId}:${name}`),
5224+
text: name,
52255225
attachments: [],
52265226
createdBy: "user",
52275227
creationSource: "web",
52285228
modelSelection: selection,
52295229
dispatchMode: { type },
52305230
});
5231-
yield* send(0, modelSelection, "start_immediately");
5232-
yield* send(1, modelSelection, "queue_after_active");
5231+
yield* send("limited", modelSelection, "start_immediately");
5232+
yield* send("queued", modelSelection, "queue_after_active");
52335233
const [limited, queued] = (yield* orchestrator.getThreadProjection(threadId)).runs;
52345234
assert.isDefined(limited);
52355235
assert.isDefined(queued);
@@ -5265,7 +5265,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
52655265
message: "Usage limit reached",
52665266
code: "usage_limit",
52675267
retryable: null,
5268-
resetAt: DateTime.formatIso(DateTime.add(now, { days: 5 })),
5268+
resetAt: DateTime.formatIso(DateTime.add(now, { hours: 1 })),
52695269
},
52705270
},
52715271
},
@@ -5280,27 +5280,31 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
52805280
},
52815281
],
52825282
});
5283-
yield* send(2, otherSelection, "start_immediately");
5284-
const other = (yield* orchestrator.getThreadProjection(threadId)).runs.at(-1);
5285-
assert.equal(other?.providerInstanceId, alternateInstanceId);
5283+
5284+
// A message on another provider starts ahead of the held queue and
5285+
// completes there.
5286+
yield* send("switch", switchedSelection, "start_immediately");
5287+
const switched = (yield* orchestrator.getThreadProjection(threadId)).runs.find(
5288+
(run) => run.userMessageId === MessageId.make(`${threadId}:switch`),
5289+
);
5290+
assert.equal(switched?.status, "starting");
52865291
yield* eventSink.write({
52875292
events: [
52885293
{
5289-
id: EventId.make(`${threadId}:other-completed`),
5294+
id: EventId.make(`${threadId}:switch:completed`),
52905295
type: "run.updated",
52915296
threadId,
5292-
runId: other!.id,
5293-
providerInstanceId: alternateInstanceId,
5297+
runId: switched!.id,
5298+
providerInstanceId: switched!.providerInstanceId,
52945299
occurredAt: now,
5295-
payload: { ...other!, status: "completed", completedAt: now },
5300+
payload: { ...switched!, status: "completed", completedAt: now },
52965301
},
52975302
],
52985303
});
52995304

53005305
yield* orchestrator.resumeQueuedRuns;
5301-
5302-
const after = yield* orchestrator.getThreadProjection(threadId);
5303-
assert.equal(after.runs.find((run) => run.id === queued.id)?.status, "queued");
5306+
const held = yield* orchestrator.getThreadProjection(threadId);
5307+
assert.equal(held.runs.find((run) => run.id === queued.id)?.status, "queued");
53045308
const blocked = yield* orchestrator
53055309
.dispatch({
53065310
type: "queue.resume",
@@ -5309,6 +5313,14 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
53095313
})
53105314
.pipe(Effect.flip);
53115315
assert.equal(blocked._tag, "OrchestratorDispatchError");
5316+
5317+
// Once the reported reset passes, the queued message starts on its own provider.
5318+
yield* TestClock.adjust("61 minutes");
5319+
yield* orchestrator.resumeQueuedRuns;
5320+
const reset = yield* orchestrator.getThreadProjection(threadId);
5321+
const resumed = reset.runs.find((run) => run.id === queued.id);
5322+
assert.equal(resumed?.status, "starting");
5323+
assert.deepEqual(resumed?.modelSelection, modelSelection);
53125324
}),
53135325
);
53145326

0 commit comments

Comments
 (0)