Skip to content

The agent that grows with you

fix(auth): a removed env:XAI_API_KEY no longer overrides xai-oauth for x_search

teknium1c265f776a813
diff --git a/tests/tools/test_xai_http_credentials.py b/tests/tools/test_xai_http_credentials.py
index 617bf521b8e..0a84ffdac80 100644
--- a/tests/tools/test_xai_http_credentials.py
+++ b/tests/tools/test_xai_http_credentials.py
@@ -184,3 +184,53 @@ def test_prefer_api_key_honors_profile_scope_only_key(tmp_path, monkeypatch):
secret_scope.reset_secret_scope(token)
secret_scope.set_multiplex_active(previous_multiplex)
invalidate_env_cache()
+
+
+# ---------------------------------------------------------------------------
+# #116155: `hermes auth remove xai` suppresses env:XAI_API_KEY, but a stale value
+# still inherited by the gateway process environment overrode the working
+# xai-oauth grant for x_search (prefer_api_key=True).
+# ---------------------------------------------------------------------------
+
+
+def _write_auth_store(tmp_path, monkeypatch, suppressed):
+ import json
+ import time
+
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+ (tmp_path / "auth.json").write_text(json.dumps({
+ "version": 1,
+ "providers": {"xai-oauth": {"tokens": {
+ "access_token": "oauth-live-token", "refresh_token": "r",
+ "expires_at": int(time.time() * 1000) + 3_600_000}}},
+ "credential_pool": {},
+ "suppressed_sources": suppressed,
+ }), encoding="utf-8")
+
+
[email protected]("suppressed, expected_provider", [
+ ({"xai": ["env:XAI_API_KEY"]}, "xai-oauth"), # removed source is ignored -> OAuth serves
+ ({}, "xai"), # control: the env key still wins when not removed
+])
+def test_suppressed_env_key_does_not_override_oauth(tmp_path, monkeypatch, suppressed, expected_provider):
+ from hermes_cli.config import invalidate_env_cache
+ from tools.xai_http import resolve_xai_http_credentials
+
+ _write_auth_store(tmp_path, monkeypatch, suppressed)
+ monkeypatch.setenv("XAI_API_KEY", "dead-creditless-team-key")
+ invalidate_env_cache()
+
+ creds = resolve_xai_http_credentials(prefer_api_key=True)
+ assert creds["provider"] == expected_provider
+ assert (creds["api_key"] == "dead-creditless-team-key") is (expected_provider == "xai")
+
+
+def test_suppression_is_scoped_to_its_provider(tmp_path, monkeypatch):
+ """Suppressing xai's env source must not blind another provider's env read."""
+ from tools.tool_backend_helpers import resolve_provider_secret
+
+ _write_auth_store(tmp_path, monkeypatch, {"xai": ["env:XAI_API_KEY"]})
+ monkeypatch.setenv("ELEVENLABS_API_KEY", "el-key")
+ monkeypatch.setenv("XAI_API_KEY", "dead-key")
+ assert resolve_provider_secret("ELEVENLABS_API_KEY", "elevenlabs") == "el-key"
+ assert resolve_provider_secret("XAI_API_KEY", "xai") == ""
diff --git a/tools/tool_backend_helpers.py b/tools/tool_backend_helpers.py
index 52796a180b7..01f75ef9abb 100644
--- a/tools/tool_backend_helpers.py
+++ b/tools/tool_backend_helpers.py
@@ -110,27 +110,45 @@ def _dotenv_value(env_var: str) -> str:
return ""
+def _env_source_suppressed(provider_id: str, env_var: str) -> bool:
+ """``hermes auth remove <provider>`` records ``env:<VAR>`` in ``suppressed_sources`` and promises
+ the variable is ignored until ``hermes auth add``; a value still living in a long-running
+ gateway's process environment (inherited across update restarts) must honor that too."""
+ if not provider_id:
+ return False
+ try:
+ from hermes_cli.auth import is_source_suppressed
+ return is_source_suppressed(provider_id, f"env:{env_var}")
+ except Exception: # pragma: no cover — auth store unreadable: keep prior behavior
+ return False
+
+
def resolve_provider_secret(env_var: str, provider_id: str, config_value: str = "",
env_getter=None) -> str:
"""Resolve a voice-provider API key (single owner for STT/TTS lookup). Order: explicit
``config_value`` -> profile secret scope / env -> ``.env`` via ``env_getter`` (or
``hermes_cli.config.get_env_value``) -> credential pool for ``provider_id``. Under an
active multiplex turn the profile scope is authoritative: a miss returns ``""`` rather
- than borrowing another profile's env or pool. Never raises.
+ than borrowing another profile's env or pool. Never raises. The env/.env tier is skipped
+ entirely while ``env:<env_var>`` is suppressed for ``provider_id`` (#116155).
Resolution order (fixes #68003 — keys added via ``hermes auth add <provider>`` were invisible to the
voice tools, which only consulted env/.env):
"""
- key = str(config_value or "").strip() or _scoped_credential(env_var)
+ key = str(config_value or "").strip()
if key:
return key
+ env_suppressed = _env_source_suppressed(provider_id, env_var)
+ if not env_suppressed and (key := _scoped_credential(env_var)):
+ return key
try:
from agent.secret_scope import is_multiplex_active
if is_multiplex_active():
return ""
except Exception: # pragma: no cover — secret_scope is in-repo
pass
- key = str(env_getter(env_var) or "").strip() if env_getter else _dotenv_value(env_var)
+ if not env_suppressed:
+ key = str(env_getter(env_var) or "").strip() if env_getter else _dotenv_value(env_var)
if key or not provider_id:
return key
try: