@devries-cameron20 / t3code Mirror · read-only / commits / 26dc3688e2
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 12:00 UTC

fix(pi): show the provider error when Pi text generation fails (#23741)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Victor Vogel committed Oct 30, 2026 at 15:58 UTC 26dc3688e2c927053bc3613fff1e506328a1a430
2 files changed +94 -3
packages/provider-pi/src/server/textGeneration.test.ts
+73 -1
@@ -14,7 +14,11 @@ import { makePiTextGeneration } from "./textGeneration.ts";
14 const decodeJsonLine = Schema.decodeUnknownSync(Schema.fromJsonString(Schema.Unknown));
15 const encodeJsonLine = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));
16
17 -const makeFakePi = (reply: string, setModelError?: string) =>
17 +const makeFakePi = (
18 + reply: string,
19 + setModelError?: string,
20 + turnEvents: ReadonlyArray<Record<string, unknown>> = [],
21 +) =>
22 Effect.gen(function* () {
23 const stdout = yield* Queue.unbounded<Uint8Array, Cause.Done>();
24 const received: Array<Record<string, unknown>> = [];
@@ -32,6 +36,7 @@ const makeFakePi = (reply: string, setModelError?: string) =>
36 const response = { type: "response", id: record["id"], command: record["type"] };
37 if (record["type"] === "prompt") {
38 yield* emit({ ...response, success: true });
39 + for (const event of turnEvents) yield* emit(event);
40 yield* emit({ type: "agent_settled" });
41 } else if (record["type"] === "get_last_assistant_text") {
42 yield* emit({ ...response, success: true, data: { text: reply } });
@@ -88,3 +93,70 @@ it.effect("names the model and the disabled extensions when Pi cannot load it",
93 assert.isUndefined(pi.received.find((record) => record["type"] === "prompt"));
94 }),
95 );
96 +
97 +const failedTurnWith = (errorMessage: string) => ({
98 + type: "message_end",
99 + message: { role: "assistant", stopReason: "error", errorMessage },
100 +});
101 +
102 +const failedTurn = failedTurnWith(
103 + '401: {"error":{"message":"Incorrect API key provided: sk-test-SECRET123"}}',
104 +);
105 +
106 +const generateCommitMessage = (turnEvents: ReadonlyArray<Record<string, unknown>>, reply = "") =>
107 + Effect.gen(function* () {
108 + const pi = yield* makeFakePi(reply, undefined, turnEvents);
109 + const textGeneration = yield* makePiTextGeneration(
110 + { enabled: true, binaryPath: "pi", launchArgs: "", customModels: [] },
111 + {},
112 + ).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, pi.spawner));
113 + return yield* textGeneration.generateCommitMessage({
114 + cwd: process.cwd(),
115 + branch: "main",
116 + stagedSummary: "M README.md",
117 + stagedPatch: "diff --git a/README.md b/README.md",
118 + modelSelection: createModelSelection(ProviderInstanceId.make("pi"), "default"),
119 + });
120 + });
121 +
122 +it.effect("reports Pi's turn error when the reply is empty", () =>
123 + Effect.gen(function* () {
124 + const error = yield* generateCommitMessage([failedTurn]).pipe(Effect.flip);
125 +
126 + assert.equal(error._tag, "TextGenerationError");
127 + assert.equal(error.detail, "Pi could not generate text: the model provider returned HTTP 401.");
128 + }),
129 +);
130 +
131 +it.effect("keeps Pi's raw error text out of the error", () =>
132 + Effect.gen(function* () {
133 + const error = yield* generateCommitMessage([
134 + failedTurnWith("Invalid API key sk-test-SECRET123"),
135 + ]).pipe(Effect.flip);
136 +
137 + assert.equal(error.detail, "Pi could not generate text: the model provider reported an error.");
138 + assert.notInclude(error.message, "SECRET123");
139 + }),
140 +);
141 +
142 +it.effect("keeps the reply when Pi answers after a failed attempt", () =>
143 + Effect.gen(function* () {
144 + const result = yield* generateCommitMessage(
145 + [failedTurn, { type: "message_end", message: { role: "assistant", stopReason: "stop" } }],
146 + JSON.stringify({ subject: "Update README", body: "" }),
147 + );
148 +
149 + assert.equal(result.subject, "Update README");
150 + }),
151 +);
152 +
153 +it.effect("drops an earlier error once a later attempt succeeds", () =>
154 + Effect.gen(function* () {
155 + const error = yield* generateCommitMessage([
156 + failedTurn,
157 + { type: "message_end", message: { role: "assistant", stopReason: "stop" } },
158 + ]).pipe(Effect.flip);
159 +
160 + assert.equal(error.detail, "Pi returned empty output.");
161 + }),
162 +);
packages/provider-pi/src/server/textGeneration.ts
+21 -2
@@ -7,7 +7,7 @@ import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
7 import { TextGenerationError } from "@t3tools/contracts";
8 import type { PiSettings } from "../settings.ts";
9
10 -import { makePiRpcConnection, parsePiModelSlug } from "./rpc.ts";
10 +import { makePiRpcConnection, parsePiModelSlug, piRecordString } from "./rpc.ts";
11 import { buildPiRpcLaunch, resolvePiLaunchArgs } from "./mcpInjection.ts";
12 import * as TextGenerationOperations from "@t3tools/provider-core/server/textGenerationOperations";
13
@@ -15,6 +15,15 @@ const PI_TIMEOUT_MS = 180_000;
15
16 const isTextGenerationError = Schema.is(TextGenerationError);
17
18 +// Pi's `errorMessage` is the provider's raw response text and can echo
19 +// credentials, so only a leading HTTP status reaches the error detail.
20 +const piTurnErrorDetail = (errorMessage: string | undefined) => {
21 + const status = errorMessage?.match(/^([45]\d\d)\b/)?.[1];
22 + return status === undefined
23 + ? "Pi could not generate text: the model provider reported an error."
24 + : `Pi could not generate text: the model provider returned HTTP ${status}.`;
25 +};
26 +
27 export const makePiTextGeneration = Effect.fn("makePiTextGeneration")(function* (
28 piSettings: PiSettings,
29 environment: NodeJS.ProcessEnv = process.env,
@@ -79,10 +88,20 @@ export const makePiTextGeneration = Effect.fn("makePiTextGeneration")(function*
88 }
89
90 yield* connection.request({ type: "prompt", message: prompt });
91 + // Provider failures (bad key, rate limit, 5xx) arrive on the assistant
92 + // `message_end`. Keep only the latest, since Pi can retry and still answer.
93 + let turnError: string | undefined;
94 yield* Effect.gen(function* () {
95 while (true) {
96 const event = yield* Queue.take(connection.events);
97 if (event["type"] === "agent_settled") return;
98 + const message = event["message"];
99 + if (event["type"] === "message_end" && piRecordString(message, "role") === "assistant") {
100 + turnError =
101 + piRecordString(message, "stopReason") === "error"
102 + ? piTurnErrorDetail(piRecordString(message, "errorMessage"))
103 + : undefined;
104 + }
105 }
106 });
107 const data = yield* connection.request({ type: "get_last_assistant_text" });
@@ -95,7 +114,7 @@ export const makePiTextGeneration = Effect.fn("makePiTextGeneration")(function*
114 if (!text) {
115 return yield* new TextGenerationError({
116 operation,
98 - detail: "Pi returned empty output.",
117 + detail: turnError ?? "Pi returned empty output.",
118 });
119 }
120 return yield* TextGenerationOperations.decodeJsonReply(request, "Pi", text);