@@ -477,6 +477,7 @@ function commandThreadId(command: OrchestrationV2ServerCommand): ThreadId {
477477 case "subagent.stop" :
478478 case "run.interrupt" :
479479 case "queued-message.promote-to-steer" :
480+ case "queued-run.start" :
480481 case "queue.resume" :
481482 case "queued-run.reorder" :
482483 case "queued-run.cancel" :
@@ -1277,13 +1278,17 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
12771278 const failQueuedRunStart = Effect . fn ( "Orchestrator.failQueuedRunStart" ) ( function * (
12781279 threadId : ThreadId ,
12791280 cause : unknown ,
1281+ queuedRunId ?: RunId ,
12801282 ) {
12811283 const projection = yield * projectionStore . getThreadRecords (
12821284 threadId ,
12831285 [ "runs" , "nodes" , "attempts" , "providerThreads" , "turnItems" , "messages" ] ,
12841286 { turnItemTypes : [ ] , messageRoles : [ "user" ] } ,
12851287 ) ;
1286- const queuedRun = nextQueuedRun ( projection ) ;
1288+ const queuedRun =
1289+ queuedRunId === undefined
1290+ ? nextQueuedRun ( projection )
1291+ : projection . runs . find ( ( run ) => run . id === queuedRunId && run . status === "queued" ) ;
12871292 if ( queuedRun === undefined ) return ;
12881293 const now = yield * DateTime . now ;
12891294 const rootNode = projection . nodes . find ( ( node ) => node . id === queuedRun . rootNodeId ) ;
@@ -1384,7 +1389,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
13841389 const startNextQueuedRun = Effect . fn ( "Orchestrator.startNextQueuedRun" ) (
13851390 function * (
13861391 threadId : ThreadId ,
1387- options ?: { readonly failedRunId ?: RunId ; readonly endedRunId ?: RunId } ,
1392+ options ?: {
1393+ readonly failedRunId ?: RunId ;
1394+ readonly endedRunId ?: RunId ;
1395+ readonly queuedRunId ?: RunId ;
1396+ } ,
13881397 ) {
13891398 if ( ! ( yield * projectionStore . canStartQueuedRun ( threadId ) ) ) return ;
13901399 const projection = yield * readCommandProjection ( threadId ) ;
@@ -1397,7 +1406,12 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
13971406 return ;
13981407 }
13991408
1400- const storedQueuedRun = nextQueuedRun ( projection ) ;
1409+ const storedQueuedRun =
1410+ options ?. queuedRunId === undefined
1411+ ? nextQueuedRun ( projection )
1412+ : projection . runs . find (
1413+ ( run ) => run . id === options . queuedRunId && run . status === "queued" ,
1414+ ) ;
14011415 if ( storedQueuedRun === undefined ) {
14021416 return ;
14031417 }
@@ -2087,8 +2101,10 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
20872101 ] ,
20882102 ) ;
20892103 } ,
2090- ( effect , threadId , _options ?) =>
2091- effect . pipe ( Effect . catch ( ( cause ) => failQueuedRunStart ( threadId , cause ) ) ) ,
2104+ ( effect , threadId , options ?) =>
2105+ effect . pipe (
2106+ Effect . catch ( ( cause ) => failQueuedRunStart ( threadId , cause , options ?. queuedRunId ) ) ,
2107+ ) ,
20922108 ) ;
20932109
20942110 const resumeQueuedRuns = Effect . gen ( function * ( ) {
@@ -11423,6 +11439,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1142311439 case "queued-message.promote-to-steer" :
1142411440 yield * dispatchQueuedMessagePromoteToSteer ( command , events , effects ) ;
1142511441 break ;
11442+ case "queued-run.start" :
1142611443 case "queue.resume" : {
1142711444 const projection = yield * loadProjectionForCommand (
1142811445 command ,
@@ -11436,7 +11453,30 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1143611453 cause : `Thread ${ command . threadId } is not active.` ,
1143711454 } ) ;
1143811455 }
11439- const nextRun = nextQueuedRun ( projection ) ;
11456+ const selectedRunId = command . type === "queued-run.start" ? command . runId : undefined ;
11457+ const selectedRun =
11458+ selectedRunId === undefined
11459+ ? undefined
11460+ : projection . runs . find ( ( run ) => run . id === selectedRunId && run . status === "queued" ) ;
11461+ if ( selectedRunId !== undefined ) {
11462+ const selectedMessage = projection . messages . find (
11463+ ( message ) => message . id === selectedRun ?. userMessageId ,
11464+ ) ;
11465+ if (
11466+ selectedRun === undefined ||
11467+ selectedMessage === undefined ||
11468+ selectedMessage . delegatedCompletion !== undefined ||
11469+ selectedMessage . notification !== undefined ||
11470+ projection . runs . some ( isBlockingRun )
11471+ ) {
11472+ return yield * new OrchestratorDispatchError ( {
11473+ commandId : command . commandId ,
11474+ commandType : command . type ,
11475+ cause : "A queued user message can only be sent now while the thread is stopped." ,
11476+ } ) ;
11477+ }
11478+ }
11479+ const nextRun = selectedRun ?? nextQueuedRun ( projection ) ;
1144011480 if (
1144111481 usageLimitHoldsQueue (
1144211482 projection ,
@@ -11579,8 +11619,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1157911619 } ) ,
1158011620 ) ,
1158111621 ) ;
11582- if ( command . type === "queue.resume" ) {
11583- yield * mapDispatchError ( command ) ( startNextQueuedRun ( command . threadId ) ) ;
11622+ if ( command . type === "queue.resume" || command . type === "queued-run.start" ) {
11623+ yield * mapDispatchError ( command ) (
11624+ startNextQueuedRun (
11625+ command . threadId ,
11626+ command . type === "queued-run.start" ? { queuedRunId : command . runId } : undefined ,
11627+ ) ,
11628+ ) ;
1158411629 }
1158511630 return {
1158611631 sequence : receipt . resultSequence ,
@@ -11712,8 +11757,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1171211757 detail : committed . receipt . error ?? "Previously rejected." ,
1171311758 } ) ;
1171411759 }
11715- if ( command . type === "queue.resume" ) {
11716- yield * mapDispatchError ( command ) ( startNextQueuedRun ( command . threadId ) ) ;
11760+ if ( command . type === "queue.resume" || command . type === "queued-run.start" ) {
11761+ yield * mapDispatchError ( command ) (
11762+ startNextQueuedRun (
11763+ command . threadId ,
11764+ command . type === "queued-run.start" ? { queuedRunId : command . runId } : undefined ,
11765+ ) ,
11766+ ) ;
1171711767 }
1171811768 if ( command . type === "notification.delivery.accept" ) {
1171911769 yield * mapDispatchError ( command ) ( offerDelegatedCompletionDeliveries ( command . threadId ) ) ;
0 commit comments