Skip to content

The agent that grows with you

fix(auth): normalize configured provider pool keys

fangliquanflqc527b2c0a41d
diff --git a/hermes_cli/auth_commands.py b/hermes_cli/auth_commands.py
index a23c0f2a9cb..b06c9a6eb19 100644
--- a/hermes_cli/auth_commands.py
+++ b/hermes_cli/auth_commands.py
@@ -60,7 +60,7 @@ def _get_custom_provider_names() -> list:
def _resolve_custom_provider_input(raw: str) -> str | None:
- """If raw input matches a custom_providers entry name (case-insensitive), return its pool key."""
+ """Resolve legacy names and keyed providers to their credential-pool ID."""
normalized = (raw or "").strip().lower().replace(" ", "-")
if not normalized:
return None
@@ -68,10 +68,14 @@ def _resolve_custom_provider_input(raw: str) -> str | None:
if normalized.startswith(CUSTOM_POOL_PREFIX):
return normalized
for display_name, pool_key, provider_key in _get_custom_provider_names():
+ # ``providers:`` entries already have a durable runtime slug. Keep
+ # credentials under that slug instead of leaking the legacy
+ # ``custom:`` compatibility identity into auth.json and discovery.
+ normalized_provider_key = provider_key.strip().lower()
+ if normalized_provider_key and normalized_provider_key == normalized:
+ return normalized_provider_key
if _normalize_custom_pool_name(display_name) == normalized:
return pool_key
- if provider_key and provider_key.strip().lower() == normalized:
- return pool_key
return None
@@ -88,6 +92,45 @@ def _normalize_provider(provider: str) -> str:
return normalized
+def _migrate_legacy_custom_pool_key(provider: str) -> None:
+ """Move a keyed provider's old ``custom:`` pool into its runtime slug."""
+ legacy_key = f"{CUSTOM_POOL_PREFIX}{provider}"
+ with auth_mod._auth_store_lock():
+ auth_store = auth_mod._load_auth_store()
+ credential_pool = auth_store.get("credential_pool")
+ if not isinstance(credential_pool, dict):
+ return
+ legacy_entries = credential_pool.get(legacy_key)
+ if not isinstance(legacy_entries, list) or not legacy_entries:
+ return
+
+ current_entries = credential_pool.get(provider)
+ merged = list(current_entries) if isinstance(current_entries, list) else []
+ known_ids = {
+ entry.get("id")
+ for entry in merged
+ if isinstance(entry, dict) and entry.get("id")
+ }
+ for entry in legacy_entries:
+ entry_id = entry.get("id") if isinstance(entry, dict) else None
+ if entry_id and entry_id in known_ids:
+ continue
+ merged.append(entry)
+ if entry_id:
+ known_ids.add(entry_id)
+
+ credential_pool[provider] = merged
+ del credential_pool[legacy_key]
+ auth_mod._save_auth_store(auth_store)
+
+ try:
+ from hermes_cli.models import clear_provider_models_cache
+
+ clear_provider_models_cache(legacy_key)
+ except Exception:
+ pass
+
+
def _provider_base_url(provider: str) -> str:
if provider == "openrouter":
return OPENROUTER_BASE_URL
@@ -164,8 +207,16 @@ def _format_exhausted_status(entry) -> str:
def auth_add_command(args) -> None:
provider = _normalize_provider(getattr(args, "provider", ""))
- if provider not in PROVIDER_REGISTRY and provider != "openrouter" and not provider.startswith(CUSTOM_POOL_PREFIX):
+ configured_provider = _resolve_custom_provider_input(provider) == provider
+ if (
+ provider not in PROVIDER_REGISTRY
+ and provider != "openrouter"
+ and not provider.startswith(CUSTOM_POOL_PREFIX)
+ and not configured_provider
+ ):
raise SystemExit(f"Unknown provider: {provider}")
+ if configured_provider and not provider.startswith(CUSTOM_POOL_PREFIX):
+ _migrate_legacy_custom_pool_key(provider)
requested_type = str(getattr(args, "auth_type", "") or "").strip().lower()
if requested_type in {AUTH_TYPE_API_KEY, "api-key"}:
diff --git a/tests/hermes_cli/test_auth_commands.py b/tests/hermes_cli/test_auth_commands.py
index 0e423d0cd9c..6ebb787959b 100644
--- a/tests/hermes_cli/test_auth_commands.py
+++ b/tests/hermes_cli/test_auth_commands.py
@@ -18,6 +18,25 @@ def _write_auth_store(tmp_path, payload: dict) -> None:
(hermes_home / "auth.json").write_text(json.dumps(payload, indent=2))
+def _write_groq_provider_config(tmp_path, *, name="Groq", base_url=None) -> None:
+ hermes_home = tmp_path / "hermes"
+ (hermes_home / "config.yaml").write_text(
+ yaml.safe_dump(
+ {
+ "providers": {
+ "groq": {
+ "name": name,
+ "base_url": base_url or "https://api.groq.com/openai/v1",
+ "key_env": "GROQ_API_KEY",
+ "discover_models": True,
+ }
+ }
+ }
+ ),
+ encoding="utf-8",
+ )
+
+
def _jwt_with_email(email: str) -> str:
header = base64.urlsafe_b64encode(b'{"alg":"RS256","typ":"JWT"}').rstrip(b"=").decode()
payload = base64.urlsafe_b64encode(
@@ -94,6 +113,100 @@ def test_auth_add_api_key_persists_manual_entry(tmp_path, monkeypatch):
assert entry["access_token"] == "sk-or-manual"
+def test_auth_add_configured_provider_uses_canonical_pool_key(tmp_path, monkeypatch):
+ """A keyed providers row must keep its runtime slug in the auth pool."""
+ hermes_home = tmp_path / "hermes"
+ monkeypatch.setenv("HERMES_HOME", str(hermes_home))
+ monkeypatch.delenv("GROQ_API_KEY", raising=False)
+ _write_auth_store(tmp_path, {"version": 1, "providers": {}})
+ _write_groq_provider_config(tmp_path)
+
+ from hermes_cli.auth_commands import auth_add_command
+
+ class _Args:
+ provider = "groq"
+ auth_type = "api-key"
+ api_key = "gsk-test"
+ label = "primary"
+
+ auth_add_command(_Args())
+
+ payload = json.loads((hermes_home / "auth.json").read_text(encoding="utf-8"))
+ assert "groq" in payload["credential_pool"]
+ assert "custom:groq" not in payload["credential_pool"]
+
+
+def test_auth_add_migrates_legacy_prefixed_key_for_configured_provider(
+ tmp_path, monkeypatch
+):
+ hermes_home = tmp_path / "hermes"
+ monkeypatch.setenv("HERMES_HOME", str(hermes_home))
+ monkeypatch.delenv("GROQ_API_KEY", raising=False)
+ _write_auth_store(
+ tmp_path,
+ {
+ "version": 1,
+ "providers": {},
+ "credential_pool": {
+ "custom:groq": [
+ {
+ "id": "legacy-key",
+ "label": "legacy",
+ "auth_type": "api_key",
+ "priority": 0,
+ "source": "manual",
+ "access_token": "gsk-legacy",
+ }
+ ]
+ },
+ },
+ )
+ _write_groq_provider_config(tmp_path)
+
+ from hermes_cli.auth_commands import auth_add_command
+
+ class _Args:
+ provider = "groq"
+ auth_type = "api-key"
+ api_key = "gsk-new"
+ label = "new"
+
+ auth_add_command(_Args())
+
+ payload = json.loads((hermes_home / "auth.json").read_text(encoding="utf-8"))
+ assert "custom:groq" not in payload["credential_pool"]
+ assert {
+ entry["access_token"] for entry in payload["credential_pool"]["groq"]
+ } == {"gsk-legacy", "gsk-new"}
+
+
+def test_auth_add_explicit_custom_provider_keeps_prefixed_pool_key(
+ tmp_path, monkeypatch
+):
+ hermes_home = tmp_path / "hermes"
+ monkeypatch.setenv("HERMES_HOME", str(hermes_home))
+ _write_auth_store(tmp_path, {"version": 1, "providers": {}})
+ _write_groq_provider_config(
+ tmp_path,
+ name="Groq Proxy",
+ base_url="https://proxy.example/v1",
+ )
+
+ from hermes_cli.auth_commands import auth_add_command
+
+ class _Args:
+ provider = "custom:groq"
+ auth_type = "api-key"
+ api_key = "proxy-key"
+ label = "proxy"
+
+ auth_add_command(_Args())
+
+ payload = json.loads((hermes_home / "auth.json").read_text(encoding="utf-8"))
+ assert "custom:groq" in payload["credential_pool"]
+ assert "groq" not in payload["credential_pool"]
+
+
def test_auth_add_nous_oauth_persists_pool_entry(tmp_path, monkeypatch):
monkeypatch.setenv("HERMES_HOME", str(tmp_path / "hermes"))
_write_auth_store(tmp_path, {"version": 1, "providers": {}})