@@ -3666,6 +3666,77 @@ describe("PiAdapterV2", () => {
36663666 } ) . pipe ( Effect . scoped , Effect . provide ( layerTest ) ) ,
36673667 ) ;
36683668
3669+ it . effect ( "rolls back a persisted thread on a fresh runtime after its session was released" , ( ) =>
3670+ Effect . gen ( function * ( ) {
3671+ const previousFake = yield * makeFakePi ;
3672+ const { runtime : previousRuntime } = yield * openRuntime ( previousFake ) ;
3673+ const providerThread = yield * previousRuntime . ensureThread ( {
3674+ threadId : THREAD_ID ,
3675+ modelSelection : modelSelection ( "default" ) ,
3676+ runtimePolicy,
3677+ } ) ;
3678+ const sessionFile = providerThread . nativeThreadRef ?. nativeId ;
3679+ assert . isDefined ( sessionFile ) ;
3680+ const turn = ( ordinal : number , entryId : string ) : OrchestrationV2ProviderTurn => ( {
3681+ id : ProviderTurnId . make ( `turn-${ ordinal } ` ) ,
3682+ providerThreadId : providerThread . id ,
3683+ nodeId : NodeId . make ( `node-${ ordinal } ` ) ,
3684+ runAttemptId : null ,
3685+ nativeTurnRef : { driver : PI_PROVIDER , nativeId : entryId , strength : "strong" } ,
3686+ ordinal,
3687+ status : "completed" ,
3688+ startedAt : null ,
3689+ completedAt : null ,
3690+ } ) ;
3691+ const turns = [ turn ( 1 , "u1" ) , turn ( 2 , "u2" ) ] ;
3692+
3693+ // Rollback opens the session the way CheckpointRollbackService does, without resumeThread.
3694+ const fake = yield * makeFakePi ;
3695+ const adapter = yield * makeAdapter ( fake ) ;
3696+ const runtime = yield * adapter . openSession ( {
3697+ threadId : THREAD_ID ,
3698+ providerSessionId : SESSION_ID ,
3699+ modelSelection : modelSelection ( "default" ) ,
3700+ runtimePolicy,
3701+ initialNativeThreadId : sessionFile ! ,
3702+ } ) ;
3703+ yield * runtime . events . pipe ( Stream . runDrain , Effect . forkScoped ) ;
3704+ fake . queueState ( { sessionFile : sessionFile ! } ) ;
3705+ fake . queueState ( { sessionFile : "/fake/rolled-back-session.jsonl" } ) ;
3706+ const result = yield * runtime
3707+ . rollbackThread ( {
3708+ providerThread,
3709+ target : {
3710+ type : "provider_turn" ,
3711+ checkpointId : CheckpointId . make ( "checkpoint-1" ) ,
3712+ appRunOrdinal : 1 ,
3713+ providerTurn : turns [ 0 ] ! ,
3714+ } ,
3715+ providerThreadTurns : turns ,
3716+ } )
3717+ . pipe ( Effect . result ) ;
3718+
3719+ assert . equal (
3720+ result . _tag ,
3721+ "Success" ,
3722+ `rollback on a fresh Pi runtime failed: ${ result . _tag === "Failure" ? String ( result . failure . cause ) : "" } ` ,
3723+ ) ;
3724+ const requests = fake . allRequests ( ) ;
3725+ const switchIndex = requests . findIndex (
3726+ ( request ) => request . type === "switch_session" && request . sessionPath === sessionFile ,
3727+ ) ;
3728+ const forkIndex = requests . findIndex ( ( request ) => request . type === "fork" ) ;
3729+ assert . isAtLeast ( switchIndex , 0 , "fresh runtime never switched to the persisted session" ) ;
3730+ assert . isAbove ( forkIndex , switchIndex , "fork ran before switching to the persisted session" ) ;
3731+ assert . equal ( requests [ forkIndex ] ?. entryId , "u2" ) ;
3732+ if ( result . _tag !== "Success" ) return ;
3733+ assert . equal (
3734+ result . success . providerThread . nativeThreadRef ?. nativeId ,
3735+ "/fake/rolled-back-session.jsonl" ,
3736+ ) ;
3737+ } ) . pipe ( Effect . scoped , Effect . provide ( layerTest ) ) ,
3738+ ) ;
3739+
36693740 it . effect ( "settles a command-only prompt from its deferred ack and idle probe" , ( ) =>
36703741 Effect . gen ( function * ( ) {
36713742 const fake = yield * makeFakePi ;
0 commit comments