@@ -335,6 +335,7 @@ const makeProviderAdapter = Effect.fnUntraced(function* (
335335 readonly backgroundTasksEndWithProcess ?: boolean ;
336336 readonly hangSessionScopeClose ?: boolean ;
337337 readonly startTurn ?: Effect . Effect < void > ;
338+ readonly compactThread ?: Effect . Effect < void > ;
338339 readonly beforeUnload ?: Effect . Effect < void > ;
339340 readonly spawnBeforeOpen ?: boolean ;
340341 readonly scopeCloseReached ?: Deferred . Deferred < void > ;
@@ -435,6 +436,9 @@ const makeProviderAdapter = Effect.fnUntraced(function* (
435436 Effect . as ( threadInput . providerThread ) ,
436437 ) ,
437438 startTurn : ( ) => options . startTurn ?? Effect . void ,
439+ ...( options . compactThread === undefined
440+ ? { }
441+ : { compactThread : ( ) => options . compactThread ! } ) ,
438442 steerTurn : ( ) => Effect . void ,
439443 interruptTurn : ( ) =>
440444 Ref . update ( state , ( current ) => ( {
@@ -490,6 +494,7 @@ function layerTest(input: {
490494 readonly backgroundTasksEndWithProcess ?: boolean ;
491495 readonly hangSessionScopeClose ?: boolean ;
492496 readonly startTurn ?: Effect . Effect < void > ;
497+ readonly compactThread ?: Effect . Effect < void > ;
493498 readonly beforeUnload ?: Effect . Effect < void > ;
494499 readonly spawnBeforeOpen ?: boolean ;
495500 readonly scopeCloseReached ?: Deferred . Deferred < void > ;
@@ -529,6 +534,7 @@ function layerTest(input: {
529534 ? { }
530535 : { hangSessionScopeClose : input . hangSessionScopeClose } ) ,
531536 ...( input . startTurn === undefined ? { } : { startTurn : input . startTurn } ) ,
537+ ...( input . compactThread === undefined ? { } : { compactThread : input . compactThread } ) ,
532538 ...( input . beforeUnload === undefined ? { } : { beforeUnload : input . beforeUnload } ) ,
533539 ...( input . spawnBeforeOpen === undefined ? { } : { spawnBeforeOpen : input . spawnBeforeOpen } ) ,
534540 ...( input . scopeCloseReached === undefined
@@ -3557,9 +3563,9 @@ it.effect(
35573563 } ) ,
35583564) ;
35593565
3560- it . effect (
3561- "ProviderSessionManagerV2 replaces stale credentials only after shared work settles and resumes native history" ,
3562- ( ) =>
3566+ it . effect . each ( [ "startTurn" , "compactThread" ] as const ) (
3567+ "ProviderSessionManagerV2 replaces stale credentials only after shared work settles and resumes native history with %s " ,
3568+ ( operation ) =>
35633569 Effect . gen ( function * ( ) {
35643570 const state = yield * Ref . make ( emptyState ) ;
35653571 const stale = yield * Ref . make ( false ) ;
@@ -3633,7 +3639,7 @@ it.effect(
36333639 modelSelection,
36343640 runtimePolicy,
36353641 } as const ;
3636- yield * first . startTurn ( startInput ) ;
3642+ yield * first [ operation ] ! ( startInput ) ;
36373643 yield * Ref . set ( stale , true ) ;
36383644 assert . strictEqual ( yield * open ( b ) , first ) ;
36393645 assert . equal ( ( yield * Ref . get ( state ) ) . closeCount , 0 ) ;
@@ -3669,14 +3675,14 @@ it.effect(
36693675 yield * Fiber . join ( pending ) ;
36703676 yield * manager . detach ( { providerSessionId, threadId : a } ) ;
36713677 yield * Ref . set ( pauseAttachWrite . armed , true ) ;
3672- const pendingStart = yield * first . startTurn ( startInput ) . pipe ( Effect . forkChild ) ;
3678+ const pendingStart = yield * first [ operation ] ! ( startInput ) . pipe ( Effect . forkChild ) ;
36733679 yield * Deferred . await ( pauseAttachWrite . paused ) ;
36743680 assert . strictEqual ( yield * open ( b ) , first ) ;
36753681 yield * Fiber . interrupt ( pendingStart ) ;
36763682 yield * Ref . set ( pauseProbe , true ) ;
36773683 const reopening = yield * open ( b ) . pipe ( Effect . forkChild ) ;
36783684 yield * Deferred . await ( probeEntered ) ;
3679- yield * first . startTurn ( { ...startInput , runOrdinal : 2 } ) ;
3685+ yield * first [ operation ] ! ( { ...startInput , runOrdinal : 2 } ) ;
36803686 yield * Deferred . succeed ( probeContinue , undefined ) ;
36813687 assert . strictEqual ( yield * Fiber . join ( reopening ) , first ) ;
36823688 const finished = yield * first . subscribeEvents ! ;
@@ -3693,22 +3699,22 @@ it.effect(
36933699 ( thread ) => thread . id === providerThread . id ,
36943700 ) ! ;
36953701 assert . equal ( saved . nativeThreadRef ?. nativeId , "persisted-native-history" ) ;
3696- const resumed = yield * replacement . resumeThread ( {
3702+ yield * replacement . resumeThread ( {
36973703 threadId : a ,
36983704 providerThread : saved ,
36993705 modelSelection,
37003706 runtimePolicy,
37013707 } ) ;
3702- assert . equal ( resumed . nativeThreadRef ?. nativeId , "persisted-native-history" ) ;
37033708 assert . equal ( ( yield * Ref . get ( state ) ) . resumeCount , 2 ) ;
3704- assert . equal ( ( yield * first . startTurn ( startInput ) . pipe ( Effect . result ) ) . _tag , "Failure" ) ;
3709+ assert . equal ( ( yield * first [ operation ] ! ( startInput ) . pipe ( Effect . result ) ) . _tag , "Failure" ) ;
37053710 assert . strictEqual ( yield * open ( a ) , replacement ) ;
37063711 assert . equal ( ( yield * Ref . get ( state ) ) . openCount , 2 ) ;
37073712 } ) . pipe (
37083713 Effect . provide (
37093714 layerTest ( {
37103715 state,
37113716 pauseAttachWrite,
3717+ compactThread : Effect . void ,
37123718 idleTimeoutMs : 60_000 ,
37133719 beforeOpen : ( ) => Ref . set ( stale , false ) ,
37143720 hasStaleCredentials : Ref . get ( stale ) ,
0 commit comments