Skip to content

fix(opensecret): return context_length_exceeded for provider context rejections - #1627

Merged
AnthonyRonning merged 1 commit into
masterfrom
fix/provider-context-length-error
Nov 17, 2026
Merged

AnthonyRonning merged 1 commit into
masterfrom
fix/provider-context-length-error

Conversation

@AnthonyRonning

@AnthonyRonning AnthonyRonning commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Problem

When a provider rejects a request as too long, clients get upstream_context_limit in a header, a flat {"status","message"} body, and wording no client recognizes. Coding agents decide "the context overflowed, compact and retry" by matching OpenAI's context_length_exceeded code or a known phrase. Ours matched neither, so OpenCode, and Maple Work's own agent, failed the turn instead of compacting. A user hit this with Kimi K3.1 Pro through the local proxy and OpenCode.

Where the fix lives

In the enclave, in PublicProviderError, the one type that owns this error's public code and message. The proxy forwards backend errors byte for byte and both desktop apps embed that proxy, so a proxy-side translation would reach only proxy users, only after they update, and would leave the stream payload and Responses failure on the old code.

Why context_length_exceeded

It is OpenAI's own code for this rejection on the API shape Maple serves. upstream_context_limit was a name of ours that no client knew. Checked in each client's source:

  • OpenCode 1.19.5 and 2.2.5 treat error.code == "context_length_exceeded" as an overflow and compact.
  • Goose checks error.code for it before falling back to message matching.
  • Zed matches context_length_exceeded or the phrase "exceeds the context window".
  • OpenClaw's compaction-retry scenarios are built on it.
  • pi-style clients, Maple Work's port included, match it in the error text.
  • The enclave's own diagnostic allowlist already expects providers to send it.

Change

  • The context-limit detail's code is context_length_exceeded on every surface: x-opensecret-error-code, the pre-stream body, the Chat stream error payload and the Responses failure.
  • Its message says the request "exceeds the context window", for clients that only see the message.
  • Pre-stream provider errors add the nested OpenAI error object (message, code, type), as usage and plan errors already do and as the stream payload already did. The flat status and message stay.

Before:

{"status":400,"message":"Upstream provider rejected the context or token limit. Reduce the input or requested output tokens."}

After:

{"status":400,"message":"Upstream provider rejected the request because it exceeds the context window. Reduce the input or requested output tokens.","error":{"message":"Upstream provider rejected the request because it exceeds the context window. Reduce the input or requested output tokens.","code":"context_length_exceeded","type":"invalid_request_error"}}

No proxy or SDK runtime change. Their diffs rename the code in pass-through fixtures.

Compatibility

  • upstream_context_limit no longer exists. In this repo only tests and the log allowlist used it. An outside client keyed on that header value needs the new code.
  • Every upstream_* provider error gains the nested error object before streaming starts. Their codes, messages and statuses are unchanged.
  • The public_error_code log field reports context_length_exceeded.
  • max_tokens_limit_exceeded from a provider still maps to this same detail, as before.

Evidence

  • Backend gate (format, Clippy with warnings denied, all-feature tests): 1213 passed, 0 failed, 106 existing ignored.
  • Pre-commit gates passed for all five touched components: OpenSecret, the TypeScript and Rust SDKs, the proxy, and Maple Work's lint and test recipes.
  • New exact-bytes contract test for the body and header; the mock-provider matrix and the Transport V2 gateway test assert the nested object.
  • New Maple Work test: the enclave's body reads as an overflow on both the HTTP and SDK paths. The old wording did not (checked with a throwaway probe, not committed).
  • Offline, against the overflow rules in OpenCode 1.19.5 and 2.2.5: the new message, body and stream payload are recognized; today's body is not. In 1.19.5 a recognized overflow sets the session to compact and continue.

Not verified: an end-to-end OpenCode session, a live provider rejection, or a deployed enclave. Nothing changes for users until the enclave is deployed.

🤖 Generated with Claude Code

…rejections

A provider's context rejection reached clients as upstream_context_limit,
in a flat body, with wording no client recognizes. Coding agents detect an
overflow by OpenAI's context_length_exceeded code or by known wording, then
compact and retry; under ours they failed the turn.

The context-limit detail of PublicProviderError now uses OpenAI's code on
every surface (error-code header, pre-stream body, Chat stream payload,
Responses failure) and says the request exceeds the context window.
Pre-stream provider errors add the nested OpenAI error object that usage
and plan errors already carry; the flat fields stay.

The proxy forwards backend errors unchanged, so proxy and SDK changes are
fixture renames. A Maple Work test pins that the enclave's body reads as an
overflow on both of its paths.

Co-Authored-By: Claude <noreply@anthropic.com>
@AnthonyRonning
AnthonyRonning merged commit 3e1ed07 into master Nov 17, 2026
29 checks passed
@AnthonyRonning
AnthonyRonning deleted the fix/provider-context-length-error branch November 17, 2026 20:51
AnthonyRonning added a commit that referenced this pull request Nov 21, 2026
The Rust SDK lost public Transport V1 items in #1650, so its next
publication is a major version. The proxy moves to 0.5.1 because its
runtime changed since the released 0.5.0 (#1627) and it now requires
maple-sdk 5.0.0. Only the two local package entries change in each
lockfile.

Co-authored-by: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant