Skip to content

The agent that grows with you

fix(gateway): don't send messaging users to /login for a host-side resolution failure

kshitijk4poorbf3866dda11b
diff --git a/gateway/run_turn_runner.py b/gateway/run_turn_runner.py
index ed02429f196..4d6137aadb9 100644
--- a/gateway/run_turn_runner.py
+++ b/gateway/run_turn_runner.py
@@ -131,6 +131,36 @@ def _settle_api_runtime_lock(agent, result, captured):
result.update(final_response='', failed=True, completed=False, error=str(exc))
+def _resolution_failure_reply(exc: Exception, platform: Platform) -> tuple[str, str]:
+ """``(chat text, failure_reason)`` for a model/credential resolution failure before the turn."""
+ from hermes_cli.auth import AuthError, is_rate_limited_auth_error
+ from hermes_state_runtime import RuntimeStoreError
+ if isinstance(exc, RuntimeStoreError):
+ # Session-policy refusals carry a stable reason code (e.g. a CLI launch key revoked by
+ # daemon restart): keep it in the reply so clients can act on it, and do not suggest
+ # /login — the profile's own credentials were never in play.
+ return (f"⚠️ This session's launch credentials are no longer available ({exc.reason}), so this "
+ "message wasn't processed. Start a new session from the CLI to bind them again.", "auth_permanent")
+ if is_rate_limited_auth_error(exc.__cause__):
+ # Quota cap with valid credentials: /login cannot help; name the reset window (#89401).
+ from gateway.run import _gateway_provider_error_reply
+ return _gateway_provider_error_reply(str(exc)), "auth_permanent"
+ if platform == Platform.LOCAL:
+ # The local operator reads the terminal: the resolver's own sentence ("provider 'custom'
+ # resolved without credentials ...") is the diagnosis; /login is not.
+ return f"⚠️ {exc}", "auth_permanent"
+ chain: list[BaseException] = []
+ link: BaseException | None = exc
+ while link is not None and link not in chain:
+ chain.append(link)
+ link = link.__cause__ or link.__context__
+ if any(isinstance(e, OSError) for e in chain) and not any(isinstance(e, AuthError) for e in chain):
+ # A host-side failure (a CA bundle deleted under a long-lived process, an unreadable file)
+ # with no credential verdict: /login cannot fix it, the gateway log can (#140344).
+ return t("gateway.errors.generic_failed"), "unknown"
+ return t("gateway.errors.no_credentials"), "auth_permanent"
+
+
class TurnRunner(GatewayTurnProgressMixin, GatewaySessionAgentMixin):
"""Per-turn collaborator carrying ``GatewayRunner._run_agent_inner``'s tool-progress callbacks."""
@@ -1191,31 +1221,10 @@ class TurnRunner(GatewayTurnProgressMixin, GatewaySessionAgentMixin):
# a FAILED turn: a one-shot client exits non-zero, transcript persistence closes the
# turn, and an API receipt reports failure, never a completed turn with an apology.
logger.warning("Model resolution failed for session %s: %s", ctx.session_key or "", exc, exc_info=True)
- from hermes_state_runtime import RuntimeStoreError
from agent.turn_failure_copy import stamp_failure
- resolution_error = str(exc)
-
- def _unresolved(text: str) -> dict:
- return stamp_failure({"final_response": text, "messages": [], "api_calls": 0, "tools": [],
- "failed": True, "completed": False, "error": resolution_error},
- "auth_permanent", False)
- if isinstance(exc, RuntimeStoreError):
- # Session-policy refusals carry a stable reason code (e.g. a CLI launch key
- # revoked by daemon restart): keep it in the reply so clients can act on it, and
- # do not suggest /login — the profile's own credentials were never in play.
- return _unresolved(f"⚠️ This session's launch credentials are no longer available "
- f"({exc.reason}), so this message wasn't processed. Start a new "
- "session from the CLI to bind them again.")
- from hermes_cli.auth import is_rate_limited_auth_error
- if is_rate_limited_auth_error(exc.__cause__):
- # Quota cap with valid credentials: /login cannot help; name the reset window (#89401).
- from gateway.run import _gateway_provider_error_reply
- return _unresolved(_gateway_provider_error_reply(str(exc)))
- if ctx.source.platform == Platform.LOCAL:
- # The local operator reads the terminal: the resolver's own sentence ("provider
- # 'custom' resolved without credentials ...") is the diagnosis; /login is not.
- return _unresolved(f"⚠️ {exc}")
- return _unresolved(t("gateway.errors.no_credentials"))
+ text, reason = _resolution_failure_reply(exc, ctx.source.platform)
+ return stamp_failure({"final_response": text, "messages": [], "api_calls": 0, "tools": [],
+ "failed": True, "completed": False, "error": str(exc)}, reason, False)
from gateway.session_authorities import active_authority
from gateway.session_worker_construct import session_routing
pr = session_routing(active_authority(runner), policy) if policy else runner._provider_routing
diff --git a/tests/gateway/test_local_model_connection_reply.py b/tests/gateway/test_local_model_connection_reply.py
index 3b82289ad76..2d033615824 100644
--- a/tests/gateway/test_local_model_connection_reply.py
+++ b/tests/gateway/test_local_model_connection_reply.py
@@ -249,3 +249,39 @@ class TestQuotaExhaustedIsNotAnAuthFailure:
assert detail in result["final_response"] and "/login" not in result["final_response"]
else:
assert detail not in result["final_response"] and "/login" in result["final_response"]
+
+
+def test_turn_runner_host_side_resolution_failure_does_not_send_user_to_login(monkeypatch):
+ """A local OSError under resolution (the CA bundle deleted by an install-environment rotation)
+ is not a credential verdict: messaging users get the gateway-log reply, never /login (#140344).
+ The same chain with an AuthError in it keeps the sign-in reply (control)."""
+ from types import SimpleNamespace
+ import gateway.run as gateway_run
+ from gateway.run_turn_runner import TurnRunner
+ from gateway.session import SessionSource
+ from gateway.turn_context import TurnContext
+ from hermes_cli.auth import AuthError
+
+ def _raising(cause):
+ def _resolve(**_kwargs):
+ try:
+ raise cause
+ except Exception as exc:
+ raise RuntimeError(str(exc)) from exc
+ return _resolve
+
+ monkeypatch.setattr(gateway_run, "_current_max_iterations", lambda: 30)
+ replies = {}
+ for label, cause in (("oserror", FileNotFoundError(2, "No such file or directory")),
+ ("auth", AuthError("No usable credentials found for provider 'nous'."))):
+ runner = SimpleNamespace(_resolve_session_agent_runtime=_raising(cause),
+ _get_system_prompt_for_channel=lambda *a, **k: "",
+ _ephemeral_system_prompt="", _adapter_for_source=lambda _s: None)
+ ctx = TurnContext(source=SessionSource(platform=Platform.DISCORD, chat_id="T1", chat_type="thread"),
+ session_key="discord:T1", user_config={}, message="retry")
+ result = TurnRunner(runner, ctx).run_sync()
+ assert result["failed"] is True and result["api_calls"] == 0
+ replies[label] = (result["final_response"], result["failure_reason"])
+ assert "/login" not in replies["oserror"][0] and "hermes logs" in replies["oserror"][0]
+ assert replies["oserror"][1] == "unknown"
+ assert "/login" in replies["auth"][0] and replies["auth"][1] == "auth_permanent"