@@ -530,65 +530,124 @@ it.effect("Stop still stops the thread when a delegated child cannot be read", (
530530 } ) . pipe ( Effect . provide ( layerTest ) ) ,
531531) ;
532532
533- it . effect ( "Stop ends watches, holds queues, and stops the delegated tasks under the thread" , ( ) =>
533+ it . effect . each ( [ "run.interrupt" , "thread.stop" ] as const ) (
534+ "%s Stop ends watches, holds queues, and stops the delegated tasks under the thread" ,
535+ ( type ) =>
536+ Effect . gen ( function * ( ) {
537+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
538+ const projections = yield * ProjectionStore . ProjectionStoreV2 ;
539+ const threads = yield * ThreadManagementService . ThreadManagementService ;
540+ const sql = yield * SqlClient . SqlClient ;
541+ const parentThreadId = ThreadId . make ( "thread:stop-parent" ) ;
542+ yield * createWatchingThread ( parentThreadId , 1 ) ;
543+ yield * send ( parentThreadId , "first" , "start_immediately" ) ;
544+ const childThreadId = yield * delegate ( parentThreadId , "child task" ) ;
545+ assert . isTrue ( Exit . isFailure ( yield * Effect . exit ( watch ( childThreadId , 2 ) ) ) ) ;
546+ const grandchildThreadId = yield * delegate ( childThreadId , "grandchild task" ) ;
547+ yield * send ( childThreadId , "child follow-up" , "queue_after_active" ) ;
548+
549+ const now = yield * DateTime . now ;
550+ const firstRun = ( yield * orchestrator . getThreadProjection ( parentThreadId ) ) . runs [ 0 ] ! ;
551+ yield * projections . apply ( {
552+ id : EventId . make ( "event:stop-parent:first-run-completed" ) ,
553+ type : "run.updated" ,
554+ threadId : parentThreadId ,
555+ runId : firstRun . id ,
556+ occurredAt : now ,
557+ payload : { ...firstRun , status : "completed" , completedAt : now } ,
558+ } ) ;
559+ yield * send ( parentThreadId , "second" , "start_immediately" ) ;
560+ const secondRun = ( yield * orchestrator . getThreadProjection ( parentThreadId ) ) . runs . at ( - 1 ) ! ;
561+
562+ const stopCommandId = CommandId . make ( "stop-parent" ) ;
563+ yield * orchestrator . dispatch (
564+ type === "thread.stop"
565+ ? { type, commandId : stopCommandId , threadId : parentThreadId }
566+ : {
567+ type,
568+ commandId : stopCommandId ,
569+ threadId : parentThreadId ,
570+ runId : secondRun . id ,
571+ holdQueue : true ,
572+ } ,
573+ ) ;
574+
575+ assert . deepEqual ( yield * threadState ( parentThreadId ) , {
576+ runs : [ "completed" , "interrupted" ] ,
577+ watched : [ ] ,
578+ } ) ;
579+ const parent = yield * orchestrator . getThreadProjection ( parentThreadId ) ;
580+ assert . equal ( parent . subagents [ 0 ] ?. completionDelivery ?. state , "disposed" ) ;
581+ const effects = yield * sql < { readonly effect_type : string } > `
582+ SELECT effect_type FROM orchestration_v2_effect_outbox WHERE command_id = ${ stopCommandId }
583+ ` ;
584+ assert . include (
585+ effects . map ( ( row ) => row . effect_type ) ,
586+ "delegated-tasks.stop" ,
587+ ) ;
588+
589+ yield * threads . stopDelegatedTasks ( { threadId : parentThreadId , commandId : stopCommandId } ) ;
590+ assert . deepEqual ( yield * threadState ( childThreadId ) , {
591+ runs : [ "interrupted" , "queued:held" ] ,
592+ watched : [ ] ,
593+ } ) ;
594+ assert . deepEqual ( ( yield * threadState ( grandchildThreadId ) ) . runs , [ "interrupted" ] ) ;
595+
596+ yield * threads . stopDelegatedTasks ( { threadId : parentThreadId , commandId : stopCommandId } ) ;
597+ assert . deepEqual ( ( yield * threadState ( childThreadId ) ) . runs , [ "interrupted" , "queued:held" ] ) ;
598+ // The walk stops each level itself, so the child stops it sends queue no walk of their own.
599+ const walks = yield * sql < { readonly command_id : string } > `
600+ SELECT command_id FROM orchestration_v2_effect_outbox
601+ WHERE effect_type = 'delegated-tasks.stop'
602+ ` ;
603+ assert . deepEqual (
604+ walks . map ( ( row ) => row . command_id ) ,
605+ [ stopCommandId ] ,
606+ ) ;
607+ } ) . pipe ( Effect . provide ( layerTest ) ) ,
608+ ) ;
609+
610+ it . effect ( "a repeated Stop that changes nothing still stops the delegated tasks" , ( ) =>
534611 Effect . gen ( function * ( ) {
535612 const orchestrator = yield * Orchestrator . OrchestratorV2 ;
536613 const projections = yield * ProjectionStore . ProjectionStoreV2 ;
537- const threads = yield * ThreadManagementService . ThreadManagementService ;
538614 const sql = yield * SqlClient . SqlClient ;
539- const parentThreadId = ThreadId . make ( "thread:stop-parent " ) ;
615+ const parentThreadId = ThreadId . make ( "thread:stop-again " ) ;
540616 yield * createWatchingThread ( parentThreadId , 1 ) ;
541617 yield * send ( parentThreadId , "first" , "start_immediately" ) ;
542- const childThreadId = yield * delegate ( parentThreadId , "child task" ) ;
543- assert . isTrue ( Exit . isFailure ( yield * Effect . exit ( watch ( childThreadId , 2 ) ) ) ) ;
544- const grandchildThreadId = yield * delegate ( childThreadId , "grandchild task" ) ;
545- yield * send ( childThreadId , "child follow-up" , "queue_after_active" ) ;
546-
618+ yield * delegate ( parentThreadId , "stop-again child" ) ;
547619 const now = yield * DateTime . now ;
548- const firstRun = ( yield * orchestrator . getThreadProjection ( parentThreadId ) ) . runs [ 0 ] ! ;
620+ const run = ( yield * orchestrator . getThreadProjection ( parentThreadId ) ) . runs [ 0 ] ! ;
549621 yield * projections . apply ( {
550- id : EventId . make ( "event:stop-parent:first- run-completed" ) ,
622+ id : EventId . make ( "event:stop-again: run-completed" ) ,
551623 type : "run.updated" ,
552624 threadId : parentThreadId ,
553- runId : firstRun . id ,
625+ runId : run . id ,
554626 occurredAt : now ,
555- payload : { ...firstRun , status : "completed" , completedAt : now } ,
627+ payload : { ...run , status : "completed" , completedAt : now } ,
556628 } ) ;
557- yield * send ( parentThreadId , "second" , "start_immediately" ) ;
558- const secondRun = ( yield * orchestrator . getThreadProjection ( parentThreadId ) ) . runs . at ( - 1 ) ! ;
559-
560- const stopCommandId = CommandId . make ( "stop-parent" ) ;
561629 yield * orchestrator . dispatch ( {
562- type : "run.interrupt " ,
563- commandId : stopCommandId ,
630+ type : "thread.stop " ,
631+ commandId : CommandId . make ( "stop-again:first" ) ,
564632 threadId : parentThreadId ,
565- runId : secondRun . id ,
566- holdQueue : true ,
567633 } ) ;
568634
569- assert . deepEqual ( yield * threadState ( parentThreadId ) , {
570- runs : [ "completed" , "interrupted" ] ,
571- watched : [ ] ,
635+ // The first Stop already disposed the delivery and ended the watch, so this one writes no
636+ // events, like Stop on a thread an older server stopped without stopping its children.
637+ const again = yield * orchestrator . dispatch ( {
638+ type : "thread.stop" ,
639+ commandId : CommandId . make ( "stop-again:second" ) ,
640+ threadId : parentThreadId ,
572641 } ) ;
573- const parent = yield * orchestrator . getThreadProjection ( parentThreadId ) ;
574- assert . equal ( parent . subagents [ 0 ] ?. completionDelivery ?. state , "disposed" ) ;
575- const effects = yield * sql < { readonly effect_type : string } > `
576- SELECT effect_type FROM orchestration_v2_effect_outbox WHERE command_id = ${ stopCommandId }
642+ assert . lengthOf ( again . storedEvents , 0 ) ;
643+ const walks = yield * sql < { readonly command_id : string } > `
644+ SELECT command_id FROM orchestration_v2_effect_outbox
645+ WHERE effect_type = 'delegated-tasks.stop'
577646 ` ;
578- assert . include (
579- effects . map ( ( row ) => row . effect_type ) ,
580- "delegated-tasks. stop" ,
647+ assert . deepEqual (
648+ walks . map ( ( row ) => row . command_id ) ,
649+ [ "stop-again:first" , " stop-again:second" ] ,
581650 ) ;
582-
583- yield * threads . stopDelegatedTasks ( { threadId : parentThreadId , commandId : stopCommandId } ) ;
584- assert . deepEqual ( yield * threadState ( childThreadId ) , {
585- runs : [ "interrupted" , "queued:held" ] ,
586- watched : [ ] ,
587- } ) ;
588- assert . deepEqual ( ( yield * threadState ( grandchildThreadId ) ) . runs , [ "interrupted" ] ) ;
589-
590- yield * threads . stopDelegatedTasks ( { threadId : parentThreadId , commandId : stopCommandId } ) ;
591- assert . deepEqual ( ( yield * threadState ( childThreadId ) ) . runs , [ "interrupted" , "queued:held" ] ) ;
592651 } ) . pipe ( Effect . provide ( layerTest ) ) ,
593652) ;
594653
0 commit comments