From aa181307eb3bf3cd2b998783f9529ff87d2bc2cd Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Mon, 30 Nov 2026 23:15:57 -0800 Subject: [PATCH] fix(agent): a 403 that names its reset window retries as a rate limit A proxy that soft-throttles with HTTP 403 and puts the window in the body ("HTTP 403 (reset after 2m)") fell through to the auth verdict, so the turn aborted with "your API key was rejected" while the sibling 429 on the same proxy retried and recovered (#42167). _status_403 now reads the same reset grammar the 429 handler reads (body fields, Retry-After, message) after the billing and WAF checks, and the shared message grammar accepts "reset after". Also narrow the relay server_error escape hatch to the envelope it was verified on ("Upstream request failed: [server_error] ..."): the relay reuses the "Upstream request failed:" wrapper for a missing subscription and for region refusals, which a retry loop on the same key cannot fix. Co-authored-by: Harry Riddle --- agent/error_classifier.py | 11 +++++++++-- agent/retry_utils.py | 5 +++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/agent/error_classifier.py b/agent/error_classifier.py index 55df8b363e..329607b931 100644 --- a/agent/error_classifier.py +++ b/agent/error_classifier.py @@ -1274,8 +1274,10 @@ _403_TRANSIENT_CODES = frozenset({"upstream_unavailable"}) # wrapper this was verified on (OpenCode stamping "Upstream request failed: # [...]" when the upstream answer was unparseable, #117869): the code alone is # not proof, because ``error.code`` falls back to ``error.type`` and several -# providers reuse the same word for plain permission refusals. -_403_SERVER_ERROR_WRAPPER_MARKERS = ("upstream request failed",) +# providers reuse the same word for plain permission refusals. The bracketed inner +# code must say ``server_error`` too: the relay reuses the same wrapper for a missing +# subscription and for region refusals, which retrying cannot fix. +_403_SERVER_ERROR_WRAPPER_MARKERS = ("upstream request failed: [server_error]",) def _explicit_error_code_field(body: Any) -> str: @@ -1306,6 +1308,11 @@ def _status_403(c: _Ctx) -> Verdict: # 403 and on established block/challenge markers; any other 403 stays auth. if any(p in c.msg for p in _UPSTREAM_BLOCKED_PATTERNS): return _V_UPSTREAM_BLOCKED + # A proxy soft throttle names its reset window in the 403 body ("HTTP 403 (reset after + # 2m)", #42167): the key is fine and the window reopens, so it is the 429 rate limit. + reset = _rate_limit_reset_seconds(c.msg, c.body, c.headers) + if reset: + return _v(_R.rate_limit, **_ROTATE_FALLBACK, error_context={"reset_at": time.time() + reset}) if c.code in _403_TRANSIENT_CODES: return _V_OVERLOADED if _explicit_error_code_field(c.body) == "server_error" and any( diff --git a/agent/retry_utils.py b/agent/retry_utils.py index f51ca2036a..8e6ba3fb70 100644 --- a/agent/retry_utils.py +++ b/agent/retry_utils.py @@ -67,9 +67,10 @@ def parse_retry_after_seconds(value_or_headers: Any) -> Optional[float]: # Free-text "reset" grammars providers put in error bodies, tried in order. One table so the # conversation loop's error context and the credential pool's cooldown agree on the same wait. _QUOTA_RESET_DELAY_RE = re.compile(r"quotaResetDelay[:\s\"]+(\d+(?:\.\d+)?)(ms|s)", re.IGNORECASE) -# "Resets in 4hr 5min" (weekly usage limits), "resets in 2 hours 5 minutes", "resets in 30s". +# "Resets in 4hr 5min" (weekly usage limits), "resets in 2 hours 5 minutes", "resets in 30s"; +# proxies word the same window "(reset after 2m)" (#42167). _RESETS_IN_RE = re.compile( - r"resets?\s+in\s+" + r"resets?\s+(?:in|after)\s+" r"(?:(\d+(?:\.\d+)?)\s*(?:h|hr|hrs|hour|hours)\b\s*)?" r"(?:(\d+(?:\.\d+)?)\s*(?:m|min|mins|minute|minutes)\b\s*)?" r"(?:(\d+(?:\.\d+)?)\s*(?:s|sec|secs|second|seconds)\b)?", re.IGNORECASE,