@@ -190,6 +190,8 @@ export class OrchestratorProjectionError extends Schema.TaggedError<Orchestrator
190190 }
191191}
192192
193+ const isOrchestratorProjectionError = Schema . is ( OrchestratorProjectionError ) ;
194+
193195export class OrchestratorDomainEventStreamError extends Schema . TaggedError < OrchestratorDomainEventStreamError > ( ) (
194196 "OrchestratorDomainEventStreamError" ,
195197 {
@@ -9395,6 +9397,24 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
93959397 ] ) ;
93969398 const run = projection . runs . find ( ( candidate ) => candidate . id === command . runId ) ;
93979399 const subagent = projection . subagents . find ( ( candidate ) => candidate . id === command . subagentId ) ;
9400+ // An app-owned task stops through its child thread. Here the parent only cancels the child's
9401+ // orphaned tasks: the child is idle and no child run is bound to them (#19002).
9402+ if (
9403+ subagent ?. origin === "app_owned" &&
9404+ subagent . childThreadId !== null &&
9405+ subagent . runId === command . runId
9406+ ) {
9407+ const records = yield * loadProjectionForCommand ( command , [ "subagents" , "turnItems" ] , {
9408+ turnItemTypes : [ "subagent" ] ,
9409+ turnItemStatuses : [ "pending" , "running" , "waiting" ] ,
9410+ } ) ;
9411+ return yield * cancelIdleOrphans ( {
9412+ command,
9413+ events,
9414+ projection : records ,
9415+ childThreadId : subagent . childThreadId ,
9416+ } ) ;
9417+ }
93989418 if (
93999419 subagent === undefined ||
94009420 subagent . runId !== command . runId ||
@@ -9837,23 +9857,18 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
98379857 return undefined ;
98389858 } ) ;
98399859
9840- const dispatchThreadStop = Effect . fn ( "Orchestrator.dispatchThreadStop" ) ( function * (
9841- command : Extract < OrchestrationV2ServerCommand , { readonly type : "thread.stop" } > ,
9842- events : Ref . Ref < Array < OrchestrationV2DomainEvent > > ,
9843- effects : Ref . Ref < Array < PendingOrchestrationEffectV2 > > ,
9844- ) {
9845- const projection = yield * projectionStore
9846- . getThreadRecords ( command . threadId , [ "runs" , "providerThreads" , "turnItems" , "subagents" ] , {
9847- turnItemTypes : [ "command_execution" , "dynamic_tool" , "subagent" ] ,
9848- turnItemStatuses : [ "pending" , "running" , "waiting" ] ,
9849- } )
9850- . pipe (
9851- Effect . mapError (
9852- ( cause ) => new OrchestratorProjectionError ( { threadId : command . threadId , cause } ) ,
9853- ) ,
9854- ) ;
9855- if ( projection . thread . deletedAt !== null ) return undefined ;
9856- const cancelIdleOrphans = Effect . gen ( function * ( ) {
9860+ const cancelIdleOrphans = Effect . fn ( "Orchestrator.cancelIdleOrphans" ) (
9861+ function * ( input : {
9862+ readonly command : Extract <
9863+ OrchestrationV2ServerCommand ,
9864+ { readonly type : "run.interrupt" | "thread.stop" | "subagent.stop" }
9865+ > ;
9866+ readonly events : Ref . Ref < Array < OrchestrationV2DomainEvent > > ;
9867+ readonly projection : Pick < OrchestrationV2ThreadProjection , "subagents" | "turnItems" > ;
9868+ /** Limits the repair to tasks delegated to this child thread. */
9869+ readonly childThreadId ?: ThreadId ;
9870+ } ) {
9871+ const { command, events, projection } = input ;
98579872 const pending = yield * Ref . get ( events ) ;
98589873 const children = new Map <
98599874 ThreadId ,
@@ -9869,7 +9884,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
98699884 if (
98709885 task . origin !== "app_owned" ||
98719886 task . childThreadId === null ||
9872- ! isOrchestrationV2WorkActive ( task . status )
9887+ ! isOrchestrationV2WorkActive ( task . status ) ||
9888+ ( input . childThreadId !== undefined && task . childThreadId !== input . childThreadId )
98739889 )
98749890 continue ;
98759891 const childThreadId = task . childThreadId ;
@@ -9951,13 +9967,33 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
99519967 } ) ;
99529968 }
99539969 }
9954- } ) . pipe (
9955- Effect . mapError ( ( cause ) =>
9956- Schema . is ( OrchestratorProjectionError ) ( cause )
9957- ? cause
9958- : new OrchestratorProjectionError ( { threadId : command . threadId , cause } ) ,
9970+ } ,
9971+ ( effect , input ) =>
9972+ effect . pipe (
9973+ Effect . mapError ( ( cause ) =>
9974+ isOrchestratorProjectionError ( cause )
9975+ ? cause
9976+ : new OrchestratorProjectionError ( { threadId : input . command . threadId , cause } ) ,
9977+ ) ,
99599978 ) ,
9960- ) ;
9979+ ) ;
9980+
9981+ const dispatchThreadStop = Effect . fn ( "Orchestrator.dispatchThreadStop" ) ( function * (
9982+ command : Extract < OrchestrationV2ServerCommand , { readonly type : "thread.stop" } > ,
9983+ events : Ref . Ref < Array < OrchestrationV2DomainEvent > > ,
9984+ effects : Ref . Ref < Array < PendingOrchestrationEffectV2 > > ,
9985+ ) {
9986+ const projection = yield * projectionStore
9987+ . getThreadRecords ( command . threadId , [ "runs" , "providerThreads" , "turnItems" , "subagents" ] , {
9988+ turnItemTypes : [ "command_execution" , "dynamic_tool" , "subagent" ] ,
9989+ turnItemStatuses : [ "pending" , "running" , "waiting" ] ,
9990+ } )
9991+ . pipe (
9992+ Effect . mapError (
9993+ ( cause ) => new OrchestratorProjectionError ( { threadId : command . threadId , cause } ) ,
9994+ ) ,
9995+ ) ;
9996+ if ( projection . thread . deletedAt !== null ) return undefined ;
99619997 const latestRun = projection . runs . findLast ( ( run ) => run . status !== "queued" ) ;
99629998 const target =
99639999 projection . runs . findLast (
@@ -9996,7 +10032,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
999610032 const enqueued = yield * Ref . get ( interruptEffects ) ;
999710033 yield * Ref . update ( events , ( existing ) => [ ...existing , ...written ] ) ;
999810034 yield * Ref . update ( effects , ( existing ) => [ ...existing , ...enqueued ] ) ;
9999- yield * cancelIdleOrphans ;
10035+ yield * cancelIdleOrphans ( { command , events , projection } ) ;
1000010036 return interrupted . value ;
1000110037 }
1000210038 yield * Effect . logWarning ( "thread.stop could not interrupt the running turn" , {
@@ -10016,7 +10052,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1001610052 yield * markStoppedRun ( { command, events, thread : projection . thread , run, now } ) ;
1001710053 }
1001810054 yield * holdStoppedThread ( { command, events, projection, cohortRunIds : [ ] , now } ) ;
10019- yield * cancelIdleOrphans ;
10055+ yield * cancelIdleOrphans ( { command , events , projection } ) ;
1002010056 return undefined ;
1002110057 } ) ;
1002210058
@@ -11296,6 +11332,25 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1129611332 case "run.interrupt" :
1129711333 cancelUnsettledEffects = yield * dispatchRunInterrupt ( command , events , effects ) ;
1129811334 if ( command . holdQueue === true ) {
11335+ // Stop must still commit when a child can't be read, so a failed repair's
11336+ // partial events are dropped.
11337+ const repairEvents = yield * Ref . make ( yield * Ref . get ( events ) ) ;
11338+ const repaired = yield * Effect . exit (
11339+ loadProjectionForCommand ( command , [ "subagents" , "turnItems" ] , {
11340+ turnItemTypes : [ "subagent" ] ,
11341+ turnItemStatuses : [ "pending" , "running" , "waiting" ] ,
11342+ } ) . pipe (
11343+ Effect . flatMap ( ( projection ) =>
11344+ cancelIdleOrphans ( { command, events : repairEvents , projection } ) ,
11345+ ) ,
11346+ ) ,
11347+ ) ;
11348+ if ( Exit . isSuccess ( repaired ) ) yield * Ref . set ( events , yield * Ref . get ( repairEvents ) ) ;
11349+ else
11350+ yield * Effect . logWarning ( "Stop could not cancel orphaned delegated follow-ups" , {
11351+ threadId : command . threadId ,
11352+ cause : repaired . cause ,
11353+ } ) ;
1129911354 yield * Ref . update ( effects , ( existing ) => [
1130011355 ...existing ,
1130111356 {
0 commit comments