The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
perf(providers): cache provider list snapshots
Zeheng Huangd0be6091477a
diff --git a/providers/__init__.py b/providers/__init__.pyindex a394e74b335..4d828c561d3 100644--- a/providers/__init__.py+++ b/providers/__init__.py@@ -42,6 +42,7 @@ logger = logging.getLogger(__name__)_REGISTRY: dict[str, ProviderProfile] = {}_ALIASES: dict[str, str] = {}+_PROVIDER_LIST_CACHE: list[ProviderProfile] | None = None_discovered = False# Repo-root ``plugins/model-providers/`` — populated at discovery time.@@ -57,9 +58,11 @@ def register_provider(profile: ProviderProfile) -> None:plugins under ``$HERMES_HOME/plugins/model-providers/`` can overridebundled profiles without editing repo code."""+ global _PROVIDER_LIST_CACHE_REGISTRY[profile.name] = profilefor alias in profile.aliases:_ALIASES[alias] = profile.name+ _PROVIDER_LIST_CACHE = Nonedef get_provider_profile(name: str) -> ProviderProfile | None:@@ -75,8 +78,11 @@ def get_provider_profile(name: str) -> ProviderProfile | None:def list_providers() -> list[ProviderProfile]:"""Return all registered provider profiles (one per canonical name)."""+ global _PROVIDER_LIST_CACHEif not _discovered:_discover_providers()+ if _PROVIDER_LIST_CACHE is not None:+ return list(_PROVIDER_LIST_CACHE)# Deduplicate: _REGISTRY has canonical names; _ALIASES points to same objectsseen: set[int] = set()result: list[ProviderProfile] = []@@ -85,7 +91,8 @@ def list_providers() -> list[ProviderProfile]:if pid not in seen:seen.add(pid)result.append(profile)- return result+ _PROVIDER_LIST_CACHE = result+ return list(result)def _user_plugins_dir() -> Path | None:diff --git a/tests/providers/test_plugin_discovery.py b/tests/providers/test_plugin_discovery.pyindex 79169b1f72f..2c828631bdd 100644--- a/tests/providers/test_plugin_discovery.py+++ b/tests/providers/test_plugin_discovery.py@@ -21,6 +21,7 @@ def _clear_provider_caches():import providers as _pkg_pkg._REGISTRY.clear()_pkg._ALIASES.clear()+ _pkg._PROVIDER_LIST_CACHE = None_pkg._discovered = False# Evict any cached plugin modules so the next import re-executes.for mod in list(sys.modules.keys()):diff --git a/tests/providers/test_provider_registry.py b/tests/providers/test_provider_registry.pynew file mode 100644index 00000000000..e97fd964fcb--- /dev/null+++ b/tests/providers/test_provider_registry.py@@ -0,0 +1,61 @@+import pytest++from providers import ProviderProfile+import providers++[email protected](autouse=True)+def isolate_provider_registry():+ registry = providers._REGISTRY.copy()+ aliases = providers._ALIASES.copy()+ provider_list_cache = (+ None+ if providers._PROVIDER_LIST_CACHE is None+ else list(providers._PROVIDER_LIST_CACHE)+ )+ discovered = providers._discovered++ yield++ providers._REGISTRY.clear()+ providers._REGISTRY.update(registry)+ providers._ALIASES.clear()+ providers._ALIASES.update(aliases)+ providers._PROVIDER_LIST_CACHE = provider_list_cache+ providers._discovered = discovered+++def _profile(name: str, *aliases: str) -> ProviderProfile:+ return ProviderProfile(name=name, aliases=aliases)+++def _reset_registry() -> None:+ providers._REGISTRY.clear()+ providers._ALIASES.clear()+ providers._PROVIDER_LIST_CACHE = None+ providers._discovered = True+++def test_list_providers_reuses_cached_snapshot_until_registration_changes():+ _reset_registry()+ first = _profile("alpha")+ providers.register_provider(first)++ listed = providers.list_providers()+ listed.clear()++ assert providers.list_providers() == [first]++ second = _profile("beta")+ providers.register_provider(second)++ assert providers.list_providers() == [first, second]+++def test_list_providers_dedupes_aliases_in_cached_snapshot():+ _reset_registry()+ profile = _profile("kimi", "moonshot", "kimi-k2")+ providers.register_provider(profile)++ assert providers.get_provider_profile("moonshot") is profile+ assert providers.list_providers() == [profile]