@@ -331,6 +331,7 @@ const makeProviderAdapter = Effect.fnUntraced(function* (
331331 } ) => Effect . Effect < void > ;
332332 readonly hasPendingBackgroundWork ?: Effect . Effect < boolean > ;
333333 readonly credentialRevision ?: Ref . Ref < number > ;
334+ readonly beforeCredentialProbe ?: Effect . Effect < void > ;
334335 readonly hasPendingBackgroundWorkForThread ?: Effect . Effect < boolean > ;
335336 readonly backgroundTasksEndWithProcess ?: boolean ;
336337 readonly hangSessionScopeClose ?: boolean ;
@@ -425,7 +426,8 @@ const makeProviderAdapter = Effect.fnUntraced(function* (
425426 ...( credentialRevision === undefined
426427 ? { }
427428 : {
428- hasStaleCredentials : Ref . get ( credentialRevision ) . pipe (
429+ hasStaleCredentials : ( options . beforeCredentialProbe ?? Effect . void ) . pipe (
430+ Effect . andThen ( Ref . get ( credentialRevision ) ) ,
429431 Effect . map ( ( revision ) => revision !== launchedRevision ) ,
430432 ) ,
431433 } ) ,
@@ -491,6 +493,7 @@ function layerTest(input: {
491493 } ;
492494 readonly hasPendingBackgroundWork ?: Effect . Effect < boolean > ;
493495 readonly credentialRevision ?: Ref . Ref < number > ;
496+ readonly beforeCredentialProbe ?: Effect . Effect < void > ;
494497 readonly hasPendingBackgroundWorkForThread ?: Effect . Effect < boolean > ;
495498 readonly backgroundTasksEndWithProcess ?: boolean ;
496499 readonly hangSessionScopeClose ?: boolean ;
@@ -523,6 +526,9 @@ function layerTest(input: {
523526 ...( input . credentialRevision === undefined
524527 ? { }
525528 : { credentialRevision : input . credentialRevision } ) ,
529+ ...( input . beforeCredentialProbe === undefined
530+ ? { }
531+ : { beforeCredentialProbe : input . beforeCredentialProbe } ) ,
526532 ...( input . hasPendingBackgroundWorkForThread === undefined
527533 ? { }
528534 : { hasPendingBackgroundWorkForThread : input . hasPendingBackgroundWorkForThread } ) ,
@@ -3041,6 +3047,8 @@ it.effect(
30413047 Effect . gen ( function * ( ) {
30423048 const state = yield * Ref . make ( emptyState ) ;
30433049 const credentialRevision = yield * Ref . make ( 1 ) ;
3050+ const probeEntered = yield * Deferred . make < void > ( ) ;
3051+ const probeGate = yield * Deferred . make < void > ( ) ;
30443052 yield * Effect . gen ( function * ( ) {
30453053 const eventSink = yield * EventSink . EventSinkV2 ;
30463054 const idAllocator = yield * IdAllocator . IdAllocatorV2 ;
@@ -3128,7 +3136,25 @@ it.effect(
31283136 yield * Fiber . join ( settled ) ;
31293137
31303138 // Renewal currently takes precedence over background commands after the turn settles.
3131- const replacement = yield * open ( siblingId ) ;
3139+ const reopening = yield * open ( siblingId ) . pipe ( Effect . forkScoped ) ;
3140+ yield * Deferred . await ( probeEntered ) ;
3141+ const activitySubscription = yield * Effect . acquireRelease (
3142+ first . subscribeEvents ! ,
3143+ ( sub ) => sub . close ,
3144+ ) ;
3145+ const activityProcessed = yield * activitySubscription . events . pipe (
3146+ Stream . filter ( ( event ) => event . type === "provider_session.updated" ) ,
3147+ Stream . runHead ,
3148+ Effect . forkScoped ,
3149+ ) ;
3150+ yield * Queue . offer ( ( yield * Ref . get ( state ) ) . eventQueues . get ( String ( providerSessionId ) ) ! , {
3151+ type : "provider_session.updated" ,
3152+ driver : CODEX_DRIVER ,
3153+ providerSession : first . providerSession ,
3154+ } ) ;
3155+ assert . isTrue ( Option . isSome ( yield * Fiber . join ( activityProcessed ) ) ) ;
3156+ yield * Deferred . succeed ( probeGate , undefined ) ;
3157+ const replacement = yield * Fiber . join ( reopening ) ;
31323158 assert . notStrictEqual ( replacement , first ) ;
31333159 assert . equal ( ( yield * Ref . get ( state ) ) . openCount , 2 ) ;
31343160 assert . equal ( ( yield * Ref . get ( state ) ) . closeCount , 1 ) ;
@@ -3154,6 +3180,11 @@ it.effect(
31543180 state,
31553181 idleTimeoutMs : 60_000 ,
31563182 credentialRevision,
3183+ beforeCredentialProbe : Effect . gen ( function * ( ) {
3184+ if ( ( yield * Ref . get ( credentialRevision ) ) === 1 ) return ;
3185+ yield * Deferred . succeed ( probeEntered , undefined ) ;
3186+ yield * Deferred . await ( probeGate ) ;
3187+ } ) ,
31573188 hasPendingBackgroundWork : Effect . succeed ( true ) ,
31583189 } ) ,
31593190 ) ,
0 commit comments