Skip to content

Commit 50879cc

Browse files
fix(server): messages queued during a usage limit wait for the reset
1 parent dd14848 commit 50879cc

3 files changed

Lines changed: 104 additions & 3 deletions

File tree

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5531,7 +5531,16 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
55315531
projection.thread.worktreePath === null
55325532
? latestRun
55335533
: undefined;
5534-
const queueBehindRun = activeRun ?? worktreeMissingRun;
5534+
// A message queued while a usage limit blocks its provider waits for the
5535+
// continuation, like messages queued before the limit, instead of failing on it.
5536+
const usageLimitedRun =
5537+
activeRun === undefined &&
5538+
dispatchMode.type === "queue_after_active" &&
5539+
latestRun !== null &&
5540+
usageLimitHoldsQueue(projection, modelSelection.instanceId)
5541+
? latestRun
5542+
: undefined;
5543+
const queueBehindRun = activeRun ?? worktreeMissingRun ?? usageLimitedRun;
55355544
const pendingMergeBackTransfers = pendingMergeBackTransfersForThread(projection);
55365545
const startsOrQueuesRun =
55375546
dispatchMode.type === "defer_start" ||

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

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4932,6 +4932,97 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
49324932
}),
49334933
);
49344934

4935+
it.effect("queues a message sent with Queue while a usage limit blocks the thread", () =>
4936+
Effect.gen(function* () {
4937+
const orchestrator = yield* Orchestrator.OrchestratorV2;
4938+
const eventSink = yield* EventSink.EventSinkV2;
4939+
const threadId = ThreadId.make("runtime-layer-queue-after-limit");
4940+
yield* orchestrator.dispatch({
4941+
type: "thread.create",
4942+
commandId: CommandId.make(`${threadId}:create`),
4943+
threadId,
4944+
createdBy: "user",
4945+
creationSource: "web",
4946+
projectId: ProjectId.make(`${threadId}:project`),
4947+
title: "Queue after limit",
4948+
modelSelection,
4949+
runtimeMode: "full-access",
4950+
interactionMode: "default",
4951+
branch: null,
4952+
worktreePath: process.cwd(),
4953+
});
4954+
const send = (index: number, type: "start_immediately" | "queue_after_active") =>
4955+
orchestrator.dispatch({
4956+
type: "message.dispatch",
4957+
commandId: CommandId.make(`${threadId}:${index}`),
4958+
threadId,
4959+
messageId: MessageId.make(`${threadId}:${index}`),
4960+
text: `Prompt ${index}`,
4961+
attachments: [],
4962+
createdBy: "user",
4963+
creationSource: "web",
4964+
dispatchMode: { type },
4965+
});
4966+
yield* send(0, "start_immediately");
4967+
const [limited] = (yield* orchestrator.getThreadProjection(threadId)).runs;
4968+
assert.isDefined(limited);
4969+
const now = yield* DateTime.now;
4970+
yield* eventSink.write({
4971+
events: [
4972+
{
4973+
id: EventId.make(`${threadId}:error`),
4974+
type: "turn-item.updated",
4975+
threadId,
4976+
runId: limited.id,
4977+
nodeId: limited.rootNodeId!,
4978+
providerInstanceId: limited.providerInstanceId,
4979+
occurredAt: now,
4980+
payload: {
4981+
id: TurnItemId.make(`${threadId}:error`),
4982+
type: "error",
4983+
threadId,
4984+
runId: limited.id,
4985+
nodeId: limited.rootNodeId!,
4986+
providerThreadId: limited.providerThreadId,
4987+
providerTurnId: null,
4988+
nativeItemRef: null,
4989+
parentItemId: null,
4990+
ordinal: 2,
4991+
status: "failed",
4992+
title: "Usage limit",
4993+
startedAt: now,
4994+
completedAt: now,
4995+
updatedAt: now,
4996+
failure: {
4997+
class: "usage_limit",
4998+
message: "Usage limit reached",
4999+
code: "usage_limit",
5000+
retryable: null,
5001+
},
5002+
},
5003+
},
5004+
{
5005+
id: EventId.make(`${threadId}:failed`),
5006+
type: "run.updated",
5007+
threadId,
5008+
runId: limited.id,
5009+
providerInstanceId: limited.providerInstanceId,
5010+
occurredAt: now,
5011+
payload: { ...limited, status: "failed", completedAt: now },
5012+
},
5013+
],
5014+
});
5015+
5016+
yield* send(1, "queue_after_active");
5017+
yield* orchestrator.resumeQueuedRuns;
5018+
const queued = (yield* orchestrator.getThreadProjection(threadId)).runs.find(
5019+
(run) => run.userMessageId === MessageId.make(`${threadId}:1`),
5020+
);
5021+
assert.equal(queued?.status, "queued");
5022+
assert.notEqual(queued?.queueHeld, true);
5023+
}),
5024+
);
5025+
49355026
it.effect.each([
49365027
["server", modelSelection],
49375028
["provider", modelSelection],

‎docs/user/thread-sidebar.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,9 @@ keeps the provider's explanation. Retry after the limit resets, or switch to
239239
another provider instance.
240240
On web and desktop, press **Resume** in an empty composer to continue a limited
241241
or interrupted turn manually.
242-
Queued messages stay saved while the limit blocks the thread. They run after
243-
the continuation finishes. If the queue was held by a restart, resume it then.
242+
Queued messages stay saved while the limit blocks the thread, including messages
243+
you queue after it stops. They run after the continuation finishes. If the queue
244+
was held by a restart, resume it then.
244245

245246
When the provider reports a reset time, choose **Resume at reset** to schedule a
246247
continuation. You can cancel it from the thread. Enable **Resume after

0 commit comments

Comments
 (0)