@@ -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" :
@@ -1263,13 +1264,17 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
12631264 const failQueuedRunStart = Effect . fn ( "Orchestrator.failQueuedRunStart" ) ( function * (
12641265 threadId : ThreadId ,
12651266 cause : unknown ,
1267+ queuedRunId ?: RunId ,
12661268 ) {
12671269 const projection = yield * projectionStore . getThreadRecords (
12681270 threadId ,
12691271 [ "runs" , "nodes" , "attempts" , "providerThreads" , "turnItems" , "messages" ] ,
12701272 { turnItemTypes : [ ] , messageRoles : [ "user" ] } ,
12711273 ) ;
1272- const queuedRun = nextQueuedRun ( projection ) ;
1274+ const queuedRun =
1275+ queuedRunId === undefined
1276+ ? nextQueuedRun ( projection )
1277+ : projection . runs . find ( ( run ) => run . id === queuedRunId && run . status === "queued" ) ;
12731278 if ( queuedRun === undefined ) return ;
12741279 const now = yield * DateTime . now ;
12751280 const rootNode = projection . nodes . find ( ( node ) => node . id === queuedRun . rootNodeId ) ;
@@ -1370,7 +1375,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
13701375 const startNextQueuedRun = Effect . fn ( "Orchestrator.startNextQueuedRun" ) (
13711376 function * (
13721377 threadId : ThreadId ,
1373- options ?: { readonly failedRunId ?: RunId ; readonly endedRunId ?: RunId } ,
1378+ options ?: {
1379+ readonly failedRunId ?: RunId ;
1380+ readonly endedRunId ?: RunId ;
1381+ readonly queuedRunId ?: RunId ;
1382+ } ,
13741383 ) {
13751384 if ( ! ( yield * projectionStore . canStartQueuedRun ( threadId ) ) ) return ;
13761385 const projection = yield * readCommandProjection ( threadId ) ;
@@ -1383,7 +1392,12 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
13831392 return ;
13841393 }
13851394
1386- const storedQueuedRun = nextQueuedRun ( projection ) ;
1395+ const storedQueuedRun =
1396+ options ?. queuedRunId === undefined
1397+ ? nextQueuedRun ( projection )
1398+ : projection . runs . find (
1399+ ( run ) => run . id === options . queuedRunId && run . status === "queued" ,
1400+ ) ;
13871401 if ( storedQueuedRun === undefined ) {
13881402 return ;
13891403 }
@@ -2073,8 +2087,10 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
20732087 ] ,
20742088 ) ;
20752089 } ,
2076- ( effect , threadId , _options ?) =>
2077- effect . pipe ( Effect . catch ( ( cause ) => failQueuedRunStart ( threadId , cause ) ) ) ,
2090+ ( effect , threadId , options ?) =>
2091+ effect . pipe (
2092+ Effect . catch ( ( cause ) => failQueuedRunStart ( threadId , cause , options ?. queuedRunId ) ) ,
2093+ ) ,
20782094 ) ;
20792095
20802096 const resumeQueuedRuns = Effect . gen ( function * ( ) {
@@ -11371,6 +11387,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1137111387 case "queued-message.promote-to-steer" :
1137211388 yield * dispatchQueuedMessagePromoteToSteer ( command , events , effects ) ;
1137311389 break ;
11390+ case "queued-run.start" :
1137411391 case "queue.resume" : {
1137511392 const projection = yield * loadProjectionForCommand (
1137611393 command ,
@@ -11384,7 +11401,30 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1138411401 cause : `Thread ${ command . threadId } is not active.` ,
1138511402 } ) ;
1138611403 }
11387- const nextRun = nextQueuedRun ( projection ) ;
11404+ const selectedRunId = command . type === "queued-run.start" ? command . runId : undefined ;
11405+ const selectedRun =
11406+ selectedRunId === undefined
11407+ ? undefined
11408+ : projection . runs . find ( ( run ) => run . id === selectedRunId && run . status === "queued" ) ;
11409+ if ( selectedRunId !== undefined ) {
11410+ const selectedMessage = projection . messages . find (
11411+ ( message ) => message . id === selectedRun ?. userMessageId ,
11412+ ) ;
11413+ if (
11414+ selectedRun === undefined ||
11415+ selectedMessage === undefined ||
11416+ selectedMessage . delegatedCompletion !== undefined ||
11417+ selectedMessage . notification !== undefined ||
11418+ projection . runs . some ( isBlockingRun )
11419+ ) {
11420+ return yield * new OrchestratorDispatchError ( {
11421+ commandId : command . commandId ,
11422+ commandType : command . type ,
11423+ cause : "A queued user message can only be sent now while the thread is stopped." ,
11424+ } ) ;
11425+ }
11426+ }
11427+ const nextRun = selectedRun ?? nextQueuedRun ( projection ) ;
1138811428 if (
1138911429 usageLimitHoldsQueue (
1139011430 projection ,
@@ -11527,8 +11567,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1152711567 } ) ,
1152811568 ) ,
1152911569 ) ;
11530- if ( command . type === "queue.resume" ) {
11531- yield * mapDispatchError ( command ) ( startNextQueuedRun ( command . threadId ) ) ;
11570+ if ( command . type === "queue.resume" || command . type === "queued-run.start" ) {
11571+ yield * mapDispatchError ( command ) (
11572+ startNextQueuedRun (
11573+ command . threadId ,
11574+ command . type === "queued-run.start" ? { queuedRunId : command . runId } : undefined ,
11575+ ) ,
11576+ ) ;
1153211577 }
1153311578 return {
1153411579 sequence : receipt . resultSequence ,
@@ -11660,8 +11705,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1166011705 detail : committed . receipt . error ?? "Previously rejected." ,
1166111706 } ) ;
1166211707 }
11663- if ( command . type === "queue.resume" ) {
11664- yield * mapDispatchError ( command ) ( startNextQueuedRun ( command . threadId ) ) ;
11708+ if ( command . type === "queue.resume" || command . type === "queued-run.start" ) {
11709+ yield * mapDispatchError ( command ) (
11710+ startNextQueuedRun (
11711+ command . threadId ,
11712+ command . type === "queued-run.start" ? { queuedRunId : command . runId } : undefined ,
11713+ ) ,
11714+ ) ;
1166511715 }
1166611716 if ( command . type === "notification.delivery.accept" ) {
1166711717 yield * mapDispatchError ( command ) ( offerDelegatedCompletionDeliveries ( command . threadId ) ) ;
0 commit comments