Files
hermes-agent/agent/primary_auth_fallback.py
kshitijk4poor 2c8c70f370 fix(credential-pool): a spent plan window reads as a rate limit on every surface
Review follow-up on the plan-window bench:

- is_rate_limited_auth_error also recognises the `model_rate_limited` AuthError
  code (now MODEL_RATE_LIMITED_CODE in auth_constants) that
  _anthropic_token_or_raise raises for a benched Anthropic seat. It knew only
  `codex_rate_limited`, so the messaging gateway replaced the cooldown text with
  "Use /login to sign in again", the API server and fallback logs said "auth
  failed", and Kanban took the non-transient exit. The same applied to the
  existing per-model cooldown error.
- The pool-wide cooldown branch fires only when every live Anthropic row is a
  429 bench (pool_cooldown_message(rate_limited_only=True)). A pool benched by
  a 401 or 402 keeps the missing-credentials diagnostic and its sign-in remedy
  instead of being reported, and classified, as a rate limit.
- Docs (en, zh-Hans): any rejected plan window rotates on the first 429, the
  429 table row names that exception, and only an account-wide window benches
  every model.
- Comments and test docstrings say what the code does (reset precedence, the
  aux context, env-key coverage); the scope test also asserts that the refused
  model never goes back to the spent seat.
2026-11-27 19:20:23 +05:30

156 lines
7.6 KiB
Python

