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 5xx-wrapped context overflows as CONTEXT_TOO_LARGE
xpeng5278-web405a396caf54
diff --git a/docs/spec/03-runtime/08-error-codes.md b/docs/spec/03-runtime/08-error-codes.mdindex 6f7ca857f..8981b1b31 100644--- a/docs/spec/03-runtime/08-error-codes.md+++ b/docs/spec/03-runtime/08-error-codes.md@@ -387,7 +387,9 @@ stays `PROVIDER_RATE_LIMITED`. An unprefixed JSON error body that carries anHTTP status in `error.code` (Gemini's `@google/genai` `ApiError.message`) isclassified 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.+`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`.An exact `terminated` provider message and equivalent premature stream-closemessages map to `STREAM_FAILED`. A message is `TURN_ABORTED` only for adiff --git a/packages/agent-runtime/src/agent-errors.test.ts b/packages/agent-runtime/src/agent-errors.test.tsindex b0168a4cf..fe80dd875 100644--- a/packages/agent-runtime/src/agent-errors.test.ts+++ b/packages/agent-runtime/src/agent-errors.test.ts@@ -462,6 +462,93 @@ describe("classifyAgentError", () => {.toMatchObject({ code: "CONTEXT_TOO_LARGE" });});+ // Ollama's MLX runner returns this as HTTP 500, and other gateways wrap the+ // same upstream overflow as 502/503. pi-ai's LiteLLM rule wants "(8192)" or+ // " of 8192 tokens", so "(8192 tokens)" is invisible to isContextOverflow.+ // The status table must not retry it as a generic 5xx.+ it("classifies a 5xx-wrapped context overflow as terminal CONTEXT_TOO_LARGE", () => {+ const body =+ "input length (9000 tokens) exceeds the model's maximum context length (8192 tokens)";+ const json = (status: number) =>+ `${status} {"error":{"message":"${body}"}}`;+ const assistant = (errorMessage: string) =>+ ({ role: "assistant", stopReason: "error", errorMessage }) as AssistantMessage;++ expect(isContextOverflow(assistant(body))).toBe(false);+ expect(isContextOverflow(assistant(json(500)))).toBe(false);++ expect(classifyAgentError(json(500))).toMatchObject({+ code: "CONTEXT_TOO_LARGE",+ retriable: false,+ details: { providerStatus: 500 },+ });+ expect(classifyAgentError(Object.assign(new Error(body), { status: 500 }))).toMatchObject({+ code: "CONTEXT_TOO_LARGE",+ retriable: false,+ details: { providerStatus: 500 },+ });+ expect(classifyProviderError(assistant(json(500)))).toMatchObject({+ code: "CONTEXT_TOO_LARGE",+ retriable: false,+ details: { providerStatus: 500 },+ });+ for (const status of [502, 503]) {+ expect(classifyAgentError(json(status))).toMatchObject({+ code: "CONTEXT_TOO_LARGE",+ retriable: false,+ details: { providerStatus: status },+ });+ }+ });++ it("keeps ordinary and rate-limited 5xx responses retryable", () => {+ for (const message of [+ "500 Internal Server Error",+ "500: Internal Server Error",+ "502: upstream connect error or disconnect/reset before headers",+ "503 Service Unavailable",+ "503: Service Unavailable",+ "503: overloaded",+ "503: Too many requests",+ "503: rate limit exceeded",+ // CONTEXT_PATTERN matches "too many tokens", but this is a throttle.+ // pi-ai excludes "rate limit"; the 5xx branch must not undo that.+ "503: Throttling error: Too many tokens, please wait before trying again.",+ "503: rate limit: too many tokens, please wait before trying again.",+ ]) {+ const classified = classifyAgentError(message);+ expect(classified.code, message).not.toBe("CONTEXT_TOO_LARGE");+ expect(classified, message).toMatchObject({+ code: "PROVIDER_ERROR",+ retriable: true,+ });+ }+ expect(classifyAgentError(Object.assign(+ new Error("Internal Server Error"),+ { status: 500 },+ ))).toMatchObject({+ code: "PROVIDER_ERROR",+ retriable: true,+ details: { providerStatus: 500 },+ });+ expect(classifyAgentError(Object.assign(+ new Error("Service Unavailable"),+ { status: 503 },+ ))).toMatchObject({+ code: "PROVIDER_ERROR",+ retriable: true,+ details: { providerStatus: 503 },+ });+ expect(classifyAgentError(Object.assign(+ new Error("upstream connect error or disconnect/reset before headers"),+ { status: 502 },+ ))).toMatchObject({+ code: "PROVIDER_ERROR",+ retriable: true,+ details: { providerStatus: 502 },+ });+ });+// Each message is what pi-ai 1.1.0 reports for that provider's overflow:// OpenAI-compatible bodies arrive as "<status>: <body>", Bedrock as its// exception prefix. pi-ai's `isContextOverflow` already sends every one ofdiff --git a/packages/agent-runtime/src/agent-errors.ts b/packages/agent-runtime/src/agent-errors.tsindex e772759b2..2d762f40a 100644--- a/packages/agent-runtime/src/agent-errors.ts+++ b/packages/agent-runtime/src/agent-errors.ts@@ -643,7 +643,24 @@ export function classifyAgentError(err: unknown): ClassifiedAgentError {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);// Malformed request (wrong apiStyle, bad params) — retrying won't help.