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): treat HTTP 402 credit exhaustion as terminal
xpeng5278-web47cebe18bd96
diff --git a/docs/spec/03-runtime/08-error-codes.md b/docs/spec/03-runtime/08-error-codes.mdindex 186a6b75e..d99290a3b 100644--- a/docs/spec/03-runtime/08-error-codes.md+++ b/docs/spec/03-runtime/08-error-codes.md@@ -99,7 +99,7 @@ does not turn temporary thread pressure into a host process exit.| `AGENT_SIDECAR_CRASHED` | no | the Node agent sidecar process died mid-turn; the owning turn settles as aborted with this code instead of an unrelated plan-approval code (issue #1077) || `AGENT_SIDECAR_OOM` | no | the sidecar died after its JavaScript heap hit the configured cap, diagnosed from the V8 fatal-error banner in its stderr tail; the same turn fails the same way until the input shrinks (issue #1077) || `MODEL_NOT_CONFIGURED` | no | no usable model selected, or provider rejects the selected model as unknown |-| `PROVIDER_ERROR` | yes | upstream provider failure; retryable 5xx gateway errors get up to ten same-turn retries, while malformed 400/422 requests, adapter-refused options, and explicit billing, credit, spend, usage, or disk exhaustion (including `insufficient_quota`) are terminal |+| `PROVIDER_ERROR` | yes | upstream provider failure; retryable 5xx gateway errors get up to ten same-turn retries, while malformed 400/422 requests, adapter-refused options, HTTP 402, and explicit billing, credit, spend, usage, or disk exhaustion (including `insufficient_quota` and insufficient-credits wording) are terminal || `PROVIDER_UNAUTHORIZED` | no | bad/missing provider credentials || `PROVIDER_RATE_LIMITED` | yes | temporary provider throughput limit; runtime retries up to ten times across setup/stream. Explicit billing/account exhaustion remains terminal `PROVIDER_ERROR`, including when returned with HTTP 429 || `CONTEXT_TOO_LARGE` | no | prompt/context still exceeds the safe model budget after recovery, the second provider overflow occurred, or automatic recovery is disabled |@@ -377,9 +377,11 @@ exceeds max length`, xAI `maximum prompt length is`, Groq, llama.cpp, andBedrock `Input is too long for requested model`. `CONTEXT_TOO_LARGE` is neverreplayed by either retry budget, and a delegate reports it as`SUBAGENT_CONTEXT_OVERFLOW`. Explicit billing/account exhaustion codes and-messages (including `insufficient_quota`, `credit_balance_exhausted`, and-organization/project spend-limit codes) map to terminal `PROVIDER_ERROR` even-when the HTTP status is 429. Throughput rate-limit wording, including+messages (including `insufficient_quota`, `credit_balance_exhausted`,+organization/project spend-limit codes, `insufficient credits`, and+`requires more credits`) map to terminal `PROVIDER_ERROR` even when the HTTP+status is 429, and HTTP 402 is terminal `PROVIDER_ERROR` even when the body+omits those phrases. Throughput rate-limit wording, includingBedrock's `Throttling error: Too many tokens, please wait before trying again.`,stays `PROVIDER_RATE_LIMITED`.diff --git a/packages/agent-runtime/src/agent-errors.test.ts b/packages/agent-runtime/src/agent-errors.test.tsindex 885230610..1fd3d070b 100644--- a/packages/agent-runtime/src/agent-errors.test.ts+++ b/packages/agent-runtime/src/agent-errors.test.ts@@ -398,6 +398,62 @@ describe("classifyAgentError", () => {});});+ // OpenRouter reports an empty balance as HTTP 402. Backing off cannot add+ // credits, so the same-turn transient budget must not replay the request.+ it.each([+ "402 Insufficient credits. Add more using https://openrouter.ai/settings/credits",+ "402 Payment Required",+ "402 status code (no body)",+ "402: gateway said something unexpected",+ "This request requires more credits, or fewer max_tokens. You requested up to 65536 tokens, but can only afford 1000.",+ "Insufficient credits",+ "Payment Required",+ '429: {"error":{"message":"Insufficient credits"}}',+ ])("treats HTTP 402 and credit-exhaustion wording as terminal: %s", (message) => {+ expect(classifyAgentError(new Error(message))).toMatchObject({+ code: "PROVIDER_ERROR",+ retriable: false,+ });+ });++ it("treats a structured HTTP 402 as a terminal provider error", () => {+ expect(classifyAgentError(Object.assign(+ new Error("Payment Required"),+ { status: 402 },+ ))).toMatchObject({+ code: "PROVIDER_ERROR",+ retriable: false,+ details: { providerStatus: 402 },+ });+ });++ 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",+ retriable: true,+ });+ expect(classifyAgentError(new Error("credits remaining will reset"))).toMatchObject({+ code: "PROVIDER_ERROR",+ retriable: true,+ });+ // "credentials" shares a prefix with "credits" and must stay out of the+ // exhaustion matcher.+ expect(classifyAgentError(new Error("insufficient credentials"))).toMatchObject({+ code: "PROVIDER_ERROR",+ retriable: true,+ });+ expect(classifyAgentError(+ '429: {"error":{"type":"rate_limit_error","message":"credits remaining"}}',+ )).toMatchObject({+ code: "PROVIDER_RATE_LIMITED",+ retriable: true,+ });+ expect(classifyAgentError(new Error("429: Too many requests"))).toMatchObject({+ code: "PROVIDER_RATE_LIMITED",+ retriable: true,+ });+ });+it.each(["Rate limit exceeded","Too many requests",diff --git a/packages/agent-runtime/src/agent-errors.ts b/packages/agent-runtime/src/agent-errors.tsindex 58acfab11..77640fdc1 100644--- a/packages/agent-runtime/src/agent-errors.ts+++ b/packages/agent-runtime/src/agent-errors.ts@@ -48,16 +48,20 @@ const RATE_LIMIT_PATTERN =/*** Billing, spending, credit, and disk exhaustion are terminal, including- * OpenAI's `insufficient_quota` type and explicit 429 error codes. Checked- * before the status table because those conditions do not recover by backing- * off. Generic "quota exceeded" wording remains eligible for rate-limit- * classification below.+ * OpenAI's `insufficient_quota` type, OpenRouter-style "insufficient credits"+ * / "requires more credits" / "Payment Required" wording, and explicit 429+ * error codes. Checked before the status table because those conditions do+ * not recover by backing off. Generic "quota exceeded" wording remains+ * eligible for rate-limit classification below. A bare "credits" is not+ * exhaustion, and "insufficient credentials" must not match it either.+ * HTTP 402 is terminal in the status table even when the body omits these+ * phrases.*/const EXHAUSTED_QUOTA_CODE_PATTERN =/\b(?:insufficient_quota|credit_balance_exhausted|organization_spend_limit_exceeded|project_spend_limit_exceeded|organization_usage_limit_exceeded)\b/i;const EXHAUSTED_QUOTA_MESSAGE_PATTERN =- /\b(?:billing|spending|credit|disk)\s+quotas?\b|\b(?:organization|project)\s+spend\s+limit\s+(?:reached|exceeded)\b|\bquota\b.{0,100}\b(?:plan|billing)\b/i;+ /\b(?:billing|spending|credit|disk)\s+quotas?\b|\b(?:organization|project)\s+spend\s+limit\s+(?:reached|exceeded)\b|\bquota\b.{0,100}\b(?:plan|billing)\b|\binsufficient\s+credits\b|\brequires\s+more\s+credits\b|\bpayment\s+required\b/i;/*** Whether a provider message reports a context overflow.@@ -587,6 +591,8 @@ export function classifyAgentError(err: unknown): ClassifiedAgentError {if (status !== undefined) {if (status === 401 || status === 403) return result("PROVIDER_UNAUTHORIZED", false);+ // Payment Required. Credit exhaustion does not recover by re-sending.+ if (status === 402) return result("PROVIDER_ERROR", false);if (status === 408) return result("TIMEOUT", true);if (status === 413) return result("CONTEXT_TOO_LARGE", false);if (status === 429) return result("PROVIDER_RATE_LIMITED", true);