@@ -37,6 +37,7 @@ import * as Schema from "effect/Schema";
3737import * as TestClock from "effect/testing/TestClock" ;
3838
3939import * as ThreadLaunchService from "../orchestration-v2/ThreadLaunchService.ts" ;
40+ import * as Orchestrator from "../orchestration-v2/Orchestrator.ts" ;
4041import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts" ;
4142import * as SecretRequests from "../secrets/SecretRequests.ts" ;
4243import * as SqlitePersistence from "../persistence/Sqlite.ts" ;
@@ -2876,6 +2877,78 @@ it.effect("the domain-event reactor pauses tasks on thread archive and delete",
28762877 } ) . pipe ( Effect . provide ( SqlitePersistence . layerMemory ) ) ,
28772878) ;
28782879
2880+ it . effect ( "the domain-event reactor resumes after its event stream fails" , ( ) =>
2881+ Effect . gen ( function * ( ) {
2882+ yield * Effect . scoped (
2883+ Effect . gen ( function * ( ) {
2884+ const threadId = ThreadId . make ( "thread:reactor-resumes" ) ;
2885+ const firstFailed = yield * Deferred . make < void > ( ) ;
2886+ const cursors : Array < number | undefined > = [ ] ;
2887+ // The first subscription fails like a transient read error. The resumed
2888+ // one delivers the archive committed after the reactor's cursor.
2889+ const threadManagement = Layer . mock ( ThreadManagementService . ThreadManagementService ) ( {
2890+ streamStoredEventsFrom : ( input ) =>
2891+ Stream . unwrap (
2892+ Effect . gen ( function * ( ) {
2893+ cursors . push ( input ?. afterSequence ) ;
2894+ if ( cursors . length === 1 ) {
2895+ yield * Deferred . succeed ( firstFailed , undefined ) ;
2896+ return Stream . fail (
2897+ new Orchestrator . OrchestratorDomainEventStreamError ( {
2898+ cause : "database is locked" ,
2899+ } ) ,
2900+ ) ;
2901+ }
2902+ return Stream . make ( {
2903+ sequence : ( input ?. afterSequence ?? 0 ) + 1 ,
2904+ commandId : null ,
2905+ event : {
2906+ id : EventId . make ( "event:reactor-resumes" ) ,
2907+ type : "thread.archived" ,
2908+ threadId,
2909+ providerInstanceId : ProviderInstanceId . make ( "codex" ) ,
2910+ occurredAt : DateTime . makeUnsafe ( boundThreadArchivedAt ) ,
2911+ payload : { id : threadId } ,
2912+ } as unknown as OrchestrationV2DomainEvent ,
2913+ } satisfies OrchestrationV2StoredEvent ) . pipe ( Stream . concat ( Stream . never ) ) ;
2914+ } ) ,
2915+ ) ,
2916+ } ) ;
2917+ const context = yield * Layer . build (
2918+ ScheduledTaskService . layer . pipe (
2919+ Layer . provide (
2920+ Layer . mergeAll (
2921+ NodeCrypto . layer ,
2922+ Scheduler . layer ,
2923+ Layer . mock ( ThreadLaunchService . ThreadLaunchService ) ( { } ) ,
2924+ Layer . mock ( SecretRequests . SecretRequests ) ( { } ) ,
2925+ threadManagement ,
2926+ ) ,
2927+ ) ,
2928+ ) ,
2929+ ) ;
2930+ const tasks = Context . get ( context , ScheduledTaskService . ScheduledTaskService ) ;
2931+ yield * setBoundThreadState ( threadId , "active" ) ;
2932+ const task = yield * tasks . upsert ( { ...boundTaskInput , enabled : true , threadId } ) ;
2933+ yield * setBoundThreadState ( threadId , "archived" ) ;
2934+ yield * Deferred . await ( firstFailed ) ;
2935+
2936+ // Well before the task is due, so its fire-time check cannot pause it.
2937+ yield * TestClock . adjust ( "10 seconds" ) ;
2938+
2939+ const paused = yield * tasks . subscribeList ( ) . pipe (
2940+ Stream . map ( ( { tasks : all } ) => all . find ( ( candidate ) => candidate . id === task . task . id ) ) ,
2941+ Stream . filter ( ( candidate ) => candidate !== undefined && candidate . enabled === false ) ,
2942+ Stream . runHead ,
2943+ ) ;
2944+ assert . isTrue ( Option . isSome ( paused ) ) ;
2945+ assert . equal ( cursors . length , 2 ) ;
2946+ assert . equal ( cursors [ 1 ] , cursors [ 0 ] ) ;
2947+ } ) ,
2948+ ) ;
2949+ } ) . pipe ( Effect . provide ( SqlitePersistence . layerMemory ) ) ,
2950+ ) ;
2951+
28792952it . effect ( "replays an archive committed after the startup sweep checked its thread" , ( ) =>
28802953 Effect . gen ( function * ( ) {
28812954 yield * Effect . scoped (
0 commit comments