Skip to content

The agent that grows with you

fix(auth): status and doctor Codex reads never adopt, refresh or persist credentials

teknium1784f94fb0897
diff --git a/hermes_cli/auth.py b/hermes_cli/auth.py
index 07affc6cbfc..fe5a09936cc 100644
--- a/hermes_cli/auth.py
+++ b/hermes_cli/auth.py
@@ -1726,10 +1726,13 @@ def _codex_pool_rate_limited_status() -> Optional[Dict[str, Any]]:
def get_codex_auth_status() -> Dict[str, Any]:
- """Status snapshot for Codex auth (pool first, then legacy provider state)."""
+ """Status snapshot for Codex auth (pool first, then legacy provider state).
+
+ Read-only by contract: status/doctor must never adopt, refresh or persist a credential (#68004)."""
return _pool_first_oauth_status(
"openai-codex", is_expiring=_codex_access_token_is_expiring, auth_mode="chatgpt",
- resolve=resolve_codex_runtime_credentials, on_pool_miss=_codex_pool_rate_limited_status)
+ resolve=lambda: resolve_codex_runtime_credentials(read_only=True),
+ on_pool_miss=_codex_pool_rate_limited_status)
def get_xai_oauth_auth_status() -> Dict[str, Any]:
@@ -1737,7 +1740,7 @@ def get_xai_oauth_auth_status() -> Dict[str, Any]:
# unconditionally (auth.json may still carry a legacy ``oauth_pkce`` label).
return _pool_first_oauth_status(
"xai-oauth", is_expiring=_xai_access_token_is_expiring, auth_mode="oauth_device_code",
- resolve=resolve_xai_oauth_runtime_credentials)
+ resolve=lambda: resolve_xai_oauth_runtime_credentials(refresh_if_expiring=False))
def _provider_env_base_url(pconfig: ProviderConfig) -> str:
diff --git a/hermes_cli/auth_codex.py b/hermes_cli/auth_codex.py
index db55aefe802..bfc04e66499 100644
--- a/hermes_cli/auth_codex.py
+++ b/hermes_cli/auth_codex.py
@@ -468,9 +468,15 @@ def _import_codex_cli_tokens() -> Optional[Dict[str, str]]:
def resolve_codex_runtime_credentials(
*, force_refresh: bool = False, refresh_if_expiring: bool = True,
- refresh_skew_seconds: int = CODEX_ACCESS_TOKEN_REFRESH_SKEW_SECONDS) -> Dict[str, Any]:
+ refresh_skew_seconds: int = CODEX_ACCESS_TOKEN_REFRESH_SKEW_SECONDS,
+ read_only: bool = False) -> Dict[str, Any]:
"""Resolve runtime credentials from Hermes's own Codex token store.
+ ``read_only=True`` (status / doctor / pickers) reports the stored state as-is: no Codex CLI
+ adoption, no token refresh, no auth-store write — and it wins over ``force_refresh``. A
+ diagnostic that silently imports another program's rotating refresh token or spends one is a
+ mutation the user never asked for (#68004).
+
Falls back to the credential pool when the singleton (``providers.openai-codex.tokens``) has no
usable access_token but the pool (``credential_pool.openai-codex``) does.
@@ -489,7 +495,7 @@ def resolve_codex_runtime_credentials(
data = _read_codex_tokens()
except AuthError as exc:
read_error = exc
- if exc.relogin_required and exc.code in {
+ if not read_only and exc.relogin_required and exc.code in {
"codex_auth_missing_access_token", "codex_auth_missing_refresh_token",
"codex_auth_invalid_shape"}:
imported = _recover_codex_tokens_from_cli(str(exc.code or "auth_error"))
@@ -497,7 +503,7 @@ def resolve_codex_runtime_credentials(
data = {"tokens": imported, "last_refresh": imported.get("last_refresh")}
if data is None:
pool_token = _pool_codex_access_token()
- if pool_token and force_refresh:
+ if pool_token and force_refresh and not read_only:
# Pool-only setup: a forced refresh must rotate the pool entry, not resend its token.
from agent.credential_pool import load_pool
refreshed = load_pool("openai-codex").try_refresh_matching(api_key_hint=pool_token)
@@ -530,6 +536,8 @@ def resolve_codex_runtime_credentials(
refresh_timeout_seconds = env_float("HERMES_CODEX_REFRESH_TIMEOUT_SECONDS", 20)
def _should_refresh(token: str) -> bool:
+ if read_only:
+ return False
return bool(force_refresh) or (
refresh_if_expiring and _codex_access_token_is_expiring(token, refresh_skew_seconds))
diff --git a/tests/hermes_cli/test_oauth_status_pool_observation.py b/tests/hermes_cli/test_oauth_status_pool_observation.py
index f503b19af1a..c12f91cb06e 100644
--- a/tests/hermes_cli/test_oauth_status_pool_observation.py
+++ b/tests/hermes_cli/test_oauth_status_pool_observation.py
@@ -92,3 +92,71 @@ def test_status_snapshot_leaves_round_robin_order_and_counts_untouched(tmp_path,
# Control: a runtime selection still rotates and persists the new order.
load_pool("openai-codex").select()
assert _persisted_pool(home) != before
+
+
+def _singleton_only_codex_home(tmp_path, monkeypatch, *, tokens: dict, codex_cli_tokens: dict):
+ """HERMES_HOME whose Codex credentials are the ``providers.openai-codex`` singleton only, with a
+ valid Codex CLI login sitting beside it in ``CODEX_HOME``."""
+ home, codex_home = tmp_path / "hermes", tmp_path / "codex"
+ home.mkdir()
+ codex_home.mkdir()
+ (home / "auth.json").write_text(json.dumps({
+ "version": 1, "active_provider": "openai-codex",
+ "providers": {"openai-codex": {"tokens": tokens, "auth_mode": "chatgpt"}}}), encoding="utf-8")
+ (codex_home / "auth.json").write_text(json.dumps({"tokens": codex_cli_tokens}), encoding="utf-8")
+ monkeypatch.setenv("HERMES_HOME", str(home))
+ monkeypatch.setenv("CODEX_HOME", str(codex_home))
+ return home
+
+
+def _singleton_tokens(home) -> dict:
+ return json.loads((home / "auth.json").read_text(encoding="utf-8"))["providers"]["openai-codex"]["tokens"]
+
+
+def test_status_snapshot_never_adopts_codex_cli_tokens(tmp_path, monkeypatch):
+ """#68004: a Hermes store missing its refresh_token is recovery-eligible on the runtime path, but
+ ``hermes status`` / ``hermes doctor`` must not import the Codex CLI's single-use token family."""
+ from hermes_cli.auth import resolve_codex_runtime_credentials
+
+ stale = {"access_token": _jwt_with_exp(-60)}
+ home = _singleton_only_codex_home(
+ tmp_path, monkeypatch, tokens=stale,
+ codex_cli_tokens={"access_token": _jwt_with_exp(86400), "refresh_token": "cli-refresh"})
+
+ get_codex_auth_status()
+
+ assert _singleton_tokens(home) == stale, "a status read persisted the Codex CLI login into auth.json"
+
+ # Control: the runtime resolver still self-heals from the CLI file.
+ assert resolve_codex_runtime_credentials()["source"] == "hermes-auth-store"
+ assert _singleton_tokens(home)["refresh_token"] == "cli-refresh"
+
+
+def test_status_snapshot_never_refreshes_an_expiring_singleton(tmp_path, monkeypatch):
+ """#68004: an expiring singleton token is reported as stored; only the runtime lease may spend
+ the refresh token (and ``read_only`` wins over ``force_refresh``)."""
+ import hermes_cli.auth as auth
+ from hermes_cli.auth import resolve_codex_runtime_credentials
+
+ expiring = {"access_token": _jwt_with_exp(30), "refresh_token": "singleton-refresh"}
+ home = _singleton_only_codex_home(
+ tmp_path, monkeypatch, tokens=expiring, codex_cli_tokens={})
+ refresh_calls: list = []
+
+ def _rotate(access_token, refresh_token, *args, **kwargs):
+ refresh_calls.append(refresh_token)
+ return {"access_token": _jwt_with_exp(86400), "refresh_token": "rotated-refresh"}
+
+ monkeypatch.setattr(auth, "refresh_codex_oauth_pure", _rotate)
+
+ status = get_codex_auth_status()
+ resolve_codex_runtime_credentials(force_refresh=True, read_only=True)
+
+ assert refresh_calls == [], "a status read spent the single-use singleton refresh token"
+ assert status["logged_in"] is True and status["api_key"] == expiring["access_token"]
+ assert _singleton_tokens(home) == expiring
+
+ # Control: the runtime path refreshes and persists the rotated pair.
+ resolve_codex_runtime_credentials()
+ assert refresh_calls == ["singleton-refresh"]
+ assert _singleton_tokens(home)["refresh_token"] == "rotated-refresh"