Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
fix(agent-runtime): classify Incorrect/expired API-key wording as unauthorized
xpeng5278-webb41191bf9e2c
diff --git a/docs/spec/03-runtime/08-error-codes.md b/docs/spec/03-runtime/08-error-codes.mdindex 5bd8090c1..8b0940091 100644--- a/docs/spec/03-runtime/08-error-codes.md+++ b/docs/spec/03-runtime/08-error-codes.md@@ -434,8 +434,10 @@ classified from that 400-599 code the same way as a `<status>:` message:`MODEL_NOT_CONFIGURED`, 429 stays retryable, and 5xx stay retryable unlessthe body reports a context overflow that is not a rate limit, which isterminal `CONTEXT_TOO_LARGE`. A 400 or 422 that explicitly rejects the API-key (`invalid api key`, `api key not valid`, or `API_KEY_INVALID`, including-Gemini's `API key not valid` body) is terminal `PROVIDER_UNAUTHORIZED`;+key (`invalid api key`, `invalid x-api-key`, `incorrect api key`,+`api key expired`, `api key not valid`, or `API_KEY_INVALID`, including+Gemini's `API key not valid` body and OpenAI's statusless+`Incorrect API key provided`) is terminal `PROVIDER_UNAUTHORIZED`;`permission denied` and a bare `unauthorized` on those statuses stay`PROVIDER_ERROR`.diff --git a/packages/agent-runtime/src/agent-errors.test.ts b/packages/agent-runtime/src/agent-errors.test.tsindex 2c89cc909..4235f8edd 100644--- a/packages/agent-runtime/src/agent-errors.test.ts+++ b/packages/agent-runtime/src/agent-errors.test.ts@@ -281,6 +281,27 @@ describe("classifyAgentError", () => {});});+ // OpenAI's classic rejection and Anthropic's header name never say+ // "invalid api key". pi-ai often flattens them to a statusless+ // errorMessage, so the pattern — not only HTTP 401 — must catch them.+ it.each([+ "Incorrect API key provided",+ "Incorrect API key provided: sk-****. You can find your API key at https://platform.openai.com/account/api-keys.",+ "API key expired. Please renew the API key.",+ "invalid x-api-key",+ '{"type":"error","error":{"type":"authentication_error","message":"invalid x-api-key"}}',+ ])("classifies statusless Incorrect/expired API-key wording as unauthorized: %s", (message) => {+ expect(classifyAgentError(message)).toMatchObject({+ code: "PROVIDER_UNAUTHORIZED",+ retriable: false,+ });+ expect(classifyAgentError(new Error(message))).toMatchObject({+ code: "PROVIDER_UNAUTHORIZED",+ retriable: false,+ });+ });++it("classifies pi-ai '<status>: <body>' errorMessage strings", () => {expect(classifyAgentError('429: {"error":{"type":"rate_limit_error"}}')).toMatchObject({ code: "PROVIDER_RATE_LIMITED", retriable: true });diff --git a/packages/agent-runtime/src/agent-errors.ts b/packages/agent-runtime/src/agent-errors.tsindex 610d8d21a..8a9699067 100644--- a/packages/agent-runtime/src/agent-errors.ts+++ b/packages/agent-runtime/src/agent-errors.ts@@ -484,7 +484,10 @@ export function networkFailureDiagnostics(* Explicit API-key rejection. Safe on HTTP 400/422 as well as on a* statusless message: Gemini's `@google/genai` ApiError reports a bad key* as 400 `API key not valid` / `API_KEY_INVALID`, and some OpenAI-compatible- * gateways do the same.+ * gateways do the same. OpenAI's own SDK says `Incorrect API key provided`+ * or `API key expired`; Anthropic's Messages API says `invalid x-api-key`.+ * pi-ai often flattens those to a statusless errorMessage, so the wording+ * — not only an HTTP 401 field — must count.** Broader words such as "unauthorized" and "permission denied" stay out of* this pattern. On a 400 they can describe a bad parameter or a tool@@ -492,7 +495,7 @@ export function networkFailureDiagnostics(* below treats them as an auth failure.*/const API_KEY_REJECTION_PATTERN =- /invalid[ _]api[ _]key|api key not valid|API_KEY_INVALID/i;+ /invalid[ _]api[ _]key|invalid[ _]x-api-key|incorrect[ _]api[ _]key|api[ _]key[ _]expired|api key not valid|API_KEY_INVALID/i;/*** Credential rejection worded without an HTTP status.