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 DeepSeek, DashScope and Groq max_tokens ranges as output ceilings
xpeng5278-webaad6054f6c26
diff --git a/docs/spec/03-runtime/02-agent-runtime.md b/docs/spec/03-runtime/02-agent-runtime.mdindex bc9c3731c..382ea6480 100644--- a/docs/spec/03-runtime/02-agent-runtime.md+++ b/docs/spec/03-runtime/02-agent-runtime.md@@ -305,7 +305,10 @@ the generated output-limit fields removed: `max_tokens`,`max_completion_tokens`, and `max_output_tokens`. The same one attempt clampsthose fields down to the ceiling when the body states one (`at most Ncompletion tokens`, `max_tokens: N > M, which is the maximum allowed number of-output tokens`, or `expected a value <= N`) instead of deleting them.+output tokens`, `expected a value <= N`, a `max_tokens` range such as+DeepSeek's `valid range of max_tokens is [1, N]` or DashScope's `Range of+max_tokens should be [1, N]`, or Groq's `max_tokens must be less than or equal+to N`) instead of deleting them.Anthropic rejects a request that omits `max_tokens`. A field that is absent oralready within the ceiling is left unchanged. Credit exhaustion and contextoverflow stay on their existing terminal paths. This repair does not consumediff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.mdindex f7347948c..f16c1503d 100644--- a/docs/spec/06-delivery/04-e2e-test-plan.md+++ b/docs/spec/06-delivery/04-e2e-test-plan.md@@ -8235,7 +8235,9 @@ must keep splitting are covered by `markdown-blocks.test.mjs`.fixtures complete on their second request. A second opaque failure remainsterminal. A 400/422 that names an output ceiling (`at most N completiontokens`, `max_tokens: N > M, which is the maximum allowed number of output- tokens`, or `expected a value <= N`) gets one silent repair that clamps+ tokens`, `expected a value <= N`, `valid range of max_tokens is [1, N]`,+ `Range of max_tokens should be [1, N]`, or `max_tokens must be less than+ or equal to N`) gets one silent repair that clampsthose fields to that ceiling instead of removing them. Credit exhaustionand context overflow do not take this repair.- If the turn is aborted after the first opaque failure, the repair request isdiff --git a/packages/agent-runtime/src/provider-retry.test.ts b/packages/agent-runtime/src/provider-retry.test.tsindex ec4330553..060201852 100644--- a/packages/agent-runtime/src/provider-retry.test.ts+++ b/packages/agent-runtime/src/provider-retry.test.ts@@ -18,6 +18,7 @@ import {STREAM_IDLE_TIMEOUT_DEFAULT_MS,STREAM_IDLE_TIMEOUT_FLOOR_MS,streamIdleTimeoutMs,+ outputTokenCeiling,stripOutputLimitFields,withoutDerivedOutputLimit,withStreamIdleTimeout,@@ -1117,6 +1118,57 @@ const OPENAI_OUTPUT_CEILING ="OpenAI API error (400): max_tokens is too large: 16384. This model supports at most 4096 completion tokens, whereas you provided 16384.";const ANTHROPIC_OUTPUT_CEILING ='API Error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"max_tokens: 65537 > 64000, which is the maximum allowed number of output tokens for claude-opus-4-5-20251101"}}';+const DEEPSEEK_OUTPUT_CEILING =+ "400 Invalid max_tokens value, the valid range of max_tokens is [1, 8192]";+const GROQ_OUTPUT_CEILING =+ "`max_tokens` must be less than or equal to `16384`, the maximum value for `max_tokens` is less than the `context_window` for this model";++describe("outputTokenCeiling", () => {+ it("reads the upper bound of a DeepSeek max_tokens range", () => {+ expect(outputTokenCeiling(DEEPSEEK_OUTPUT_CEILING)).toBe(8192);+ });++ it("reads DashScope ranges, including the native error prefix", () => {+ expect(outputTokenCeiling("Range of max_tokens should be [1, 8192]")).toBe(8192);+ expect(+ outputTokenCeiling(+ "<400> InternalError.Algo.InvalidParameter: Range of max_tokens should be [1, 65536]",+ ),+ ).toBe(65536);+ });++ it("reads a max_completion_tokens range the same way", () => {+ expect(+ outputTokenCeiling("valid range of max_completion_tokens is [1, 8192]"),+ ).toBe(8192);+ expect(+ outputTokenCeiling("valid range of `max_completion_tokens` is [`1`, `8192`]"),+ ).toBe(8192);+ });++ it("reads a Groq ceiling with or without backticks", () => {+ expect(outputTokenCeiling(GROQ_OUTPUT_CEILING)).toBe(16384);+ expect(+ outputTokenCeiling(+ "max_tokens must be less than or equal to 16384, the maximum value for max_tokens is less than the context_window for this model",+ ),+ ).toBe(16384);+ });++ it("keeps comma-grouped bounds", () => {+ expect(+ outputTokenCeiling("Range of max_tokens should be [1, 65,536]"),+ ).toBe(65536);+ expect(+ outputTokenCeiling("max_completion_tokens must be less than or equal to 16,384"),+ ).toBe(16384);+ });++ it("does not treat a context-length message as an output ceiling", () => {+ expect(outputTokenCeiling("maximum context length is 131072 tokens")).toBeUndefined();+ expect(outputTokenCeiling("Range of input length should be [1, 30720]")).toBeUndefined();+ });+});describe("explicit output-limit repair", () => {async function repairPayload(@@ -1214,6 +1266,20 @@ describe("explicit output-limit repair", () => {expect(overflow.claim).toHaveBeenCalledTimes(1);});+ it("clamps a DeepSeek max_tokens range instead of failing the turn", async () => {+ const result = await repairPayload(DEEPSEEK_OUTPUT_CEILING, {+ max_tokens: 32000,+ });++ expect(result.attempts).toBe(2);+ expect(result.claim).not.toHaveBeenCalled();+ expect(result.sleep).not.toHaveBeenCalled();+ expect(result.repaired).toEqual({+ marked: true,+ max_tokens: 8192,+ });+ });+it("clamps a proxy expected-value ceiling without adding a missing field", async () => {const result = await repairPayload('400 {"error":{"code":"InvalidParameter","message":"The parameter max_tokens specified in the request is not valid: integer above maximum value, expected a value <= 32768, but got 64000 instead."}}',diff --git a/packages/agent-runtime/src/provider-retry.ts b/packages/agent-runtime/src/provider-retry.tsindex cbddc35d0..4a21981d4 100644--- a/packages/agent-runtime/src/provider-retry.ts+++ b/packages/agent-runtime/src/provider-retry.ts@@ -131,11 +131,21 @@ export function withoutDerivedOutputLimit(* - OpenAI: "max_tokens is too large: 16384. This model supports at most 4096 completion tokens"* - Anthropic: "max_tokens: 65537 > 64000, which is the maximum allowed number of output tokens"* - Anthropic-compatible proxies: "expected a value <= 32768, but got 64000 instead"+ * - DeepSeek: "Invalid max_tokens value, the valid range of max_tokens is [1, 8192]"+ * - DashScope / Bailian (Qwen): "Range of max_tokens should be [1, 8192]"+ * and "<400> InternalError.Algo.InvalidParameter: Range of max_tokens should be [1, 65536]"+ * - Groq: "`max_tokens` must be less than or equal to `16384`" (backticks optional)+ *+ * The same shapes apply to `max_completion_tokens` and `max_output_tokens`.+ * A bracketed range contributes its upper bound. Context-length wording such+ * as "maximum context length is 131072 tokens" is not an output ceiling.*/const OUTPUT_TOKEN_CEILING_PATTERNS = [/max_(?:completion_|output_)?tokens is too large:[\s\S]{0,240}?at most (\d[\d,]*) completion tokens/i,/max_tokens:\s*\d[\d,]*\s*>\s*(\d[\d,]*)\s*,\s*which is the maximum allowed number of\s+output tokens/i,/max_tokens[\s\S]{0,180}?expected a value <=\s*(\d[\d,]*)/i,+ /range of `?max_(?:completion_|output_)?tokens`?(?: is| should be) \[`?\d+`?,\s*`?(\d[\d,]*)`?\]/i,+ /`?max_(?:completion_|output_)?tokens`? must be less than or equal to `?(\d[\d,]*)`?/i,];/** The completion cap named in `message`, when it is one of the shapes above. */