@@ -1744,40 +1744,39 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
17441744 ]);
17451745 });
17461746
1747- const startNextQueuedRun = Effect.fn("Orchestrator.startNextQueuedRun")(
1748- function* (
1749- threadId: ThreadId,
1750- options?: {
1751- readonly failedRunId?: RunId;
1752- readonly endedRunId?: RunId;
1753- readonly queuedRunId?: RunId;
1754- },
1747+ const startNextQueuedRun = Effect.fn("Orchestrator.startNextQueuedRun")(function* (
1748+ threadId: ThreadId,
1749+ options?: {
1750+ readonly failedRunId?: RunId;
1751+ readonly endedRunId?: RunId;
1752+ readonly queuedRunId?: RunId;
1753+ },
1754+ ) {
1755+ if (!(yield* projectionStore.canStartQueuedRun(threadId))) return;
1756+ const projection = yield* readCommandProjection(threadId);
1757+ if (
1758+ projection.thread.archivedAt !== null ||
1759+ projection.thread.deletedAt !== null ||
1760+ projection.runs.some(isActiveRun)
17551761 ) {
1756- if (!(yield* projectionStore.canStartQueuedRun(threadId))) return;
1757- const projection = yield* readCommandProjection(threadId);
1758- if (
1759- projection.thread.archivedAt !== null ||
1760- projection.thread.deletedAt !== null ||
1761- projection.runs.some(isActiveRun)
1762- ) {
1763- return;
1764- }
1762+ return;
1763+ }
17651764
1766- const selectedQueuedRun =
1767- options?.queuedRunId === undefined
1768- ? undefined
1769- : projection.runs.find(
1770- (run) => run.id === options.queuedRunId && run.status === "queued",
1771- );
1772- const queuedCandidates =
1773- selectedQueuedRun === undefined
1774- ? queuedRunsInDeliveryOrder( projection).filter((run) => run.queueHeld !== true)
1775- : [selectedQueuedRun];
1776- const selected = yield* selectStartableQueuedRun(threadId, projection, queuedCandidates) ;
1777- if (selected === undefined) {
1778- return ;
1779- }
1780- const { storedQueuedRun, modelSelection } = selected;
1765+ const selectedQueuedRun =
1766+ options?.queuedRunId === undefined
1767+ ? undefined
1768+ : projection.runs.find((run) => run.id === options.queuedRunId && run.status === "queued");
1769+ const queuedCandidates =
1770+ selectedQueuedRun === undefined
1771+ ? queuedRunsInDeliveryOrder(projection).filter((run) => run.queueHeld !== true)
1772+ : [selectedQueuedRun];
1773+ const selected = yield* selectStartableQueuedRun(threadId, projection, queuedCandidates);
1774+ if (selected === undefined) {
1775+ return ;
1776+ }
1777+ const { storedQueuedRun, modelSelection } = selected ;
1778+ // Capture the selected id for failure attribution; the gate may clear before catch runs.
1779+ yield* Effect.gen(function* () {
17811780 const queuedMessage = projection.messages.find(
17821781 (candidate) => candidate.id === storedQueuedRun.userMessageId,
17831782 );
@@ -2530,12 +2529,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
25302529 },
25312530 ],
25322531 );
2533- },
2534- (effect, threadId, options?) =>
2535- effect.pipe(
2536- Effect.catch((cause) => failQueuedRunStart(threadId, cause, options?.queuedRunId)),
2537- ),
2538- );
2532+ }).pipe(Effect.catch((cause) => failQueuedRunStart(threadId, cause, storedQueuedRun.id)));
2533+ });
25392534
25402535 // Under load a resumed provider can time out before taking the prompt; the restart still owes the turn.
25412536 const retryContinuationStart = Effect.fn("Orchestrator.retryContinuationStart")(function* (
0 commit comments