@@ -72,7 +72,7 @@ import {
7272 materializePiT3McpExtension ,
7373 resolvePiLaunchArgs ,
7474} from "./mcpInjection.ts" ;
75- import { PI_FILE_CHANGE_TOOLS } from "./mcpExtensionSource.ts" ;
75+ import { PI_FILE_CHANGE_TOOLS , PI_T3_SESSION_END_STATUS_KEY } from "./mcpExtensionSource.ts" ;
7676
7777export const PI_PROVIDER = ProviderDriverKind . make ( "pi" ) ;
7878const PI_DRIVER_KIND = PI_PROVIDER ;
@@ -549,8 +549,9 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
549549 let pendingWake : PiWake | null = null ;
550550 // Status key to its text, which becomes the task description.
551551 const backgroundWork = new Map < string , string > ( ) ;
552- // The last bound thread, whose roster lists backgroundWork. Unlike
553- // threadState it survives a failed or vetoed session switch.
552+ // The thread whose roster lists backgroundWork. Unlike threadState it
553+ // survives a vetoed or failed switch, and it is null between a session's
554+ // teardown and the next binding, when new statuses have no owner yet.
554555 let backgroundWorkState : PiThreadState | null = null ;
555556 let rollbackBarrier : Deferred . Deferred <
556557 void ,
@@ -677,11 +678,8 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
677678 const syncBackgroundWork = ( state : PiThreadState ) =>
678679 updateProviderThread ( state , { pendingBackgroundTasks : pendingBackgroundTasks ( ) } ) ;
679680
680- const lifecycleRequest = Effect . fn ( "PiAdapter.lifecycleRequest" ) ( function * (
681- record : PiRpcRecord ,
682- ) {
683- const outgoingKeys = new Set ( backgroundWork . keys ( ) ) ;
684- const result = yield * request ( record , PI_SESSION_TIMEOUT_MS ) . pipe (
681+ const lifecycleRequest = ( record : PiRpcRecord ) =>
682+ request ( record , PI_SESSION_TIMEOUT_MS ) . pipe (
685683 // A local timeout does not cancel Pi's lifecycle hook. Retire the
686684 // process before fallback can race its eventual switch/new-session.
687685 Effect . tapError ( ( error ) =>
@@ -697,20 +695,6 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
697695 } ) ,
698696 Effect . onInterrupt ( ( ) => connection . terminate ) ,
699697 ) ;
700- if ( recordField ( result , "cancelled" ) !== true && outgoingKeys . size > 0 ) {
701- // Pi invalidates the outgoing session's extension context, so its
702- // jobs can no longer clear their statuses; Pi's own TUI drops them
703- // here too. Responses bypass the pump, so first apply every status
704- // Pi sent before replying. Keys first set during the switch belong
705- // to the replacement session's start hooks.
706- const drained = yield * Deferred . make < void > ( ) ;
707- if ( yield * Queue . offer ( connection . events , { type : "t3.drain" , drained } ) )
708- yield * Deferred . await ( drained ) ;
709- for ( const key of outgoingKeys ) backgroundWork . delete ( key ) ;
710- if ( backgroundWorkState !== null ) yield * syncBackgroundWork ( backgroundWorkState ) ;
711- }
712- return result ;
713- } ) ;
714698
715699 const tokenUsageFromStats = (
716700 stats : unknown ,
@@ -1277,6 +1261,15 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
12771261 if ( method === undefined ) return ;
12781262 if ( method === "setStatus" ) {
12791263 const statusKey = recordString ( event , "statusKey" ) ;
1264+ if ( statusKey === PI_T3_SESSION_END_STATUS_KEY ) {
1265+ // The outgoing jobs can never clear their statuses now.
1266+ const outgoingState = backgroundWorkState ;
1267+ backgroundWorkState = null ;
1268+ if ( backgroundWork . size === 0 ) return ;
1269+ backgroundWork . clear ( ) ;
1270+ if ( outgoingState !== null ) yield * syncBackgroundWork ( outgoingState ) ;
1271+ return ;
1272+ }
12801273 if (
12811274 statusKey !== PI_BACKGROUND_WORK_STATUS_KEY &&
12821275 statusKey ?. startsWith ( `${ PI_BACKGROUND_WORK_STATUS_KEY } :` ) !== true
@@ -2112,12 +2105,6 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
21122105 }
21132106 return ;
21142107 }
2115- case "t3.drain" : {
2116- const drained = event [ "drained" ] ;
2117- if ( Deferred . isDeferred < void , never > ( drained ) )
2118- yield * Deferred . succeed ( drained , undefined ) ;
2119- return ;
2120- }
21212108 case "t3.rollback_barrier" : {
21222109 if ( rollbackBarrier !== null && event [ "barrier" ] === rollbackBarrier )
21232110 yield * Deferred . succeed ( rollbackBarrier , undefined ) ;
@@ -3049,9 +3036,11 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
30493036 appliedSessionName = null ;
30503037 lastKnownLeaf = leafId ;
30513038 leafCursorStale = entriesData === undefined ;
3039+ backgroundWorkState = state ;
30523040 yield * updateProviderThread ( state , {
30533041 nativeThreadRef : providerRef ( forkSessionFile ) ,
30543042 nativeConversationHeadRef : leafId === null ? null : providerRef ( leafId ) ,
3043+ pendingBackgroundTasks : pendingBackgroundTasks ( ) ,
30553044 } ) ;
30563045 rollbackBarrier = null ;
30573046 return piThreadSnapshot ( state . providerThread ) ;
0 commit comments