@@ -6394,6 +6394,127 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
63946394 } ) ,
63956395 ) ;
63966396
6397+ it . effect ( "rejects archiving an idle parent until its delegated child completes" , ( ) =>
6398+ Effect . gen ( function * ( ) {
6399+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
6400+ const sink = yield * EventSink . EventSinkV2 ;
6401+ const threadId = ThreadId . make ( "archive-delegated-parent" ) ;
6402+ const projectId = ProjectId . make ( "archive-delegated-project" ) ;
6403+ yield * seedProject ( {
6404+ projectId,
6405+ title : "Delegated archive" ,
6406+ workspaceRoot : "/workspace/archive-delegated" ,
6407+ defaultModelSelection : null ,
6408+ createdAt : "2026-09-07T00:00:00.000Z" ,
6409+ } ) ;
6410+ yield * orchestrator . dispatch ( {
6411+ type : "thread.create" ,
6412+ createdBy : "user" ,
6413+ creationSource : "web" ,
6414+ commandId : CommandId . make ( "archive-delegated-create" ) ,
6415+ threadId,
6416+ projectId,
6417+ title : "Delegated archive" ,
6418+ modelSelection,
6419+ runtimeMode : "full-access" ,
6420+ interactionMode : "default" ,
6421+ branch : null ,
6422+ worktreePath : null ,
6423+ } ) ;
6424+ yield * orchestrator . dispatch ( {
6425+ type : "message.dispatch" ,
6426+ createdBy : "user" ,
6427+ creationSource : "web" ,
6428+ commandId : CommandId . make ( "archive-delegated-message" ) ,
6429+ threadId,
6430+ messageId : MessageId . make ( "archive-delegated-message" ) ,
6431+ text : "Delegate the work, then finish your turn." ,
6432+ attachments : [ ] ,
6433+ modelSelection,
6434+ dispatchMode : { type : "start_immediately" } ,
6435+ } ) ;
6436+ const parentRun = ( yield * orchestrator . getThreadProjection ( threadId ) ) . runs [ 0 ] ! ;
6437+ yield * orchestrator . dispatch ( {
6438+ type : "delegated_task.request" ,
6439+ createdBy : "agent" ,
6440+ creationSource : "mcp" ,
6441+ commandId : CommandId . make ( "archive-delegated-request" ) ,
6442+ parentThreadId : threadId ,
6443+ parentRunId : parentRun . id ,
6444+ parentNodeId : parentRun . rootNodeId ! ,
6445+ task : "Keep working after the parent finishes." ,
6446+ modelSelection,
6447+ runtimeMode : "full-access" ,
6448+ interactionMode : "default" ,
6449+ completionWake : "always" ,
6450+ } ) ;
6451+ const delegatedParentRun = ( yield * orchestrator . getThreadProjection ( threadId ) ) . runs [ 0 ] ! ;
6452+ const now = yield * DateTime . now ;
6453+ yield * sink . write ( {
6454+ events : [
6455+ {
6456+ id : EventId . make ( "archive-delegated-parent-completed" ) ,
6457+ type : "run.updated" ,
6458+ threadId,
6459+ runId : parentRun . id ,
6460+ occurredAt : now ,
6461+ payload : { ...delegatedParentRun , status : "completed" , completedAt : now } ,
6462+ } ,
6463+ ] ,
6464+ } ) ;
6465+ const parent = yield * orchestrator . getThreadProjection ( threadId ) ;
6466+ const task = parent . subagents [ 0 ] ! ;
6467+ assert . equal ( parent . runs [ 0 ] ?. status , "completed" ) ;
6468+ assert . equal ( task . origin , "app_owned" ) ;
6469+ assert . equal ( task . status , "running" ) ;
6470+ assert . isNotNull ( task . childThreadId ) ;
6471+
6472+ // Archive must preserve the idle parent's path for receiving this child's result.
6473+ const error = yield * orchestrator
6474+ . dispatch ( {
6475+ type : "thread.archive" ,
6476+ commandId : CommandId . make ( "archive-delegated-blocked" ) ,
6477+ threadId,
6478+ } )
6479+ . pipe ( Effect . flip ) ;
6480+ assert . instanceOf ( error , Orchestrator . OrchestratorDispatchError ) ;
6481+ assert . equal (
6482+ error . cause ,
6483+ `Thread ${ threadId } has running delegated tasks. Wait for them or cancel them before archiving.` ,
6484+ ) ;
6485+ assert . isNull ( ( yield * orchestrator . getThreadProjection ( threadId ) ) . thread . archivedAt ) ;
6486+
6487+ const childThreadId = task . childThreadId ! ;
6488+ const childRun = ( yield * orchestrator . getThreadProjection ( childThreadId ) ) . runs [ 0 ] ! ;
6489+ yield * sink . write ( {
6490+ events : [
6491+ {
6492+ id : EventId . make ( "archive-delegated-child-run-completed" ) ,
6493+ type : "run.updated" ,
6494+ threadId : childThreadId ,
6495+ runId : childRun . id ,
6496+ occurredAt : now ,
6497+ payload : { ...childRun , status : "completed" , completedAt : now } ,
6498+ } ,
6499+ {
6500+ id : EventId . make ( "archive-delegated-child-completed" ) ,
6501+ type : "subagent.updated" ,
6502+ threadId,
6503+ nodeId : task . id ,
6504+ occurredAt : now ,
6505+ payload : { ...task , status : "completed" , result : "Done" , completedAt : now } ,
6506+ } ,
6507+ ] ,
6508+ } ) ;
6509+ yield * orchestrator . dispatch ( {
6510+ type : "thread.archive" ,
6511+ commandId : CommandId . make ( "archive-delegated-allowed" ) ,
6512+ threadId,
6513+ } ) ;
6514+ assert . isNotNull ( ( yield * orchestrator . getThreadProjection ( threadId ) ) . thread . archivedAt ) ;
6515+ } ) ,
6516+ ) ;
6517+
63976518 it . effect ( "cancels preparing work on archive and keeps it cancelled through replay" , ( ) =>
63986519 Effect . gen ( function * ( ) {
63996520 const orchestrator = yield * Orchestrator . OrchestratorV2 ;
0 commit comments