"""The primary provider's failed login, carried forward when resolution fell back to a backup.
``resolve_runtime_with_fallback`` swaps an expired/missing primary login for the first fallback
entry that resolves. When that backup then fails the turn too (out of credits, its own auth,
any terminal error), the backup's error is all the turn loop knows about, so the user is told
"no credits" and never that the real fix is signing back in to the primary. This module owns
the structured reason and the one place that leads a failed turn's copy with it.
"""
from __future__ import annotations
import contextvars
from dataclasses import dataclass
from typing import Any, Optional
from hermes_cli.auth_constants import MODEL_RATE_LIMITED_CODE
# Private runtime-dict key stamped by the resolution-time fallback walker.
RUNTIME_KEY = "_primary_auth_failure"
@dataclass(frozen=True)
class PrimaryAuthFailure:
"""Why the primary provider was skipped at resolution time and the exact command that fixes it."""
provider: str
error: str
relogin: str
backup: str = ""
# AuthError codes whose fix is not a sign-in: an outage, a per-model cooldown, a CLI that is not installed,
# or a configuration error (no provider, an unknown one, a custom route without a base URL).
_NOT_A_LOGIN_PROBLEM = frozenset({"temporarily_unavailable", MODEL_RATE_LIMITED_CODE, "missing_external_process_cli",
"no_provider_configured", "invalid_provider", "missing_base_url"})
def primary_auth_failure_from_exc(exc: Any, provider: Any, backup: Any = "") -> Optional[PrimaryAuthFailure]:
"""The structured reason for a primary ``AuthError`` that a re-login fixes, else None.
A quota/rate-limit cap, a retryable refusal and an entitlement (credits/subscription) code
leave the login valid: telling the user to sign in again would send them the wrong way."""
from hermes_cli.auth import _ENTITLEMENT_ERROR_CODES, AuthError, is_rate_limited_auth_error
if not isinstance(exc, AuthError) or is_rate_limited_auth_error(exc) or exc.retryable is True:
return None
if exc.code in _ENTITLEMENT_ERROR_CODES or exc.code in _NOT_A_LOGIN_PROBLEM:
return None
from agent.turn_failure_copy import relogin_command_hint, short_detail
slug = str(exc.provider or provider or "").strip().lower()
slug = "" if slug == "auto" else slug
return PrimaryAuthFailure(provider=slug, error=short_detail(exc, 300),
relogin=relogin_command_hint(slug), backup=str(backup or "").strip())
def attach_primary_auth_failure(agent: Any, failure: Optional[PrimaryAuthFailure]) -> None:
"""Hand ``failure`` to ``agent`` for this turn (``None`` clears a cached agent's previous one).
Callers drop the failure first when the turn left the route resolution fell back to
(``route_snapshot`` before and after their overrides)."""
if agent is None:
return
agent._primary_auth_failure = failure if isinstance(failure, PrimaryAuthFailure) else None
_ROUTE_FIELDS = ("provider", "base_url", "api_key")
def route_snapshot(runtime_kwargs: Any) -> tuple:
"""The resolved route's identity: provider, endpoint and account, exactly as resolved. A turn whose
overrides (``/model --once``, a ``model_routes`` entry, an API caller's provider) change any of them
runs another route, and a refusal there is that route's own, not the primary's lapsed login."""
return tuple((runtime_kwargs or {}).get(key) for key in _ROUTE_FIELDS)
def failure_for_route(failure: Optional[PrimaryAuthFailure], resolved: tuple,
runtime_kwargs: Any) -> Optional[PrimaryAuthFailure]:
"""``failure`` while the turn still runs the ``resolved`` route snapshot, else None."""
return failure if route_snapshot(runtime_kwargs) == resolved else None
# The failure found while resolving this turn's route. A context variable, not a runner attribute: the
# gateway resolves concurrent chats on separate threads, each with its own copied context.
_PENDING: contextvars.ContextVar[Optional[PrimaryAuthFailure]] = contextvars.ContextVar(
"pending_primary_auth_failure", default=None)
def set_pending_primary_auth_failure(failure: Optional[PrimaryAuthFailure]) -> None:
_PENDING.set(failure if isinstance(failure, PrimaryAuthFailure) else None)
def take_pending_primary_auth_failure() -> Optional[PrimaryAuthFailure]:
"""This turn's pending failure, cleared so a later resolution in the same context starts clean."""
failure = _PENDING.get()
_PENDING.set(None)
return failure
# Backup failure reason -> what the backup's problem is, in the user's words.
_BACKUP_PROBLEM = {
"billing": "is out of credits (or over its usage limit)",
"billing_unverified": "reported it is out of credits (or over its usage limit)",
"auth": "rejected its sign-in as well",
"auth_permanent": "rejected its sign-in as well",
"rate_limit": "is rate-limiting requests",
"upstream_rate_limit": "is rate-limiting requests",
"overloaded": "is overloaded",
"server_error": "returned a server error",
"timeout": "did not respond in time",
"model_not_found": "does not have the configured model",
}
def primary_auth_lead(failure: PrimaryAuthFailure, *, backup: str, failure_reason: Any, error: Any) -> str:
"""Root cause first (re-login the primary, exact command), then the backup's own problem."""
from agent.redact import redact_sensitive_text
from agent.turn_failure_copy import provider_label_for, short_detail
primary = provider_label_for(failure.provider) if failure.provider else "your main AI provider"
problem = _BACKUP_PROBLEM.get(str(failure_reason or ""))
if problem is None:
detail = short_detail(error, 160) if error else "failed"
problem = f"failed too ({detail})"
why = redact_sensitive_text(failure.error, force=True)
return (
f"⚠️ {primary} needs you to sign in again: run `{failure.relogin}`. Its login failed "
f"({why}), so Hermes switched to the backup provider ({backup or 'backup'}), and the "
f"backup {problem}. Re-authenticating {primary} fixes this; topping up the backup only "
"keeps the fallback running."
)
def lead_failed_turn_with_primary_auth(agent: Any, result: Any) -> None:
"""Prefix a failed turn's ``final_response`` with the primary re-login (in place).
Applied once where every turn envelope leaves the loop, so the billing, non-retryable and
retries-exhausted paths all keep their own backup-specific copy underneath the lead."""
failure = getattr(agent, "_primary_auth_failure", None)
if not isinstance(failure, PrimaryAuthFailure) or not isinstance(result, dict):
return
if result.get("failed") is not True or result.get("interrupted") or "primary_auth_failure" in result:
return
current = str(getattr(agent, "provider", "") or "").strip()
if failure.provider and current.lower() == failure.provider != failure.backup.lower():
return # the primary itself is serving again: its failure is not this turn's story
backup = current if current and current.lower() != "custom" else (failure.backup or current)
lead = primary_auth_lead(failure, backup=backup, failure_reason=result.get("failure_reason"),
error=result.get("error"))
body = str(result.get("final_response") or "").strip()
result["final_response"] = f"{lead}\n\nBackup provider said: {body}" if body else lead
from agent.redact import redact_sensitive_text
result["primary_auth_failure"] = {"provider": failure.provider, "relogin": failure.relogin,
"error": redact_sensitive_text(failure.error, force=True)}