Skip to content

The agent that grows with you

fix(models): live Copilot catalog for CLI-login users; unbreak picker-flag-empty catalogs

unsupportedpastels54872226582a
diff --git a/hermes_cli/models.py b/hermes_cli/models.py
index 7968a3f4d93..020e81c5c62 100644
--- a/hermes_cli/models.py
+++ b/hermes_cli/models.py
@@ -4062,13 +4062,18 @@ def _resolve_copilot_catalog_api_key() -> str:
``auth.json`` under ``credential_pool.copilot[]``. The pool is
populated by ``hermes auth add copilot`` and by ``_seed_from_env``
when the env var is set in ``~/.hermes/.env``.
-
- Without (2), users whose only Copilot credential is in the pool see
- the ``/model`` picker fall back to a stale hardcoded list because the
- live catalog fetch silently 401s. To avoid wedging on a malformed pool
- entry, each candidate is exchanged via ``exchange_copilot_token`` —
- only entries that actually exchange successfully are returned, so a
- later valid entry is reachable when an earlier one is unsupported.
+ 3. ``~/.copilot/config.json`` ``copilotTokens`` — the GitHub Copilot
+ CLI's own store, written by ``copilot login`` on hosts without an
+ OS keychain. Without it, a user whose ONLY credential is the ACP
+ CLI login sees the copilot-acp picker fall back to the stale
+ curated list instead of the models their subscription serves.
+
+ Without (2)/(3), users without env-var credentials see the ``/model``
+ picker fall back to a stale hardcoded list because the live catalog
+ fetch silently 401s. To avoid wedging on a malformed entry, each
+ candidate is exchanged via ``exchange_copilot_token`` — only entries
+ that actually exchange successfully are returned, so a later valid
+ entry is reachable when an earlier one is unsupported.
"""
try:
from hermes_cli.auth import resolve_api_key_provider_credentials
@@ -4097,7 +4102,11 @@ def _resolve_copilot_catalog_api_key() -> str:
if not valid:
continue
try:
- api_token, _expires_at = exchange_copilot_token(raw)
+ # exchange_copilot_token returns (api_token, expires_at,
+ # base_url) — a 2-name unpack raises ValueError, which the
+ # except below silently swallowed, disabling this entire
+ # resolution path.
+ api_token = exchange_copilot_token(raw)[0]
except Exception:
continue
if api_token:
@@ -4105,6 +4114,41 @@ def _resolve_copilot_catalog_api_key() -> str:
except Exception:
pass
+ # 3. Copilot CLI plaintext token store (JSONC — strip //-comment lines).
+ try:
+ import json as _json
+
+ from hermes_cli.copilot_auth import (
+ exchange_copilot_token,
+ validate_copilot_token,
+ )
+
+ cli_config = os.path.expanduser("~/.copilot/config.json")
+ if os.path.isfile(cli_config):
+ with open(cli_config, "r", encoding="utf-8", errors="ignore") as fh:
+ raw_text = "\n".join(
+ line for line in fh.read().splitlines()
+ if not line.lstrip().startswith("//")
+ )
+ data = _json.loads(raw_text) if raw_text.strip() else {}
+ tokens = data.get("copilotTokens")
+ if isinstance(tokens, dict):
+ for raw in tokens.values():
+ raw = str(raw or "").strip()
+ if not raw:
+ continue
+ valid, _ = validate_copilot_token(raw)
+ if not valid:
+ continue
+ try:
+ api_token = exchange_copilot_token(raw)[0]
+ except Exception:
+ continue
+ if api_token:
+ return api_token
+ except Exception:
+ pass
+
return ""
@@ -5062,12 +5106,14 @@ def copilot_default_headers(*, is_agent_turn: bool = True) -> dict[str, str]:
}
-def _copilot_catalog_item_is_text_model(item: dict[str, Any]) -> bool:
+def _copilot_catalog_item_is_text_model(
+ item: dict[str, Any], *, ignore_picker_flag: bool = False
+) -> bool:
model_id = str(item.get("id") or "").strip()
if not model_id:
return False
- if item.get("model_picker_enabled") is False:
+ if not ignore_picker_flag and item.get("model_picker_enabled") is False:
return False
capabilities = item.get("capabilities")
@@ -5146,6 +5192,25 @@ def fetch_github_model_catalog(
continue
seen_ids.add(model_id)
models.append(item)
+ if not models and items:
+ # GitHub has been observed returning
+ # ``model_picker_enabled: false`` for EVERY model on some
+ # accounts/token types, which would silently reject the
+ # whole live catalog and strand the picker on the stale
+ # curated fallback. The flag is a display hint, not an
+ # availability contract — when honoring it empties the
+ # catalog, retry without it (chat/endpoint checks still
+ # apply, so embeddings and non-chat rows stay excluded).
+ for item in items:
+ if not _copilot_catalog_item_is_text_model(
+ item, ignore_picker_flag=True
+ ):
+ continue
+ model_id = str(item.get("id") or "").strip()
+ if not model_id or model_id in seen_ids:
+ continue
+ seen_ids.add(model_id)
+ models.append(item)
if models:
_github_model_catalog_cache = copy.deepcopy(models)
_github_model_catalog_cache_key = api_key
diff --git a/tests/hermes_cli/test_external_process_auth_status.py b/tests/hermes_cli/test_external_process_auth_status.py
index b8546d2e9c4..221459b10d1 100644
--- a/tests/hermes_cli/test_external_process_auth_status.py
+++ b/tests/hermes_cli/test_external_process_auth_status.py
@@ -236,3 +236,57 @@ def test_cli_command_default_when_no_override(monkeypatch, _clean_copilot_env):
from hermes_cli.web_server import _external_process_cli_command
assert _external_process_cli_command("copilot-acp", "copilot login") == "copilot login"
+
+
+# --- live catalog key from the Copilot CLI store -----------------------------
+
+
+def test_catalog_key_resolves_from_copilot_cli_store(tmp_path, monkeypatch, _clean_copilot_env):
+ # A user whose ONLY credential is `copilot login` must still get the live
+ # model catalog — otherwise the picker silently falls back to the stale
+ # curated list (visibly wrong vs. what their subscription serves).
+ from unittest.mock import patch as mock_patch
+
+ from hermes_cli import models as models_mod
+
+ monkeypatch.setenv("HOME", str(tmp_path))
+ cfg_dir = tmp_path / ".copilot"
+ cfg_dir.mkdir()
+ (cfg_dir / "config.json").write_text(
+ "// managed\n"
+ '{"copilotTokens": {"https://github.com:u": "gho_' + "x" * 36 + '"}}\n',
+ encoding="utf-8",
+ )
+
+ with mock_patch.object(
+ models_mod, "_resolve_copilot_catalog_api_key", wraps=models_mod._resolve_copilot_catalog_api_key
+ ), mock_patch(
+ "hermes_cli.copilot_auth.exchange_copilot_token",
+ return_value=("exchanged-api-token", 0.0, None),
+ ), mock_patch(
+ "hermes_cli.auth.resolve_api_key_provider_credentials",
+ side_effect=Exception("no env creds"),
+ ), mock_patch(
+ "hermes_cli.auth.read_credential_pool", return_value=[]
+ ):
+ key = models_mod._resolve_copilot_catalog_api_key()
+
+ assert key == "exchanged-api-token"
+
+
+def test_catalog_key_empty_when_cli_store_absent(tmp_path, monkeypatch, _clean_copilot_env):
+ from unittest.mock import patch as mock_patch
+
+ from hermes_cli import models as models_mod
+
+ monkeypatch.setenv("HOME", str(tmp_path)) # no ~/.copilot at all
+
+ with mock_patch(
+ "hermes_cli.auth.resolve_api_key_provider_credentials",
+ side_effect=Exception("no env creds"),
+ ), mock_patch(
+ "hermes_cli.auth.read_credential_pool", return_value=[]
+ ):
+ key = models_mod._resolve_copilot_catalog_api_key()
+
+ assert key == ""