@@ -434,6 +434,7 @@ const makeProviderAdapter = Effect.fnUntraced(function* (
434434 } ) => Effect . Effect < void > ;
435435 readonly hasPendingBackgroundWork ?: Effect . Effect < boolean > ;
436436 readonly hasPendingBackgroundWorkForThread ?: Effect . Effect < boolean > ;
437+ readonly hasScheduledWork ?: Effect . Effect < boolean > ;
437438 readonly backgroundTasksEndWithProcess ?: boolean ;
438439 readonly hangSessionScopeClose ?: boolean ;
439440 readonly startTurn ?: Effect . Effect < void > ;
@@ -533,6 +534,9 @@ const makeProviderAdapter = Effect.fnUntraced(function* (
533534 ...( options . hasPendingBackgroundWork === undefined
534535 ? { }
535536 : { hasPendingBackgroundWork : options . hasPendingBackgroundWork } ) ,
537+ ...( options . hasScheduledWork === undefined
538+ ? { }
539+ : { hasScheduledWork : options . hasScheduledWork } ) ,
536540 ...( options . hasPendingBackgroundWorkForThread === undefined
537541 ? { }
538542 : {
@@ -596,6 +600,7 @@ function layerTest(input: {
596600 } ;
597601 readonly hasPendingBackgroundWork ?: Effect . Effect < boolean > ;
598602 readonly hasPendingBackgroundWorkForThread ?: Effect . Effect < boolean > ;
603+ readonly hasScheduledWork ?: Effect . Effect < boolean > ;
599604 readonly backgroundTasksEndWithProcess ?: boolean ;
600605 readonly hangSessionScopeClose ?: boolean ;
601606 readonly startTurn ?: Effect . Effect < void > ;
@@ -628,6 +633,7 @@ function layerTest(input: {
628633 ...( input . hasPendingBackgroundWork === undefined
629634 ? { }
630635 : { hasPendingBackgroundWork : input . hasPendingBackgroundWork } ) ,
636+ ...( input . hasScheduledWork === undefined ? { } : { hasScheduledWork : input . hasScheduledWork } ) ,
631637 ...( input . hasPendingBackgroundWorkForThread === undefined
632638 ? { }
633639 : { hasPendingBackgroundWorkForThread : input . hasPendingBackgroundWorkForThread } ) ,
@@ -2952,6 +2958,52 @@ it.effect("ProviderSessionManagerV2 keeps a provider update that lands while rel
29522958 } ) ,
29532959) ;
29542960
2961+ it . effect ( "ProviderSessionManagerV2 keeps an idle session alive while it has scheduled work" , ( ) =>
2962+ Effect . gen ( function * ( ) {
2963+ const state = yield * Ref . make ( emptyState ) ;
2964+ const scheduled = yield * Ref . make ( true ) ;
2965+ yield * Effect . gen ( function * ( ) {
2966+ const eventSink = yield * EventSink . EventSinkV2 ;
2967+ const idAllocator = yield * IdAllocator . IdAllocatorV2 ;
2968+ const manager = yield * ProviderSessionManager . ProviderSessionManagerV2 ;
2969+ const projectId = yield * idAllocator . allocate . project ( {
2970+ fixtureName : "provider-session-manager-scheduled-work" ,
2971+ } ) ;
2972+ const threadId = yield * idAllocator . allocate . thread ( {
2973+ fixtureName : "provider-session-manager-scheduled-work" ,
2974+ projectId,
2975+ } ) ;
2976+ const providerSessionId = yield * idAllocator . allocate . providerSession ( {
2977+ providerInstanceId : modelSelection . instanceId ,
2978+ threadId,
2979+ } ) ;
2980+ yield * eventSink . write ( {
2981+ events : [
2982+ yield * makeThreadCreatedEvent ( { idAllocator, threadId, now : yield * DateTime . now } ) ,
2983+ ] ,
2984+ } ) ;
2985+ const runtime = yield * manager . open ( {
2986+ threadId,
2987+ providerSessionId,
2988+ modelSelection,
2989+ runtimePolicy,
2990+ } ) ;
2991+ yield * runtime . events . pipe ( Stream . runDrain , Effect . forkScoped ) ;
2992+
2993+ yield * TestClock . adjust ( "1 second" ) ;
2994+ assert . isTrue ( yield * manager . isLive ( providerSessionId ) ) ;
2995+
2996+ yield * Ref . set ( scheduled , false ) ;
2997+ yield * TestClock . adjust ( "1 second" ) ;
2998+ assert . isFalse ( yield * manager . isLive ( providerSessionId ) ) ;
2999+ } ) . pipe (
3000+ Effect . provide (
3001+ layerTest ( { state, idleTimeoutMs : 1000 , hasScheduledWork : Ref . get ( scheduled ) } ) ,
3002+ ) ,
3003+ ) ;
3004+ } ) ,
3005+ ) ;
3006+
29553007it . effect (
29563008 "ProviderSessionManagerV2 does not idle-release a session that turns busy during the pending-work check" ,
29573009 ( ) =>
0 commit comments