@@ -336,7 +336,8 @@ const makeProviderAdapter = Effect.fnUntraced(function* (
336336 readonly hasPendingBackgroundWorkForThread ?: Effect . Effect < boolean > ;
337337 /** False models a runtime whose background tasks can outlive its process. */
338338 readonly backgroundTasksEndWithProcess ?: boolean ;
339- readonly hangSessionScopeClose ?: boolean ;
339+ /** "first" wedges only the first session opened. */
340+ readonly hangSessionScopeClose ?: boolean | "first" ;
340341 readonly startTurn ?: Effect . Effect < void > ;
341342 readonly beforeUnload ?: Effect . Effect < void > ;
342343 /** Registers the process's closeCount finalizer before `beforeOpen` runs. */
@@ -401,7 +402,11 @@ const makeProviderAdapter = Effect.fnUntraced(function* (
401402 } ) ;
402403 if ( options . spawnBeforeOpen !== true ) {
403404 yield * countClose ;
404- if ( options . hangSessionScopeClose === true ) yield * hangClose ;
405+ if (
406+ options . hangSessionScopeClose === true ||
407+ ( options . hangSessionScopeClose === "first" && ( yield * Ref . get ( state ) ) . openCount === 1 )
408+ )
409+ yield * hangClose ;
405410 }
406411
407412 return {
@@ -489,7 +494,7 @@ function layerTest(input: {
489494 readonly hasPendingBackgroundWork ?: Effect . Effect < boolean > ;
490495 readonly hasPendingBackgroundWorkForThread ?: Effect . Effect < boolean > ;
491496 readonly backgroundTasksEndWithProcess ?: boolean ;
492- readonly hangSessionScopeClose ?: boolean ;
497+ readonly hangSessionScopeClose ?: boolean | "first" ;
493498 readonly startTurn ?: Effect . Effect < void > ;
494499 readonly beforeUnload ?: Effect . Effect < void > ;
495500 readonly spawnBeforeOpen ?: boolean ;
@@ -3912,6 +3917,68 @@ it.effect(
39123917 } ) ,
39133918) ;
39143919
3920+ it . effect ( "ProviderSessionManagerV2 leaves a closed replacement's status to it" , ( ) =>
3921+ Effect . gen ( function * ( ) {
3922+ const state = yield * Ref . make ( emptyState ) ;
3923+ const scopeCloseReached = yield * Deferred . make < void > ( ) ;
3924+ yield * Effect . gen ( function * ( ) {
3925+ const eventSink = yield * EventSink . EventSinkV2 ;
3926+ const idAllocator = yield * IdAllocator . IdAllocatorV2 ;
3927+ const manager = yield * ProviderSessionManager . ProviderSessionManagerV2 ;
3928+ const threadId = ThreadId . make ( "thread-provider-session-manager-closed-replacement" ) ;
3929+ const providerSessionId = yield * idAllocator . allocate . providerSession ( {
3930+ providerInstanceId : modelSelection . instanceId ,
3931+ threadId,
3932+ } ) ;
3933+ yield * eventSink . write ( {
3934+ events : [
3935+ yield * makeThreadCreatedEvent ( { idAllocator, threadId, now : yield * DateTime . now } ) ,
3936+ ] ,
3937+ } ) ;
3938+ yield * manager . open ( { threadId, providerSessionId, modelSelection, runtimePolicy } ) ;
3939+
3940+ // The first runtime's events end unexpectedly, and its release stalls in scope close.
3941+ const events = ( yield * Ref . get ( state ) ) . eventQueues . get ( String ( providerSessionId ) ) ;
3942+ assert . isDefined ( events ) ;
3943+ yield * Queue . end ( events ! ) ;
3944+ yield * Deferred . await ( scopeCloseReached ) ;
3945+
3946+ // A replacement opens and closes meanwhile.
3947+ yield * manager . open ( { threadId, providerSessionId, modelSelection, runtimePolicy } ) ;
3948+ yield * manager . close ( providerSessionId ) ;
3949+ const statuses = yield * eventSink
3950+ . stream ( { threadId, afterSequence : yield * eventSink . latestSequence ( { threadId } ) } )
3951+ . pipe (
3952+ Stream . filter (
3953+ ( { event } ) =>
3954+ event . type === "provider-session.updated" ||
3955+ event . type === "provider-session.attached" ,
3956+ ) ,
3957+ Stream . takeUntil ( ( { event } ) => event . type === "provider-session.attached" ) ,
3958+ Stream . map ( ( { event } ) => event . type ) ,
3959+ Stream . runCollect ,
3960+ Effect . forkScoped ,
3961+ ) ;
3962+
3963+ // The stalled close ends; the next open waits on the open lock until the
3964+ // first release has written its records.
3965+ yield * TestClock . adjust ( "30 seconds" ) ;
3966+ yield * manager . open ( { threadId, providerSessionId, modelSelection, runtimePolicy } ) ;
3967+
3968+ assert . deepEqual ( Array . from ( yield * Fiber . join ( statuses ) ) , [ "provider-session.attached" ] ) ;
3969+ } ) . pipe (
3970+ Effect . provide (
3971+ layerTest ( {
3972+ state,
3973+ idleTimeoutMs : 60_000 ,
3974+ hangSessionScopeClose : "first" ,
3975+ scopeCloseReached,
3976+ } ) ,
3977+ ) ,
3978+ ) ;
3979+ } ) ,
3980+ ) ;
3981+
39153982it . effect ( "ProviderSessionManagerV2 keeps each failed release's cleanup" , ( ) =>
39163983 Effect . gen ( function * ( ) {
39173984 const state = yield * Ref . make ( emptyState ) ;
0 commit comments