@@ -10436,7 +10436,15 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1043610436 Effect . gen ( function * ( ) {
1043710437 const childControls = yield * projectionStore . getThreadRecords (
1043810438 childThreadId ,
10439- [ "runs" , "messages" , "subagents" , "providerThreads" , "providerTurns" , "attempts" ] ,
10439+ [
10440+ "runs" ,
10441+ "messages" ,
10442+ "subagents" ,
10443+ "providerThreads" ,
10444+ "providerTurns" ,
10445+ "attempts" ,
10446+ "runtimeRequests" ,
10447+ ] ,
1044010448 { messageRoles : [ "user" ] } ,
1044110449 ) ;
1044210450 const parentThreadId = childControls . thread . lineage . parentThreadId ;
@@ -10470,6 +10478,35 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1047010478 ...( progress . resultRun === undefined ? { } : { resultRun : progress . resultRun } ) ,
1047110479 } ) ;
1047210480 }
10481+
10482+ const { resultRun } = progress ;
10483+ if ( progress . state !== "result_available" || resultRun === undefined ) return ;
10484+ const status = delegatedTaskTerminalStatus ( runOutcome ( resultRun ) ) ;
10485+ if ( status !== "cancelled" && status !== "interrupted" ) return ;
10486+ if (
10487+ yield * childAwaitsRestartContinuation (
10488+ childControls . runs ,
10489+ resultRun ,
10490+ options ?. settledContinuationOf ,
10491+ )
10492+ )
10493+ return ;
10494+ // Completed or failed tasks keep async questions: answering resumes the child.
10495+ // Callers hold the parent lock; dismiss takes the child lock, as follow-up intake does.
10496+ for ( const request of childControls . runtimeRequests ) {
10497+ if (
10498+ request . status === "pending" &&
10499+ request . kind === "user_input" &&
10500+ request . responseCapability . type === "message"
10501+ ) {
10502+ yield * dispatchWithReceipt ( {
10503+ type : "thread.user-input.dismiss" ,
10504+ commandId : CommandId . make ( `command:system:delegated-task-question:${ request . id } ` ) ,
10505+ threadId : childThreadId ,
10506+ requestId : request . id ,
10507+ } ) ;
10508+ }
10509+ }
1047310510 } ) ;
1047410511
1047510512 const finalizeDelegatedCompletionDelivery = ( threadId : ThreadId , runId : RunId ) =>
@@ -11252,85 +11289,6 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1125211289 : threadDispatch . withLock ( parentThreadId , dispatch ) ;
1125311290 } ) ;
1125411291
11255- const delegatedTaskResultPending = (
11256- childThreadId : ThreadId ,
11257- taskId ?: NodeId ,
11258- settledContinuationOf ?: RunId ,
11259- ) =>
11260- Effect . gen ( function * ( ) {
11261- const child = yield * projectionStore . getThreadRecords (
11262- childThreadId ,
11263- [ "runs" , "messages" , "subagents" , "providerThreads" , "providerTurns" , "attempts" ] ,
11264- { messageRoles : [ "user" ] } ,
11265- ) ;
11266- if ( taskId !== undefined ) {
11267- const runs = child . runs . filter ( ( run ) => run . delegatedTaskId === taskId ) ;
11268- const run = runs . toSorted ( ( a , b ) => b . ordinal - a . ordinal ) [ 0 ] ;
11269- if ( run === undefined || delegatedTaskTerminalStatus ( run . status ) === null ) return true ;
11270- return yield * childAwaitsRestartContinuation ( runs , run , settledContinuationOf ) ;
11271- }
11272- const progress = delegatedTaskProgress ( child ) ;
11273- // A caller's older read saw a result; newer work since then means it is not final.
11274- if ( progress . state !== "result_available" ) return true ;
11275- if ( progress . resultRun === undefined ) return false ;
11276- return yield * childAwaitsRestartContinuation (
11277- child . runs ,
11278- progress . resultRun ,
11279- settledContinuationOf ,
11280- ) ;
11281- } ) . pipe (
11282- Effect . mapError (
11283- ( cause ) => new OrchestratorProjectionError ( { threadId : childThreadId , cause } ) ,
11284- ) ,
11285- ) ;
11286-
11287- // Async questions do not stop a delegated child's run. A cancelled or
11288- // interrupted task's questions close, since nobody wants that work anymore.
11289- // A finished task keeps them: answering one resumes the child. The parent
11290- // lock is held throughout, so a follow-up run cannot start between the
11291- // check and the dismissal.
11292- const finishAppOwnedSubagent = (
11293- parentThreadId : ThreadId ,
11294- childThreadId : ThreadId ,
11295- options ?: { readonly settledContinuationOf ?: RunId } ,
11296- ) =>
11297- threadDispatch . withLock (
11298- parentThreadId ,
11299- Effect . gen ( function * ( ) {
11300- yield * finalizeAppOwnedSubagent ( childThreadId , options ) ;
11301- const settledContinuationOf = options ?. settledContinuationOf ;
11302- if ( yield * delegatedTaskResultPending ( childThreadId , undefined , settledContinuationOf ) )
11303- return ;
11304- const child = yield * projectionStore . getThreadRecords (
11305- childThreadId ,
11306- [ "runtimeRequests" , "runs" , "messages" , "subagents" , "providerThreads" ] ,
11307- { messageRoles : [ "user" ] } ,
11308- ) ;
11309- const { resultRun } = delegatedTaskProgress ( child ) ;
11310- const status =
11311- resultRun === undefined ? null : delegatedTaskTerminalStatus ( runOutcome ( resultRun ) ) ;
11312- if ( status !== "cancelled" && status !== "interrupted" ) return ;
11313- for ( const request of child . runtimeRequests ) {
11314- if (
11315- request . status !== "pending" ||
11316- request . kind !== "user_input" ||
11317- request . responseCapability . type !== "message"
11318- )
11319- continue ;
11320- // Takes the child lock inside the parent's, the order follow-up intake uses.
11321- yield * threadDispatch . withLock (
11322- childThreadId ,
11323- dispatchWithReceiptEffect ( {
11324- type : "thread.user-input.dismiss" ,
11325- commandId : CommandId . make ( `command:system:delegated-task-question:${ request . id } ` ) ,
11326- threadId : childThreadId ,
11327- requestId : request . id ,
11328- } ) ,
11329- ) ;
11330- }
11331- } ) ,
11332- ) ;
11333-
1133411292 const handleTerminalRun = ( stored : OrchestrationV2StoredEvent ) =>
1133511293 Effect . gen ( function * ( ) {
1133611294 const threadId = stored . event . threadId ;
@@ -11370,7 +11328,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1137011328 // wakes, and only then is the failed run the task's result.
1137111329 const parentThreadId = yield * appOwnedSubagentParentThreadId ( threadId ) ;
1137211330 if ( parentThreadId !== undefined ) {
11373- yield * finishAppOwnedSubagent ( parentThreadId , threadId ) ;
11331+ yield * threadDispatch . withLock ( parentThreadId , finalizeAppOwnedSubagent ( threadId ) ) ;
1137411332 }
1137511333 } ) . pipe (
1137611334 Effect . catchCause ( ( cause ) =>
@@ -11420,7 +11378,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1142011378 const thread = yield * projectionStore . getThreadShell ( threadId ) ;
1142111379 const parentThreadId = thread ?. lineage . parentThreadId ;
1142211380 if ( parentThreadId === undefined || parentThreadId === null ) return ;
11423- yield * finishAppOwnedSubagent ( parentThreadId , threadId ) ;
11381+ yield * threadDispatch . withLock ( parentThreadId , finalizeAppOwnedSubagent ( threadId ) ) ;
1142411382 } ) . pipe (
1142511383 Effect . catchCause ( ( cause ) =>
1142611384 Effect . logWarning ( "Failed to recover terminal app-owned subagent" , {
@@ -11499,13 +11457,38 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1149911457 ) ;
1150011458 } ) ;
1150111459
11460+ const delegatedTaskResultPending = ( childThreadId : ThreadId , taskId ?: NodeId ) =>
11461+ Effect . gen ( function * ( ) {
11462+ const child = yield * projectionStore . getThreadRecords (
11463+ childThreadId ,
11464+ [ "runs" , "messages" , "subagents" , "providerThreads" , "providerTurns" , "attempts" ] ,
11465+ { messageRoles : [ "user" ] } ,
11466+ ) ;
11467+ if ( taskId !== undefined ) {
11468+ const runs = child . runs . filter ( ( run ) => run . delegatedTaskId === taskId ) ;
11469+ const run = runs . toSorted ( ( a , b ) => b . ordinal - a . ordinal ) [ 0 ] ;
11470+ if ( run === undefined || delegatedTaskTerminalStatus ( run . status ) === null ) return true ;
11471+ return yield * childAwaitsRestartContinuation ( runs , run ) ;
11472+ }
11473+ const progress = delegatedTaskProgress ( child ) ;
11474+ // A caller's older read saw a result; newer work since then means it is not final.
11475+ if ( progress . state !== "result_available" ) return true ;
11476+ if ( progress . resultRun === undefined ) return false ;
11477+ return yield * childAwaitsRestartContinuation ( child . runs , progress . resultRun ) ;
11478+ } ) . pipe (
11479+ Effect . mapError (
11480+ ( cause ) => new OrchestratorProjectionError ( { threadId : childThreadId , cause } ) ,
11481+ ) ,
11482+ ) ;
11483+
1150211484 const recoverDelegatedTask = ( threadId : ThreadId , sourceRunId : RunId ) =>
1150311485 Effect . gen ( function * ( ) {
1150411486 const parentThreadId = yield * appOwnedSubagentParentThreadId ( threadId ) ;
1150511487 if ( parentThreadId === undefined ) return ;
11506- yield * finishAppOwnedSubagent ( parentThreadId , threadId , {
11507- settledContinuationOf : sourceRunId ,
11508- } ) ;
11488+ yield * threadDispatch . withLock (
11489+ parentThreadId ,
11490+ finalizeAppOwnedSubagent ( threadId , { settledContinuationOf : sourceRunId } ) ,
11491+ ) ;
1150911492 } ) . pipe (
1151011493 Effect . catchCause ( ( cause ) =>
1151111494 Effect . logWarning ( "Failed to recover delegated task after restart" , {
0 commit comments