Skip to content

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

Merge main and retain both provider error classification contracts

vastsa32e911f56e90
diff --cc docs/spec/03-runtime/08-error-codes.md
index cceed87c3,8981b1b31..393087826
--- a/docs/spec/03-runtime/08-error-codes.md
+++ b/docs/spec/03-runtime/08-error-codes.md
@@@ -387,11 -387,9 +387,13 @@@ stays `PROVIDER_RATE_LIMITED`. An unpre
HTTP status in `error.code` (Gemini's `@google/genai` `ApiError.message`) is
classified from that 400-599 code the same way as a `<status>:` message:
400/422 stay terminal unless they report a context overflow, 404 is
- `MODEL_NOT_CONFIGURED`, and 429 / 5xx stay retryable. 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`; `permission denied` and a bare `unauthorized` on
- those statuses stay `PROVIDER_ERROR`.
+ `MODEL_NOT_CONFIGURED`, 429 stays retryable, and 5xx stay retryable unless
+ the body reports a context overflow that is not a rate limit, which is
-terminal `CONTEXT_TOO_LARGE`.
++terminal `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`;
++`permission denied` and a bare `unauthorized` on those statuses stay
++`PROVIDER_ERROR`.
An exact `terminated` provider message and equivalent premature stream-close
messages map to `STREAM_FAILED`. A message is `TURN_ABORTED` only for a
diff --cc packages/agent-runtime/src/agent-errors.ts
index 0f9ba51fe,2d762f40a..c67df54a1
--- a/packages/agent-runtime/src/agent-errors.ts
+++ b/packages/agent-runtime/src/agent-errors.ts
@@@ -660,15 -643,26 +660,32 @@@ export function classifyAgentError(err
if (status === 413) return result("CONTEXT_TOO_LARGE", false);
if (status === 429) return result("PROVIDER_RATE_LIMITED", true);
if (status === 404) return result("MODEL_NOT_CONFIGURED", false);
- if (status >= 500) return result("PROVIDER_ERROR", true);
+ // Gateways wrap an upstream context overflow as 500/502/503. Ollama's MLX
+ // runner returns HTTP 500 for "input length (N tokens) exceeds the model's
+ // maximum context length (M tokens)", which pi-ai's LiteLLM rule does not
+ // match ("(M)" or " of M tokens", not "(M tokens)"). Re-sending that prompt
+ // fails the same way, so it is terminal. A throttle that also says "too
+ // many tokens" stays retryable: `CONTEXT_PATTERN` would undo the rate-limit
+ // exclusion inside `isContextOverflow`, and the statusless branch already
+ // probes `RATE_LIMIT_PATTERN` first. 400/422 skip that probe because both
+ // of their outcomes are terminal.
+ if (status >= 500) {
+ if (
+ !RATE_LIMIT_PATTERN.test(rawMessage) &&
+ isContextOverflowMessage(rawMessage)
+ ) {
+ return result("CONTEXT_TOO_LARGE", false);
+ }
+ return result("PROVIDER_ERROR", true);
+ }
if (status === 400 || status === 422) {
if (isContextOverflowMessage(rawMessage)) return result("CONTEXT_TOO_LARGE", false);
+ // Only an explicit API-key rejection is a credential failure here.
+ // "unauthorized" / "permission denied" stay PROVIDER_ERROR: a 400 often
+ // uses those words for a parameter or tool problem, not the key.
+ if (API_KEY_REJECTION_PATTERN.test(rawMessage)) {
+ return result("PROVIDER_UNAUTHORIZED", false);
+ }
// Malformed request (wrong apiStyle, bad params) — retrying won't help.
return result("PROVIDER_ERROR", false);
}