@@ -20,6 +20,7 @@ import * as DateTime from "effect/DateTime";
2020import * as Effect from "effect/Effect" ;
2121import * as Exit from "effect/Exit" ;
2222import * as Layer from "effect/Layer" ;
23+ import * as Option from "effect/Option" ;
2324import * as Schema from "effect/Schema" ;
2425import * as SqlClient from "effect/sql/SqlClient" ;
2526import * as Stream from "effect/Stream" ;
@@ -483,6 +484,76 @@ it.effect("Stop ends watches, holds queues, and stops the delegated tasks under
483484 } ) . pipe ( Effect . provide ( layerTest ) ) ,
484485) ;
485486
487+ it . effect . each ( [ 1 , 2 , 8 ] ) (
488+ "Stop visits each descendant once with %s historical tasks per child" ,
489+ ( tasksPerChild ) =>
490+ Effect . gen ( function * ( ) {
491+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
492+ const projections = yield * ProjectionStore . ProjectionStoreV2 ;
493+ const receipts = yield * CommandReceiptStore . CommandReceiptStoreV2 ;
494+ const rootThreadId = ThreadId . make ( "thread:stop-history" ) ;
495+ yield * createWatchingThread ( rootThreadId , 30 ) ;
496+ yield * send ( rootThreadId , "work" , "start_immediately" ) ;
497+ const descendants : Array < ThreadId > = [ ] ;
498+ let parentThreadId = rootThreadId ;
499+ for ( let depth = 0 ; depth < 3 ; depth ++ ) {
500+ const childThreadId = yield * delegate ( parentThreadId , `history child ${ depth } ` ) ;
501+ const task = ( yield * orchestrator . getThreadProjection ( parentThreadId ) ) . subagents [ 0 ] ! ;
502+ // Published tasks and later follow-ups all retain the same child thread.
503+ for ( let history = 1 ; history < tasksPerChild ; history ++ ) {
504+ const taskId = NodeId . make ( `node:stop-history:${ depth } :${ history } ` ) ;
505+ yield * projections . apply ( {
506+ id : EventId . make ( `event:stop-history:${ depth } :${ history } ` ) ,
507+ type : "subagent.updated" ,
508+ threadId : parentThreadId ,
509+ runId : task . runId ! ,
510+ nodeId : taskId ,
511+ occurredAt : yield * DateTime . now ,
512+ payload : { ...task , id : taskId , status : "completed" } ,
513+ } ) ;
514+ }
515+ descendants . push ( childThreadId ) ;
516+ parentThreadId = childThreadId ;
517+ }
518+
519+ const stopped : Array < ThreadId > = [ ] ;
520+ const read : Array < ThreadId > = [ ] ;
521+ const measuredOrchestrator = Orchestrator . OrchestratorV2 . of ( {
522+ ...orchestrator ,
523+ dispatch : ( command ) =>
524+ Effect . sync ( ( ) => {
525+ if ( command . type === "thread.stop" ) stopped . push ( command . threadId ) ;
526+ } ) . pipe ( Effect . andThen ( orchestrator . dispatch ( command ) ) ) ,
527+ getThreadRecords : ( threadId , fields , filter ) =>
528+ Effect . sync ( ( ) => read . push ( threadId ) ) . pipe (
529+ Effect . andThen ( orchestrator . getThreadRecords ( threadId , fields , filter ) ) ,
530+ ) ,
531+ } ) ;
532+ const commandId = CommandId . make ( "stop-history" ) ;
533+ yield * Effect . gen ( function * ( ) {
534+ const threads = yield * ThreadManagementService . ThreadManagementService ;
535+ yield * threads . stopDelegatedTasks ( { threadId : rootThreadId , commandId } ) ;
536+ } ) . pipe (
537+ Effect . provide (
538+ Layer . fresh ( ThreadManagementService . layer ) . pipe (
539+ Layer . provide ( Layer . succeed ( Orchestrator . OrchestratorV2 , measuredOrchestrator ) ) ,
540+ ) ,
541+ ) ,
542+ ) ;
543+
544+ // Check real persisted state and receipts before asserting traversal cost.
545+ for ( const threadId of descendants ) {
546+ assert . deepEqual ( ( yield * threadState ( threadId ) ) . runs , [ "interrupted" ] ) ;
547+ const receipt = yield * receipts . getByCommandId (
548+ CommandId . make ( `${ commandId } :stop:${ threadId } ` ) ,
549+ ) ;
550+ assert . isTrue ( Option . isSome ( receipt ) ) ;
551+ }
552+ assert . deepEqual ( stopped , descendants ) ;
553+ assert . deepEqual ( read , [ rootThreadId , ...descendants ] ) ;
554+ } ) . pipe ( Effect . provide ( layerTest ) ) ,
555+ ) ;
556+
486557it . effect . each ( [ "thread.stop" , "run.interrupt" ] as const ) (
487558 "%s cancels buffered wakes and preserves durable queued messages" ,
488559 ( type ) =>
0 commit comments