@@ -97,6 +97,7 @@ import {
9797 goalCommandStopsActiveRun ,
9898 isNativeMaintenanceCommand ,
9999 resolveMessageDispatchIntent ,
100+ runAwaitsProviderTurn ,
100101} from "./CommandPolicy.ts" ;
101102import { CommandReceiptStoreV2 } from "./CommandReceiptStore.ts" ;
102103import { ContextHandoffServiceV2 } from "./ContextHandoffService.ts" ;
@@ -5496,6 +5497,20 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
54965497 payload : { ...held , queueHeld : false } ,
54975498 } ) ;
54985499 }
5500+ const activeRunMessage = projection . messages . find (
5501+ ( message ) => message . id === activeRun ?. userMessageId ,
5502+ ) ;
5503+ const startupDeliveryIntent =
5504+ activeRun !== undefined &&
5505+ ( command . deliveryIntent === "auto" || command . deliveryIntent === "steer" ) &&
5506+ goalCommand === null &&
5507+ delegatedCompletion === undefined &&
5508+ command . notification === undefined &&
5509+ ! isNativeMaintenanceCommand ( command ) &&
5510+ ( activeRunMessage === undefined || ! isNativeMaintenanceCommand ( activeRunMessage ) ) &&
5511+ runAwaitsProviderTurn ( projection , activeRun )
5512+ ? command . deliveryIntent
5513+ : undefined ;
54995514 if ( shouldQueue ) {
55005515 if ( pendingMergeBackTransfers . length > 0 ) {
55015516 return yield * new OrchestratorDispatchError ( {
@@ -5630,6 +5645,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
56305645 ? { }
56315646 : { restartContinuationOfRunId : command . restartContinuationOfRunId } ) ,
56325647 ...( continuedTaskId === undefined ? { } : { delegatedTaskId : continuedTaskId } ) ,
5648+ ...( startupDeliveryIntent === undefined ? { } : { deliveryIntent : startupDeliveryIntent } ) ,
56335649 } ;
56345650 const attempt : OrchestrationV2RunAttempt = {
56355651 id : attemptId ,
@@ -12401,6 +12417,90 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1240112417 Effect . forkDetach ,
1240212418 ) ;
1240312419
12420+ const deliverStartupQueuedMessages = Effect . fn ( "Orchestrator.deliverStartupQueuedMessages" ) (
12421+ function * ( stored : OrchestrationV2StoredEvent ) {
12422+ if ( stored . event . type !== "provider-turn.updated" ) return ;
12423+ const threadId = stored . event . threadId ;
12424+ // Under the lock so a send still committing its queued run is seen. Each promotion can
12425+ // restart the target, so the next one decides from fresh records.
12426+ yield * threadDispatch . withLock (
12427+ threadId ,
12428+ Effect . gen ( function * ( ) {
12429+ const attempted = new Set < RunId > ( ) ;
12430+ while ( true ) {
12431+ const projection = yield * projectionStore . getThreadRecords (
12432+ threadId ,
12433+ [ "runs" , "messages" , "providerTurns" , "providerThreads" , "providerSessions" ] ,
12434+ { turnItemTypes : [ ] , messageRoles : [ "user" ] } ,
12435+ ) ;
12436+ const next = queuedRunsInDeliveryOrder ( projection )
12437+ . filter ( ( queued ) => {
12438+ if ( queued . deliveryIntent === undefined || queued . queueHeld === true ) return false ;
12439+ if ( attempted . has ( queued . id ) ) return false ;
12440+ // The message may have been edited into a command that must run as its own turn.
12441+ const message = projection . messages . find ( ( m ) => m . id === queued . userMessageId ) ;
12442+ return (
12443+ message !== undefined &&
12444+ parseGoalCommand ( message ) === null &&
12445+ ! isNativeMaintenanceCommand ( message )
12446+ ) ;
12447+ } )
12448+ . map ( ( queued ) => ( {
12449+ queued,
12450+ mode : resolveMessageDispatchIntent (
12451+ projection ,
12452+ { type : "queue_after_active" } ,
12453+ queued . deliveryIntent ,
12454+ ) ,
12455+ } ) )
12456+ . find (
12457+ ( { queued, mode } ) =>
12458+ mode . type === "steer_active" &&
12459+ // A message for another model starts its own turn. Promotion steers with the
12460+ // thread's selection, so that must match too or the steer would restart the turn.
12461+ modelSelectionsEqual ( queued . modelSelection , projection . thread . modelSelection ) &&
12462+ projection . runs . some (
12463+ ( run ) =>
12464+ run . id === mode . targetRunId &&
12465+ modelSelectionsEqual ( queued . modelSelection , run . modelSelection ) ,
12466+ ) ,
12467+ ) ;
12468+ if ( next ?. mode . type !== "steer_active" ) return ;
12469+ attempted . add ( next . queued . id ) ;
12470+ yield * dispatchWithReceiptEffect ( {
12471+ type : "queued-message.promote-to-steer" ,
12472+ commandId : CommandId . make ( `command:system:startup-steer:${ next . queued . id } ` ) ,
12473+ threadId,
12474+ queuedRunId : next . queued . id ,
12475+ targetRunId : next . mode . targetRunId ,
12476+ } ) ;
12477+ }
12478+ } ) ,
12479+ ) ;
12480+ } ,
12481+ ( effect , stored ) =>
12482+ effect . pipe (
12483+ Effect . catchCause ( ( cause ) =>
12484+ Effect . logWarning ( "Failed to steer messages queued before the turn started" , {
12485+ threadId : stored . event . threadId ,
12486+ sequence : stored . sequence ,
12487+ cause,
12488+ } ) ,
12489+ ) ,
12490+ ) ,
12491+ ) ;
12492+ yield * eventSink
12493+ . stream ( { afterSequence : terminalEventsAfterSequence , eventType : "provider-turn.updated" } )
12494+ . pipe (
12495+ Stream . filter (
12496+ ( stored ) =>
12497+ stored . event . type === "provider-turn.updated" &&
12498+ stored . event . payload . status === "running" ,
12499+ ) ,
12500+ Stream . runForEach ( deliverStartupQueuedMessages ) ,
12501+ Effect . forkDetach ,
12502+ ) ;
12503+
1240412504 // A child whose run ends while its background tasks or its own subagents still run has no
1240512505 // result yet. When that work settles there is no run update to finalize it, so these events
1240612506 // do it. A background roster counts once it empties after it was seen non-empty.
0 commit comments