Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(agent-runtime): parse OpenAI "Error code: NNN" statuses

koeria9f0cd68502f6
diff --git a/docs/spec/03-runtime/08-error-codes.md b/docs/spec/03-runtime/08-error-codes.md
index 5bd8090c1..a7d1e5cc7 100644
--- a/docs/spec/03-runtime/08-error-codes.md
+++ b/docs/spec/03-runtime/08-error-codes.md
@@ -91,12 +91,14 @@ does not turn temporary thread pressure into a host process exit.
### 3.2 Agent / session
Provider status parsing also recognizes DashScope `Code: <status>` envelopes
-at the start of an error message. A streamed `InvalidParameter: <400>`
+at the start of an error message, and an OpenAI SDK `Error code: NNN` prefix
+(HTTP 400-599) at the start of the message. A streamed `InvalidParameter: <400>`
rejection is terminal even when the enclosing HTTP response was 200:
resending unchanged tool history cannot repair it. Structured error status
fields retain precedence; arbitrary angle-bracket numbers in prose are not
-HTTP statuses. Authentication, rate-limit and transient 5xx envelopes retain
-their existing classifications.
+HTTP statuses, and neither is a bare `code: NNN` business field such as
+SiliconFlow `30001` or Zhipu JSON `"code":1308`. Authentication, rate-limit
+and transient 5xx envelopes retain their existing classifications.
| code | retriable | meaning |
|---|---|---|
diff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.md
index f7347948c..0044be0e6 100644
--- a/docs/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/spec/06-delivery/04-e2e-test-plan.md
@@ -112,6 +112,24 @@ being fetched on every subsequent poll.
- **Automated coverage:** `agent-errors.test.ts` and `provider-retry.test.ts`.
- **Specs:** `03-runtime/08-error-codes.md` §3.2.
+## E2E-PROVIDER-openai-error-code-status
+
+- **Preconditions:** Isolated Agent runtime classifier; no credentials, network,
+ or user history.
+- **Steps:** Classify messages that begin `Error code: 402`, `Error code: 401`,
+ `Error code: 429`, and `Error code: 404`, including a body that also contains
+ a parenthesized status. Also classify SiliconFlow-style `Error code: 30001`,
+ prose that only mentions `error code: 500`, a bare `code: 401`, and a Zhipu
+ `"code":1308` body.
+- **Expected:** `providerStatus` is the prefix and the status table applies:
+ 402 is terminal `PROVIDER_ERROR`, 401 is `PROVIDER_UNAUTHORIZED`, 429 is
+ `PROVIDER_RATE_LIMITED`, and 404 is `MODEL_NOT_CONFIGURED`. The prefix wins
+ over a later parenthesized number. Business codes and prose do not become an
+ HTTP status; Zhipu `1308` stays terminal `PROVIDER_ERROR` without
+ `providerStatus`. A structured `status` field still wins over the prefix.
+- **Automated coverage:** `packages/agent-runtime/src/agent-errors.test.ts`.
+- **Specs:** `03-runtime/08-error-codes.md` §3.2.
+
## E2E-TASK-DOCK-tab-content-isolation
diff --git a/packages/agent-runtime/src/agent-errors.test.ts b/packages/agent-runtime/src/agent-errors.test.ts
index 2c89cc909..a6d5103c4 100644
--- a/packages/agent-runtime/src/agent-errors.test.ts
+++ b/packages/agent-runtime/src/agent-errors.test.ts
@@ -750,6 +750,81 @@ describe("classifyAgentError", () => {
});
});
+ // pi-ai often keeps only the OpenAI SDK string "Error code: <status> - <body>"
+ // and drops err.status. The prefix is the HTTP status; a bare business code is not.
+ it.each([
+ [
+ "Error code: 402 - {'error': {'message': 'Insufficient Balance', 'type': 'invalid_request_error'}}",
+ "PROVIDER_ERROR",
+ false,
+ 402,
+ ],
+ [
+ "Error code: 401 - Incorrect API key provided",
+ "PROVIDER_UNAUTHORIZED",
+ false,
+ 401,
+ ],
+ [
+ "Error code: 429 - Rate limit reached for model",
+ "PROVIDER_RATE_LIMITED",
+ true,
+ 429,
+ ],
+ [
+ "Error code: 404 - The model does not exist",
+ "MODEL_NOT_CONFIGURED",
+ false,
+ 404,
+ ],
+ [
+ "Error code: 402 - see note (500)",
+ "PROVIDER_ERROR",
+ false,
+ 402,
+ ],
+ ])(
+ "reads an OpenAI SDK Error code prefix as the HTTP status: %s",
+ (message, code, retriable, status) => {
+ expect(classifyAgentError(new Error(message))).toMatchObject({
+ code,
+ retriable,
+ details: { providerStatus: status },
+ });
+ },
+ );
+
+ it("does not read a business code or prose as an OpenAI Error code status", () => {
+ const silicon = classifyAgentError(new Error("Error code: 30001 - account balance"));
+ expect(silicon.details?.providerStatus).toBeUndefined();
+ expect(silicon).toMatchObject({ code: "PROVIDER_ERROR", retriable: true });
+
+ const bare = classifyAgentError(new Error("provider said code: 401"));
+ expect(bare.details?.providerStatus).toBeUndefined();
+ expect(bare).toMatchObject({ code: "PROVIDER_ERROR", retriable: true });
+
+ const prose = classifyAgentError(new Error("see the error code: 500 in the handbook"));
+ expect(prose.details?.providerStatus).toBeUndefined();
+ expect(prose).toMatchObject({ code: "PROVIDER_ERROR", retriable: true });
+
+ const zhipu = classifyAgentError(
+ '{"error":{"code":"1308","message":"Usage limit reached"}}',
+ );
+ expect(zhipu.details?.providerStatus).toBeUndefined();
+ expect(zhipu).toMatchObject({ code: "PROVIDER_ERROR", retriable: false });
+ });
+
+ it("keeps a structured status ahead of an OpenAI Error code prefix", () => {
+ expect(classifyAgentError(Object.assign(
+ new Error("Error code: 402 - Insufficient Balance"),
+ { status: 503 },
+ ))).toMatchObject({
+ code: "PROVIDER_ERROR",
+ retriable: true,
+ details: { providerStatus: 503 },
+ });
+ });
+
it("does not treat a bare credits mention or a 429 rate limit as credit exhaustion", () => {
expect(classifyAgentError(new Error("credits"))).toMatchObject({
code: "PROVIDER_ERROR",
diff --git a/packages/agent-runtime/src/agent-errors.ts b/packages/agent-runtime/src/agent-errors.ts
index 610d8d21a..73c59d03d 100644
--- a/packages/agent-runtime/src/agent-errors.ts
+++ b/packages/agent-runtime/src/agent-errors.ts
@@ -126,11 +126,12 @@ function redactSensitiveErrorText(message: string): string {
/**
* Probe the HTTP status across SDK error shapes: `status`/`statusCode`
- * fields (walking the `cause` chain), then a leading "<status>:" or a
- * "(status)" / "status code NNN" marker, or a DashScope "Code: <status>"
- * envelope in the message. Gemini's
+ * fields (walking the `cause` chain), then a leading "<status>:", an
+ * OpenAI SDK "Error code: NNN" prefix, a "(status)" / "status code NNN"
+ * marker, or a DashScope "Code: <status>" envelope in the message. Gemini's
* `@google/genai` ApiError.message is the JSON body with no prefix, so the
- * last probe reads a 400-599 `code` from that object.
+ * last probe reads a 400-599 `code` from that object. A bare `code: NNN`
+ * business field is not an HTTP status.
*/
function extractStatus(err: unknown, message: string): number | undefined {
let current: any = err;
@@ -143,6 +144,10 @@ function extractStatus(err: unknown, message: string): number | undefined {
/^\s*[A-Za-z][A-Za-z0-9_.-]*:\s*<(\d{3})>/,
/^\s*(\d{3})\s*:/,
/^\s*(\d{3})\b/,
+ // OpenAI Node/Python SDK: "Error code: 402 - …". Anchored so a later
+ // "(NNN)" or a business `code` field cannot replace the prefix. The
+ // word boundary keeps a longer code such as 30001 from matching.
+ /^\s*error\s+code\s*:\s*(\d{3})\b/i,
/\((\d{3})\)/,
/status(?: code)?[ :]+(\d{3})\b/i,
];