fix: sanitize tool-call ids and map upstream errors to real status codes - #2
Merged
Merged
Conversation
Two things an OpenAI client trips over, both found while verifying a live deployment end to end. Tool-call ids were passed through verbatim from `McpArgs.tool_call_id`, and grok models return two ids joined by a literal newline (`call-<uuid>-0\nfc_<uuid>_0`). The caller round-trips that string as `tool_call_id`, so anything that re-encodes it under a stricter schema rejects it — Anthropic's `tool_use_id` pattern, for one. Fold everything outside the id alphabet into `_`, and generate an id when upstream sends an empty one rather than only when it sends none. Composer's ids were already clean; multi-round agent loops still replay correctly with the sanitized form. Every upstream failure was reported as `502`. Quota exhaustion is the case that matters: as a bad gateway it reads as a dead upstream, so clients and aggregators give up instead of backing off. Carry the Connect error code on the done event and map the codes a caller can act on differently — chiefly `resource_exhausted` -> `429 rate_limit_exceeded`. Unrecognised codes stay `502` rather than being dressed up as a client error. Verified against a quota-exhausted model: `429` at the gateway and through CLIProxyAPI, which then retries instead of surfacing the failure. Also correct the READMEs: the multi-account cooldown they advertise is not wired — `AccountPool.markFailure` has no callers — so describe the round-robin that actually runs. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two defects an OpenAI client trips over, both found while verifying a live deployment end to end (16 models, multi-round agent loops in both OpenAI and Anthropic shapes, direct and behind CLIProxyAPI).
Tool-call ids could contain a literal newline
McpArgs.tool_call_idwas passed through verbatim. Grok models return two ids joined by a newline:The caller round-trips that string as
tool_call_id, so anything re-encoding it under a stricter schema rejects it — Anthropic'stool_use_idpattern, for one. Composer's ids were already clean, which is why this only shows up on some models.Everything outside
[A-Za-z0-9_-]now folds into_, and an id is generated when upstream sends an empty one rather than only when it sends none. Multi-round agent loops still replay correctly with the sanitized form.Every upstream failure was a
502Quota exhaustion is the case that matters. Reported as a bad gateway it reads as a dead upstream, so clients and aggregators give up instead of backing off and retrying.
The Connect error code now rides on the done event, and the codes a caller can act on differently are mapped — chiefly
resource_exhausted→429/rate_limit_exceeded. Unrecognised codes stay502rather than being dressed up as client errors. A non-200 upstream response and our own turn timeout get codes too (deadline_exceeded→504).Verified against a quota-exhausted model:
Same through CLIProxyAPI, which then retries — intermittent
-fastmodel failures that previously surfaced to the caller now recover on their own.Docs
The READMEs advertise a multi-account failure cooldown that is not wired:
AccountPool.markFailurehas no callers. Corrected to describe the round-robin that actually runs, rather than shipping a promise the code does not keep. Building the failover for real is a separate change.🤖 Generated with Claude Code