@@ -23,6 +23,7 @@ import {
2323 type ChatAttachment ,
2424 CommandId ,
2525 type GuestMessageId ,
26+ isCompactionCommandMessage ,
2627 isOrchestrationV2WorkActive ,
2728 isProviderNativeSubagentThread ,
2829 MessageId ,
@@ -5774,13 +5775,14 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
57745775 } ) ;
57755776 }
57765777 if ( goalStopRunId !== undefined ) {
5777- return yield * stopRunForGoalCommand ( {
5778+ return yield * stopRunForQueuedCommand ( {
57785779 command,
57795780 events,
57805781 effects,
57815782 projection : yield * getProjectionWithPendingEvents ( command . threadId , events ) ,
57825783 queuedRunId : runId ,
57835784 targetRunId : goalStopRunId ,
5785+ reason : "Stopped to apply /goal" ,
57845786 now,
57855787 } ) ;
57865788 }
@@ -8355,15 +8357,44 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
83558357 ( yield * goalDriverFor ( command , queuedRun . providerInstanceId ) ) === undefined
83568358 ? null
83578359 : parsedGoalCommand ;
8360+ const target = projection . runs . find ( ( run ) => run . id === command . targetRunId ) ;
8361+ const targetMessage = projection . messages . find (
8362+ ( message ) => message . id === target ?. userMessageId ,
8363+ ) ;
8364+ // A compacting turn cannot take steering input, so steering skips the compaction instead.
8365+ if (
8366+ goalCommand === null &&
8367+ target !== undefined &&
8368+ targetMessage !== undefined &&
8369+ isCompactionCommandMessage ( targetMessage )
8370+ ) {
8371+ yield * emitEvent ( {
8372+ type : "run.updated" ,
8373+ threadId : command . threadId ,
8374+ runId : queuedRun . id ,
8375+ nodeId : queuedRootNode . id ,
8376+ providerInstanceId : queuedRun . providerInstanceId ,
8377+ occurredAt : now ,
8378+ payload : { ...queuedRun , queuePosition : 1 } ,
8379+ } ) ;
8380+ const stops =
8381+ isLiveRun ( target ) && ! ( yield * stopReachedRun ( command , command . threadId , target . id ) ) ;
8382+ return yield * stopRunForQueuedCommand ( {
8383+ command,
8384+ events,
8385+ effects,
8386+ projection,
8387+ queuedRunId : queuedRun . id ,
8388+ targetRunId : stops ? target . id : undefined ,
8389+ reason : "Skipped compaction to send a queued message" ,
8390+ now,
8391+ } ) ;
8392+ }
83588393 if ( goalCommand !== null ) {
83598394 yield * ensureGoalCommandSupported ( command , queuedMessage , queuedRun . providerInstanceId ) ;
8360- const target = projection . runs . find ( ( run ) => run . id === command . targetRunId ) ;
83618395 const goal = projection . providerThreads . find (
83628396 ( candidate ) => candidate . id === target ?. providerThreadId ,
83638397 ) ?. goal ;
8364- const targetMessage = projection . messages . find (
8365- ( message ) => message . id === target ?. userMessageId ,
8366- ) ;
83678398 yield * emitEvent ( {
83688399 type : "run.updated" ,
83698400 threadId : command . threadId ,
@@ -8379,13 +8410,14 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
83798410 goalCommandStopsActiveRun ( goalCommand , goal ?. status ?? null ) &&
83808411 ( targetMessage === undefined || ! isNativeMaintenanceCommand ( targetMessage ) ) &&
83818412 ! ( yield * stopReachedRun ( command , command . threadId , target . id ) ) ;
8382- return yield * stopRunForGoalCommand ( {
8413+ return yield * stopRunForQueuedCommand ( {
83838414 command,
83848415 events,
83858416 effects,
83868417 projection,
83878418 queuedRunId : queuedRun . id ,
83888419 targetRunId : stops ? target . id : undefined ,
8420+ reason : "Stopped to apply /goal" ,
83898421 now,
83908422 } ) ;
83918423 }
@@ -10496,71 +10528,74 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1049610528 ) ;
1049710529
1049810530 /** Puts a queued goal command first and, given a target, stops the run in its way. */
10499- const stopRunForGoalCommand = Effect . fn ( "Orchestrator.stopRunForGoalCommand" ) ( function * ( input : {
10500- readonly command : Extract <
10501- OrchestrationV2Command ,
10502- { readonly type : "message.dispatch" | "queued-message.promote-to-steer" }
10503- > ;
10504- readonly events : Ref . Ref < Array < OrchestrationV2DomainEvent > > ;
10505- readonly effects : Ref . Ref < Array < PendingOrchestrationEffectV2 > > ;
10506- readonly projection : Pick < OrchestrationV2ThreadProjection , "runs" | "messages" > ;
10507- readonly queuedRunId : RunId ;
10508- readonly targetRunId : RunId | undefined ;
10509- readonly now : DateTime . Utc ;
10510- } ) {
10511- const { command, events, effects, projection, now, targetRunId } = input ;
10512- const others = queuedRunsInDeliveryOrder ( projection ) . filter (
10513- ( run ) => run . id !== input . queuedRunId && ! isAutomaticCompletionRun ( projection , run ) ,
10514- ) ;
10515- for ( const [ index , run ] of others . entries ( ) ) {
10516- const queuePosition = index + 2 ;
10517- if ( run . queuePosition === queuePosition ) continue ;
10518- yield * emit (
10519- events ,
10520- command ,
10521- ) ( {
10522- type : "run.updated" ,
10523- threadId : command . threadId ,
10524- runId : run . id ,
10525- ...( run . rootNodeId === null ? { } : { nodeId : run . rootNodeId } ) ,
10526- providerInstanceId : run . providerInstanceId ,
10527- occurredAt : now ,
10528- payload : { ...run , queuePosition } ,
10529- } ) ;
10530- }
10531- if ( targetRunId === undefined ) return undefined ;
10532- const interruptEvents = yield * Ref . make < Array < OrchestrationV2DomainEvent > > ( [ ] ) ;
10533- const interruptEffects = yield * Ref . make < Array < PendingOrchestrationEffectV2 > > ( [ ] ) ;
10534- const interrupted = yield * Effect . exit (
10535- dispatchRunInterrupt (
10536- {
10537- type : "run.interrupt" ,
10538- commandId : command . commandId ,
10531+ const stopRunForQueuedCommand = Effect . fn ( "Orchestrator.stopRunForQueuedCommand" ) (
10532+ function * ( input : {
10533+ readonly command : Extract <
10534+ OrchestrationV2Command ,
10535+ { readonly type : "message.dispatch" | "queued-message.promote-to-steer" }
10536+ > ;
10537+ readonly events : Ref . Ref < Array < OrchestrationV2DomainEvent > > ;
10538+ readonly effects : Ref . Ref < Array < PendingOrchestrationEffectV2 > > ;
10539+ readonly projection : Pick < OrchestrationV2ThreadProjection , "runs" | "messages" > ;
10540+ readonly queuedRunId : RunId ;
10541+ readonly targetRunId : RunId | undefined ;
10542+ readonly reason : string ;
10543+ readonly now : DateTime . Utc ;
10544+ } ) {
10545+ const { command, events, effects, projection, now, targetRunId } = input ;
10546+ const others = queuedRunsInDeliveryOrder ( projection ) . filter (
10547+ ( run ) => run . id !== input . queuedRunId && ! isAutomaticCompletionRun ( projection , run ) ,
10548+ ) ;
10549+ for ( const [ index , run ] of others . entries ( ) ) {
10550+ const queuePosition = index + 2 ;
10551+ if ( run . queuePosition === queuePosition ) continue ;
10552+ yield * emit (
10553+ events ,
10554+ command ,
10555+ ) ( {
10556+ type : "run.updated" ,
10557+ threadId : command . threadId ,
10558+ runId : run . id ,
10559+ ...( run . rootNodeId === null ? { } : { nodeId : run . rootNodeId } ) ,
10560+ providerInstanceId : run . providerInstanceId ,
10561+ occurredAt : now ,
10562+ payload : { ...run , queuePosition } ,
10563+ } ) ;
10564+ }
10565+ if ( targetRunId === undefined ) return undefined ;
10566+ const interruptEvents = yield * Ref . make < Array < OrchestrationV2DomainEvent > > ( [ ] ) ;
10567+ const interruptEffects = yield * Ref . make < Array < PendingOrchestrationEffectV2 > > ( [ ] ) ;
10568+ const interrupted = yield * Effect . exit (
10569+ dispatchRunInterrupt (
10570+ {
10571+ type : "run.interrupt" ,
10572+ commandId : command . commandId ,
10573+ threadId : command . threadId ,
10574+ runId : targetRunId ,
10575+ reason : input . reason ,
10576+ } ,
10577+ interruptEvents ,
10578+ interruptEffects ,
10579+ ) ,
10580+ ) ;
10581+ // A failed interrupt may have written part of its events, so they are kept apart.
10582+ // The queued command then waits for the turn to end.
10583+ if ( Exit . isFailure ( interrupted ) ) {
10584+ yield * Effect . logWarning ( "queued command could not stop the running turn" , {
1053910585 threadId : command . threadId ,
1054010586 runId : targetRunId ,
10541- reason : "Stopped to apply /goal" ,
10542- } ,
10543- interruptEvents ,
10544- interruptEffects ,
10545- ) ,
10546- ) ;
10547- // A failed interrupt may have written part of its events, so they are kept apart.
10548- // The goal command then waits for the turn to end.
10549- if ( Exit . isFailure ( interrupted ) ) {
10550- yield * Effect . logWarning ( "goal command could not stop the running turn" , {
10551- threadId : command . threadId ,
10552- runId : targetRunId ,
10553- cause : interrupted . cause ,
10554- } ) ;
10555- return undefined ;
10556- }
10557- const written = yield * Ref . get ( interruptEvents ) ;
10558- const enqueued = yield * Ref . get ( interruptEffects ) ;
10559- yield * Ref . update ( events , ( existing ) => [ ...existing , ...written ] ) ;
10560- yield * Ref . update ( effects , ( existing ) => [ ...existing , ...enqueued ] ) ;
10561- // Like Stop: a run stopped before its provider started also abandons that start.
10562- return interrupted . value ;
10563- } ) ;
10587+ cause : interrupted . cause ,
10588+ } ) ;
10589+ return undefined ;
10590+ }
10591+ const written = yield * Ref . get ( interruptEvents ) ;
10592+ const enqueued = yield * Ref . get ( interruptEffects ) ;
10593+ yield * Ref . update ( events , ( existing ) => [ ...existing , ...written ] ) ;
10594+ yield * Ref . update ( effects , ( existing ) => [ ...existing , ...enqueued ] ) ;
10595+ // Like Stop: a run stopped before its provider started also abandons that start.
10596+ return interrupted . value ;
10597+ } ,
10598+ ) ;
1056410599
1056510600 const dispatchThreadStop = Effect . fn ( "Orchestrator.dispatchThreadStop" ) ( function * (
1056610601 command : Extract < OrchestrationV2ServerCommand , { readonly type : "thread.stop" } > ,
0 commit comments