The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(agent): exhausted plan-quota 429 names its reset window instead of 'wait a minute'
teknium15d0ddb7dd85e
diff --git a/agent/error_classifier.py b/agent/error_classifier.pyindex 4c988b5c455..f951a7458ee 100644--- a/agent/error_classifier.py+++ b/agent/error_classifier.py@@ -950,6 +950,10 @@ def _status_429(c: _Ctx) -> Verdict:explicit_rate_limit = any(p in c.msg for p in _RATE_LIMIT_PATTERNS)if quota_wall and not explicit_rate_limit and not _has_usage_limit_transient_signal(c.msg, c.body, c.headers):return _V_BILLING+ # Carry the reset window so the terminal copy can name it instead of "wait a minute" (#89401).+ 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})return _V_RATE_LIMIT@@ -1117,6 +1121,24 @@ def _has_usage_limit_transient_signal(error_msg: str, body: dict, response_headereturn False+def _rate_limit_reset_seconds(error_msg: str, body: dict, response_headers) -> Optional[float]:+ """Seconds until a 429's window reopens, from the body's reset fields, ``Retry-After`` or the+ message grammar (``retry after Ns`` / ``resets in 4hr``); None when the response names none."""+ from agent.retry_utils import parse_retry_after_seconds, reset_delay_from_message+ for payload in (p for p in (body, _error_obj(body)) if isinstance(p, dict)):+ for name in _RESET_FIELDS:+ value = payload.get(name)+ if value in (None, ""):+ continue+ if name.endswith("_at") and isinstance(value, (int, float)):+ return max(0.0, float(value) - time.time())+ if (seconds := parse_retry_after_seconds(value)) is not None:+ return seconds+ if (seconds := parse_retry_after_seconds(response_headers)) is not None:+ return seconds+ return reset_delay_from_message(error_msg)++def _model_id_missing_known_prefix(model: str, provider: str) -> bool:"""True when a bare model id is only known to the provider as ``vendor/id``.diff --git a/agent/turn_failure_copy.py b/agent/turn_failure_copy.pyindex fec973dd595..7369458406b 100644--- a/agent/turn_failure_copy.py+++ b/agent/turn_failure_copy.py@@ -301,11 +301,18 @@ def site_copy(code: str, **fields: Any) -> str:return _SITE_COPY[code].format_map(_Defaults(fields))-def exhausted_copy(reason: str, *, label: str, attempts: int, summary: str) -> str:- """Chat copy once retries + fallback are exhausted (``max_retries_exhausted_result``)."""+def exhausted_copy(reason: str, *, label: str, attempts: int, summary: str, reset_seconds: Optional[float] = None) -> str:+ """Chat copy once retries + fallback are exhausted (``max_retries_exhausted_result``). A rate+ limit whose reset window is known names it: an 8.6h plan quota is not "wait a minute" (#89401)."""lead = _EXHAUSTED_LEADS.get(reason, _EXHAUSTED_DEFAULT_LEAD).format(label=label, attempts=attempts)+ if reset_seconds is not None and reset_seconds >= 120:+ from agent.retry_utils import format_reset_window+ situation = (f"its usage limit resets in {format_reset_window(reset_seconds)}. "+ "Send /retry after that, or switch models with /model.")+ else:+ situation = f"it looks temporarily unavailable. {_NEXT_STEPS_RETRY}"return (- f"{lead} — it looks temporarily unavailable. {_NEXT_STEPS_RETRY} To avoid this in future, "+ f"{lead} — {situation} To avoid this in future, "f"add a backup provider with `hermes fallback add`.\n\nProvider said: {summary}")diff --git a/agent/turn_recovery.py b/agent/turn_recovery.pyindex 7c471178cc8..1c338b12b5b 100644--- a/agent/turn_recovery.py+++ b/agent/turn_recovery.py@@ -1052,9 +1052,10 @@ def max_retries_exhausted_result(else:# Every surface reads final_response (the 💡 lines above are CLI-only), so the chat# text carries the plain what-happened + next step itself.+ _reset_at = classified.error_context.get("reset_at")_final_response = exhausted_copy(classified.reason.value, label=provider_label_for(provider), attempts=max_retries,- summary=_final_summary,+ summary=_final_summary, reset_seconds=_reset_at - time.time() if _reset_at else None,)if _welcome_hint:_final_response = _welcome_tier_guidance(classified, model=model, in_chat=True)diff --git a/tests/agent/test_failed_turn_chat_copy.py b/tests/agent/test_failed_turn_chat_copy.pyindex def6056f864..541ea6836e4 100644--- a/tests/agent/test_failed_turn_chat_copy.py+++ b/tests/agent/test_failed_turn_chat_copy.py@@ -122,6 +122,40 @@ def test_max_retries_exhausted_chat_text_has_next_step_and_no_mechanism_lead():assert result["failure_retryable"] is True+def test_exhausted_plan_quota_429_names_the_reset_window_not_wait_a_minute():+ """The real usage-limit envelope: ``_summarize_api_error`` reduces the body to ``HTTP 429: The+ usage limit has been reached``, so the reset must travel through the classifier, not the text (#89401)."""+ import httpx+ import openai+ from agent.api_error_summary import ApiErrorSummaryMixin++ body = {"error": {"type": "usage_limit_reached", "message": "The usage limit has been reached",+ "resets_in_seconds": 30995, "plan_type": "pro"}}+ response = httpx.Response(429, json=body, request=httpx.Request("POST", "https://chatgpt.com/backend-api/codex/responses"))+ error = openai.RateLimitError(f"Error code: 429 - {body}", response=response, body=body)+ classified = classify_api_error(error, provider="openai-codex", model="gpt-5.3-codex")+ agent = _Agent()+ agent._summarize_api_error = ApiErrorSummaryMixin._summarize_api_error+ result = max_retries_exhausted_result(+ agent, error, classified, max_retries=3, is_rate_limited=True, error_msg=str(error).lower(),+ api_kwargs=None, api_messages=[], messages=[], conversation_history=None, api_call_count=3,+ approx_tokens=10, provider="openai-codex", base_url="https://chatgpt.com/backend-api/codex", model="gpt-5.3-codex",+ )+ text = result["final_response"]+ assert result["error"] == "HTTP 429: The usage limit has been reached"+ assert "resets in ~9h" in text and "/retry" in text and "/model" in text+ assert "Wait a minute" not in text+ # A throttle with no reset window keeps the short-wait copy.+ short = _Http(429, "HTTP 429: Rate limit exceeded")+ plain = max_retries_exhausted_result(+ _Agent(), short, classify_api_error(short, provider="openrouter", model="m"), max_retries=3,+ is_rate_limited=True, error_msg=str(short).lower(), api_kwargs=None, api_messages=[], messages=[],+ conversation_history=None, api_call_count=3, approx_tokens=10, provider="openrouter",+ base_url="https://openrouter.ai/api/v1", model="m",+ )+ assert "Wait a minute" in plain["final_response"] and "resets in" not in plain["final_response"]++def test_invalid_response_stamps_reason_from_embedded_provider_code():"""An HTTP-200 body carrying a 429 is rate limiting for the UI, not 'unknown'."""agent = _Agent()