@@ -319,6 +319,8 @@ function makeProviderAdapter(
319319 readonly initialProviderItemIdentityVersion ?: 2 ;
320320 } ) => Effect . Effect < void > ;
321321 readonly hasPendingBackgroundWork ?: Effect . Effect < boolean > ;
322+ /** Each session launches with the current value and is stale once it changes. */
323+ readonly credentialRevision ?: Ref . Ref < number > ;
322324 readonly hangSessionScopeClose ?: boolean ;
323325 readonly startTurn ?: Effect . Effect < void > ;
324326 readonly beforeUnload ?: Effect . Effect < void > ;
@@ -389,6 +391,9 @@ function makeProviderAdapter(
389391 yield * countClose ;
390392 if ( options . hangSessionScopeClose === true ) yield * hangClose ;
391393 }
394+ const credentialRevision = options . credentialRevision ;
395+ const launchedRevision =
396+ credentialRevision === undefined ? undefined : yield * Ref . get ( credentialRevision ) ;
392397
393398 return {
394399 instanceId : ProviderInstanceId . make ( "codex" ) ,
@@ -407,6 +412,13 @@ function makeProviderAdapter(
407412 ...( options . hasPendingBackgroundWork === undefined
408413 ? { }
409414 : { hasPendingBackgroundWork : options . hasPendingBackgroundWork } ) ,
415+ ...( credentialRevision === undefined
416+ ? { }
417+ : {
418+ hasStaleCredentials : Ref . get ( credentialRevision ) . pipe (
419+ Effect . map ( ( revision ) => revision !== launchedRevision ) ,
420+ ) ,
421+ } ) ,
410422 ensureThread : ( ) => unimplemented ( "ensureThread unused in test" ) ,
411423 resumeThread : ( threadInput ) =>
412424 Ref . update ( state , ( current ) => ( {
@@ -463,6 +475,7 @@ function layerTest(input: {
463475 readonly paused : Deferred . Deferred < void > ;
464476 } ;
465477 readonly hasPendingBackgroundWork ?: Effect . Effect < boolean > ;
478+ readonly credentialRevision ?: Ref . Ref < number > ;
466479 readonly hangSessionScopeClose ?: boolean ;
467480 readonly startTurn ?: Effect . Effect < void > ;
468481 readonly beforeUnload ?: Effect . Effect < void > ;
@@ -488,6 +501,9 @@ function layerTest(input: {
488501 ...( input . hasPendingBackgroundWork === undefined
489502 ? { }
490503 : { hasPendingBackgroundWork : input . hasPendingBackgroundWork } ) ,
504+ ...( input . credentialRevision === undefined
505+ ? { }
506+ : { credentialRevision : input . credentialRevision } ) ,
491507 ...( input . hangSessionScopeClose === undefined
492508 ? { }
493509 : { hangSessionScopeClose : input . hangSessionScopeClose } ) ,
@@ -2716,6 +2732,82 @@ it.effect("ProviderSessionManagerV2 defers idle release while background work is
27162732 } ) ,
27172733) ;
27182734
2735+ it . effect ( "ProviderSessionManagerV2 replaces an idle session whose credentials were renewed" , ( ) =>
2736+ Effect . gen ( function * ( ) {
2737+ const state = yield * Ref . make ( emptyState ) ;
2738+ const credentialRevision = yield * Ref . make ( 1 ) ;
2739+ const startTurnReached = yield * Deferred . make < void > ( ) ;
2740+ yield * Effect . gen ( function * ( ) {
2741+ const eventSink = yield * EventSink . EventSinkV2 ;
2742+ const idAllocator = yield * IdAllocator . IdAllocatorV2 ;
2743+ const manager = yield * ProviderSessionManager . ProviderSessionManagerV2 ;
2744+ const projectionStore = yield * ProjectionStore . ProjectionStoreV2 ;
2745+ const now = yield * DateTime . now ;
2746+ const threadId = ThreadId . make ( "thread-provider-session-manager-renewed-credentials" ) ;
2747+ const providerSessionId = yield * idAllocator . allocate . providerSession ( {
2748+ providerInstanceId : modelSelection . instanceId ,
2749+ threadId,
2750+ } ) ;
2751+ yield * eventSink . write ( {
2752+ events : [ yield * makeThreadCreatedEvent ( { idAllocator, threadId, now } ) ] ,
2753+ } ) ;
2754+ const open = manager . open ( { threadId, providerSessionId, modelSelection, runtimePolicy } ) ;
2755+ const first = yield * open ;
2756+ assert . strictEqual ( yield * open , first ) ;
2757+
2758+ const runId = idAllocator . derive . run ( { threadId, ordinal : 1 } ) ;
2759+ const running = yield * first
2760+ . startTurn ( {
2761+ appThread : ( yield * projectionStore . getThreadProjection ( threadId ) ) . thread ,
2762+ threadId,
2763+ runId,
2764+ runOrdinal : 1 ,
2765+ providerTurnOrdinal : 1 ,
2766+ attemptId : idAllocator . derive . runAttempt ( { runId, attemptOrdinal : 1 } ) ,
2767+ rootNodeId : idAllocator . derive . rootNode ( { runId } ) ,
2768+ providerThread : makeProviderThread ( { idAllocator, threadId, providerSessionId, now } ) ,
2769+ message : {
2770+ createdBy : "user" ,
2771+ creationSource : "web" ,
2772+ messageId : yield * idAllocator . allocate . message ( { threadId, ordinal : 1 } ) ,
2773+ text : "running while the token renews" ,
2774+ attachments : [ ] ,
2775+ } ,
2776+ modelSelection,
2777+ runtimePolicy,
2778+ } )
2779+ . pipe ( Effect . forkChild ) ;
2780+ yield * Deferred . await ( startTurnReached ) ;
2781+ yield * Ref . set ( credentialRevision , 2 ) ;
2782+
2783+ // A running turn keeps the process it started on.
2784+ assert . strictEqual ( yield * open , first ) ;
2785+ assert . equal ( ( yield * Ref . get ( state ) ) . closeCount , 0 ) ;
2786+
2787+ yield * Fiber . interrupt ( running ) ;
2788+ const replacement = yield * open ;
2789+ assert . notStrictEqual ( replacement , first ) ;
2790+ assert . equal ( ( yield * Ref . get ( state ) ) . openCount , 2 ) ;
2791+ assert . equal ( ( yield * Ref . get ( state ) ) . closeCount , 1 ) ;
2792+ assert . strictEqual ( yield * open , replacement ) ;
2793+ const session = ( yield * projectionStore . getThreadRecords ( threadId , [ "providerSessions" ] ) )
2794+ . providerSessions [ 0 ] ;
2795+ assert . equal ( session ?. status , "ready" ) ;
2796+ } ) . pipe (
2797+ Effect . provide (
2798+ layerTest ( {
2799+ state,
2800+ idleTimeoutMs : 60_000 ,
2801+ credentialRevision,
2802+ startTurn : Deferred . succeed ( startTurnReached , undefined ) . pipe (
2803+ Effect . andThen ( Effect . never ) ,
2804+ ) ,
2805+ } ) ,
2806+ ) ,
2807+ ) ;
2808+ } ) ,
2809+ ) ;
2810+
27192811it . effect ( "ProviderSessionManagerV2 releases pinned idle sessions once the pin cap expires" , ( ) =>
27202812 Effect . gen ( function * ( ) {
27212813 const state = yield * Ref . make ( emptyState ) ;
0 commit comments