Skip to content

The agent that grows with you

fix: runtime-selected copilot still gets the exchanged token (review follow-up)

teknium1f4c83fea4485
diff --git a/hermes_cli/runtime_provider.py b/hermes_cli/runtime_provider.py
index 3e1ef690b46..8dd2deab1a6 100644
--- a/hermes_cli/runtime_provider.py
+++ b/hermes_cli/runtime_provider.py
@@ -590,6 +590,27 @@ def _refresh_nous_pool_entry(pool: CredentialPool, entry: Any, pool_api_key: str
return entry, pool_api_key
+def _exchange_copilot_pool_entry(entry: Any, pool_api_key: str) -> str:
+ """Exchange a copilot pool entry that still carries the RAW GitHub token.
+
+ The seeder skips the exchange while copilot is merely discovered (ambient gh login, not in
+ config); here copilot IS the runtime target (`/model copilot/… --session`, `--provider copilot`,
+ delegation/cron overrides), and a raw token routes to the language-server integrator whose
+ allowlist omits enterprise-only models (400 model_not_available_for_integrator)."""
+ from hermes_cli.copilot_auth import get_copilot_api_token, validate_copilot_token
+ if not pool_api_key or not validate_copilot_token(pool_api_key)[0]:
+ return pool_api_key # already an exchanged API token
+ api_token, enterprise_base_url = get_copilot_api_token(pool_api_key)
+ if api_token == pool_api_key and not enterprise_base_url:
+ from agent.credential_pool import _warn_copilot_raw_degradation_once
+ _warn_copilot_raw_degradation_once(pool_api_key)
+ return pool_api_key
+ entry.access_token = api_token
+ if enterprise_base_url:
+ entry.base_url = enterprise_base_url
+ return api_token
+
+
def _resolve_from_pool(provider: str, requested_provider: str, model_cfg: Dict[str, Any], explicit_api_key, explicit_base_url,
target_model) -> Optional[Dict[str, Any]]:
"""Runtime from the provider's credential pool, or None to continue down the ladder."""
@@ -607,6 +628,8 @@ def _resolve_from_pool(provider: str, requested_provider: str, model_cfg: Dict[s
pool_api_key = _pool_entry_api_key(entry)
if provider == "nous":
entry, pool_api_key = _refresh_nous_pool_entry(pool, entry, pool_api_key)
+ elif provider == "copilot":
+ pool_api_key = _exchange_copilot_pool_entry(entry, pool_api_key)
if not has_usable_secret(pool_api_key):
return None
if pool_api_key and credential_pool_matches_provider(pool, provider, base_url=_pool_entry_base_url(entry)):
diff --git a/tests/hermes_cli/test_runtime_provider_resolution.py b/tests/hermes_cli/test_runtime_provider_resolution.py
index 0798424efe6..62d6a92d388 100644
--- a/tests/hermes_cli/test_runtime_provider_resolution.py
+++ b/tests/hermes_cli/test_runtime_provider_resolution.py
@@ -62,6 +62,32 @@ def _fake_invoke_jwt(ttl_seconds=3600):
return f"{header}.{payload}.sig"
+def test_runtime_selected_copilot_exchanges_ambient_pool_token(tmp_path, monkeypatch):
+ """Copilot picked at runtime without a config write (`/model copilot/<m> --session`,
+ `--provider copilot`) must still hand the EXCHANGED token and the enterprise base_url to the
+ client: the seeder leaves an ambient gh-CLI credential raw while copilot is not configured
+ (#114740), and a raw token 400s on enterprise-only models."""
+ hermes_home = tmp_path / "hermes"
+ hermes_home.mkdir()
+ monkeypatch.setenv("HERMES_HOME", str(hermes_home))
+ (hermes_home / "auth.json").write_text(json.dumps({"version": 1, "credential_pool": {}}))
+ (hermes_home / "config.yaml").write_text("model:\n provider: deepseek\n default: deepseek-chat\n")
+ from hermes_cli import config as _cfg
+ _cfg._LOAD_CONFIG_CACHE.clear()
+ _cfg._RAW_CONFIG_CACHE.clear()
+ monkeypatch.setattr("hermes_cli.copilot_auth.resolve_copilot_token", lambda: ("ghu_raw_gh_token", "gh auth token"))
+ monkeypatch.setattr("hermes_cli.copilot_auth.get_copilot_api_token",
+ lambda tok: ("tid=exchanged;exp=1", "https://api.enterprise.ghe.example"))
+ monkeypatch.setattr(rp._models, "copilot_model_api_mode", lambda *a, **k: "chat_completions")
+ monkeypatch.setattr(rp, "resolve_provider", lambda *a, **k: "copilot")
+
+ resolved = rp.resolve_runtime_provider(requested="copilot", target_model="gpt-4.1")
+
+ assert resolved["provider"] == "copilot"
+ assert resolved["api_key"] == "tid=exchanged;exp=1"
+ assert resolved["base_url"].startswith("https://api.enterprise.ghe.example")
+
+
def test_resolve_runtime_provider_uses_credential_pool(monkeypatch):
class _Entry:
access_token = "pool-token"