@@ -516,6 +516,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
516516 > ( ) ;
517517 const pendingPrompts = new Map < string , PendingPiPrompt > ( ) ;
518518 const sessionApprovals = new Set < string > ( ) ;
519+ let lifecycleInFlight = false ;
519520 // Answering a dialog and terminalizing a turn both publish lifecycle
520521 // events. Pi can settle immediately after `extension_ui_response`, so
521522 // serialize the two paths to stop `turn.terminal` from overtaking the
@@ -640,7 +641,11 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
640641 } ;
641642
642643 const lifecycleRequest = ( record : PiRpcRecord ) =>
643- request ( record , PI_SESSION_TIMEOUT_MS ) . pipe (
644+ Effect . suspend ( ( ) => {
645+ sessionApprovals . clear ( ) ;
646+ lifecycleInFlight = true ;
647+ return request ( record , PI_SESSION_TIMEOUT_MS ) ;
648+ } ) . pipe (
644649 // A local timeout does not cancel Pi's lifecycle hook. Retire the
645650 // process before fallback can race its eventual switch/new-session.
646651 Effect . tapError ( ( error ) =>
@@ -655,6 +660,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
655660 connection . terminate . pipe ( Effect . andThen ( Effect . fail ( error ) ) ) ,
656661 } ) ,
657662 Effect . onInterrupt ( ( ) => connection . terminate ) ,
663+ Effect . ensuring ( Effect . sync ( ( ) => ( lifecycleInFlight = false ) ) ) ,
658664 ) ;
659665
660666 const tokenUsageFromStats = (
@@ -2205,7 +2211,6 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
22052211 ? { type : "switch_session" , sessionPath : resumeId }
22062212 : { type : "new_session" } ,
22072213 ) ;
2208- sessionApprovals . clear ( ) ;
22092214 if ( recordField ( result , "cancelled" ) === true ) {
22102215 return yield * protocolError ( "A Pi extension cancelled the session switch" ) ;
22112216 }
@@ -2755,7 +2760,11 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
27552760 if ( pendingPrompts . get ( String ( requestInput . requestId ) ) !== pending ) return ;
27562761 pendingPrompts . delete ( String ( requestInput . requestId ) ) ;
27572762 if ( pending . expirationFiber !== null ) yield * Fiber . interrupt ( pending . expirationFiber ) ;
2758- if ( pending . method === "confirm" && requestInput . decision === "acceptForSession" ) {
2763+ if (
2764+ pending . method === "confirm" &&
2765+ requestInput . decision === "acceptForSession" &&
2766+ ! lifecycleInFlight
2767+ ) {
27592768 sessionApprovals . add ( pending . approvalKey ) ;
27602769 }
27612770 const resolvedAt = yield * DateTime . now ;
@@ -2911,7 +2920,6 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
29112920 ( barrier ) =>
29122921 Effect . gen ( function * ( ) {
29132922 const forkData = yield * lifecycleRequest ( { type : "fork" , entryId : forkEntryId } ) ;
2914- sessionApprovals . clear ( ) ;
29152923 if ( recordField ( forkData , "cancelled" ) === true )
29162924 return yield * protocolError ( "A Pi extension cancelled the session fork" ) ;
29172925 const forkState = yield * request ( { type : "get_state" } ) . pipe (
0 commit comments