Skip to content

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

fix(agent-runtime): classify DashScope status envelopes before retrying

Linuxdazhao11a0da858875
diff --git a/docs/spec/03-runtime/08-error-codes.md b/docs/spec/03-runtime/08-error-codes.md
index 51b106535..5bd8090c1 100644
--- a/docs/spec/03-runtime/08-error-codes.md
+++ b/docs/spec/03-runtime/08-error-codes.md
@@ -90,6 +90,14 @@ does not turn temporary thread pressure into a host process exit.
### 3.2 Agent / session
+Provider status parsing also recognizes DashScope `Code: <status>` envelopes
+at the start of an error message. A streamed `InvalidParameter: <400>`
+rejection is terminal even when the enclosing HTTP response was 200:
+resending unchanged tool history cannot repair it. Structured error status
+fields retain precedence; arbitrary angle-bracket numbers in prose are not
+HTTP statuses. Authentication, rate-limit and transient 5xx envelopes retain
+their existing classifications.
+
| code | retriable | meaning |
|---|---|---|
| `AGENT_BUSY` | no | session already has active turn; leftover subagents after a terminal parent error do not keep the session busy (D352) |
diff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.md
index 285429a01..8350eab7f 100644
--- a/docs/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/spec/06-delivery/04-e2e-test-plan.md
@@ -1,5 +1,22 @@
# 04. E2E Test Plan
+## E2E-PROVIDER-DASHSCOPE-terminal-parameter-error
+
+- **Preconditions:** Isolated Agent runtime and a synthetic OpenAI-compatible
+ provider; no real credentials or user history.
+- **Steps:** Submit a prompt. Return HTTP 200 with a streamed provider error
+ whose message starts `InvalidParameter: <400>` and reports repetitive tool
+ calls. Also exercise 400/422, authentication, 429 and 5xx envelopes through
+ the shared classifier.
+- **Expected:** The 400 error ends the turn as non-retriable `PROVIDER_ERROR`
+ with provider status 400, without another provider request or retry backoff.
+ The HTTP 200 transport does not override the embedded error status.
+ Authentication stays terminal; throughput limits and 5xx remain retriable.
+ Structured status takes precedence and prose numbers are ignored.
+- **Automated coverage:** `agent-errors.test.ts` and `provider-retry.test.ts`.
+- **Specs:** `03-runtime/08-error-codes.md` §3.2.
+
+
## E2E-TASK-DOCK-tab-content-isolation
- **Preconditions:** Isolated Electron renderer with synthetic todos and a running
diff --git a/packages/agent-runtime/src/agent-errors.test.ts b/packages/agent-runtime/src/agent-errors.test.ts
index ffc16a70d..2c89cc909 100644
--- a/packages/agent-runtime/src/agent-errors.test.ts
+++ b/packages/agent-runtime/src/agent-errors.test.ts
@@ -297,6 +297,29 @@ describe("classifyAgentError", () => {
.toMatchObject({ code: "PROVIDER_ERROR", retriable: false });
});
+ it.each([
+ ["InvalidParameter: <400> InternalError.Algo.InvalidParameter: Repetitive tool calls detected in the conversation history.", "PROVIDER_ERROR", false, 400],
+ ["InvalidParameter: <422> Invalid tool arguments.", "PROVIDER_ERROR", false, 422],
+ ["InvalidApiKey: <401> Invalid API key.", "PROVIDER_UNAUTHORIZED", false, 401],
+ ["Throttling: <429> Too many requests.", "PROVIDER_RATE_LIMITED", true, 429],
+ ["InternalError: <500> The service is temporarily unavailable.", "PROVIDER_ERROR", true, 500],
+ ])("classifies DashScope code/status envelopes: %s", (message, code, retriable, status) => {
+ expect(classifyAgentError(message)).toMatchObject({
+ code, retriable, details: { providerStatus: status },
+ });
+ });
+
+ it("does not infer an HTTP status from an angle-bracket number in prose", () => {
+ expect(classifyAgentError("Provider failed while processing item <400>."))
+ .toMatchObject({ code: "PROVIDER_ERROR", retriable: true });
+ expect(classifyAgentError("InvalidParameter: <400> Unknown parameter. <429>"))
+ .toMatchObject({ code: "PROVIDER_ERROR", retriable: false, details: { providerStatus: 400 } });
+ expect(classifyAgentError(Object.assign(
+ new Error("InvalidParameter: <400> Unknown parameter."),
+ { status: 503 },
+ ))).toMatchObject({ code: "PROVIDER_ERROR", retriable: true, details: { providerStatus: 503 } });
+ });
+
// @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
diff --git a/packages/agent-runtime/src/agent-errors.ts b/packages/agent-runtime/src/agent-errors.ts
index ca94df4bd..610d8d21a 100644
--- a/packages/agent-runtime/src/agent-errors.ts
+++ b/packages/agent-runtime/src/agent-errors.ts
@@ -127,7 +127,8 @@ 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. Gemini's
+ * "(status)" / "status code NNN" marker, or a DashScope "Code: <status>"
+ * envelope 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.
*/
@@ -139,6 +140,7 @@ function extractStatus(err: unknown, message: string): number | undefined {
current = current.cause;
}
const patterns = [
+ /^\s*[A-Za-z][A-Za-z0-9_.-]*:\s*<(\d{3})>/,
/^\s*(\d{3})\s*:/,
/^\s*(\d{3})\b/,
/\((\d{3})\)/,
diff --git a/packages/agent-runtime/src/provider-retry.test.ts b/packages/agent-runtime/src/provider-retry.test.ts
index 7c276caef..5cd2a0559 100644
--- a/packages/agent-runtime/src/provider-retry.test.ts
+++ b/packages/agent-runtime/src/provider-retry.test.ts
@@ -444,6 +444,29 @@ describe("provider rate-limit retry", () => {
expect(events).toEqual(["start", "done"]);
});
+ it("does not replay a DashScope parameter rejection delivered over HTTP 200", async () => {
+ let retryClaims = 0;
+ const claim = vi.fn((error: ClassifiedAgentError) =>
+ error.retriable && retryClaims++ === 0 ? 1 : undefined);
+ const sleep = vi.fn(async () => undefined);
+ const createStream = vi.fn(() => failedStream({
+ errorMessage: "InvalidParameter: <400> InternalError.Algo.InvalidParameter: Repetitive tool calls detected in the conversation history.",
+ }));
+ const stream = createProviderRetryStream(model, context, {}, createStream, {
+ claim, sleep, headers: () => undefined, status: () => 200,
+ });
+
+ const events: string[] = [];
+ for await (const event of stream) events.push(event.type);
+
+ expect(createStream).toHaveBeenCalledTimes(1);
+ expect(sleep).not.toHaveBeenCalled();
+ expect(claim).toHaveBeenCalledWith(expect.objectContaining({
+ code: "PROVIDER_ERROR", retriable: false, details: { providerStatus: 400 },
+ }), "request");
+ expect(events).toEqual(["error"]);
+ });
+
it("does not promote known non-retryable errors just because status is 429", async () => {
const claim = vi.fn(() => undefined);
const stream = createProviderRetryStream(