Skip to content

Commit 592b238

Browse files
only21milclaude
andcommitted
fix(pi): keep Pi's raw provider error out of text generation errors
Pi's errorMessage is the model provider's raw response and can echo credentials. The TextGenerationError detail now carries only a leading HTTP status, or a generic provider-error line when there is none. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 6599356 commit 592b238

2 files changed

Lines changed: 30 additions & 9 deletions

File tree

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,14 @@ it.effect("names the model and the disabled extensions when Pi cannot load it",
9494
}),
9595
);
9696

97-
const failedTurn = {
97+
const failedTurnWith = (errorMessage: string) => ({
9898
type: "message_end",
99-
message: { role: "assistant", stopReason: "error", errorMessage: "401 Invalid API key" },
100-
};
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+
);
101105

102106
const generateCommitMessage = (turnEvents: ReadonlyArray<Record<string, unknown>>, reply = "") =>
103107
Effect.gen(function* () {
@@ -120,7 +124,18 @@ it.effect("reports Pi's turn error when the reply is empty", () =>
120124
const error = yield* generateCommitMessage([failedTurn]).pipe(Effect.flip);
121125

122126
assert.equal(error._tag, "TextGenerationError");
123-
assert.equal(error.detail, "Pi could not generate text: 401 Invalid API key");
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");
124139
}),
125140
);
126141

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ const PI_TIMEOUT_MS = 180_000;
1515

1616
const isTextGenerationError = Schema.is(TextGenerationError);
1717

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+
1827
export const makePiTextGeneration = Effect.fn("makePiTextGeneration")(function* (
1928
piSettings: PiSettings,
2029
environment: NodeJS.ProcessEnv = process.env,
@@ -92,7 +101,7 @@ export const makePiTextGeneration = Effect.fn("makePiTextGeneration")(function*
92101
if (event["type"] === "message_end" && piRecordString(message, "role") === "assistant") {
93102
turnError =
94103
piRecordString(message, "stopReason") === "error"
95-
? (piRecordString(message, "errorMessage") ?? "Pi reported a model error.")
104+
? piTurnErrorDetail(piRecordString(message, "errorMessage"))
96105
: undefined;
97106
}
98107
}
@@ -107,10 +116,7 @@ export const makePiTextGeneration = Effect.fn("makePiTextGeneration")(function*
107116
if (!text) {
108117
return yield* new TextGenerationError({
109118
operation,
110-
detail:
111-
turnError === undefined
112-
? "Pi returned empty output."
113-
: `Pi could not generate text: ${turnError.slice(0, 1_000)}`,
119+
detail: turnError ?? "Pi returned empty output.",
114120
});
115121
}
116122
return yield* TextGenerationOperations.decodeJsonReply(request, "Pi", text);

0 commit comments

Comments
 (0)