@@ -334,6 +334,7 @@ const makeProviderAdapter = Effect.fnUntraced(function* (
334334 readonly backgroundTasksEndWithProcess ?: boolean ;
335335 readonly hangSessionScopeClose ?: boolean ;
336336 readonly startTurn ?: Effect . Effect < void > ;
337+ readonly startVoiceSession ?: Effect . Effect < void > ;
337338 readonly beforeUnload ?: Effect . Effect < void > ;
338339 readonly spawnBeforeOpen ?: boolean ;
339340 readonly scopeCloseReached ?: Deferred . Deferred < void > ;
@@ -448,6 +449,15 @@ const makeProviderAdapter = Effect.fnUntraced(function* (
448449 } ) ) ,
449450 ) ,
450451 ) ,
452+ ...( options . startVoiceSession === undefined
453+ ? { }
454+ : {
455+ startVoiceSession : ( ) =>
456+ Stream . fromEffect ( options . startVoiceSession ! ) . pipe (
457+ Stream . drain ,
458+ Stream . concat ( Stream . never ) ,
459+ ) ,
460+ } ) ,
451461 respondToRuntimeRequest : ( ) => Effect . void ,
452462 readThreadSnapshot : ( ) => unimplemented ( "readThreadSnapshot unused in test" ) ,
453463 rollbackThread : ( ) => unimplemented ( "rollbackThread unused in test" ) ,
@@ -483,6 +493,7 @@ function layerTest(input: {
483493 readonly backgroundTasksEndWithProcess ?: boolean ;
484494 readonly hangSessionScopeClose ?: boolean ;
485495 readonly startTurn ?: Effect . Effect < void > ;
496+ readonly startVoiceSession ?: Effect . Effect < void > ;
486497 readonly beforeUnload ?: Effect . Effect < void > ;
487498 readonly spawnBeforeOpen ?: boolean ;
488499 readonly scopeCloseReached ?: Deferred . Deferred < void > ;
@@ -518,6 +529,9 @@ function layerTest(input: {
518529 ? { }
519530 : { hangSessionScopeClose : input . hangSessionScopeClose } ) ,
520531 ...( input . startTurn === undefined ? { } : { startTurn : input . startTurn } ) ,
532+ ...( input . startVoiceSession === undefined
533+ ? { }
534+ : { startVoiceSession : input . startVoiceSession } ) ,
521535 ...( input . beforeUnload === undefined ? { } : { beforeUnload : input . beforeUnload } ) ,
522536 ...( input . spawnBeforeOpen === undefined ? { } : { spawnBeforeOpen : input . spawnBeforeOpen } ) ,
523537 ...( input . scopeCloseReached === undefined
@@ -5029,3 +5043,62 @@ it.effect(
50295043 assert . isFalse ( denied ?. capabilities ?. has ( "device" ) ) ;
50305044 } ) ,
50315045) ;
5046+
5047+ it . effect (
5048+ "protects the provider session while voice streams and restores idle release after disconnect" ,
5049+ ( ) =>
5050+ Effect . gen ( function * ( ) {
5051+ const state = yield * Ref . make ( emptyState ) ;
5052+ const started = yield * Deferred . make < void > ( ) ;
5053+ yield * Effect . gen ( function * ( ) {
5054+ const sink = yield * EventSink . EventSinkV2 ;
5055+ const allocator = yield * IdAllocator . IdAllocatorV2 ;
5056+ const manager = yield * ProviderSessionManager . ProviderSessionManagerV2 ;
5057+ const now = yield * DateTime . now ;
5058+ const threadId = ThreadId . make ( "voice-idle-test" ) ;
5059+ const providerSessionId = yield * allocator . allocate . providerSession ( {
5060+ providerInstanceId : modelSelection . instanceId ,
5061+ threadId,
5062+ } ) ;
5063+ const providerThread = makeProviderThread ( {
5064+ idAllocator : allocator ,
5065+ threadId,
5066+ providerSessionId,
5067+ now,
5068+ } ) ;
5069+ yield * sink . write ( {
5070+ events : [
5071+ yield * makeThreadCreatedEvent ( { idAllocator : allocator , threadId, now } ) ,
5072+ {
5073+ id : yield * allocator . allocate . event ( { threadId } ) ,
5074+ type : "provider-thread.updated" ,
5075+ threadId,
5076+ driver : CODEX_DRIVER ,
5077+ occurredAt : now ,
5078+ payload : providerThread ,
5079+ } ,
5080+ ] ,
5081+ } ) ;
5082+ yield * manager . open ( { threadId, providerSessionId, modelSelection, runtimePolicy } ) ;
5083+ const voice = yield * manager
5084+ . startVoiceSession ( { threadId, offerSdp : "offer" } )
5085+ . pipe ( Stream . runDrain , Effect . forkChild ) ;
5086+ yield * Deferred . await ( started ) ;
5087+ yield * TestClock . adjust ( "2 seconds" ) ;
5088+ assert . isTrue ( Option . isSome ( yield * manager . get ( providerSessionId ) ) ) ;
5089+ assert . equal ( ( yield * Ref . get ( state ) ) . closeCount , 0 ) ;
5090+ yield * Fiber . interrupt ( voice ) ;
5091+ yield * TestClock . adjust ( "2 seconds" ) ;
5092+ assert . isTrue ( Option . isNone ( yield * manager . get ( providerSessionId ) ) ) ;
5093+ assert . equal ( ( yield * Ref . get ( state ) ) . closeCount , 1 ) ;
5094+ } ) . pipe (
5095+ Effect . provide (
5096+ layerTest ( {
5097+ state,
5098+ idleTimeoutMs : 1000 ,
5099+ startVoiceSession : Deferred . succeed ( started , undefined ) ,
5100+ } ) ,
5101+ ) ,
5102+ ) ;
5103+ } ) ,
5104+ ) ;
0 commit comments