@@ -6,7 +6,12 @@ import * as NodeModule from "node:module";
66import * as NodeVM from "node:vm" ;
77import { assert , describe , it } from "@effect/vitest" ;
88
9- import { PI_T3_MCP_EXTENSION_SOURCE , PI_T3_SESSION_END_STATUS_KEY } from "./mcpExtensionSource.ts" ;
9+ import {
10+ PI_T3_LIFECYCLE_EXTENSION_SOURCE ,
11+ PI_T3_MCP_EXTENSION_SOURCE ,
12+ PI_T3_SESSION_END_STATUS_KEY ,
13+ PI_T3_SESSION_START_STATUS_KEY ,
14+ } from "./mcpExtensionSource.ts" ;
1015import { loadMcpBridge } from "./mcpBridge.testkit.ts" ;
1116
1217type RequestHook = (
@@ -78,33 +83,62 @@ describe("Pi orchestration prompt section", () => {
7883} ) ;
7984
8085describe ( "Pi session teardown" , ( ) => {
81- it . each ( [ "MCP" , "permission-only" ] ) (
82- "marks shutdown and replacement start with %s" ,
83- async ( mode ) => {
84- const handlers = mode === "MCP" ? ( await loadMcpBridge ( ) ) . handlers : await loadHooks ( ) ;
85- const statuses : Array < readonly [ string , string | undefined ] > = [ ] ;
86- const ui = {
87- notify : ( ) => undefined ,
88- setStatus : ( key : string , text : string | undefined ) => statuses . push ( [ key , text ] ) ,
89- } ;
90- type LifecycleHook = (
91- event : { systemPrompt : string ; reason ?: string } ,
92- ctx : { ui : typeof ui } ,
93- ) => unknown ;
94- const shutdown = handlers . get ( "session_shutdown" ) as LifecycleHook ;
95- const start = handlers . get ( "session_start" ) as LifecycleHook ;
96- for ( const reason of [ "reload" , "new" ] ) {
97- await shutdown ( Object . assign ( { systemPrompt : "" } , { reason } ) , { ui } ) ;
98- await start ( { systemPrompt : "" } , { ui } ) ;
99- }
100- assert . deepStrictEqual ( statuses , [
101- [ PI_T3_SESSION_END_STATUS_KEY , "reload" ] ,
102- [ "t3:session-start" , "started" ] ,
103- [ PI_T3_SESSION_END_STATUS_KEY , "new" ] ,
104- [ "t3:session-start" , "started" ] ,
105- ] ) ;
106- } ,
107- ) ;
86+ type LifecycleHook = (
87+ event : { systemPrompt : string ; reason ?: string } ,
88+ ctx : { ui : { notify : ( ) => void ; setStatus : ( key : string , text ?: string ) => void } } ,
89+ ) => unknown ;
90+
91+ async function loadLifecycleHooks ( ) {
92+ const handlers = new Map < string , LifecycleHook > ( ) ;
93+ const source = NodeModule . stripTypeScriptTypes (
94+ PI_T3_LIFECYCLE_EXTENSION_SOURCE . replace ( / ^ i m p o r t .* ; $ / gm, "" ) . replace (
95+ "export default function" ,
96+ "function" ,
97+ ) ,
98+ ) ;
99+ NodeVM . runInNewContext ( `${ source } \nt3LifecycleExtension(pi)` , {
100+ pi : { on : ( name : string , handler : LifecycleHook ) => handlers . set ( name , handler ) } ,
101+ } ) ;
102+ return handlers ;
103+ }
104+
105+ it ( "marks shutdown and replacement start from the lifecycle sentinel only" , async ( ) => {
106+ const statuses : Array < readonly [ string , string | undefined ] > = [ ] ;
107+ const ui = {
108+ notify : ( ) => undefined ,
109+ setStatus : ( key : string , text : string | undefined ) => statuses . push ( [ key , text ] ) ,
110+ } ;
111+ const sentinel = await loadLifecycleHooks ( ) ;
112+ for ( const reason of [ "reload" , "new" ] ) {
113+ await sentinel . get ( "session_shutdown" ) ! ( Object . assign ( { systemPrompt : "" } , { reason } ) , {
114+ ui,
115+ } ) ;
116+ await sentinel . get ( "session_start" ) ! ( { systemPrompt : "" } , { ui } ) ;
117+ }
118+ assert . deepStrictEqual ( statuses , [
119+ [ PI_T3_SESSION_END_STATUS_KEY , "reload" ] ,
120+ [ PI_T3_SESSION_START_STATUS_KEY , "started" ] ,
121+ [ PI_T3_SESSION_END_STATUS_KEY , "new" ] ,
122+ [ PI_T3_SESSION_START_STATUS_KEY , "started" ] ,
123+ ] ) ;
124+
125+ // The bridge loads after user extensions; a marker from it would arrive
126+ // after their new background work.
127+ statuses . length = 0 ;
128+ for ( const handlers of [ ( await loadMcpBridge ( ) ) . handlers , await loadHooks ( ) ] ) {
129+ await ( handlers . get ( "session_shutdown" ) as LifecycleHook | undefined ) ?.(
130+ Object . assign ( { systemPrompt : "" } , { reason : "new" } ) ,
131+ { ui } ,
132+ ) ;
133+ await ( handlers . get ( "session_start" ) as LifecycleHook | undefined ) ?.(
134+ { systemPrompt : "" } ,
135+ {
136+ ui,
137+ } ,
138+ ) ;
139+ }
140+ assert . deepStrictEqual ( statuses , [ ] ) ;
141+ } ) ;
108142} ) ;
109143
110144describe ( "Pi MCP tool results" , ( ) => {
0 commit comments