Skip to content

The agent that grows with you

fix(auth): only an explicit OAuth grant-dead code makes a Nous refresh terminal

kshitijk4poor8a11d6342f76
diff --git a/hermes_cli/auth_nous.py b/hermes_cli/auth_nous.py
index 83338cf991c..a20e250ae2c 100644
--- a/hermes_cli/auth_nous.py
+++ b/hermes_cli/auth_nous.py
@@ -613,13 +613,17 @@ def _refresh_access_token(
raise AuthError(
f"Nous Portal is temporarily unavailable (HTTP {response.status_code}); retry shortly.",
provider="nous", code="temporarily_unavailable", retryable=True)
+ from hermes_cli.auth import _OAUTH_GRANT_DEAD_CODES
try:
error_payload = response.json()
except Exception as exc:
- raise _nous_err("Refresh token exchange failed", relogin=True) from exc
- code = str(error_payload.get("error", "invalid_grant"))
+ raise _nous_err("Refresh token exchange failed") from exc
+ # Only an explicit OAuth grant-dead code is terminal: a 429/404 gateway body without an
+ # ``error`` key says nothing about the refresh token, so it must not wipe credentials.
+ code = error_payload.get("error")
+ code = str(code) if code is not None else None
description = str(error_payload.get("error_description") or "Refresh token exchange failed")
- relogin = code in {"invalid_grant", "invalid_token", "refresh_token_reused"}
+ relogin = code in _OAUTH_GRANT_DEAD_CODES
# OAuth 2.1 "refresh token reuse": an external process (health check, monitoring tool, custom
# self-heal hook) redeemed Hermes's refresh_token without persisting the rotated token, so the
# server retired the original and revoked the whole session chain as a token-theft signal.
diff --git a/tests/hermes_cli/test_auth_nous_provider.py b/tests/hermes_cli/test_auth_nous_provider.py
index 9449fad80a4..d4531081568 100644
--- a/tests/hermes_cli/test_auth_nous_provider.py
+++ b/tests/hermes_cli/test_auth_nous_provider.py
@@ -684,17 +684,34 @@ def test_refresh_token_reuse_detection_surfaces_actionable_message():
assert exc_info.value.relogin_required is True
[email protected]("status_code", [500, 503, 599])
-def test_refresh_token_exchange_5xx_is_retryable_without_parsing_error_body(status_code):
- """A Portal 5xx is transient even when its body is not OAuth JSON (#120976)."""
- from hermes_cli.auth import _refresh_access_token
+ "status_code, body, expected_code, expected_retryable",
+ [
+ (500, None, "temporarily_unavailable", True),
+ (503, None, "temporarily_unavailable", True),
+ (599, None, "temporarily_unavailable", True),
+ (429, {"code": "429", "message": "rate limited"}, None, None),
+ (404, {"message": "not found"}, None, None),
+ (400, ValueError("not json"), None, None),
+ ],
+)
+def test_refresh_token_exchange_without_grant_error_code_is_not_terminal(
+ status_code, body, expected_code, expected_retryable
+):
+ """A Portal 5xx is transient even when its body is not OAuth JSON (#120976), and a
+ non-5xx body that carries no OAuth ``error`` code must not be treated as a dead grant."""
+ from hermes_cli.auth import _is_terminal_nous_refresh_error, _refresh_access_token
class _FakeResponse:
def __init__(self):
self.status_code = status_code
def json(self):
- raise AssertionError("5xx refresh handling must not parse response.json()")
+ if body is None:
+ raise AssertionError("5xx refresh handling must not parse response.json()")
+ if isinstance(body, Exception):
+ raise body
+ return body
class _FakeClient:
def post(self, *args, **kwargs):
@@ -708,9 +725,10 @@ def test_refresh_token_exchange_5xx_is_retryable_without_parsing_error_body(stat
refresh_token="refresh-still-valid",
)
- assert exc_info.value.code == "temporarily_unavailable"
+ assert exc_info.value.code == expected_code
assert exc_info.value.relogin_required is False
- assert exc_info.value.retryable is True
+ assert exc_info.value.retryable is expected_retryable
+ assert _is_terminal_nous_refresh_error(exc_info.value) is False
def test_runtime_refresh_503_preserves_nous_oauth_credentials(tmp_path, monkeypatch):