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): read the HTTP status from Gemini JSON error bodies
xpeng5278-web5f4bb8d67c0a
diff --git a/docs/spec/03-runtime/08-error-codes.md b/docs/spec/03-runtime/08-error-codes.mdindex 186a6b75e..f88132406 100644--- a/docs/spec/03-runtime/08-error-codes.md+++ b/docs/spec/03-runtime/08-error-codes.md@@ -381,7 +381,11 @@ messages (including `insufficient_quota`, `credit_balance_exhausted`, andorganization/project spend-limit codes) map to terminal `PROVIDER_ERROR` evenwhen the HTTP status is 429. Throughput rate-limit wording, includingBedrock's `Throttling error: Too many tokens, please wait before trying again.`,-stays `PROVIDER_RATE_LIMITED`.+stays `PROVIDER_RATE_LIMITED`. An unprefixed JSON error body that carries an+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.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 885230610..d1e9ea0d6 100644--- a/packages/agent-runtime/src/agent-errors.test.ts+++ b/packages/agent-runtime/src/agent-errors.test.ts@@ -297,6 +297,141 @@ describe("classifyAgentError", () => {.toMatchObject({ code: "PROVIDER_ERROR", retriable: false });});+ // @google/genai ApiError.message is JSON.stringify(errorBody). pi-ai does+ // not prefix "<status>:", and the Google adapter never records+ // providerResponseStatus, so the main session and subagents classify this+ // assistant message through classifyProviderError with no status argument.+ it.each([+ [+ "400 INVALID_ARGUMENT",+ {+ error: {+ code: 400,+ message: "User location is not supported for the API use.",+ status: "INVALID_ARGUMENT",+ },+ },+ { code: "PROVIDER_ERROR", retriable: false, details: { providerStatus: 400 } },+ ],+ [+ "404 NOT_FOUND",+ {+ error: {+ code: 404,+ message:+ "models/gemini-3-pro-preview is not found for API version v1beta, or is not supported for generateContent. Call ListModels to see the list of available models and their supported methods.",+ status: "NOT_FOUND",+ },+ },+ {+ code: "MODEL_NOT_CONFIGURED",+ retriable: false,+ details: { providerStatus: 404 },+ },+ ],+ [+ "429 RESOURCE_EXHAUSTED",+ {+ error: {+ code: 429,+ message: "Resource has been exhausted (e.g. check quota).",+ status: "RESOURCE_EXHAUSTED",+ },+ },+ {+ code: "PROVIDER_RATE_LIMITED",+ retriable: true,+ details: { providerStatus: 429 },+ },+ ],+ [+ "429 billing exhaustion",+ {+ error: {+ code: 429,+ message:+ "You exceeded your current quota, please check your plan and billing details.",+ status: "RESOURCE_EXHAUSTED",+ },+ },+ { code: "PROVIDER_ERROR", retriable: false, details: { providerStatus: 429 } },+ ],+ [+ "503 UNAVAILABLE",+ {+ error: {+ code: 503,+ message: "The service is currently unavailable.",+ status: "UNAVAILABLE",+ },+ },+ { code: "PROVIDER_ERROR", retriable: true, details: { providerStatus: 503 } },+ ],+ [+ "400 input token count",+ {+ error: {+ code: 400,+ message:+ "The input token count exceeds the maximum number of tokens allowed.",+ status: "INVALID_ARGUMENT",+ },+ },+ {+ code: "CONTEXT_TOO_LARGE",+ retriable: false,+ details: { providerStatus: 400 },+ },+ ],+ [+ "409 unknown 4xx",+ {+ error: {+ code: 409,+ message: "Resource already exists.",+ status: "ALREADY_EXISTS",+ },+ },+ { code: "PROVIDER_ERROR", retriable: true, details: { providerStatus: 409 } },+ ],+ ])(+ "classifies an unprefixed Gemini JSON %s body",+ (_label, body, expected) => {+ const errorMessage = JSON.stringify(body);+ const message = { role: "assistant", stopReason: "error", errorMessage };+ expect(classifyAgentError(errorMessage)).toMatchObject(expected);+ expect(classifyAgentError(new Error(errorMessage))).toMatchObject(expected);+ expect(classifyAgentError(message)).toMatchObject(expected);+ expect(classifyProviderError(message)).toMatchObject(expected);+ },+ );++ it("does not treat a non-HTTP JSON code as a provider status", () => {+ const cases = [+ JSON.stringify({ error: { code: 123, message: "not an http status" } }),+ JSON.stringify({ code: 400, message: "plain code is not an http status" }),+ JSON.stringify({+ error: {+ code: "400",+ message: "string code is not an http status",+ status: "INVALID_ARGUMENT",+ },+ }),+ ];+ for (const errorMessage of cases) {+ const classified = classifyProviderError({+ role: "assistant",+ stopReason: "error",+ errorMessage,+ });+ expect(classified).toMatchObject({+ code: "PROVIDER_ERROR",+ retriable: true,+ });+ expect(classified.details?.providerStatus).toBeUndefined();+ }+ });+it("treats a request option an adapter refuses as non-retriable", () => {expect(classifyAgentError(diff --git a/packages/agent-runtime/src/agent-errors.ts b/packages/agent-runtime/src/agent-errors.tsindex 58acfab11..05fc9a132 100644--- a/packages/agent-runtime/src/agent-errors.ts+++ b/packages/agent-runtime/src/agent-errors.ts@@ -115,7 +115,9 @@ 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 in the message.+ * "(status)" / "status code NNN" marker 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.*/function extractStatus(err: unknown, message: string): number | undefined {let current: any = err;@@ -137,6 +139,50 @@ function extractStatus(err: unknown, message: string): number | undefined {if (status >= 400 && status < 600) return status;}}+ return httpStatusFromJsonErrorBody(message);+}++/** Integer HTTP error status. Numeric strings and out-of-range codes are not. */+function isHttpErrorStatus(value: unknown): value is number {+ return (+ typeof value === "number" &&+ Number.isInteger(value) &&+ value >= 400 &&+ value <= 599+ );+}++/** Google RPC status enums are UPPER_SNAKE, e.g. INVALID_ARGUMENT. */+function isGoogleRpcStatus(value: unknown): boolean {+ return typeof value === "string" && /^[A-Z][A-Z0-9_]*$/.test(value);+}++/**+ * Read an HTTP status from a JSON error body when the message itself is that+ * body. A 400-599 numeric `code` counts only when it sits on an `error`+ * object, or beside a Google RPC `status`. A bare `"code": 123` is not one.+ */+function httpStatusFromJsonErrorBody(message: string): number | undefined {+ const trimmed = message.trim();+ if (!trimmed.startsWith("{")) return undefined;+ let parsed: unknown;+ try {+ parsed = JSON.parse(trimmed);+ } catch {+ return undefined;+ }+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {+ return undefined;+ }+ const record = parsed as Record<string, unknown>;+ const nested = record.error;+ if (nested && typeof nested === "object" && !Array.isArray(nested)) {+ const error = nested as Record<string, unknown>;+ if (isHttpErrorStatus(error.code)) return error.code;+ }+ if (isHttpErrorStatus(record.code) && isGoogleRpcStatus(record.status)) {+ return record.code;+ }return undefined;}