@@ -452,6 +452,125 @@ function makeProviderServiceLayer(
452452 } ;
453453}
454454
455+ for ( const [ enabled , completed ] of [
456+ [ false , false ] ,
457+ [ true , false ] ,
458+ [ true , true ] ,
459+ ] as const ) {
460+ it . effect (
461+ `persists shutdown recovery before stopping providers when enabled=${ enabled } , completed=${ completed } ` ,
462+ ( ) =>
463+ Effect . gen ( function * ( ) {
464+ const codex = makeFakeCodexAdapter ( ) ;
465+ const persistence = yield * Layer . build (
466+ ProviderSessionDirectoryLive . pipe (
467+ Layer . provide (
468+ ProviderSessionRuntime . layer . pipe ( Layer . provide ( SqlitePersistenceMemory ) ) ,
469+ ) ,
470+ ) ,
471+ ) ;
472+ const directory = yield * ProviderSessionDirectory . ProviderSessionDirectory . pipe (
473+ Effect . provide ( persistence ) ,
474+ ) ;
475+ const threadId = asThreadId ( "shutdown-recovery" ) ;
476+ const turnId = asTurnId ( "shutdown-recovery-turn" ) ;
477+ const scope = yield * Scope . make ( ) ;
478+ const services = yield * Layer . build (
479+ makeProviderServiceLive ( ) . pipe (
480+ Layer . provide (
481+ Layer . succeed ( ProviderSessionDirectory . ProviderSessionDirectory , directory ) ,
482+ ) ,
483+ Layer . provide (
484+ Layer . succeed (
485+ ProviderAdapterRegistry . ProviderAdapterRegistry ,
486+ makeStaticInstanceRegistry ( [ [ codexInstanceId , codex . adapter ] ] ) ,
487+ ) ,
488+ ) ,
489+ Layer . provide ( ServerSettings . layerTest ( { continueThreadsAfterServerUpdate : enabled } ) ) ,
490+ Layer . provide ( serverConfigTestLayer ) ,
491+ Layer . provide ( AnalyticsService . layerTest ) ,
492+ Layer . provide (
493+ Layer . succeed (
494+ ProviderEventLoggers . ProviderEventLoggers ,
495+ ProviderEventLoggers . NoOpProviderEventLoggers ,
496+ ) ,
497+ ) ,
498+ ) ,
499+ ) . pipe ( Scope . provide ( scope ) ) ;
500+ const provider = yield * ProviderService . ProviderService . pipe ( Effect . provide ( services ) ) ;
501+ const session = yield * provider . startSession ( threadId , {
502+ provider : CODEX_DRIVER ,
503+ providerInstanceId : codexInstanceId ,
504+ threadId,
505+ runtimeMode : "full-access" ,
506+ } ) ;
507+ codex . listSessions . mockReturnValue (
508+ Effect . succeed ( [
509+ {
510+ ...session ,
511+ status : completed ? "ready" : "running" ,
512+ activeTurnId : completed ? undefined : turnId ,
513+ } ,
514+ ] ) ,
515+ ) ;
516+ const pending = yield * directory . getBinding ( threadId ) ;
517+ assert ( Option . isSome ( pending ) ) ;
518+ yield * directory . upsert ( {
519+ ...pending . value ,
520+ runtimePayload : { activeTurnId : null , continueAfterServerUpdate : turnId } ,
521+ } ) ;
522+ const accepted = yield * provider . sendTurn ( { threadId, continuation : true } ) ;
523+ const admitted = yield * directory . getBinding ( threadId ) ;
524+ assert ( Option . isSome ( admitted ) ) ;
525+ assert . propertyVal ( admitted . value . runtimePayload , "activeTurnId" , accepted . turnId ) ;
526+ assert . propertyVal ( admitted . value . runtimePayload , "continueAfterServerUpdate" , null ) ;
527+ if ( completed ) {
528+ // Updates can mark an already-admitted turn immediately before it finishes.
529+ yield * directory . upsert ( {
530+ ...admitted . value ,
531+ runtimePayload : {
532+ continueAfterServerUpdate : accepted . turnId ,
533+ continueAfterServerUpdatePrepared : null ,
534+ } ,
535+ } ) ;
536+ }
537+ const markers : unknown [ ] = [ ] ;
538+ codex . stopAll . mockImplementation ( ( ) =>
539+ Effect . gen ( function * ( ) {
540+ const binding = yield * directory . getBinding ( threadId ) ;
541+ assert ( Option . isSome ( binding ) ) ;
542+ markers . push ( binding . value . runtimePayload ) ;
543+ } ) . pipe ( Effect . orDie ) ,
544+ ) ;
545+ yield * Scope . close ( scope , Exit . void ) ;
546+ const binding = yield * directory . getBinding ( threadId ) ;
547+ assert ( Option . isSome ( binding ) ) ;
548+ assert . equal ( codex . stopAll . mock . calls . length , 1 ) ;
549+ assert . deepStrictEqual ( binding . value . resumeCursor , session . resumeCursor ) ;
550+ assert . equal ( binding . value . status , "stopped" ) ;
551+ assert . propertyVal ( markers [ 0 ] , "activeTurnId" , completed ? null : turnId ) ;
552+ if ( enabled && ! completed ) {
553+ assert . propertyVal ( markers [ 0 ] , "continueAfterServerUpdate" , turnId ) ;
554+ assert . propertyVal ( binding . value . runtimePayload , "continueAfterServerUpdate" , turnId ) ;
555+ } else if ( completed ) {
556+ assert . propertyVal (
557+ binding . value . runtimePayload ,
558+ "continueAfterServerUpdate" ,
559+ accepted . turnId ,
560+ ) ;
561+ assert . propertyVal (
562+ binding . value . runtimePayload ,
563+ "continueAfterServerUpdatePrepared" ,
564+ null ,
565+ ) ;
566+ } else {
567+ assert . propertyVal ( markers [ 0 ] , "continueAfterServerUpdate" , null ) ;
568+ assert . propertyVal ( binding . value . runtimePayload , "continueAfterServerUpdate" , null ) ;
569+ }
570+ } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
571+ ) ;
572+ }
573+
455574it . effect ( "ProviderServiceLive catches stopAll failures during shutdown" , ( ) =>
456575 Effect . gen ( function * ( ) {
457576 const codex = makeFakeCodexAdapter ( ) ;
0 commit comments