Skip to content

Commit d20e566

Browse files
committed
fix(pi): short-lived Pi processes no longer lose rotated OAuth tokens
T3 starts `pi --mode rpc` for jobs it stops as soon as they answer: provider status and per-workspace command discovery, text generation and thread forks. Every RPC-mode Pi starts a background model-catalog refresh at launch, and that refresh also renews any expired OAuth credential. When T3's stop (SIGTERM milliseconds after the last answer, or taskkill /T /F on Windows) lands after the token request has left Pi but before the response is saved, the provider has already spent the single-use refresh token and auth.json keeps the spent one. The next refresh fails with invalid_grant and the user must log in again. With ChatGPT sign-in's one-hour tokens, the 5-minute status probe ends the login after the first idle hour. buildPiRpcLaunch takes `shortLived`, and those four spawn sites pass it. A short-lived process runs with --offline, which skips Pi's startup network work, the catalog refresh included; the request-path token refresh is unaffected. A user's own --offline is not repeated. Thread processes keep their launch arguments, so they still refresh the catalog that discovery then reads from Pi's stored copy.
1 parent cefb544 commit d20e566

8 files changed

Lines changed: 63 additions & 0 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,6 +1591,9 @@ describe("PiAdapterV2", () => {
15911591
arg === "--extension" ? [spawn.args[index + 1]] : [],
15921592
);
15931593
assert.isFalse(spawn.args.includes("--no-extensions"));
1594+
// A thread's long-lived process keeps Pi's startup model refresh unless the user opts out.
1595+
assert.notInclude(spawn.args, "--offline");
1596+
assert.isUndefined(spawn.env.PI_OFFLINE);
15941597
assert.isTrue(extensions.some((path) => path?.endsWith("pi-t3-mcp-extension.ts")));
15951598
assert.equal(spawn.env.T3_MCP_URL, "http://127.0.0.1:43123/mcp");
15961599
assert.equal(spawn.env.T3_MCP_BEARER_TOKEN, "secret-pi-token");
@@ -1891,6 +1894,7 @@ describe("PiAdapterV2", () => {
18911894
assert.equal(args[args.indexOf("--fork") + 1], FAKE_SESSION_FILE);
18921895
assert.include(args, "--no-extensions");
18931896
assert.include(args, "--no-tools");
1897+
assert.include(args, "--offline");
18941898
assert.notInclude(args, "--no-session");
18951899
assert.deepEqual(
18961900
forkFake

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3103,6 +3103,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
31033103
environment: options.environment,
31043104
mcpSession: undefined,
31053105
extensionPath: undefined,
3106+
shortLived: true,
31063107
disableExtensions: true,
31073108
disableTools: true,
31083109
});

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,17 @@ it.layer(layerTest)("PiDriver workspace discovery", (it) => {
169169
["--approve", "--skill", "extra skill", "--no-session"],
170170
);
171171
assert.equal(workspaceLaunch!.options.env?.PI_CODING_AGENT_DIR, "/isolated-pi");
172+
// Discovery processes are stopped as soon as they answer, so neither may start Pi's
173+
// network refresh at launch.
174+
for (const cwd of ["/machine", "/first"]) {
175+
const discovery = launches.find(
176+
(launch) => launch.options.cwd === cwd && launch.args.includes("--mode"),
177+
);
178+
assert.deepEqual(
179+
discovery?.args.filter((arg) => arg === "--offline"),
180+
["--offline"],
181+
);
182+
}
172183
}).pipe(Effect.scoped),
173184
);
174185

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ describe("pi T3 MCP injection", () => {
102102
extensionPath: "/tmp/cache/pi-t3-mcp-extension.ts",
103103
sessionPath: "/fake/persisted.jsonl",
104104
ephemeral: true,
105+
shortLived: true,
105106
disableExtensions: true,
106107
disableTools: true,
107108
});
@@ -111,9 +112,19 @@ describe("pi T3 MCP injection", () => {
111112
"--no-session",
112113
"--provider",
113114
"anthropic",
115+
"--offline",
114116
"--no-extensions",
115117
"--no-tools",
116118
]);
119+
const alreadyOffline = buildPiRpcLaunch({
120+
launchArgs: ["--offline"],
121+
environment: {},
122+
mcpSession: undefined,
123+
extensionPath: undefined,
124+
ephemeral: true,
125+
shortLived: true,
126+
});
127+
assert.deepEqual(alreadyOffline.args, ["--mode", "rpc", "--no-session", "--offline"]);
117128
assert.deepInclude(resolvePiLaunchArgs("--mode text"), {
118129
ok: false,
119130
message: "Pi launch argument '--mode' is controlled by T3 Code and cannot be overridden.",

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ export function buildPiRpcLaunch(input: {
244244
readonly extensionPath: string | undefined;
245245
readonly sessionPath?: string | undefined;
246246
readonly ephemeral?: boolean;
247+
// T3 stops these processes as soon as they answer. They run --offline because Pi's startup
248+
// catalog refresh renews expired OAuth tokens, and a stop mid-request loses the rotated one.
249+
readonly shortLived?: boolean;
247250
readonly disableExtensions?: boolean;
248251
readonly disableTools?: boolean;
249252
readonly runtimeMode?: "approval-required" | "auto-accept-edits" | "auto" | "full-access";
@@ -268,6 +271,7 @@ export function buildPiRpcLaunch(input: {
268271
? ["--session", input.sessionPath]
269272
: []),
270273
...launchArgs,
274+
...(input.shortLived === true && !launchArgs.includes("--offline") ? ["--offline"] : []),
271275
// Restrictions must follow launch args so user flags cannot re-enable unattended tools/extensions.
272276
...(input.disableExtensions === true ? ["--no-extensions"] : []),
273277
...(input.disableTools === true ? ["--no-tools"] : []),

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ const makePiDiscoveryConnection = Effect.fnUntraced(function* (
150150
mcpSession: undefined,
151151
extensionPath: undefined,
152152
ephemeral: true,
153+
shortLived: true,
153154
});
154155
const connection = yield* makePiRpcConnection({
155156
command: piSettings.binaryPath || "pi",

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as Queue from "effect/Queue";
77
import * as Schema from "effect/Schema";
88
import * as Sink from "effect/Sink";
99
import * as Stream from "effect/Stream";
10+
import * as ChildProcess from "effect/process/ChildProcess";
1011
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
1112

1213
import { makePiTextGeneration } from "./textGeneration.ts";
@@ -68,6 +69,35 @@ const makeFakePi = (
6869
return { spawner, received };
6970
});
7071

72+
it.effect("runs Pi offline, since the process is stopped as soon as it answers", () =>
73+
Effect.gen(function* () {
74+
const pi = yield* makeFakePi(JSON.stringify({ subject: "Update README", body: "" }));
75+
const launches: Array<ReadonlyArray<string>> = [];
76+
const spawner = ChildProcessSpawner.make((command) => {
77+
if (ChildProcess.isStandardCommand(command)) launches.push(command.args.map(String));
78+
return pi.spawner.spawn(command);
79+
});
80+
const textGeneration = yield* makePiTextGeneration(
81+
{ enabled: true, binaryPath: "pi", launchArgs: "", customModels: [] },
82+
{},
83+
).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner));
84+
85+
const result = yield* textGeneration.generateCommitMessage({
86+
cwd: process.cwd(),
87+
branch: "main",
88+
stagedSummary: "M README.md",
89+
stagedPatch: "diff --git a/README.md b/README.md",
90+
modelSelection: createModelSelection(ProviderInstanceId.make("pi"), "default"),
91+
});
92+
93+
assert.equal(result.subject, "Update README");
94+
assert.deepEqual(
95+
launches.map((args) => args.filter((arg) => arg === "--offline")),
96+
[["--offline"]],
97+
);
98+
}),
99+
);
100+
71101
it.effect("names the model and the disabled extensions when Pi cannot load it", () =>
72102
Effect.gen(function* () {
73103
const pi = yield* makeFakePi("", "Model not found: demo/demo-model");

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export const makePiTextGeneration = Effect.fn("makePiTextGeneration")(function*
4747
mcpSession: undefined,
4848
extensionPath: undefined,
4949
ephemeral: true,
50+
shortLived: true,
5051
disableExtensions: true,
5152
// Background naming/content helpers must never mutate the workspace.
5253
disableTools: true,

0 commit comments

Comments
 (0)