The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(auth): skip the Copilot token exchange while copilot is only an ambient gh-CLI credential
teknium1d03b5f3770a8
diff --git a/agent/credential_pool.py b/agent/credential_pool.pyindex 6d47010633d..533435533aa 100644--- a/agent/credential_pool.py+++ b/agent/credential_pool.py@@ -2447,13 +2447,21 @@ def _seed_copilot_singleton(seed: _Seeder) -> None:# Per-source gate BEFORE the (~35s worst case) network exchange.if seed.is_suppressed(seed.provider, source_name):return- api_token, enterprise_base_url = get_copilot_api_token(token)- # get_copilot_api_token falls back to the RAW token when the exchange- # fails; the Copilot API then routes it to the fallback- # "copilot-language-server" integrator whose allowlist omits- # enterprise-only models -> HTTP 400 on every turn. Surface it once.- if api_token == token and not enterprise_base_url:- _warn_copilot_raw_degradation_once(token)+ from hermes_cli.auth import is_provider_explicitly_configured+ if not is_provider_explicitly_configured(seed.provider):+ # Copilot is only discovered here (ambient gh CLI login), not selected anywhere: no+ # model will be routed to it, so the network exchange — and its degradation warning on+ # every pool load — buys nothing (#114740). Seed the raw token; the load that follows+ # the user selecting copilot re-seeds and exchanges.+ api_token, enterprise_base_url = token, None+ else:+ api_token, enterprise_base_url = get_copilot_api_token(token)+ # get_copilot_api_token falls back to the RAW token when the exchange+ # fails; the Copilot API then routes it to the fallback+ # "copilot-language-server" integrator whose allowlist omits+ # enterprise-only models -> HTTP 400 on every turn. Surface it once.+ if api_token == token and not enterprise_base_url:+ _warn_copilot_raw_degradation_once(token)pconfig = PROVIDER_REGISTRY.get(seed.provider)seed.upsert(source_name, {"auth_type": AUTH_TYPE_API_KEY,diff --git a/hermes_cli/auth.py b/hermes_cli/auth.pyindex 26a6422baf4..56a5a0102a8 100644--- a/hermes_cli/auth.py+++ b/hermes_cli/auth.py@@ -976,6 +976,11 @@ def _config_selects_provider(normalized: str) -> bool:cfg = load_config()if _slot_selects(cfg.get("model"), normalized):return True+ # ``auxiliary.<task>.provider: copilot`` selects the provider for that task the same way a MoA+ # slot does — without this the seeder treats the credential as merely discovered (#114740).+ aux_cfg = cfg.get("auxiliary")+ if isinstance(aux_cfg, dict) and any(_slot_selects(s, normalized) for s in aux_cfg.values()):+ return Truedef _moa_block_matches(block: Any) -> bool:return isinstance(block, dict) and (diff --git a/tests/agent/test_auxiliary_client.py b/tests/agent/test_auxiliary_client.pyindex d39ba535cd0..8e5d7edf2f1 100644--- a/tests/agent/test_auxiliary_client.py+++ b/tests/agent/test_auxiliary_client.py@@ -2131,41 +2131,6 @@ def test_resolve_api_key_provider_skips_unconfigured_copilot(monkeypatch):"_select_pool_entry() should not be called for unconfigured copilot"-def test_resolve_api_key_provider_skips_provider_without_aux_model(monkeypatch):- """_resolve_api_key_provider must not load pool or credentials for providers without an aux model (#114740)."""- from collections import OrderedDict- from hermes_cli.auth import ProviderConfig-- fake_registry = OrderedDict({- "some-provider": ProviderConfig(- id="some-provider",- name="Some Provider",- auth_type="api_key",- inference_base_url="https://api.example.com",- api_key_env_vars=("SOME_API_KEY",),- ),- })-- pool_selected = []-- def mock_select_pool_entry(provider_id):- pool_selected.append(provider_id)- return False, None-- monkeypatch.setattr("agent.auxiliary_client._select_pool_entry", mock_select_pool_entry)- monkeypatch.setattr("hermes_cli.auth.PROVIDER_REGISTRY", fake_registry)- monkeypatch.setattr("agent.auxiliary_client._get_aux_model_for_provider", lambda pid: "")-- from agent.auxiliary_client import _resolve_api_key_provider- client, model = _resolve_api_key_provider()-- assert client is None- assert model is None- assert len(pool_selected) == 0, \- "_select_pool_entry() should not be called when provider has no aux model"---# ---------------------------------------------------------------------------# model="default" elimination (#7512)# ---------------------------------------------------------------------------diff --git a/tests/agent/test_credential_pool.py b/tests/agent/test_credential_pool.pyindex e866e7b3491..b604a4cd906 100644--- a/tests/agent/test_credential_pool.py+++ b/tests/agent/test_credential_pool.py@@ -1552,47 +1552,51 @@ def test_load_pool_skips_resolve_when_all_copilot_sources_suppressed(tmp_path, massert pool.entries() == []-def test_load_pool_copilot_raw_degradation_warns_once(tmp_path, monkeypatch, caplog):- """Copilot token exchange degraded to RAW token warning is emitted once per token (#114740)."""+def test_load_pool_copilot_exchange_only_when_selected_and_warns_once(tmp_path, monkeypatch, caplog):+ """An ambient gh-CLI Copilot credential is seeded without the token exchange (and without the+ 'degraded to RAW token' warning) until copilot is actually selected; once selected, the+ degradation is reported once per token, not on every pool load (#114740)."""import loggingmonkeypatch.setenv("HERMES_HOME", str(tmp_path / "hermes"))_write_auth_store(tmp_path, {"version": 1, "credential_pool": {}})from agent.credential_pool import _reset_copilot_raw_degradation_warned, load_pool_reset_copilot_raw_degradation_warned()+ monkeypatch.setattr("hermes_cli.copilot_auth.resolve_copilot_token", lambda: ("gho_raw_initial", "gh auth token"))+ exchanges = []- current_token = ["gho_raw_initial"]- monkeypatch.setattr(- "hermes_cli.copilot_auth.resolve_copilot_token",- lambda: (current_token[0], "gh auth token"),- )- # Exchange degrades to raw token (no enterprise url, returns raw token)- monkeypatch.setattr(- "hermes_cli.copilot_auth.get_copilot_api_token",- lambda token: (token, None),- )+ def degraded_exchange(token):+ exchanges.append(token)+ return token, None # exchange unavailable -> RAW token, no enterprise URL++ monkeypatch.setattr("hermes_cli.copilot_auth.get_copilot_api_token", degraded_exchange)++ def degradation_warnings():+ return [r for r in caplog.records if "Copilot token exchange degraded to RAW token" in r.message]with caplog.at_level(logging.WARNING, logger="agent.credential_pool"):- # First pool load emits warning+ # Main provider is deepseek; copilot is merely discovered via `gh auth token`.+ (tmp_path / "hermes" / "config.yaml").write_text("model:\n provider: deepseek\n default: deepseek-chat\n", encoding="utf-8")+ pool = load_pool("copilot")load_pool("copilot")- warnings = [r.message for r in caplog.records if "Copilot token exchange degraded to RAW token" in r.message]- assert len(warnings) == 1-- # Subsequent pool load with same token must not emit duplicate warning- caplog.clear()+ assert exchanges == [] and degradation_warnings() == []+ assert [e.access_token for e in pool.entries()] == ["gho_raw_initial"] # credential still listed++ # The user selects copilot for one auxiliary task: the exchange runs, the degradation is+ # reported exactly once across repeated loads.+ (tmp_path / "hermes" / "config.yaml").write_text(+ "model:\n provider: deepseek\n default: deepseek-chat\nauxiliary:\n approval:\n provider: copilot\n", encoding="utf-8")+ from hermes_cli import config as _cfg+ _cfg._LOAD_CONFIG_CACHE.clear()+ _cfg._RAW_CONFIG_CACHE.clear() # same-second rewrite: the mtime signature may not changeload_pool("copilot")- warnings = [r.message for r in caplog.records if "Copilot token exchange degraded to RAW token" in r.message]- assert len(warnings) == 0-- # Changing the token produces a warning for the new token- current_token[0] = "gho_raw_different_token"- caplog.clear()load_pool("copilot")- warnings = [r.message for r in caplog.records if "Copilot token exchange degraded to RAW token" in r.message]- assert len(warnings) == 1--+ assert len(exchanges) == 2 and len(degradation_warnings()) == 1+ # A different token is a different degradation: warned again, once.+ monkeypatch.setattr("hermes_cli.copilot_auth.resolve_copilot_token", lambda: ("gho_raw_rotated", "gh auth token"))+ load_pool("copilot")+ assert len(degradation_warnings()) == 2def test_load_pool_seeds_qwen_oauth_via_cli_tokens(tmp_path, monkeypatch):