@@ -617,6 +617,37 @@ function nextQueuedRun(
617617 return queuedRunsInDeliveryOrder ( projection ) [ 0 ] ;
618618}
619619
620+ function queuedRunModelSelection (
621+ projection : Pick < OrchestrationV2ThreadProjection , "thread" | "messages" > ,
622+ run : OrchestrationV2Run ,
623+ ) : ModelSelection {
624+ const message = projection . messages . find ( ( candidate ) => candidate . id === run . userMessageId ) ;
625+ // A PR update belongs to the thread's current agent. One queued before a
626+ // provider handoff must not switch the thread back to the old provider.
627+ return message ?. notification ?. source . kind === "monitor"
628+ ? projection . thread . modelSelection
629+ : run . modelSelection ;
630+ }
631+
632+ // The limit already stopped this provider. Starting the queue on it would send
633+ // every waiting message and drop it from the queue as each one fails.
634+ function usageLimitHoldsQueue (
635+ projection : Pick < OrchestrationV2ThreadProjection , "runs" | "turnItems" | "providerSessions" > ,
636+ providerInstanceId : ModelSelection [ "instanceId" ] ,
637+ ) : boolean {
638+ const sessionError =
639+ projection . providerSessions
640+ . filter ( ( session ) => session . providerInstanceId === providerInstanceId )
641+ . toSorted (
642+ ( left , right ) =>
643+ DateTime . toEpochMillis ( right . updatedAt ) - DateTime . toEpochMillis ( left . updatedAt ) ,
644+ ) [ 0 ] ?. lastError ?? null ;
645+ return (
646+ usageLimitBlockedRun ( projection . runs , projection . turnItems , sessionError )
647+ ?. providerInstanceId === providerInstanceId
648+ ) ;
649+ }
650+
620651function latestStableRun (
621652 projection : Pick < OrchestrationV2ThreadProjection , "runs" > ,
622653) : OrchestrationV2Run | null {
@@ -1302,31 +1333,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
13021333 const queuedMessage = projection . messages . find (
13031334 ( candidate ) => candidate . id === storedQueuedRun . userMessageId ,
13041335 ) ;
1305- // A PR update belongs to the thread's current agent. One queued before a
1306- // provider handoff must not switch the thread back to the old provider.
1307- const followsThreadSelection = queuedMessage ?. notification ?. source . kind === "monitor" ;
1308- const modelSelection = followsThreadSelection
1309- ? projection . thread . modelSelection
1310- : storedQueuedRun . modelSelection ;
1336+ const modelSelection = queuedRunModelSelection ( projection , storedQueuedRun ) ;
13111337 const queuedRun = {
13121338 ...storedQueuedRun ,
13131339 modelSelection,
13141340 providerInstanceId : modelSelection . instanceId ,
13151341 } ;
1316-
1317- // The limit already stopped this provider. Starting the queue would send
1318- // every waiting message and drop it from the queue as each one fails.
1319- const sessionError =
1320- projection . providerSessions
1321- . filter ( ( session ) => session . providerInstanceId === queuedRun . providerInstanceId )
1322- . toSorted (
1323- ( left , right ) =>
1324- DateTime . toEpochMillis ( right . updatedAt ) - DateTime . toEpochMillis ( left . updatedAt ) ,
1325- ) [ 0 ] ?. lastError ?? null ;
1326- if (
1327- usageLimitBlockedRun ( projection . runs , projection . turnItems , sessionError )
1328- ?. providerInstanceId === queuedRun . providerInstanceId
1329- ) {
1342+ if ( usageLimitHoldsQueue ( projection , queuedRun . providerInstanceId ) ) {
13301343 return ;
13311344 }
13321345 // A message queued behind a worktree that was cancelled or never created
@@ -11097,8 +11110,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1109711110 case "queue.resume" : {
1109811111 const projection = yield * loadProjectionForCommand (
1109911112 command ,
11100- [ "runs" , "turnItems" , "providerSessions" ] ,
11101- { turnItemTypes : [ "error" ] } ,
11113+ [ "runs" , "turnItems" , "providerSessions" , "messages" ] ,
11114+ { turnItemTypes : [ "error" ] , messageRoles : [ "user" ] } ,
1110211115 ) ;
1110311116 if ( projection . thread . archivedAt !== null || projection . thread . deletedAt !== null ) {
1110411117 return yield * new OrchestratorDispatchError ( {
@@ -11107,16 +11120,15 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1110711120 cause : `Thread ${ command . threadId } is not active.` ,
1110811121 } ) ;
1110911122 }
11110- const sessionError =
11111- projection . providerSessions
11112- . filter (
11113- ( session ) => session . providerInstanceId === projection . thread . providerInstanceId ,
11114- )
11115- . toSorted (
11116- ( left , right ) =>
11117- DateTime . toEpochMillis ( right . updatedAt ) - DateTime . toEpochMillis ( left . updatedAt ) ,
11118- ) [ 0 ] ?. lastError ?? null ;
11119- if ( usageLimitBlockedRun ( projection . runs , projection . turnItems , sessionError ) !== null ) {
11123+ const nextRun = nextQueuedRun ( projection ) ;
11124+ if (
11125+ usageLimitHoldsQueue (
11126+ projection ,
11127+ nextRun === undefined
11128+ ? projection . thread . providerInstanceId
11129+ : queuedRunModelSelection ( projection , nextRun ) . instanceId ,
11130+ )
11131+ ) {
1112011132 return yield * new OrchestratorDispatchError ( {
1112111133 commandId : command . commandId ,
1112211134 commandType : command . type ,
0 commit comments