Skip to content

The agent that grows with you

perf(providers): normalize_provider answers built-in ids without the profile scan

Tekniumf79cb0a6df09
diff --git a/hermes_cli/providers.py b/hermes_cli/providers.py
index b47a6bbb48c..3d57e4a2e01 100644
--- a/hermes_cli/providers.py
+++ b/hermes_cli/providers.py
@@ -167,11 +167,12 @@ def _overlay_profiles() -> list:
return [prof for prof in list_providers() if prof.provider_overlay]
-def _overlay_profile(name: str):
- """The overlay-declaring profile *name* (its id, models.dev id or an overlay alias) belongs to, or None."""
+def _overlay_profile(name: str, profiles: Optional[list] = None):
+ """The overlay-declaring profile *name* (its id, models.dev id or an overlay alias) belongs to, or
+ None. *profiles*: an ``_overlay_profiles()`` result a loop already fetched."""
if not name:
return None
- return next((prof for prof in _overlay_profiles()
+ return next((prof for prof in (_overlay_profiles() if profiles is None else profiles)
if name in (prof.name, prof.models_dev_id, *prof.overlay_aliases)), None)
@@ -187,12 +188,35 @@ def hermes_overlays() -> dict[str, HermesOverlay]:
return merged
-def normalize_provider(name: str) -> str:
- """Resolve aliases and normalise casing to a canonical provider id."""
+_STATIC_CANONICAL_IDS: frozenset[str] = frozenset()
+
+
+def _is_static_canonical(key: str) -> bool:
+ """Whether *key* is a built-in canonical id (static tables + the auth registry's built-in rows).
+
+ Those never take a profile overlay's name, so ``normalize_provider`` answers them without
+ the per-home profile scan. Asked mid-``hermes_cli.auth`` import (plugin discovery), before its
+ built-in set exists, only the local tables answer and nothing is cached.
+ """
+ global _STATIC_CANONICAL_IDS
+ if not _STATIC_CANONICAL_IDS:
+ try:
+ from hermes_cli.auth import BUILTIN_PROVIDER_IDS
+ except ImportError:
+ return key in HERMES_OVERLAYS or key in _ALIAS_GROUPS
+ _STATIC_CANONICAL_IDS = BUILTIN_PROVIDER_IDS | frozenset(_ALIAS_GROUPS) | frozenset(HERMES_OVERLAYS)
+ return key in _STATIC_CANONICAL_IDS
+
+
+def normalize_provider(name: str, overlay_profiles: Optional[list] = None) -> str:
+ """Resolve aliases and normalise casing to a canonical provider id.
+
+ *overlay_profiles*: an ``_overlay_profiles()`` result, so a loop over many names reads the
+ profile list once instead of per name."""
key = name.strip().lower()
if key in ALIASES:
return ALIASES[key]
- prof = _overlay_profile(key) if key not in HERMES_OVERLAYS else None
+ prof = None if _is_static_canonical(key) else _overlay_profile(key, overlay_profiles)
return _profile_overlay_key(prof) if prof is not None else key
@@ -529,9 +553,10 @@ def _lossy_alias_registry_pdef(raw: str, canonical: str) -> Optional[ProviderDef
_pcfg = _AUTH_PROVIDER_REGISTRY.get(raw)
if _pcfg is None:
return None
- _rows = [_row for _rid, _row in _AUTH_PROVIDER_REGISTRY.items() if normalize_provider(_rid) == canonical]
+ overlays = _overlay_profiles()
+ _rows = [_row for _rid, _row in _AUTH_PROVIDER_REGISTRY.items() if normalize_provider(_rid, overlays) == canonical]
# A profile-declared overlay routes its own mirrored alias keys through the built-in chain.
- if all(_row is _pcfg for _row in _rows) and _overlay_profile(canonical) is not None:
+ if all(_row is _pcfg for _row in _rows) and _overlay_profile(canonical, overlays) is not None:
return None
if len(_rows) > 1:
return ProviderDef(id=_pcfg.id, name=_pcfg.name, transport="openai_chat",
diff --git a/tests/hermes_cli/test_provider_normalize_cost.py b/tests/hermes_cli/test_provider_normalize_cost.py
new file mode 100644
index 00000000000..31ca1ecdf9f
--- /dev/null
+++ b/tests/hermes_cli/test_provider_normalize_cost.py
@@ -0,0 +1,55 @@
+"""``normalize_provider`` answers built-in ids without reading the provider-profile list (R4 H3).
+
+Profile ``provider_overlay`` lookups made every non-alias name scan the bound home's profiles
+(~100x the dict hit it used to be), and registry-wide loops paid that once per row.
+"""
+
+from __future__ import annotations
+
+import pytest
+
+from providers.base import ProviderProfile
+
+
+def counted_profiles(monkeypatch):
+ import providers
+
+ overlaid = ProviderProfile(
+ name="zz-overlaid", models_dev_id="zz-catalog", overlay_aliases=("zz-alias",),
+ provider_overlay={"transport": "openai_chat", "auth_type": "api_key"})
+ calls = []
+
+ def list_providers():
+ calls.append(1)
+ return [overlaid]
+
+ monkeypatch.setattr(providers, "list_providers", list_providers)
+ return calls
+
+
[email protected]("name, expected", [
+ ("gemini", "gemini"), ("Anthropic", "anthropic"), ("openrouter", "openrouter"), ("custom", "custom"),
+ ("nous", "nous"), ("claude", "anthropic"), ("kimi-coding", "kimi-for-coding"),
+])
+def test_builtin_names_skip_the_profile_scan(counted_profiles, name, expected):
+ from hermes_cli.providers import normalize_provider
+
+ assert normalize_provider(name) == expected
+ assert counted_profiles == []
+
+
+def test_overlay_names_still_resolve_through_profiles(counted_profiles):
+ from hermes_cli.providers import normalize_provider
+
+ assert normalize_provider("zz-alias") == "zz-catalog"
+ assert normalize_provider("zz-overlaid") == "zz-catalog"
+ assert normalize_provider("not-a-provider") == "not-a-provider"
+ assert counted_profiles
+
+
+def test_registry_wide_alias_check_reads_profiles_once(counted_profiles):
+ from hermes_cli.providers import _lossy_alias_registry_pdef
+
+ assert _lossy_alias_registry_pdef("kimi-coding", "kimi-for-coding") is not None
+ assert len(counted_profiles) == 1