The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(auth): Codex credential and relogin copy name the failing profile's own sign-in
teknium169deb4337259
diff --git a/hermes_cli/auth.py b/hermes_cli/auth.pyindex ed63ed45800..9b8df2caf9f 100644--- a/hermes_cli/auth.py+++ b/hermes_cli/auth.py@@ -444,7 +444,10 @@ def format_auth_error(error: Exception) -> str:# Rate-limit / quota errors are not credential problems: never append "re-authenticate".return str(error)if error.relogin_required:- return f"{error} Run `hermes model` to re-authenticate."+ # Profile-aware: a bare `hermes model` from a named profile re-signs the ROOT store (#114012).+ from hermes_constants import profile_cli_selector++ return f"{error} Run `hermes {profile_cli_selector()}model` to re-authenticate."if error.code in _ENTITLEMENT_ERROR_CODES:if error.provider == "nous":return _format_nous_entitlement_auth_error(error)diff --git a/hermes_cli/auth_codex.py b/hermes_cli/auth_codex.pyindex 713934c041e..f4c029462b4 100644--- a/hermes_cli/auth_codex.py+++ b/hermes_cli/auth_codex.py@@ -31,11 +31,17 @@ if TYPE_CHECKING: # annotation-only; the runtime import would be a cycle# Log-record parity with the origin module (caplog tests pin "hermes_cli.auth").logger = logging.getLogger("hermes_cli.auth")-_MISSING_ACCESS_TOKEN_MSG = (- "Codex auth is missing access_token. Run `hermes auth` to re-authenticate.")-_MISSING_REFRESH_TOKEN_MSG = (- "Codex auth is missing refresh_token. Run `hermes auth` to re-authenticate.")-_NO_CREDENTIALS_MSG = "No Codex credentials stored. Run `hermes auth` to authenticate."+# ``{relogin}`` is filled at raise time with the profile-aware sign-in command: a bare+# ``hermes auth`` from a named profile re-signs the ROOT store (93889b770da, #114012).+_MISSING_ACCESS_TOKEN_MSG = "Codex auth is missing access_token. Run `{relogin}` to re-authenticate."+_MISSING_REFRESH_TOKEN_MSG = "Codex auth is missing refresh_token. Run `{relogin}` to re-authenticate."+_NO_CREDENTIALS_MSG = "No Codex credentials stored. Run `{relogin}` to authenticate."+++def _codex_relogin_command() -> str:+ from agent.turn_failure_copy import oauth_relogin_command++ return oauth_relogin_command("openai-codex")def _parse_retry_after_seconds(headers: Any) -> Optional[int]:@@ -87,17 +93,19 @@ def _read_codex_tokens(*, _lock: bool = True) -> Dict[str, Any]:auth_store = _load_auth_store_maybe_locked(_lock)state = _load_provider_state(auth_store, "openai-codex")if not state:- raise _codex_err(_NO_CREDENTIALS_MSG, "codex_auth_missing", relogin=True)+ raise _codex_err(_NO_CREDENTIALS_MSG.format(relogin=_codex_relogin_command()),+ "codex_auth_missing", relogin=True)tokens = state.get("tokens")if not isinstance(tokens, dict):raise _codex_err(- "Codex auth state is missing tokens. Run `hermes auth` to re-authenticate.",+ f"Codex auth state is missing tokens. Run `{_codex_relogin_command()}` to re-authenticate.","codex_auth_invalid_shape", relogin=True)if not _nonempty_str(tokens.get("access_token")):- raise _codex_err(_MISSING_ACCESS_TOKEN_MSG, "codex_auth_missing_access_token", relogin=True)+ raise _codex_err(_MISSING_ACCESS_TOKEN_MSG.format(relogin=_codex_relogin_command()),+ "codex_auth_missing_access_token", relogin=True)if not _nonempty_str(tokens.get("refresh_token")):- raise _codex_err(- _MISSING_REFRESH_TOKEN_MSG, "codex_auth_missing_refresh_token", relogin=True)+ raise _codex_err(_MISSING_REFRESH_TOKEN_MSG.format(relogin=_codex_relogin_command()),+ "codex_auth_missing_refresh_token", relogin=True)return {"tokens": tokens, "last_refresh": state.get("last_refresh")}@@ -339,7 +347,7 @@ def _codex_refresh_failure_error(response: "httpx.Response") -> AuthError:"Codex refresh token was already consumed by another client ""(e.g. Codex CLI or VS Code extension). ""Run `codex` in your terminal to generate fresh tokens, "- "then run `hermes auth` to re-authenticate.")+ f"then run `{_codex_relogin_command()}` to re-authenticate.")# A 401/403 from the token endpoint always means the refresh token is invalid/expired —# force relogin even if the body error code wasn't one of the known strings.relogin_required = (@@ -354,8 +362,8 @@ def refresh_codex_oauth_pure(from hermes_cli.auth import _nonempty_str, _utc_now_zdel access_token # Access token is only used by callers to decide whether to refresh.if not _nonempty_str(refresh_token):- raise _codex_err(- _MISSING_REFRESH_TOKEN_MSG, "codex_auth_missing_refresh_token", relogin=True)+ raise _codex_err(_MISSING_REFRESH_TOKEN_MSG.format(relogin=_codex_relogin_command()),+ "codex_auth_missing_refresh_token", relogin=True)with _codex_http_client(timeout=httpx.Timeout(max(5.0, float(timeout_seconds))),headers={"Accept": "application/json", "User-Agent": CODEX_OAUTH_USER_AGENT}) as client:@@ -509,7 +517,8 @@ def resolve_codex_runtime_credentials(raise _codex_quota_exhausted_error(int(reset_at - time.time()) if in_future else None)if read_error is not None:raise read_error- raise _codex_err(_NO_CREDENTIALS_MSG, "codex_auth_missing", relogin=True)+ raise _codex_err(_NO_CREDENTIALS_MSG.format(relogin=_codex_relogin_command()),+ "codex_auth_missing", relogin=True)tokens = dict(data["tokens"])access_token = _stripped(tokens.get("access_token"))refresh_timeout_seconds = env_float("HERMES_CODEX_REFRESH_TIMEOUT_SECONDS", 20)diff --git a/tests/hermes_cli/test_codex_relogin_copy_names_profile.py b/tests/hermes_cli/test_codex_relogin_copy_names_profile.pynew file mode 100644index 00000000000..d512105a955--- /dev/null+++ b/tests/hermes_cli/test_codex_relogin_copy_names_profile.py@@ -0,0 +1,46 @@+"""Codex credential-failure copy sends the user to THIS profile's own sign-in.++Profiles are islands (93889b770da): a bare ``hermes auth`` / ``hermes model`` from a named+profile's error text re-signs the ROOT store, which is exactly the loop #114012 measured.+Every relogin hint raised by ``hermes_cli/auth_codex.py`` and appended by+``format_auth_error`` must carry the ``-p <profile>`` selector under a profile HERMES_HOME.+"""++from __future__ import annotations++from types import SimpleNamespace++import pytest++from hermes_cli.auth import AuthError, _read_codex_tokens, format_auth_error+from hermes_cli.auth_codex import _codex_refresh_failure_error+++def codex_profile_home(tmp_path, monkeypatch):+ profile_home = tmp_path / ".hermes" / "profiles" / "codex"+ profile_home.mkdir(parents=True)+ monkeypatch.setenv("HOME", str(tmp_path))+ monkeypatch.setenv("HERMES_HOME", str(profile_home))+ return profile_home+++def test_missing_and_reused_codex_credential_copy_names_the_profile(codex_profile_home):+ with pytest.raises(AuthError) as missing:+ _read_codex_tokens()+ reused = _codex_refresh_failure_error(SimpleNamespace(+ status_code=400,+ json=lambda: {"error": "refresh_token_reused", "error_description": "already used"},+ ))+ for err in (missing.value, reused):+ text = str(err)+ assert "`hermes -p codex auth add openai-codex --type oauth`" in text, text+ assert "`hermes auth`" not in text, text+++def test_format_auth_error_relogin_suffix_names_the_profile(codex_profile_home):+ err = AuthError("Codex token refresh failed: invalid_grant", provider="openai-codex",+ code="invalid_grant", relogin_required=True)+ rendered = format_auth_error(err)+ assert "`hermes -p codex model`" in rendered, rendered+ assert "`hermes model`" not in rendered, rendered