From 69692d0d3a34bb3bd0e2ecf9594a239809cfe7f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9D=92=E9=9B=B2?= <137844255@qq.com> Date: Mon, 23 Feb 2026 23:03:56 +0800 Subject: [PATCH] fix: detect additional context overflow error patterns to prevent leak to user (#20539) * fix: detect additional context overflow error patterns to prevent leak to user Fixes #9951 The error 'input length and max_tokens exceed context limit: 170636 + 34048 > 200000' was not caught by isContextOverflowError() and leaked to users via formatAssistantErrorText()'s invalidRequest fallback. Add three new patterns to isContextOverflowError(): - 'exceed context limit' (direct match) - 'exceeds the model\'s maximum context' - max_tokens/input length + exceed + context (compound match) These are now rewritten to the friendly context overflow message. * Overflow: add regression tests and changelog credits * Update CHANGELOG.md * Update pi-embedded-helpers.isbillingerrormessage.test.ts --------- Co-authored-by: echoVic Co-authored-by: Vincent Koc --- CHANGELOG.md | 1 + ...pi-embedded-helpers.isbillingerrormessage.test.ts | 12 ++++++++++++ src/agents/pi-embedded-helpers/errors.ts | 4 ++++ 3 files changed, 17 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 64e8ad6bb..0755d0af5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ Docs: https://docs.openclaw.ai - Security/CI: add pre-commit security hook coverage for private-key detection and production dependency auditing, and enforce those checks in CI alongside baseline secret scanning. Thanks @vincentkoc. - Skills/Python: harden skill script packaging and validation edge cases (self-including `.skill` outputs, CRLF frontmatter parsing, strict `--days` validation, and safer image file loading), with expanded Python regression coverage. Thanks @vincentkoc. - Config/Write: apply `unsetPaths` with immutable path-copy updates so config writes never mutate caller-provided objects, and harden `openclaw config get/set/unset` path traversal by rejecting prototype-key segments and inherited-property traversal. (#24134) thanks @frankekn. +- Agents/Overflow: detect additional provider context-overflow error shapes (including `input length` + `max_tokens` exceed-context variants) so failures route through compaction/recovery paths instead of leaking raw provider errors to users. (#9951) Thanks @echoVic and @Glucksberg. - Agents/Failover: treat HTTP 502/503/504 errors as failover-eligible transient timeouts so fallback chains can switch providers/models during upstream outages instead of retrying the same failing target. (#20999) Thanks @taw0002 and @vincentkoc. ## 2026.2.23 diff --git a/src/agents/pi-embedded-helpers.isbillingerrormessage.test.ts b/src/agents/pi-embedded-helpers.isbillingerrormessage.test.ts index ba06360ac..5900ec5df 100644 --- a/src/agents/pi-embedded-helpers.isbillingerrormessage.test.ts +++ b/src/agents/pi-embedded-helpers.isbillingerrormessage.test.ts @@ -189,6 +189,18 @@ describe("isContextOverflowError", () => { } }); + it("matches exceed/context/max_tokens overflow variants", () => { + const samples = [ + "input length and max_tokens exceed context limit (i.e 156321 + 48384 > 200000)", + "This request exceeds the model's maximum context length", + "LLM request rejected: max_tokens would exceed context window", + "input length would exceed context budget for this model", + ]; + for (const sample of samples) { + expect(isContextOverflowError(sample)).toBe(true); + } + }); + it("ignores normal conversation text mentioning context overflow", () => { // These are legitimate conversation snippets, not error messages expect(isContextOverflowError("Let's investigate the context overflow bug")).toBe(false); diff --git a/src/agents/pi-embedded-helpers/errors.ts b/src/agents/pi-embedded-helpers/errors.ts index 6d6c355d0..86ded7856 100644 --- a/src/agents/pi-embedded-helpers/errors.ts +++ b/src/agents/pi-embedded-helpers/errors.ts @@ -54,6 +54,10 @@ export function isContextOverflowError(errorMessage?: string): boolean { lower.includes("model token limit") || (hasRequestSizeExceeds && hasContextWindow) || lower.includes("context overflow:") || + lower.includes("exceed context limit") || + lower.includes("exceeds the model's maximum context") || + (lower.includes("max_tokens") && lower.includes("exceed") && lower.includes("context")) || + (lower.includes("input length") && lower.includes("exceed") && lower.includes("context")) || (lower.includes("413") && lower.includes("too large")) ); }