@@ -29,6 +29,7 @@ import * as Schema from "effect/Schema";
2929import * as SqlClient from "effect/sql/SqlClient" ;
3030import * as Stream from "effect/Stream" ;
3131import * as EventStore from "./EventStore.ts" ;
32+ import * as EventSink from "./EventSink.ts" ;
3233import * as TestClock from "effect/testing/TestClock" ;
3334
3435import * as SqlitePersistence from "../persistence/Sqlite.ts" ;
@@ -456,7 +457,7 @@ it.effect.each([
456457it . effect ( "Stop ends watches, holds queues, and stops the delegated tasks under the thread" , ( ) =>
457458 Effect . gen ( function * ( ) {
458459 const orchestrator = yield * Orchestrator . OrchestratorV2 ;
459- const projections = yield * ProjectionStore . ProjectionStoreV2 ;
460+ const sink = yield * EventSink . EventSinkV2 ;
460461 const threads = yield * ThreadManagementService . ThreadManagementService ;
461462 const sql = yield * SqlClient . SqlClient ;
462463 const parentThreadId = ThreadId . make ( "thread:stop-parent" ) ;
@@ -466,16 +467,21 @@ it.effect("Stop ends watches, holds queues, and stops the delegated tasks under
466467 assert . isTrue ( Exit . isFailure ( yield * Effect . exit ( watch ( childThreadId , 2 ) ) ) ) ;
467468 const grandchildThreadId = yield * delegate ( childThreadId , "grandchild task" ) ;
468469 yield * send ( childThreadId , "child follow-up" , "queue_after_active" ) ;
470+ yield * send ( childThreadId , "child queued after follow-up" , "queue_after_active" ) ;
469471
470472 const now = yield * DateTime . now ;
471473 const firstRun = ( yield * orchestrator . getThreadProjection ( parentThreadId ) ) . runs [ 0 ] ! ;
472- yield * projections . apply ( {
473- id : EventId . make ( "event:stop-parent:first-run-completed" ) ,
474- type : "run.updated" ,
475- threadId : parentThreadId ,
476- runId : firstRun . id ,
477- occurredAt : now ,
478- payload : { ...firstRun , status : "completed" , completedAt : now } ,
474+ yield * sink . write ( {
475+ events : [
476+ {
477+ id : EventId . make ( "event:stop-parent:first-run-completed" ) ,
478+ type : "run.updated" ,
479+ threadId : parentThreadId ,
480+ runId : firstRun . id ,
481+ occurredAt : now ,
482+ payload : { ...firstRun , status : "completed" , completedAt : now } ,
483+ } ,
484+ ] ,
479485 } ) ;
480486 yield * send ( parentThreadId , "second" , "start_immediately" ) ;
481487 const secondRun = ( yield * orchestrator . getThreadProjection ( parentThreadId ) ) . runs . at ( - 1 ) ! ;
@@ -503,15 +509,39 @@ it.effect("Stop ends watches, holds queues, and stops the delegated tasks under
503509 "delegated-tasks.stop" ,
504510 ) ;
505511
512+ const childRun = ( yield * orchestrator . getThreadProjection ( childThreadId ) ) . runs [ 0 ] ! ;
513+ yield * sink . write ( {
514+ events : [
515+ {
516+ id : EventId . make ( "event:stop-child:first-run-completed" ) ,
517+ type : "run.updated" ,
518+ threadId : childThreadId ,
519+ runId : childRun . id ,
520+ occurredAt : now ,
521+ payload : { ...childRun , status : "completed" , completedAt : now } ,
522+ } ,
523+ ] ,
524+ } ) ;
525+ yield * orchestrator . dispatch ( {
526+ type : "queue.resume" ,
527+ commandId : CommandId . make ( "resume-child-before-stop-walk" ) ,
528+ threadId : childThreadId ,
529+ } ) ;
530+ assert . deepEqual ( ( yield * threadState ( childThreadId ) ) . runs , [ "completed" , "starting" , "queued" ] ) ;
531+
506532 yield * threads . stopDelegatedTasks ( { threadId : parentThreadId , commandId : stopCommandId } ) ;
507533 assert . deepEqual ( yield * threadState ( childThreadId ) , {
508- runs : [ "interrupted" , "queued:held" ] ,
534+ runs : [ "completed" , " interrupted", "queued:held" ] ,
509535 watched : [ ] ,
510536 } ) ;
511537 assert . deepEqual ( ( yield * threadState ( grandchildThreadId ) ) . runs , [ "interrupted" ] ) ;
512538
513539 yield * threads . stopDelegatedTasks ( { threadId : parentThreadId , commandId : stopCommandId } ) ;
514- assert . deepEqual ( ( yield * threadState ( childThreadId ) ) . runs , [ "interrupted" , "queued:held" ] ) ;
540+ assert . deepEqual ( ( yield * threadState ( childThreadId ) ) . runs , [
541+ "completed" ,
542+ "interrupted" ,
543+ "queued:held" ,
544+ ] ) ;
515545
516546 yield * send ( parentThreadId , "new work" , "start_immediately" ) ;
517547 const newChild = yield * delegate ( parentThreadId , "new task after Stop" ) ;
@@ -1408,13 +1438,25 @@ it.effect("an MCP Stop replays its original result after the database and servic
14081438 const idleProjection = yield * ( yield * Orchestrator . OrchestratorV2 ) . getThreadProjection ( idle ) ;
14091439 const idleRun = idleProjection . runs [ 0 ] ! ;
14101440 const now = yield * DateTime . now ;
1411- yield * ( yield * ProjectionStore . ProjectionStoreV2 ) . apply ( {
1412- id : EventId . make ( "event:mcp-stop-idle:completed" ) ,
1413- type : "run.updated" ,
1414- threadId : idle ,
1415- runId : idleRun . id ,
1416- occurredAt : now ,
1417- payload : { ...idleRun , status : "completed" , completedAt : now } ,
1441+ yield * ( yield * EventSink . EventSinkV2 ) . write ( {
1442+ events : [
1443+ {
1444+ id : EventId . make ( "event:mcp-stop-idle:completed" ) ,
1445+ type : "run.updated" ,
1446+ threadId : idle ,
1447+ runId : idleRun . id ,
1448+ occurredAt : now ,
1449+ payload : { ...idleRun , status : "completed" , completedAt : now } ,
1450+ } ,
1451+ {
1452+ id : EventId . make ( "event:mcp-stop-idle:held" ) ,
1453+ type : "run.updated" ,
1454+ threadId : idle ,
1455+ runId : idleProjection . runs [ 1 ] ! . id ,
1456+ occurredAt : now ,
1457+ payload : { ...idleProjection . runs [ 1 ] ! , queueHeld : true } ,
1458+ } ,
1459+ ] ,
14181460 } ) ;
14191461 assert . deepEqual (
14201462 yield * mcp . interruptThread ( scope , {
@@ -1438,7 +1480,27 @@ it.effect("an MCP Stop replays its original result after the database and servic
14381480 yield * delegate ( childThreadId , "original grandchild" ) ;
14391481 const stopped = yield * mcp . interruptThread ( scope , stop ) ;
14401482 assert . equal ( stopped . runId , sent . runId ) ;
1441- return { sent, stopped } ;
1483+ yield * send ( childThreadId , "child work after parent stopped" , "start_immediately" ) ;
1484+ const store = yield * EventStore . EventStoreV2 ;
1485+ const childSequence = yield * store . latestSequence ( { threadId : childThreadId } ) ;
1486+ const noopStop = { ...stop , clientRequestId : "already-stopped-parent" } ;
1487+ const noopStopped = yield * mcp . interruptThread ( scope , noopStop ) ;
1488+ assert . deepEqual ( noopStopped , { threadId, runId : null , status : "no_active_run" } ) ;
1489+ assert . deepEqual ( ( yield * threadState ( childThreadId ) ) . runs , [ "interrupted" , "interrupted" ] ) ;
1490+ const sql = yield * SqlClient . SqlClient ;
1491+ const [ noopReceipt ] = yield * sql < { command_id : string ; result_sequence : number } > `
1492+ SELECT command_id, result_sequence FROM orchestration_command_receipts
1493+ WHERE aggregate_id = ${ threadId } AND command_type = 'thread.stop'
1494+ ORDER BY result_sequence DESC LIMIT 1
1495+ ` ;
1496+ assert . isAtLeast ( noopReceipt ! . result_sequence , childSequence ) ;
1497+ assert . deepEqual (
1498+ yield * store
1499+ . readByCommandId ( { commandId : CommandId . make ( noopReceipt ! . command_id ) } )
1500+ . pipe ( Stream . runCollect ) ,
1501+ [ ] ,
1502+ ) ;
1503+ return { sent, stopped, childThreadId, noopStop, noopStopped } ;
14421504 } ) . pipe ( Effect . provide ( layerMcp ( database ) . pipe ( Layer . fresh ) ) ) ;
14431505
14441506 yield * Effect . gen ( function * ( ) {
@@ -1453,6 +1515,13 @@ it.effect("an MCP Stop replays its original result after the database and servic
14531515 assert . equal ( ( yield * orchestrator . getThreadProjection ( threadId ) ) . runs . length , 1 ) ;
14541516 yield * send ( threadId , "after restart" , "start_immediately" ) ;
14551517 const newChild = yield * delegate ( threadId , "child after restart" ) ;
1518+ yield * send ( first . childThreadId , "child work after restart" , "start_immediately" ) ;
1519+ assert . deepEqual ( yield * mcp . interruptThread ( scope , first . noopStop ) , first . noopStopped ) ;
1520+ assert . deepEqual ( ( yield * threadState ( first . childThreadId ) ) . runs , [
1521+ "interrupted" ,
1522+ "interrupted" ,
1523+ "starting" ,
1524+ ] ) ;
14561525 assert . deepEqual ( yield * mcp . interruptThread ( scope , stop ) , first . stopped ) ;
14571526 assert . deepEqual ( ( yield * threadState ( threadId ) ) . runs , [ "interrupted" , "starting" ] ) ;
14581527 assert . deepEqual ( ( yield * threadState ( newChild ) ) . runs , [ "starting" ] ) ;
0 commit comments