@@ -11115,6 +11115,63 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1111511115 : threadDispatch . withLock ( parentThreadId , dispatch ) ;
1111611116 } ) ;
1111711117
11118+ const delegatedTaskResultPending = ( childThreadId : ThreadId , taskId ?: NodeId ) =>
11119+ Effect . gen ( function * ( ) {
11120+ const child = yield * projectionStore . getThreadRecords (
11121+ childThreadId ,
11122+ [ "runs" , "messages" , "subagents" , "providerThreads" , "providerTurns" , "attempts" ] ,
11123+ { messageRoles : [ "user" ] } ,
11124+ ) ;
11125+ if ( taskId !== undefined ) {
11126+ const runs = child . runs . filter ( ( run ) => run . delegatedTaskId === taskId ) ;
11127+ const run = runs . toSorted ( ( a , b ) => b . ordinal - a . ordinal ) [ 0 ] ;
11128+ if ( run === undefined || delegatedTaskTerminalStatus ( run . status ) === null ) return true ;
11129+ return yield * childAwaitsRestartContinuation ( runs , run ) ;
11130+ }
11131+ const progress = delegatedTaskProgress ( child ) ;
11132+ // A caller's older read saw a result; newer work since then means it is not final.
11133+ if ( progress . state !== "result_available" ) return true ;
11134+ if ( progress . resultRun === undefined ) return false ;
11135+ return yield * childAwaitsRestartContinuation ( child . runs , progress . resultRun ) ;
11136+ } ) . pipe (
11137+ Effect . mapError (
11138+ ( cause ) => new OrchestratorProjectionError ( { threadId : childThreadId , cause } ) ,
11139+ ) ,
11140+ ) ;
11141+
11142+ // Async questions do not stop a delegated child's run. Once its task result is
11143+ // final, nothing reads the answers, so the questions close instead of
11144+ // showing on the parent forever.
11145+ const finishAppOwnedSubagent = (
11146+ parentThreadId : ThreadId ,
11147+ childThreadId : ThreadId ,
11148+ options ?: { readonly settledContinuationOf ?: RunId } ,
11149+ ) =>
11150+ Effect . gen ( function * ( ) {
11151+ yield * threadDispatch . withLock (
11152+ parentThreadId ,
11153+ finalizeAppOwnedSubagent ( childThreadId , options ) ,
11154+ ) ;
11155+ if ( yield * delegatedTaskResultPending ( childThreadId ) ) return ;
11156+ const { runtimeRequests } = yield * projectionStore . getThreadRecords ( childThreadId , [
11157+ "runtimeRequests" ,
11158+ ] ) ;
11159+ for ( const request of runtimeRequests ) {
11160+ if (
11161+ request . status !== "pending" ||
11162+ request . kind !== "user_input" ||
11163+ request . responseCapability . type !== "message"
11164+ )
11165+ continue ;
11166+ yield * dispatchWithReceipt ( {
11167+ type : "thread.user-input.dismiss" ,
11168+ commandId : CommandId . make ( `command:system:delegated-task-question:${ request . id } ` ) ,
11169+ threadId : childThreadId ,
11170+ requestId : request . id ,
11171+ } ) ;
11172+ }
11173+ } ) ;
11174+
1111811175 const handleTerminalRun = ( stored : OrchestrationV2StoredEvent ) =>
1111911176 Effect . gen ( function * ( ) {
1112011177 const threadId = stored . event . threadId ;
@@ -11154,7 +11211,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1115411211 // wakes, and only then is the failed run the task's result.
1115511212 const parentThreadId = yield * appOwnedSubagentParentThreadId ( threadId ) ;
1115611213 if ( parentThreadId !== undefined ) {
11157- yield * threadDispatch . withLock ( parentThreadId , finalizeAppOwnedSubagent ( threadId ) ) ;
11214+ yield * finishAppOwnedSubagent ( parentThreadId , threadId ) ;
1115811215 }
1115911216 } ) . pipe (
1116011217 Effect . catchCause ( ( cause ) =>
@@ -11204,7 +11261,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1120411261 const thread = yield * projectionStore . getThreadShell ( threadId ) ;
1120511262 const parentThreadId = thread ?. lineage . parentThreadId ;
1120611263 if ( parentThreadId === undefined || parentThreadId === null ) return ;
11207- yield * threadDispatch . withLock ( parentThreadId , finalizeAppOwnedSubagent ( threadId ) ) ;
11264+ yield * finishAppOwnedSubagent ( parentThreadId , threadId ) ;
1120811265 } ) . pipe (
1120911266 Effect . catchCause ( ( cause ) =>
1121011267 Effect . logWarning ( "Failed to recover terminal app-owned subagent" , {
@@ -11283,38 +11340,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1128311340 ) ;
1128411341 } ) ;
1128511342
11286- const delegatedTaskResultPending = ( childThreadId : ThreadId , taskId ?: NodeId ) =>
11287- Effect . gen ( function * ( ) {
11288- const child = yield * projectionStore . getThreadRecords (
11289- childThreadId ,
11290- [ "runs" , "messages" , "subagents" , "providerThreads" , "providerTurns" , "attempts" ] ,
11291- { messageRoles : [ "user" ] } ,
11292- ) ;
11293- if ( taskId !== undefined ) {
11294- const runs = child . runs . filter ( ( run ) => run . delegatedTaskId === taskId ) ;
11295- const run = runs . toSorted ( ( a , b ) => b . ordinal - a . ordinal ) [ 0 ] ;
11296- if ( run === undefined || delegatedTaskTerminalStatus ( run . status ) === null ) return true ;
11297- return yield * childAwaitsRestartContinuation ( runs , run ) ;
11298- }
11299- const progress = delegatedTaskProgress ( child ) ;
11300- // A caller's older read saw a result; newer work since then means it is not final.
11301- if ( progress . state !== "result_available" ) return true ;
11302- if ( progress . resultRun === undefined ) return false ;
11303- return yield * childAwaitsRestartContinuation ( child . runs , progress . resultRun ) ;
11304- } ) . pipe (
11305- Effect . mapError (
11306- ( cause ) => new OrchestratorProjectionError ( { threadId : childThreadId , cause } ) ,
11307- ) ,
11308- ) ;
11309-
1131011343 const recoverDelegatedTask = ( threadId : ThreadId , sourceRunId : RunId ) =>
1131111344 Effect . gen ( function * ( ) {
1131211345 const parentThreadId = yield * appOwnedSubagentParentThreadId ( threadId ) ;
1131311346 if ( parentThreadId === undefined ) return ;
11314- yield * threadDispatch . withLock (
11315- parentThreadId ,
11316- finalizeAppOwnedSubagent ( threadId , { settledContinuationOf : sourceRunId } ) ,
11317- ) ;
11347+ yield * finishAppOwnedSubagent ( parentThreadId , threadId , {
11348+ settledContinuationOf : sourceRunId ,
11349+ } ) ;
1131811350 } ) . pipe (
1131911351 Effect . catchCause ( ( cause ) =>
1132011352 Effect . logWarning ( "Failed to recover delegated task after restart" , {
0 commit comments