@@ -1395,6 +1395,54 @@ describe("PiAdapterV2", () => {
13951395 } ) . pipe ( Effect . scoped , Effect . provide ( layerTest ) ) ,
13961396 ) ;
13971397
1398+ it . effect ( "binds a replacement session's work only after Pi's teardown is applied" , ( ) =>
1399+ Effect . gen ( function * ( ) {
1400+ const fake = yield * makeFakePi ;
1401+ const { runtime } = yield * openRuntime ( fake ) ;
1402+ yield * runtime . ensureThread ( {
1403+ threadId : THREAD_ID ,
1404+ modelSelection : modelSelection ( "default" ) ,
1405+ runtimePolicy,
1406+ } ) ;
1407+ const setStatus = ( statusKey : string , statusText : string ) =>
1408+ fake . emit ( {
1409+ type : "extension_ui_request" ,
1410+ id : `status-${ statusKey } ` ,
1411+ method : "setStatus" ,
1412+ statusKey,
1413+ statusText,
1414+ } ) ;
1415+ yield * setStatus ( "t3:background:build" , "Build running" ) ;
1416+ fake . deferNextLifecycle ( "new_session" ) ;
1417+ fake . queueState ( { sessionFile : "/fake/replaced.jsonl" } ) ;
1418+ const switching = yield * runtime
1419+ . ensureThread ( {
1420+ threadId : THREAD_ID ,
1421+ modelSelection : modelSelection ( "default" ) ,
1422+ runtimePolicy,
1423+ } )
1424+ . pipe ( Effect . forkScoped ) ;
1425+ const newSession = yield * fake . takeRequest ( "new_session" ) ;
1426+ // Pi writes the teardown, the replacement's start hooks and its reply in
1427+ // one burst; the reply skips the event queue the statuses wait in.
1428+ yield * setStatus ( "t3:session-end" , "new" ) ;
1429+ yield * setStatus ( "t3:background:watch" , "Watching" ) ;
1430+ yield * fake . emit ( {
1431+ type : "response" ,
1432+ id : newSession [ "id" ] ,
1433+ command : "new_session" ,
1434+ success : true ,
1435+ data : { cancelled : false } ,
1436+ } ) ;
1437+ const replacement = yield * Fiber . join ( switching ) ;
1438+ assert . deepStrictEqual (
1439+ replacement . pendingBackgroundTasks ?. map ( ( task ) => task . taskId ) ,
1440+ [ "t3:background:watch" ] ,
1441+ ) ;
1442+ assert . isTrue ( yield * runtime . hasPendingBackgroundWorkForThread ! ( replacement ) ) ;
1443+ } ) . pipe ( Effect . scoped , Effect . provide ( layerTest ) ) ,
1444+ ) ;
1445+
13981446 it . effect ( "keeps the thread binding when Pi reloads its extensions" , ( ) =>
13991447 Effect . gen ( function * ( ) {
14001448 const fake = yield * makeFakePi ;
0 commit comments