@@ -2937,6 +2937,251 @@ it.layer(Layer.merge(layerTest, ProviderContinuationRequests.layer))(
29372937 } ;
29382938 } ) ;
29392939
2940+ it . effect ( "holds a follow-up open while the child's own subagent still runs" , ( ) =>
2941+ Effect . gen ( function * ( ) {
2942+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
2943+ const eventSink = yield * EventSink . EventSinkV2 ;
2944+ const now = yield * DateTime . now ;
2945+ const parentThreadId = ThreadId . make ( "thread:followup-grandchild-parent" ) ;
2946+ const projectId = ProjectId . make ( "project:followup-grandchild" ) ;
2947+ const parentRunId = RunId . make ( "run:followup-grandchild-parent" ) ;
2948+ const rootNodeId = NodeId . make ( "node:followup-grandchild-parent" ) ;
2949+ yield * seedParentWithTerminalTask ( {
2950+ threadId : parentThreadId ,
2951+ projectId,
2952+ runId : parentRunId ,
2953+ rootNodeId,
2954+ taskId : NodeId . make ( "node:followup-grandchild-seed" ) ,
2955+ deliveryState : "acknowledged" ,
2956+ now,
2957+ } ) ;
2958+ const child = yield * seedRestartCancelledChild ( {
2959+ parentThreadId,
2960+ projectId,
2961+ parentRunId,
2962+ rootNodeId,
2963+ name : "followup-grandchild-child" ,
2964+ completionWake : "always" ,
2965+ continuationPending : false ,
2966+ runStatus : "completed" ,
2967+ now,
2968+ } ) ;
2969+ yield * orchestrator . recoverDelegatedTasks ;
2970+ assert . equal (
2971+ ( yield * orchestrator . getThreadProjection ( parentThreadId ) ) . subagents . find (
2972+ ( task ) => task . id === child . taskId ,
2973+ ) ?. status ,
2974+ "completed" ,
2975+ ) ;
2976+
2977+ const followupMessageId = MessageId . make ( "message:followup-grandchild-send" ) ;
2978+ yield * orchestrator . dispatch ( {
2979+ type : "message.dispatch" ,
2980+ createdBy : "agent" ,
2981+ creationSource : "mcp" ,
2982+ commandId : CommandId . make ( "command:followup-grandchild-send" ) ,
2983+ threadId : child . childThreadId ,
2984+ senderThreadId : parentThreadId ,
2985+ messageId : followupMessageId ,
2986+ text : "Now delegate the next step." ,
2987+ attachments : [ ] ,
2988+ dispatchMode : { type : "queue_after_active" } ,
2989+ } ) ;
2990+ const followupRun = ( yield * orchestrator . getThreadProjection (
2991+ child . childThreadId ,
2992+ ) ) . runs . find ( ( run ) => run . userMessageId === followupMessageId ) ! ;
2993+ assert . isDefined ( followupRun . delegatedTaskId ) ;
2994+ const followupTaskId = followupRun . delegatedTaskId ! ;
2995+
2996+ const grandchildId = NodeId . make ( "node:followup-grandchild-grandchild" ) ;
2997+ const grandchild = {
2998+ id : grandchildId ,
2999+ threadId : child . childThreadId ,
3000+ runId : followupRun . id ,
3001+ parentNodeId : NodeId . make ( "node:followup-grandchild-child-root" ) ,
3002+ origin : "app_owned" as const ,
3003+ createdBy : "agent" as const ,
3004+ driver,
3005+ providerInstanceId : modelSelection . instanceId ,
3006+ providerThreadId : null ,
3007+ childThreadId : ThreadId . make ( "thread:followup-grandchild-grandchild" ) ,
3008+ nativeTaskRef : null ,
3009+ prompt : "Do the next step." ,
3010+ title : null ,
3011+ model : null ,
3012+ completionWake : "always" as const ,
3013+ status : "running" as const ,
3014+ result : null ,
3015+ startedAt : now ,
3016+ completedAt : null ,
3017+ updatedAt : now ,
3018+ } ;
3019+ yield * eventSink . write ( {
3020+ commandId : reconcileCommandId ( "followup-grandchild-turn-ended" ) ,
3021+ events : [
3022+ {
3023+ id : EventId . make ( "event:followup-grandchild-started" ) ,
3024+ type : "subagent.updated" ,
3025+ threadId : child . childThreadId ,
3026+ runId : followupRun . id ,
3027+ nodeId : grandchildId ,
3028+ driver,
3029+ providerInstanceId : modelSelection . instanceId ,
3030+ occurredAt : now ,
3031+ payload : grandchild ,
3032+ } ,
3033+ {
3034+ id : EventId . make ( "event:followup-grandchild-run-completed" ) ,
3035+ type : "run.updated" ,
3036+ threadId : child . childThreadId ,
3037+ runId : followupRun . id ,
3038+ occurredAt : now ,
3039+ payload : { ...followupRun , status : "completed" , startedAt : now , completedAt : now } ,
3040+ } ,
3041+ ] ,
3042+ } ) ;
3043+ yield * orchestrator . recoverDelegatedTasks ;
3044+
3045+ const waiting = yield * orchestrator . getThreadProjection ( parentThreadId ) ;
3046+ const heldFollowup = waiting . subagents . find ( ( task ) => task . id === followupTaskId ) ;
3047+ assert . isNull (
3048+ heldFollowup ?. result ?? null ,
3049+ "the follow-up closed while the child's own subagent was still running" ,
3050+ ) ;
3051+ assert . equal ( heldFollowup ?. status , "running" ) ;
3052+ assert . equal (
3053+ waiting . contextTransfers . filter (
3054+ ( transfer ) =>
3055+ transfer . type === "subagent_result" &&
3056+ transfer . sourceThreadId === child . childThreadId ,
3057+ ) . length ,
3058+ 1 ,
3059+ ) ;
3060+
3061+ // Another turn in the child does not release the hold while the grandchild runs.
3062+ for ( const status of [ "running" , "completed" ] as const ) {
3063+ yield * eventSink . write ( {
3064+ commandId : reconcileCommandId ( `followup-grandchild-user-${ status } ` ) ,
3065+ events : [
3066+ runEvent ( {
3067+ threadId : child . childThreadId ,
3068+ runId : RunId . make ( "run:followup-grandchild-user" ) ,
3069+ ordinal : followupRun . ordinal + 1 ,
3070+ status,
3071+ now,
3072+ } ) ,
3073+ ] ,
3074+ } ) ;
3075+ yield * orchestrator . recoverDelegatedTasks ;
3076+ assert . isNull (
3077+ ( yield * orchestrator . getThreadProjection ( parentThreadId ) ) . subagents . find (
3078+ ( task ) => task . id === followupTaskId ,
3079+ ) ?. result ?? null ,
3080+ `the follow-up closed while another child run was ${ status } ` ,
3081+ ) ;
3082+ }
3083+
3084+ // The grandchild's completion wakes the child in a run without a delegatedTaskId.
3085+ const wakeRunId = RunId . make ( "run:followup-grandchild-wake" ) ;
3086+ const wakeRun = ( status : "running" | "completed" ) =>
3087+ runEvent ( {
3088+ threadId : child . childThreadId ,
3089+ runId : wakeRunId ,
3090+ ordinal : followupRun . ordinal + 2 ,
3091+ status,
3092+ now,
3093+ } ) ;
3094+ yield * eventSink . write ( {
3095+ commandId : reconcileCommandId ( "followup-grandchild-settled" ) ,
3096+ events : [
3097+ {
3098+ id : EventId . make ( "event:followup-grandchild-completed" ) ,
3099+ type : "subagent.updated" ,
3100+ threadId : child . childThreadId ,
3101+ runId : followupRun . id ,
3102+ nodeId : grandchildId ,
3103+ driver,
3104+ providerInstanceId : modelSelection . instanceId ,
3105+ occurredAt : now ,
3106+ payload : {
3107+ ...grandchild ,
3108+ status : "completed" ,
3109+ result : "grandchild finished" ,
3110+ completionDelivery : { state : "acknowledged" , observedByRunId : wakeRunId } ,
3111+ completedAt : now ,
3112+ } ,
3113+ } ,
3114+ {
3115+ id : EventId . make ( "event:followup-grandchild-wake-message" ) ,
3116+ type : "message.updated" ,
3117+ threadId : child . childThreadId ,
3118+ runId : wakeRunId ,
3119+ occurredAt : now ,
3120+ payload : {
3121+ id : MessageId . make ( `message:${ wakeRunId } ` ) ,
3122+ threadId : child . childThreadId ,
3123+ runId : wakeRunId ,
3124+ nodeId : null ,
3125+ role : "user" ,
3126+ text : "Delegated task finished." ,
3127+ attachments : [ ] ,
3128+ streaming : false ,
3129+ createdBy : "agent" ,
3130+ creationSource : "server" ,
3131+ delegatedCompletion : {
3132+ parentRunId : followupRun . id ,
3133+ generation : 1 ,
3134+ taskIds : [ grandchildId ] ,
3135+ } ,
3136+ createdAt : now ,
3137+ updatedAt : now ,
3138+ } ,
3139+ } ,
3140+ wakeRun ( "running" ) ,
3141+ ] ,
3142+ } ) ;
3143+ yield * orchestrator . recoverDelegatedTasks ;
3144+ const waking = ( yield * orchestrator . getThreadProjection ( parentThreadId ) ) . subagents . find (
3145+ ( task ) => task . id === followupTaskId ,
3146+ ) ;
3147+ assert . isNull ( waking ?. result ?? null , "the follow-up closed before the child's wake ended" ) ;
3148+
3149+ yield * eventSink . write ( {
3150+ commandId : reconcileCommandId ( "followup-grandchild-wake-finished" ) ,
3151+ events : [
3152+ {
3153+ id : EventId . make ( "event:followup-grandchild-wake-result" ) ,
3154+ type : "message.updated" ,
3155+ threadId : child . childThreadId ,
3156+ runId : wakeRunId ,
3157+ occurredAt : now ,
3158+ payload : {
3159+ id : MessageId . make ( "message:followup-grandchild-wake-result" ) ,
3160+ threadId : child . childThreadId ,
3161+ runId : wakeRunId ,
3162+ nodeId : null ,
3163+ role : "assistant" ,
3164+ text : "The grandchild finished the next step." ,
3165+ attachments : [ ] ,
3166+ streaming : false ,
3167+ createdBy : "agent" ,
3168+ creationSource : "server" ,
3169+ createdAt : now ,
3170+ updatedAt : now ,
3171+ } ,
3172+ } ,
3173+ wakeRun ( "completed" ) ,
3174+ ] ,
3175+ } ) ;
3176+ yield * orchestrator . recoverDelegatedTasks ;
3177+ const settled = ( yield * orchestrator . getThreadProjection ( parentThreadId ) ) . subagents . find (
3178+ ( task ) => task . id === followupTaskId ,
3179+ ) ;
3180+ assert . equal ( settled ?. status , "completed" ) ;
3181+ assert . equal ( settled ?. result , "The grandchild finished the next step." ) ;
3182+ } ) ,
3183+ ) ;
3184+
29403185 it . effect ( "waits for a run the follow-up's child queued on its own thread" , ( ) =>
29413186 Effect . gen ( function * ( ) {
29423187 const requests = yield * ProviderContinuationRequests . ProviderContinuationRequests ;
0 commit comments