Skip to content

The agent that grows with you

fix(error-classifier): a profile hook asking for fallback on a terminal reason is non-retryable

kshitijk4poor838214c8f795
diff --git a/agent/error_classifier.py b/agent/error_classifier.py
index 9127cede3ae..24655c9ea8e 100644
--- a/agent/error_classifier.py
+++ b/agent/error_classifier.py
@@ -732,7 +732,14 @@ def _profile_verdict(c: _Ctx) -> Optional[Verdict]:
return None
if not isinstance(reason, FailoverReason):
return None
- verdict = _v(reason, **{k: bool(result[k]) for k in _HINT_FLAGS if k in result})
+ hints = {k: bool(result[k]) for k in _HINT_FLAGS if k in result}
+ # turn_api_error walks the fallback chain only for non-retryable verdicts outside
+ # RETRYABLE_CLIENT_REASONS; the built-in terminal verdicts (billing, auth, model_not_found …) pin
+ # retryable=False, the rate-limit family stays retryable and reaches fallback after backoff. Give
+ # a hook that asks for fallback the built-in default for its reason, so it cascades like one.
+ if hints.get("should_fallback") and "retryable" not in hints and reason not in RETRYABLE_CLIENT_REASONS:
+ hints["retryable"] = False
+ verdict = _v(reason, **hints)
if isinstance(result.get("error_context"), dict):
verdict["error_context"] = result["error_context"]
logger.info("API error classified by provider profile: %s (provider=%s, status=%s)",
@@ -742,6 +749,14 @@ def _profile_verdict(c: _Ctx) -> Optional[Verdict]:
_HINT_FLAGS = ("retryable", "should_compress", "should_rotate_credential", "should_fallback")
+# Reasons the retry loop keeps retrying (with backoff) even though the verdict may also carry
+# ``should_fallback``: the cascade for these runs after the backoff budget, not immediately.
+RETRYABLE_CLIENT_REASONS = frozenset({
+ FailoverReason.rate_limit, FailoverReason.upstream_rate_limit, FailoverReason.overloaded,
+ FailoverReason.context_overflow, FailoverReason.payload_too_large, FailoverReason.long_context_tier,
+ FailoverReason.thinking_signature,
+})
+
# A welcome-host 403 that spells one of these out is a safety block or a billing wall, not the
# tier refusing. The free-tier refusal phrases are left OUT: on the free route they mean exactly
diff --git a/agent/turn_api_error.py b/agent/turn_api_error.py
index 464801c0cc5..9b2440b6475 100644
--- a/agent/turn_api_error.py
+++ b/agent/turn_api_error.py
@@ -15,7 +15,7 @@ import ssl
import time
from typing import Any, Dict, Optional
-from agent.error_classifier import FailoverReason, classify_api_error
+from agent.error_classifier import RETRYABLE_CLIENT_REASONS, FailoverReason, classify_api_error
from agent.turn_overflow import recover_from_overflow
from agent.turn_recovery import (
_NONRETRYABLE_LABELS, abort_turn_on_interrupt, compute_error_backoff, interruptible_backoff_sleep,
@@ -232,13 +232,6 @@ def _is_local_validation_error(api_error: Any) -> bool:
return not (isinstance(api_error, TypeError) and "nonetype" in _text and "not iterable" in _text)
-# Non-retryable per the classifier, yet handled by the overflow/backoff paths instead.
-_RETRYABLE_CLIENT_REASONS = frozenset({
- FailoverReason.rate_limit, FailoverReason.overloaded, FailoverReason.context_overflow,
- FailoverReason.payload_too_large, FailoverReason.long_context_tier, FailoverReason.thinking_signature,
-})
-
-
@dataclass
class UnrecoveredErrorVerdict:
"""``action``: ``"continue"`` (retry), ``"break"`` (fallback armed / redirect pending) or
@@ -300,7 +293,7 @@ def settle_unrecovered_error(
or (
not classified.retryable
and not classified.should_compress
- and classified.reason not in _RETRYABLE_CLIENT_REASONS
+ and classified.reason not in RETRYABLE_CLIENT_REASONS
)
) and not is_context_length_error
diff --git a/tests/agent/test_provider_profile_recovery_seam.py b/tests/agent/test_provider_profile_recovery_seam.py
index 586d629bf44..3fd046b2131 100644
--- a/tests/agent/test_provider_profile_recovery_seam.py
+++ b/tests/agent/test_provider_profile_recovery_seam.py
@@ -55,3 +55,31 @@ def test_plugin_refresh_returning_none_benches_instead_of_phantom_success(plugin
monkeypatch.setattr(pool, "_persist", lambda *a, **k: None)
assert pool._refresh_entry_impl(entry, force=True) is None
assert pool.entries()[0].last_status == STATUS_EXHAUSTED
+
+
+def test_profile_hook_should_fallback_is_non_retryable_by_default(monkeypatch):
+ """The fallback walk only runs for non-retryable verdicts outside the retryable-client reasons
+ (the built-in terminal verdicts pin ``retryable=False``). A hook asking for fallback on such a
+ reason gets the built-in default instead of a retry against the dead route; a rate-limit hook
+ keeps the built-in retry-then-fallback shape."""
+ def classify(error, *, status_code, error_code, message, body, model):
+ return {"reason": "billing", "should_fallback": True}
+
+ providers.register_provider(ProviderProfile(name="example-fallback", auth_type="oauth_external",
+ base_url="https://example.invalid/v1", classify_api_error=classify))
+ try:
+ verdict = classify_api_error(_error(403, "quota_exhausted"), provider="example-fallback", model="m")
+ finally:
+ providers._REGISTRY.pop("example-fallback", None)
+ providers._PROVIDER_LIST_CACHE = None
+ assert (verdict.reason, verdict.should_fallback, verdict.retryable) == (FailoverReason.billing, True, False)
+
+ providers.register_provider(ProviderProfile(
+ name="example-ratelimit", auth_type="oauth_external", base_url="https://example.invalid/v1",
+ classify_api_error=lambda error, **kw: {"reason": "rate_limit", "should_fallback": True}))
+ try:
+ limited = classify_api_error(_error(429, "slow_down"), provider="example-ratelimit", model="m")
+ finally:
+ providers._REGISTRY.pop("example-ratelimit", None)
+ providers._PROVIDER_LIST_CACHE = None
+ assert (limited.reason, limited.should_fallback, limited.retryable) == (FailoverReason.rate_limit, True, True)
diff --git a/website/docs/developer-guide/model-provider-plugin.md b/website/docs/developer-guide/model-provider-plugin.md
index 54f337bc66d..b238024c336 100644
--- a/website/docs/developer-guide/model-provider-plugin.md
+++ b/website/docs/developer-guide/model-provider-plugin.md
@@ -452,7 +452,7 @@ register_provider(ProviderProfile(name="example-oauth", auth_type="oauth_externa
| Contract | |
|---|---|
-| `classify_api_error(error, *, status_code, error_code, message, body, model)` | Consulted by `agent.error_classifier.classify_api_error` for failures of **this provider only**, after any generic `transform_api_error_classification` hooks and before the built-in pipeline. `message` is the lower-cased error text, `body` the parsed JSON body (may be empty). Return `{"reason": <FailoverReason name>}` plus optional `retryable` / `should_compress` / `should_rotate_credential` / `should_fallback` / `error_context` to override; `None` (or an unknown reason) leaves the built-in verdict. Exceptions are swallowed and logged at DEBUG. The verdict drives the same recovery as for built-ins — e.g. `billing` benches the credential for the billing TTL instead of the transient 403 cooldown. |
+| `classify_api_error(error, *, status_code, error_code, message, body, model)` | Consulted by `agent.error_classifier.classify_api_error` for failures of **this provider only**, after any generic `transform_api_error_classification` hooks and before the built-in pipeline. `message` is the lower-cased error text, `body` the parsed JSON body (may be empty). Return `{"reason": <FailoverReason name>}` plus optional `retryable` / `should_compress` / `should_rotate_credential` / `should_fallback` / `error_context` to override (for terminal reasons — billing, auth, model_not_found … — `should_fallback: True` implies `retryable: False` unless you set it, because the fallback chain only runs for non-retryable verdicts; rate-limit reasons keep the built-in retry-then-fallback shape); `None` (or an unknown reason) leaves the built-in verdict. Exceptions are swallowed and logged at DEBUG. The verdict drives the same recovery as for built-ins — e.g. `billing` benches the credential for the billing TTL instead of the transient 403 cooldown. |
| 401 on a plugin credential | Handled by the credential pool, no core edit: the failing pooled row is refreshed through `refresh_credential` once per attempt (capped at two refreshes per row per session), the client is rebuilt with the rotated token and the request retried. A `None`/empty return or an exception benches the row — the request then rotates or falls to the generic "sign in again: `hermes auth add <name>`" copy, never to a built-in provider's guidance. |
| Auxiliary calls | Auxiliary-client 401s take the same pool refresh (`try_refresh_current` → `refresh_credential`). |