Skip to content

Commit e038235

Browse files
fix(server): resuming or sending a queued message now is not blocked by another provider's limit hold
The per-provider reset hold also guarded queue.resume and queued-run.start, so once another provider had run, the user's explicit resume was refused while the client showed no limit to act on. The reset hold now applies only to automatic queue starts; explicit commands keep main's latest-run check. AI-assisted; verified with the tests and tools listed above.
1 parent 03bda9c commit e038235

2 files changed

Lines changed: 35 additions & 29 deletions

File tree

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

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -871,14 +871,14 @@ function queueTailModelSelection(
871871
}
872872

873873
/**
874-
* The provider's latest run stopped on its usage limit, and either nothing
875-
* ran after it or it reported a reset still in the future. A run on another
876-
* provider since lifts a limit whose reset is unknown, as transient 429s are.
874+
* The provider's latest run stopped on its usage limit and nothing ran after it.
875+
* With `now` (automatic starts), a run on another provider since does not lift
876+
* a reported reset still in the future; an unknown reset is lifted, as transient 429s are.
877877
*/
878878
function usageLimitHoldsQueue(
879879
projection: Pick<OrchestrationV2ThreadProjection, "runs" | "turnItems" | "providerSessions">,
880880
providerInstanceId: ModelSelection["instanceId"],
881-
now: DateTime.Utc,
881+
now?: DateTime.Utc,
882882
): boolean {
883883
let latestSession: OrchestrationV2ThreadProjection["providerSessions"][number] | undefined;
884884
for (const session of projection.providerSessions) {
@@ -897,6 +897,7 @@ function usageLimitHoldsQueue(
897897
);
898898
if (limited === null) return false;
899899
if (latestExecutedRun(projection.runs)?.id === limited.id) return true;
900+
if (now === undefined) return false;
900901
const resetAt = latestRootProviderFailure(limited, projection.turnItems)?.resetAt;
901902
return resetAt != null && Date.parse(resetAt) > DateTime.toEpochMillis(now);
902903
}
@@ -1902,6 +1903,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
19021903
readonly failedRunId?: RunId;
19031904
readonly endedRunId?: RunId;
19041905
readonly queuedRunId?: RunId;
1906+
readonly userStarted?: boolean;
19051907
},
19061908
) {
19071909
if (!(yield* projectionStore.canStartQueuedRun(threadId))) return;
@@ -1933,7 +1935,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
19331935
providerInstanceId: modelSelection.instanceId,
19341936
};
19351937
// A limited provider would fail and consume every waiting message.
1936-
if (usageLimitHoldsQueue(projection, queuedRun.providerInstanceId, yield* DateTime.now)) {
1938+
if (
1939+
usageLimitHoldsQueue(
1940+
projection,
1941+
queuedRun.providerInstanceId,
1942+
options?.userStarted === true ? undefined : yield* DateTime.now,
1943+
)
1944+
) {
19371945
return;
19381946
}
19391947
// Waits without a hold: lowering the thread's mode starts the queue again.
@@ -13801,14 +13809,12 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1380113809
}
1380213810
}
1380313811
const nextRun = selectedRun ?? queuedRunsInDeliveryOrder(projection)[0];
13804-
const now = yield* DateTime.now;
1380513812
if (
1380613813
usageLimitHoldsQueue(
1380713814
projection,
1380813815
nextRun === undefined
1380913816
? projection.thread.providerInstanceId
1381013817
: queuedRunModelSelection(projection, nextRun).instanceId,
13811-
now,
1381213818
)
1381313819
) {
1381413820
return yield* new OrchestratorDispatchError({
@@ -13817,6 +13823,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1381713823
cause: "Continue the limited thread before resuming its queue.",
1381813824
});
1381913825
}
13826+
const now = yield* DateTime.now;
1382013827
const queued = projection.runs.filter((run) => run.status === "queued");
1382113828
for (const run of queued) {
1382213829
yield* emit(
@@ -13954,10 +13961,10 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1395413961
);
1395513962
if (command.type === "queue.resume" || command.type === "queued-run.start") {
1395613963
yield* mapDispatchError(command)(
13957-
startNextQueuedRun(
13958-
command.threadId,
13959-
command.type === "queued-run.start" ? { queuedRunId: command.runId } : undefined,
13960-
),
13964+
startNextQueuedRun(command.threadId, {
13965+
userStarted: true,
13966+
...(command.type === "queued-run.start" ? { queuedRunId: command.runId } : {}),
13967+
}),
1396113968
);
1396213969
}
1396313970
return {
@@ -14174,10 +14181,10 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1417414181
(command.type === "thread.runtime-mode.set" && runtimeModeCeiling !== undefined)
1417514182
) {
1417614183
yield* mapDispatchError(command)(
14177-
startNextQueuedRun(
14178-
command.threadId,
14179-
command.type === "queued-run.start" ? { queuedRunId: command.runId } : undefined,
14180-
),
14184+
startNextQueuedRun(command.threadId, {
14185+
userStarted: command.type !== "thread.runtime-mode.set",
14186+
...(command.type === "queued-run.start" ? { queuedRunId: command.runId } : {}),
14187+
}),
1418114188
);
1418214189
}
1418314190
if (

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5951,13 +5951,13 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
59515951
}),
59525952
);
59535953

5954-
it.effect(
5955-
"holds a limited provider's queue through another provider's turn until its reset passes",
5956-
() =>
5954+
it.effect.each(["the reset passes", "the user resumes"] as const)(
5955+
"holds a limited provider's queue through another provider's turn until %s",
5956+
(release) =>
59575957
Effect.gen(function* () {
59585958
const orchestrator = yield* Orchestrator.OrchestratorV2;
59595959
const eventSink = yield* EventSink.EventSinkV2;
5960-
const threadId = ThreadId.make("runtime-layer-queue-limit-switch");
5960+
const threadId = ThreadId.make(`runtime-layer-queue-limit-switch-${release}`);
59615961
const switchedSelection = { instanceId: alternateInstanceId, model: "gpt-5.5" };
59625962
yield* orchestrator.dispatch({
59635963
type: "thread.create",
@@ -6067,18 +6067,17 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
60676067
yield* orchestrator.resumeQueuedRuns;
60686068
const held = yield* orchestrator.getThreadProjection(threadId);
60696069
assert.equal(held.runs.find((run) => run.id === queued.id)?.status, "queued");
6070-
const blocked = yield* orchestrator
6071-
.dispatch({
6070+
6071+
if (release === "the reset passes") {
6072+
yield* TestClock.adjust("61 minutes");
6073+
yield* orchestrator.resumeQueuedRuns;
6074+
} else {
6075+
yield* orchestrator.dispatch({
60726076
type: "queue.resume",
6073-
commandId: CommandId.make(`${threadId}:blocked`),
6077+
commandId: CommandId.make(`${threadId}:resume`),
60746078
threadId,
6075-
})
6076-
.pipe(Effect.flip);
6077-
assert.equal(blocked._tag, "OrchestratorDispatchError");
6078-
6079-
// Once the reported reset passes, the queued message starts on its own provider.
6080-
yield* TestClock.adjust("61 minutes");
6081-
yield* orchestrator.resumeQueuedRuns;
6079+
});
6080+
}
60826081
const reset = yield* orchestrator.getThreadProjection(threadId);
60836082
const resumed = reset.runs.find((run) => run.id === queued.id);
60846083
assert.equal(resumed?.status, "starting");

0 commit comments

Comments
 (0)