Skip to content

Commit dbe5d82

Browse files
PR Batch Testerclaude
andcommitted
fix(pi): keep the thread binding when Pi reloads its extensions
A reload tears extensions down and starts them again in the same session, with no new T3 registration, so new background work had no owner. The bridge now reports Pi's shutdown reason, and a reload keeps the binding. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 3a07bfb commit dbe5d82

4 files changed

Lines changed: 53 additions & 8 deletions

File tree

‎packages/provider-pi/src/server/adapter.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,6 +1395,43 @@ describe("PiAdapterV2", () => {
13951395
}).pipe(Effect.scoped, Effect.provide(layerTest)),
13961396
);
13971397

1398+
it.effect("keeps the thread binding when Pi reloads its extensions", () =>
1399+
Effect.gen(function* () {
1400+
const fake = yield* makeFakePi;
1401+
const { runtime, takeEvent } = yield* openRuntime(fake);
1402+
const providerThread = 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+
const takeRoster = (taskCount: number) =>
1416+
takeEvent(
1417+
(event) =>
1418+
event.type === "provider_thread.updated" &&
1419+
event.providerThread.id === providerThread.id &&
1420+
event.providerThread.pendingBackgroundTasks?.length === taskCount,
1421+
);
1422+
yield* setStatus("t3:background:build", "Build running");
1423+
yield* takeRoster(1);
1424+
// The reload ends the old extension instances, so their jobs can never clear.
1425+
yield* setStatus("t3:session-end", "reload");
1426+
yield* takeRoster(0);
1427+
assert.isFalse(yield* runtime.hasPendingBackgroundWork!);
1428+
// The reloaded extensions run in the same session and thread.
1429+
yield* setStatus("t3:background:build", "Build restarted");
1430+
yield* takeRoster(1);
1431+
assert.isTrue(yield* runtime.hasPendingBackgroundWorkForThread!(providerThread));
1432+
}).pipe(Effect.scoped, Effect.provide(layerTest)),
1433+
);
1434+
13981435
it.effect("keeps background work reported while a thread registers", () =>
13991436
Effect.gen(function* () {
14001437
const fake = yield* makeFakePi;

‎packages/provider-pi/src/server/adapter.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,9 +1262,10 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
12621262
if (method === "setStatus") {
12631263
const statusKey = recordString(event, "statusKey");
12641264
if (statusKey === PI_T3_SESSION_END_STATUS_KEY) {
1265-
// The outgoing jobs can never clear their statuses now.
1265+
// The outgoing jobs can never clear their statuses now. A reload
1266+
// restarts extensions in the same session, so its binding stays.
12661267
const outgoingState = backgroundWorkState;
1267-
backgroundWorkState = null;
1268+
if (recordString(event, "statusText") !== "reload") backgroundWorkState = null;
12681269
if (backgroundWork.size === 0) return;
12691270
backgroundWork.clear();
12701271
if (outgoingState !== null) yield* syncBackgroundWork(outgoingState);

‎packages/provider-pi/src/server/mcpExtensionSource.test.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,14 @@ describe("Pi session teardown", () => {
8585
notify: assert.fail,
8686
setStatus: (key: string, text: string | undefined) => statuses.push([key, text]),
8787
};
88-
await bridge.handlers.get("session_shutdown")!({ systemPrompt: "" }, { ui });
89-
assert.deepStrictEqual(statuses, [[PI_T3_SESSION_END_STATUS_KEY, "ended"]]);
88+
const shutdown = bridge.handlers.get("session_shutdown")!;
89+
for (const reason of ["reload", "new"]) {
90+
await shutdown(Object.assign({ systemPrompt: "" }, { reason }), { ui });
91+
}
92+
assert.deepStrictEqual(statuses, [
93+
[PI_T3_SESSION_END_STATUS_KEY, "reload"],
94+
[PI_T3_SESSION_END_STATUS_KEY, "new"],
95+
]);
9096
});
9197
});
9298

‎packages/provider-pi/src/server/mcpExtensionSource.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ export const T3_PI_MCP_EXTENSION_PATH_ENV = "T3_PI_MCP_EXTENSION_PATH";
1818
export const PI_FILE_CHANGE_TOOLS = ["edit", "write"] as const;
1919

2020
/**
21-
* The bridge sets this status as Pi tears down a session. Pi's RPC stream does
22-
* not mark the boundary otherwise, and the outgoing extension context dies here.
21+
* The bridge sets this status, with Pi's shutdown reason as its text, as Pi
22+
* tears down a session. Pi's RPC stream does not mark the boundary otherwise,
23+
* and the outgoing extension contexts die here.
2324
*/
2425
export const PI_T3_SESSION_END_STATUS_KEY = "t3:session-end";
2526

@@ -348,8 +349,8 @@ export default async function t3McpExtension(pi: ExtensionAPI) {
348349
349350
// T3 drops the outgoing session's background work here, where Pi's own TUI
350351
// clears extension statuses before invalidating their contexts.
351-
pi.on("session_shutdown", (_event, ctx) => {
352-
ctx.ui.setStatus(SESSION_END_STATUS_KEY, "ended");
352+
pi.on("session_shutdown", (event, ctx) => {
353+
ctx.ui.setStatus(SESSION_END_STATUS_KEY, event.reason ?? "ended");
353354
});
354355
355356
const endpoint = env(URL_ENV);

0 commit comments

Comments
 (0)