fix(hermes_cli): keep TTS/setup xAI OAuth from switching active chat provider

Save side-tool OAuth tokens without promoting xai-oauth via active_provider
or model.provider so hermes setup tts login no longer hijacks inference routing.
This commit is contained in:
Fangliquan 2026-07-25 20:41:57 +08:00 • committed by Teknium
parent a54067a6fd
commit fce06e909d
5 changed files with 134 additions and 6 deletions

View file

@ -281,6 +281,40 @@ def test_save_and_read_xai_oauth_tokens_roundtrip(tmp_path, monkeypatch):
assert data["discovery"]["token_endpoint"] == "https://auth.x.ai/oauth2/token"
def test_save_xai_oauth_tokens_set_active_false_preserves_active_provider(
tmp_path, monkeypatch
):
"""Side-tool credential saves must not flip auth.json active_provider."""
hermes_home = tmp_path / "hermes"
hermes_home.mkdir(parents=True, exist_ok=True)
auth_path = hermes_home / "auth.json"
auth_path.write_text(
json.dumps(
{
"version": 1,
"active_provider": "openrouter",
"providers": {},
}
)
)
monkeypatch.setenv("HERMES_HOME", str(hermes_home))
_save_xai_oauth_tokens(
{
"access_token": "side-tool-at",
"refresh_token": "side-tool-rt",
"id_token": "",
"token_type": "Bearer",
},
discovery={"token_endpoint": "https://auth.x.ai/oauth2/token"},
set_active=False,
)
raw = json.loads(auth_path.read_text())
assert raw["active_provider"] == "openrouter"
assert raw["providers"]["xai-oauth"]["tokens"]["access_token"] == "side-tool-at"
def test_read_xai_oauth_tokens_missing(tmp_path, monkeypatch):
hermes_home = tmp_path / "hermes"
hermes_home.mkdir(parents=True, exist_ok=True)

View file

@ -0,0 +1,78 @@
"""Regression: TTS/setup xAI OAuth must not hijack the active chat provider."""
import json
import yaml
def test_run_xai_oauth_login_from_setup_does_not_hijack_active_provider(
tmp_path, monkeypatch
):
"""TTS/setup OAuth must save tokens without switching chat inference routing.
Regression: `_run_xai_oauth_login_from_setup` used to call
`_update_config_for_provider("xai-oauth")` (and token save flipped
`active_provider`), so `hermes setup tts` OAuth login hijacked the main
chat provider.
"""
hermes_home = tmp_path / "hermes"
hermes_home.mkdir(parents=True, exist_ok=True)
monkeypatch.setenv("HERMES_HOME", str(hermes_home))
auth_path = hermes_home / "auth.json"
auth_path.write_text(
json.dumps(
{
"version": 1,
"active_provider": "openrouter",
"providers": {},
}
),
encoding="utf-8",
)
config_path = hermes_home / "config.yaml"
config_path.write_text(
yaml.safe_dump(
{
"model": {
"provider": "openrouter",
"default": "anthropic/claude-sonnet-4",
"base_url": "https://openrouter.ai/api/v1",
},
"tts": {"provider": "edge"},
}
),
encoding="utf-8",
)
monkeypatch.setattr(
"hermes_cli.auth._xai_oauth_device_code_login",
lambda **kwargs: {
"tokens": {
"access_token": "tts-xai-access",
"refresh_token": "tts-xai-refresh",
"id_token": "",
"token_type": "Bearer",
},
"discovery": {"token_endpoint": "https://auth.x.ai/oauth2/token"},
"redirect_uri": "",
"base_url": "https://api.x.ai/v1",
"last_refresh": "2026-07-25T12:00:00Z",
},
)
monkeypatch.setattr("hermes_cli.auth._is_remote_session", lambda: True)
from hermes_cli.setup import _run_xai_oauth_login_from_setup
assert _run_xai_oauth_login_from_setup() is True
auth = json.loads(auth_path.read_text(encoding="utf-8"))
assert auth["active_provider"] == "openrouter"
xai_state = auth["providers"]["xai-oauth"]
assert xai_state["tokens"]["access_token"] == "tts-xai-access"
assert xai_state["tokens"]["refresh_token"] == "tts-xai-refresh"
config = yaml.safe_load(config_path.read_text(encoding="utf-8"))
assert config["model"]["provider"] == "openrouter"
assert config["model"]["base_url"] == "https://openrouter.ai/api/v1"
assert config["model"]["default"] == "anthropic/claude-sonnet-4"