The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
perf(providers): a repeated profile miss waits for the plugin-dir stamp TTL
Teknium3b73d82ec83d
diff --git a/providers/__init__.py b/providers/__init__.pyindex 776fc310efc..8f72a081543 100644--- a/providers/__init__.py+++ b/providers/__init__.py@@ -81,6 +81,8 @@ class _HomeLayer:aliases: dict[str, str] = field(default_factory=dict)stamps: tuple = ()stamp_checked_at: float | None = None+ # name -> when its miss last forced a stamp check (see ``_miss_recheck_due``)+ misses: dict[str, float] = field(default_factory=dict)_HOME_LAYERS: dict[str, _HomeLayer] = {}@@ -216,7 +218,7 @@ def get_provider_profile(name: str) -> ProviderProfile | None:# periodic check. ``custom:<route>`` misses resolve to the generic profile below and the# picker asks for them once per model, so they wait for the periodic check.is_custom_route = isinstance(name, str) and name.lower().startswith("custom:")- if profile is None and not is_custom_route and not checked:+ if profile is None and not is_custom_route and not checked and _miss_recheck_due(layer, name):if _refresh_home_layer(layer, home, key, force=True):profile = lookup(name)# Named custom routes share the generic wire policy unless a plugin@@ -233,6 +235,23 @@ def get_provider_profile(name: str) -> ProviderProfile | None:return profile+def _miss_recheck_due(layer: _HomeLayer, name: str) -> bool:+ """Whether a miss for *name* may force a plugin-dir check now: once per name per stamp TTL.++ Registry ids with no profile (``openai-api``, ``xai-oauth`` …) are looked up on every request;+ re-statting for each defeated the TTL. A new name still forces at once, so a just-installed+ provider is found on its first request; a repeat miss waits for the periodic check.+ """+ now = time.monotonic()+ last = layer.misses.get(name)+ if last is not None and now - last < _PLUGIN_DIR_STAMP_TTL_SECONDS:+ return False+ if len(layer.misses) >= 512:+ layer.misses.clear()+ layer.misses[name] = now+ return True++def routed_model_rejects_vision_tool_messages(provider: str, model: str) -> bool:"""Whether an active route or its aggregator-targeted model rejects image tool parts.diff --git a/tests/providers/test_profile_miss_ttl.py b/tests/providers/test_profile_miss_ttl.pynew file mode 100644index 00000000000..aca1d524172--- /dev/null+++ b/tests/providers/test_profile_miss_ttl.py@@ -0,0 +1,94 @@+"""Repeated profile misses honour the plugin-dir stamp TTL (R4 H4).++Registry ids with no profile (``openai-api``, ``xai-oauth`` …) are looked up by per-request hooks;+each miss forced a plugin-dir re-stat, defeating the 1 s TTL. A new name still forces a check, so a+provider installed while the process runs is found on its first request.+"""++from __future__ import annotations++import sys+import textwrap+import types++import pytest++_PLUGIN = textwrap.dedent(+ """+ from providers import register_provider+ from providers.base import ProviderProfile++ register_provider(ProviderProfile(name="{name}", auth_type="api_key", env_vars=("X_KEY",),+ base_url="https://{name}.invalid/v1"))+ """+)+++def layer_env(tmp_path, monkeypatch):+ import providers++ home = tmp_path / "home"+ home.mkdir()+ monkeypatch.setenv("HERMES_HOME", str(home))+ monkeypatch.setattr(providers, "_REGISTRY", dict(providers._REGISTRY))+ monkeypatch.setattr(providers, "_ALIASES", dict(providers._ALIASES))+ monkeypatch.setattr(providers, "_PROVIDER_LIST_CACHE", None)+ monkeypatch.setattr(providers, "_HOME_LAYERS", {})+ clock = types.SimpleNamespace(now=1000.0)+ monkeypatch.setattr(providers, "time", types.SimpleNamespace(monotonic=lambda: clock.now))+ stats = []+ real = providers._plugin_dir_stamps+ monkeypatch.setattr(providers, "_plugin_dir_stamps", lambda h: stats.append(1) or real(h))+ yield home, clock, stats+ for mod in [m for m in sys.modules if m.startswith("_hermes_user_provider")]:+ del sys.modules[mod]+++def _install(home, name):+ plugin = home / "plugins" / "model-providers" / name+ plugin.mkdir(parents=True)+ (plugin / "__init__.py").write_text(_PLUGIN.format(name=name), encoding="utf-8")+++def test_repeated_misses_do_not_restat_within_the_ttl(layer_env):+ import providers++ _home, clock, stats = layer_env+ assert providers.get_provider_profile("no-profile-id") is None # first lookup: periodic check+ stats.clear()+ clock.now += 0.1+ for _ in range(50):+ assert providers.get_provider_profile("no-profile-id") is None+ assert len(stats) <= 1 # the first miss of this name may force one check, never one per call++ clock.now += providers._PLUGIN_DIR_STAMP_TTL_SECONDS+ stats.clear()+ assert providers.get_provider_profile("no-profile-id") is None+ assert len(stats) == 1 # TTL elapsed: checked again+++def test_new_install_found_by_new_name_and_after_the_ttl(layer_env):+ import providers++ home, clock, _stats = layer_env+ assert providers.get_provider_profile("late-a") is None+ clock.now += 0.1+ assert providers.get_provider_profile("late-a") is None++ _install(home, "late-b")+ clock.now += 0.1+ assert providers.get_provider_profile("late-b") is not None # a new name forces a check++ _install(home, "late-a")+ clock.now += providers._PLUGIN_DIR_STAMP_TTL_SECONDS+ assert providers.get_provider_profile("late-a") is not None+++def test_runtime_registration_is_found_despite_a_cached_miss(layer_env):+ import providers+ from providers.base import ProviderProfile++ assert providers.get_provider_profile("late-runtime") is None+ providers.register_provider(ProviderProfile(name="late-runtime", auth_type="api_key"))+ assert providers.get_provider_profile("late-runtime") is not None