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 <ntconguit@gmail.com>
This commit is contained in:
Teknium
2026-11-30 23:15:57 -08:00
parent 749c23e9bf
commit aa181307eb
2 changed files with 12 additions and 4 deletions

View File

@@ -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(

View File

@@ -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,