@@ -27,6 +27,7 @@ import * as FileSystem from "effect/FileSystem";
2727import * as Layer from "effect/Layer" ;
2828import * as Option from "effect/Option" ;
2929import * as Queue from "effect/Queue" ;
30+ import * as PubSub from "effect/PubSub" ;
3031import * as Ref from "effect/Ref" ;
3132import * as Stream from "effect/Stream" ;
3233import { TestClock } from "effect/testing" ;
@@ -456,7 +457,7 @@ const makeProviderAdapter = Effect.fnUntraced(function* (
456457
457458function layerTest ( input : {
458459 readonly state : Ref . Ref < TestProviderRuntimeState > ;
459- readonly idleTimeoutMs : number ;
460+ readonly idleTimeoutMs ? : number ;
460461 readonly maxIdlePinMs ?: number ;
461462 readonly failEventStream ?: boolean ;
462463 readonly capabilities ?: OrchestrationV2ProviderCapabilities ;
@@ -546,7 +547,7 @@ function layerTest(input: {
546547 layerConfiguredMcpRegistry ,
547548 McpProviderSessions . layer ,
548549 ProviderSessionManager . layerWithOptions ( {
549- idleTimeoutMs : input . idleTimeoutMs ,
550+ ... ( input . idleTimeoutMs === undefined ? { } : { idleTimeoutMs : input . idleTimeoutMs } ) ,
550551 ...( input . maxIdlePinMs === undefined ? { } : { maxIdlePinMs : input . maxIdlePinMs } ) ,
551552 } ) . pipe (
552553 Layer . provide (
@@ -3542,3 +3543,228 @@ it.effect(
35423543 assert . isFalse ( denied ?. capabilities ?. has ( "device" ) ) ;
35433544 } ) ,
35443545) ;
3546+
3547+ it . effect . each ( [
3548+ { timeoutMinutes : null , label : "disabled" } ,
3549+ { timeoutMinutes : 1 , label : "one minute" } ,
3550+ ] as const ) (
3551+ "ProviderSessionManagerV2 applies configured idle timeout $label" ,
3552+ ( { timeoutMinutes } ) =>
3553+ Effect . gen ( function * ( ) {
3554+ const state = yield * Ref . make ( emptyState ) ;
3555+ yield * Effect . gen ( function * ( ) {
3556+ const eventSink = yield * EventSink . EventSinkV2 ;
3557+ const idAllocator = yield * IdAllocator . IdAllocatorV2 ;
3558+ const manager = yield * ProviderSessionManager . ProviderSessionManagerV2 ;
3559+ const now = yield * DateTime . now ;
3560+ const threadId = ThreadId . make ( "configured-idle-timeout" ) ;
3561+ const providerSessionId = yield * idAllocator . allocate . providerSession ( {
3562+ providerInstanceId : modelSelection . instanceId ,
3563+ threadId,
3564+ } ) ;
3565+ yield * eventSink . write ( {
3566+ events : [ yield * makeThreadCreatedEvent ( { idAllocator, threadId, now } ) ] ,
3567+ } ) ;
3568+ const runtime = yield * manager . open ( {
3569+ threadId,
3570+ providerSessionId,
3571+ modelSelection,
3572+ runtimePolicy,
3573+ } ) ;
3574+ const providerThread = makeProviderThread ( {
3575+ idAllocator,
3576+ threadId,
3577+ providerSessionId,
3578+ now,
3579+ nativeThreadId : "saved-conversation" ,
3580+ } ) ;
3581+ yield * runtime . resumeThread ( { providerThread, threadId, modelSelection, runtimePolicy } ) ;
3582+ yield * TestClock . adjust ( "59 seconds" ) ;
3583+ assert . equal ( ( yield * Ref . get ( state ) ) . closeCount , 0 ) ;
3584+ if ( timeoutMinutes === null ) {
3585+ yield * TestClock . adjust ( "2 hours" ) ;
3586+ assert . equal ( ( yield * Ref . get ( state ) ) . closeCount , 0 ) ;
3587+ assert . isTrue ( Option . isSome ( yield * manager . get ( providerSessionId ) ) ) ;
3588+ } else {
3589+ const stopped = yield * eventSink
3590+ . stream ( {
3591+ threadId,
3592+ afterSequence : yield * eventSink . latestSequence ( { threadId } ) ,
3593+ eventType : "provider-session.updated" ,
3594+ } )
3595+ . pipe (
3596+ Stream . filter (
3597+ ( stored ) =>
3598+ stored . event . type === "provider-session.updated" &&
3599+ stored . event . payload . status === "stopped" ,
3600+ ) ,
3601+ Stream . runHead ,
3602+ Effect . forkScoped ,
3603+ ) ;
3604+ yield * TestClock . adjust ( "1 second" ) ;
3605+ yield * Fiber . join ( stopped ) ;
3606+ assert . equal ( ( yield * Ref . get ( state ) ) . closeCount , 1 ) ;
3607+ const reopened = yield * manager . open ( {
3608+ threadId,
3609+ providerSessionId,
3610+ modelSelection,
3611+ runtimePolicy,
3612+ } ) ;
3613+ const resumed = yield * reopened . resumeThread ( {
3614+ providerThread,
3615+ threadId,
3616+ modelSelection,
3617+ runtimePolicy,
3618+ } ) ;
3619+ assert . deepEqual ( resumed . nativeThreadRef , providerThread . nativeThreadRef ) ;
3620+ assert . equal ( ( yield * Ref . get ( state ) ) . openCount , 2 ) ;
3621+ assert . equal ( ( yield * Ref . get ( state ) ) . resumeCount , 2 ) ;
3622+ }
3623+ } ) . pipe (
3624+ Effect . provide (
3625+ layerTest ( {
3626+ state,
3627+ serverSettingsLayer : ServerSettings . layerTest ( {
3628+ providerSessionIdleTimeoutMinutes : timeoutMinutes ,
3629+ } ) ,
3630+ } ) ,
3631+ ) ,
3632+ ) ;
3633+ } ) ,
3634+ ) ;
3635+
3636+ it . effect (
3637+ "ProviderSessionManagerV2 updates pending idle waits without resetting elapsed time" ,
3638+ ( ) =>
3639+ Effect . gen ( function * ( ) {
3640+ const state = yield * Ref . make ( emptyState ) ;
3641+ const settings = yield * ServerSettings . ServerSettingsService . pipe (
3642+ Effect . provide ( ServerSettings . layerTest ( { providerSessionIdleTimeoutMinutes : 1 } ) ) ,
3643+ ) ;
3644+ const changes = yield * PubSub . unbounded < import ( "@t3tools/contracts" ) . ServerSettings > ( ) ;
3645+ const settingsLayer = Layer . succeed ( ServerSettings . ServerSettingsService , {
3646+ ...settings ,
3647+ subscribeChanges : PubSub . subscribe ( changes ) . pipe ( Effect . map ( Stream . fromSubscription ) ) ,
3648+ } ) ;
3649+ const updateTimeout = ( value : number | null ) =>
3650+ settings
3651+ . updateSettings ( { providerSessionIdleTimeoutMinutes : value } )
3652+ . pipe ( Effect . flatMap ( ( updated ) => PubSub . publish ( changes , updated ) ) ) ;
3653+ yield * Effect . gen ( function * ( ) {
3654+ const eventSink = yield * EventSink . EventSinkV2 ;
3655+ const idAllocator = yield * IdAllocator . IdAllocatorV2 ;
3656+ const manager = yield * ProviderSessionManager . ProviderSessionManagerV2 ;
3657+ const now = yield * DateTime . now ;
3658+ const threadId = ThreadId . make ( "changed-idle-timeout" ) ;
3659+ const providerSessionId = yield * idAllocator . allocate . providerSession ( {
3660+ providerInstanceId : modelSelection . instanceId ,
3661+ threadId,
3662+ } ) ;
3663+ yield * eventSink . write ( {
3664+ events : [ yield * makeThreadCreatedEvent ( { idAllocator, threadId, now } ) ] ,
3665+ } ) ;
3666+ yield * manager . open ( { threadId, providerSessionId, modelSelection, runtimePolicy } ) ;
3667+ yield * TestClock . adjust ( "30 seconds" ) ;
3668+ yield * updateTimeout ( 5 ) ;
3669+ yield * TestClock . adjust ( "1 minute" ) ;
3670+ assert . equal ( ( yield * Ref . get ( state ) ) . closeCount , 0 ) ;
3671+ yield * updateTimeout ( null ) ;
3672+ yield * TestClock . adjust ( "2 hours" ) ;
3673+ assert . equal ( ( yield * Ref . get ( state ) ) . closeCount , 0 ) ;
3674+ const stopped = yield * eventSink
3675+ . stream ( {
3676+ threadId,
3677+ afterSequence : yield * eventSink . latestSequence ( { threadId } ) ,
3678+ eventType : "provider-session.updated" ,
3679+ } )
3680+ . pipe (
3681+ Stream . filter (
3682+ ( stored ) =>
3683+ stored . event . type === "provider-session.updated" &&
3684+ stored . event . payload . status === "stopped" ,
3685+ ) ,
3686+ Stream . runHead ,
3687+ Effect . forkScoped ,
3688+ ) ;
3689+ yield * updateTimeout ( 1 ) ;
3690+ yield * TestClock . adjust ( "0 millis" ) ;
3691+ yield * Fiber . join ( stopped ) ;
3692+ assert . equal ( ( yield * Ref . get ( state ) ) . closeCount , 1 ) ;
3693+ } ) . pipe ( Effect . provide ( layerTest ( { state, serverSettingsLayer : settingsLayer } ) ) ) ;
3694+ } ) ,
3695+ ) ;
3696+
3697+ it . effect ( "ProviderSessionManagerV2 uses the configured timeout for shared thread unloading" , ( ) =>
3698+ Effect . gen ( function * ( ) {
3699+ const state = yield * Ref . make ( emptyState ) ;
3700+ const unloading = yield * Deferred . make < void > ( ) ;
3701+ yield * Effect . gen ( function * ( ) {
3702+ const eventSink = yield * EventSink . EventSinkV2 ;
3703+ const idAllocator = yield * IdAllocator . IdAllocatorV2 ;
3704+ const manager = yield * ProviderSessionManager . ProviderSessionManagerV2 ;
3705+ const now = yield * DateTime . now ;
3706+ const threadId = ThreadId . make ( "configured-shared-idle-timeout" ) ;
3707+ const providerSessionId = idAllocator . derive . providerSession ( {
3708+ providerInstanceId : modelSelection . instanceId ,
3709+ } ) ;
3710+ yield * eventSink . write ( {
3711+ events : [ yield * makeThreadCreatedEvent ( { idAllocator, threadId, now } ) ] ,
3712+ } ) ;
3713+ const runtime = yield * manager . open ( {
3714+ threadId,
3715+ providerSessionId,
3716+ modelSelection,
3717+ runtimePolicy,
3718+ } ) ;
3719+ const providerThread = makeProviderThread ( {
3720+ idAllocator,
3721+ threadId,
3722+ providerSessionId,
3723+ now,
3724+ nativeThreadId : "shared-saved-conversation" ,
3725+ } ) ;
3726+ const resume = runtime . resumeThread ( {
3727+ providerThread,
3728+ threadId,
3729+ modelSelection,
3730+ runtimePolicy,
3731+ } ) ;
3732+ yield * resume ;
3733+ const subscription = yield * runtime . subscribeEvents ! ;
3734+ const queue = ( yield * Ref . get ( state ) ) . eventQueues . get ( String ( providerSessionId ) ) ;
3735+ assert . isDefined ( queue ) ;
3736+ yield * Queue . offer ( queue ! , {
3737+ type : "turn.terminal" ,
3738+ driver : CODEX_DRIVER ,
3739+ providerThreadId : providerThread . id ,
3740+ providerTurnId : idAllocator . derive . providerTurn ( {
3741+ driver : CODEX_DRIVER ,
3742+ nativeTurnId : "completed-shared-turn" ,
3743+ } ) ,
3744+ runOrdinal : 1 ,
3745+ status : "completed" ,
3746+ failure : null ,
3747+ threadDisposition : "reusable" ,
3748+ } ) ;
3749+ yield * subscription . events . pipe ( Stream . runHead ) ;
3750+ yield * TestClock . adjust ( "59 seconds" ) ;
3751+ assert . deepEqual ( ( yield * Ref . get ( state ) ) . unloadedNativeThreadIds , [ ] ) ;
3752+ yield * TestClock . adjust ( "1 second" ) ;
3753+ yield * Deferred . await ( unloading ) ;
3754+ yield * resume ;
3755+ const observed = yield * Ref . get ( state ) ;
3756+ assert . equal ( observed . closeCount , 0 ) ;
3757+ assert . deepEqual ( observed . unloadedNativeThreadIds , [ "shared-saved-conversation" ] ) ;
3758+ assert . equal ( observed . resumeCount , 2 ) ;
3759+ } ) . pipe (
3760+ Effect . provide (
3761+ layerTest ( {
3762+ state,
3763+ hasPendingBackgroundWork : Effect . succeed ( true ) ,
3764+ beforeUnload : Deferred . succeed ( unloading , undefined ) . pipe ( Effect . asVoid ) ,
3765+ serverSettingsLayer : ServerSettings . layerTest ( { providerSessionIdleTimeoutMinutes : 1 } ) ,
3766+ } ) ,
3767+ ) ,
3768+ ) ;
3769+ } ) ,
3770+ ) ;
0 commit comments