@@ -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 */
878878function 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 (
0 commit comments