Skip to content

Commit 0d691cb

Browse files
PR Batch Testerclaude
andcommitted
fix(pi): keep the session alive while an extension reports background work
Pi has no native signal for extension work that outlives a turn, so T3 released the Pi process after 30 idle minutes and killed that work. An extension now holds a status under the `t3:background` key (or `t3:background:<job>`) while it works, and the idle release waits for it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 5bb54a0 commit 0d691cb

3 files changed

Lines changed: 80 additions & 1 deletion

File tree

‎docs/user/providers-pi.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ Pi loads its normal user and project extensions. Blocking `select`, `confirm`, `
5151
dialogs work in T3 Code. Notifications appear in the work log. Pi terminal decoration such as
5252
titles, status lines, and widgets does not have a T3 Code equivalent.
5353

54+
T3 Code stops an idle Pi process after 30 minutes. An extension that keeps working after a turn
55+
ends, such as a background command, can keep the process alive by holding a status under the
56+
`t3:background` key, or `t3:background:<job>` for several jobs, until the work ends:
57+
`ctx.ui.setStatus("t3:background:build", "Build running")`, then
58+
`ctx.ui.setStatus("t3:background:build", undefined)`. The process stays up for at most four hours of
59+
background work. When the extension wakes the agent with the result, the reply appears in the
60+
thread.
61+
5462
## Permission Modes
5563

5664
T3 Code applies the composer permission mode through Pi's blocking tool hook:

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,53 @@ describe("PiAdapterV2", () => {
12681268
}).pipe(Effect.scoped, Effect.provide(layerTest)),
12691269
);
12701270

1271+
it.effect("keeps the session while an extension reports background work", () =>
1272+
Effect.gen(function* () {
1273+
const fake = yield* makeFakePi;
1274+
const { runtime, takeEvent } = yield* openRuntime(fake);
1275+
const providerThread = yield* runtime.ensureThread({
1276+
threadId: THREAD_ID,
1277+
modelSelection: modelSelection("default"),
1278+
runtimePolicy,
1279+
});
1280+
let barrier = 0;
1281+
const setStatus = Effect.fnUntraced(function* (statusKey: string, statusText?: string) {
1282+
yield* fake.emit({
1283+
type: "extension_ui_request",
1284+
id: `status-${statusKey}`,
1285+
method: "setStatus",
1286+
statusKey,
1287+
...(statusText === undefined ? {} : { statusText }),
1288+
});
1289+
// setStatus is fire-and-forget; a dialog marks that Pi's earlier output was handled.
1290+
barrier += 1;
1291+
yield* fake.emit({
1292+
type: "extension_ui_request",
1293+
id: `barrier-${barrier}`,
1294+
method: "confirm",
1295+
title: `Barrier ${barrier}`,
1296+
});
1297+
yield* takeEvent(
1298+
(event) =>
1299+
event.type === "runtime_request.updated" &&
1300+
event.runtimeRequest.nativeRequestRef?.nativeId === `barrier-${barrier}`,
1301+
);
1302+
});
1303+
1304+
yield* setStatus("lint", "Lint clean");
1305+
assert.isFalse(yield* runtime.hasPendingBackgroundWork!);
1306+
yield* setStatus("t3:background:build", "Build running");
1307+
yield* setStatus("t3:background:tests", "Tests running");
1308+
assert.isTrue(yield* runtime.hasPendingBackgroundWork!);
1309+
// Completion arrives as a native wake, so the settled run stays closed.
1310+
assert.isFalse(yield* runtime.hasPendingBackgroundWorkForThread!(providerThread));
1311+
yield* setStatus("t3:background:build");
1312+
assert.isTrue(yield* runtime.hasPendingBackgroundWork!);
1313+
yield* setStatus("t3:background:tests", "");
1314+
assert.isFalse(yield* runtime.hasPendingBackgroundWork!);
1315+
}).pipe(Effect.scoped, Effect.provide(layerTest)),
1316+
);
1317+
12711318
it.effect.each(["archive", "stop", "transport death"] as const)(
12721319
"retiring a wake by %s cancels visible dialogs and invalidates its offer",
12731320
(retirement) =>

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ const PI_SESSION_TIMEOUT_MS = 60_000;
9090
const PI_UNSOLICITED_ACTIVITY_ERROR =
9191
"Pi started agent work outside an active T3 turn. The session was stopped to prevent invisible tool execution.";
9292
const SETTLE_PROBE_MAX_ATTEMPTS = 3;
93+
/**
94+
* Pi has no native signal for extension work that outlives a turn. Extensions
95+
* hold a status under this key, or `<key>:<job>`, until the work ends, so the
96+
* idle release keeps the process (and the work) alive.
97+
*/
98+
const PI_BACKGROUND_WORK_STATUS_KEY = "t3:background";
9399
const SETTLE_PROBE_RETRY_DELAY = Duration.millis(100);
94100

95101
const PiProviderCapabilitiesV2 = {
@@ -539,6 +545,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
539545
let stopRequested = false;
540546
let unsolicitedActivityDetected = false;
541547
let pendingWake: PiWake | null = null;
548+
const backgroundWorkStatusKeys = new Set<string>();
542549
let rollbackBarrier: Deferred.Deferred<
543550
void,
544551
ProviderAdapter.ProviderAdapterProtocolError
@@ -1234,6 +1241,21 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
12341241
const method = recordString(event, "method");
12351242
const nativeRequestId = recordString(event, "id");
12361243
if (method === undefined) return;
1244+
if (method === "setStatus") {
1245+
const statusKey = recordString(event, "statusKey");
1246+
if (
1247+
statusKey !== PI_BACKGROUND_WORK_STATUS_KEY &&
1248+
statusKey?.startsWith(`${PI_BACKGROUND_WORK_STATUS_KEY}:`) !== true
1249+
) {
1250+
return;
1251+
}
1252+
if ((recordString(event, "statusText") ?? "").length > 0) {
1253+
backgroundWorkStatusKeys.add(statusKey);
1254+
} else {
1255+
backgroundWorkStatusKeys.delete(statusKey);
1256+
}
1257+
return;
1258+
}
12371259
if (method === "notify") {
12381260
const state = threadState;
12391261
const turn = state?.activeTurn ?? null;
@@ -2374,7 +2396,9 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
23742396
return sessionEntity;
23752397
},
23762398
events: Stream.fromQueue(events),
2377-
hasPendingBackgroundWork: Effect.sync(() => pendingWake !== null),
2399+
hasPendingBackgroundWork: Effect.sync(
2400+
() => pendingWake !== null || backgroundWorkStatusKeys.size > 0,
2401+
),
23782402
hasPendingBackgroundWorkForThread: (providerThread) =>
23792403
Effect.sync(
23802404
() => pendingWake !== null && pendingWake.state.providerThread.id === providerThread.id,

0 commit comments

Comments
 (0)