@@ -652,3 +652,101 @@ it.layer(TestLayer)("delegated completion delivery repairs", (it) => {
652652 } ) ,
653653 ) ;
654654} ) ;
655+
656+ it . effect (
657+ "Stop disposes tasks spawned during a completion-wake run as well as its incoming cohort" ,
658+ ( ) =>
659+ Effect . gen ( function * ( ) {
660+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
661+ const sink = yield * EventSink . EventSinkV2 ;
662+ const now = yield * DateTime . now ;
663+ const threadId = ThreadId . make ( "stop-wake-parent" ) ;
664+ const originRunId = RunId . make ( "stop-wake-origin" ) ;
665+ const taskId = NodeId . make ( "stop-wake-incoming-task" ) ;
666+ yield * seedParentWithTerminalTask ( {
667+ threadId,
668+ projectId : ProjectId . make ( "stop-wake-project" ) ,
669+ runId : originRunId ,
670+ rootNodeId : NodeId . make ( "stop-wake-origin-root" ) ,
671+ taskId,
672+ deliveryState : "claimed" ,
673+ deliveryTaskIds : [ taskId ] ,
674+ completionWake : "always" ,
675+ now,
676+ } ) ;
677+ const original = yield * orchestrator . getThreadProjection ( threadId ) ;
678+ yield * sink . write ( {
679+ events : [
680+ {
681+ id : EventId . make ( "stop-wake-origin-completed" ) ,
682+ type : "run.updated" ,
683+ threadId,
684+ runId : originRunId ,
685+ occurredAt : now ,
686+ payload : { ...original . runs [ 0 ] ! , status : "completed" , completedAt : now } ,
687+ } ,
688+ ] ,
689+ } ) ;
690+ const delivery = original . runs [ 0 ] ! . delegatedCompletion ! . delivery ! ;
691+ yield * orchestrator . dispatch ( {
692+ type : "message.dispatch" ,
693+ createdBy : "agent" ,
694+ creationSource : "server" ,
695+ commandId : CommandId . make ( "stop-wake-start" ) ,
696+ threadId,
697+ messageId : delivery . messageId ,
698+ text : "Delegated task completed" ,
699+ attachments : [ ] ,
700+ modelSelection,
701+ dispatchMode : { type : "queue_after_active" } ,
702+ delegatedCompletion : {
703+ parentRunId : originRunId ,
704+ generation : delivery . generation ,
705+ taskIds : [ taskId ] ,
706+ } ,
707+ } ) ;
708+ yield * orchestrator . resumeQueuedRuns ;
709+ const wake = ( yield * orchestrator . getThreadProjection ( threadId ) ) . runs . find (
710+ ( run ) => run . userMessageId === delivery . messageId ,
711+ ) ! ;
712+ const requested = yield * orchestrator . dispatch ( {
713+ type : "delegated_task.request" ,
714+ createdBy : "agent" ,
715+ creationSource : "mcp" ,
716+ commandId : CommandId . make ( "stop-wake-new-task" ) ,
717+ parentThreadId : threadId ,
718+ parentRunId : wake . id ,
719+ parentNodeId : wake . rootNodeId ! ,
720+ task : "Do the next round" ,
721+ modelSelection,
722+ runtimeMode : "full-access" ,
723+ interactionMode : "default" ,
724+ completionWake : "always" ,
725+ } ) ;
726+ const taskEvent = requested . storedEvents . find (
727+ ( stored ) => stored . event . type === "subagent.updated" ,
728+ ) ;
729+ if ( taskEvent ?. event . type !== "subagent.updated" )
730+ return yield * Effect . die ( "Missing new task" ) ;
731+ const newTaskId = taskEvent . event . payload . id ;
732+ yield * orchestrator . dispatch ( {
733+ type : "run.interrupt" ,
734+ commandId : CommandId . make ( "stop-wake-stop" ) ,
735+ threadId,
736+ runId : wake . id ,
737+ } ) ;
738+ const stopped = yield * orchestrator . getThreadProjection ( threadId ) ;
739+ assert . equal (
740+ stopped . subagents . find ( ( task ) => task . id === newTaskId ) ?. completionDelivery ?. state ,
741+ "disposed" ,
742+ ) ;
743+ assert . equal (
744+ stopped . runs . find ( ( run ) => run . id === wake . id ) ?. delegatedCompletion ?. disposition ,
745+ "stopped" ,
746+ ) ;
747+ assert . equal (
748+ stopped . runs . find ( ( run ) => run . id === originRunId ) ?. delegatedCompletion ?. disposition ,
749+ "stopped" ,
750+ ) ;
751+ } ) . pipe ( Effect . provide ( TestLayer ) ) ,
752+ ) ;
0 commit comments