@@ -9412,14 +9412,31 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
94129412 const run = projection . runs . find ( ( candidate ) => candidate . id === command . runId ) ;
94139413 const subagent = projection . subagents . find ( ( candidate ) => candidate . id === command . subagentId ) ;
94149414 // A user Stop ends an app-owned task here, whatever state its child is in, so a task that lost
9415- // its child run (#19002) cannot outlive Stop. The client then stops the child thread; a child
9416- // run that settles later still records its real outcome. A task that already ended is written
9415+ // its child run (#19002) cannot outlive Stop. The client then stops the child thread. A live
9416+ // child run bound to the task keeps its delivery: when it settles it records the real outcome
9417+ // and wakes the parent, whose own task may wait on it. A task that already ended is written
94179418 // again, so a client holding a stale copy stops showing it as running.
94189419 if (
94199420 subagent ?. origin === "app_owned" &&
94209421 subagent . childThreadId !== null &&
94219422 subagent . runId === command . runId
94229423 ) {
9424+ const childThreadId = subagent . childThreadId ;
9425+ const child = yield * projectionStore . getThreadRecords ( childThreadId , [ "runs" ] ) . pipe (
9426+ Effect . catchTags ( { ProjectionStoreThreadNotFoundError : ( ) => Effect . succeed ( null ) } ) ,
9427+ Effect . mapError (
9428+ ( cause ) => new OrchestratorProjectionError ( { threadId : childThreadId , cause } ) ,
9429+ ) ,
9430+ ) ;
9431+ const original =
9432+ child ?. thread . forkedFrom ?. type === "node" && child . thread . forkedFrom . nodeId === subagent . id ;
9433+ const willSettle =
9434+ child ?. runs . some (
9435+ ( run ) =>
9436+ ( run . status === "queued" || isBlockingRun ( run ) ) &&
9437+ ( run . delegatedTaskId === subagent . id ||
9438+ ( original && run . delegatedTaskId === undefined ) ) ,
9439+ ) ?? false ;
94239440 const records = yield * loadProjectionForCommand ( command , [ "nodes" , "turnItems" ] , {
94249441 turnItemTypes : [ "subagent" ] ,
94259442 turnItemRunIds : [ subagent . runId ] ,
@@ -9429,9 +9446,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
94299446 return yield * writeAppOwnedTask ( {
94309447 command,
94319448 events,
9432- task : isOrchestrationV2WorkActive ( subagent . status )
9433- ? cancelledAppOwnedTask ( subagent , now )
9434- : subagent ,
9449+ task : ! isOrchestrationV2WorkActive ( subagent . status )
9450+ ? subagent
9451+ : willSettle
9452+ ? { ...subagent , status : "cancelled" , completedAt : now , updatedAt : now }
9453+ : cancelledAppOwnedTask ( subagent , now ) ,
94359454 nodes : records . nodes ,
94369455 turnItems : records . turnItems ,
94379456 now,
0 commit comments