Skip to content

The agent that grows with you

fix(providers): a plugin installed in the same mtime tick as the last scan is still found

Teknium7e946ef7035d
diff --git a/providers/__init__.py b/providers/__init__.py
index 1ee3f2e511f..21ed06fe40b 100644
--- a/providers/__init__.py
+++ b/providers/__init__.py
@@ -64,6 +64,9 @@ _REGISTRY_GENERATION = 0
_discovered = False
_discovering = False
_PLUGIN_DIR_STAMP_TTL_SECONDS = 1.0
+# Widest directory mtime granularity in use (FAT rounds to 2 s, HFS+ to 1 s; a kernel without
+# fine-grained timestamps stamps every change within one ~4 ms clock tick alike).
+_PLUGIN_DIR_MTIME_TICK_NS = 2_000_000_000
@dataclass
@@ -453,7 +456,10 @@ def _refresh_home_layer(layer: _HomeLayer, home: Path | None, key: str, *, force
stamps = _plugin_dir_stamps(home)
if stamps != layer.stamps:
_scan_home_layer(layer, key)
- layer.stamps = stamps
+ # A stamp still inside its mtime tick may not move for an install landing in that same
+ # tick, which would hide the plugin until some later change: keep it unremembered so the
+ # next periodic check scans again, until the stamps settle.
+ layer.stamps = stamps if _plugin_dir_stamps_settled(stamps) else ()
# Publish the completed layer -- stamps AND check time -- before auth
# sync: it calls list_providers(), which re-enters this function. An
# unpublished stamp rescanned forever; an unpublished check time
@@ -475,6 +481,12 @@ def _plugin_dir_stamps(home: Path) -> tuple:
return (stamp(home / "plugins"), stamp(home / "plugins" / "model-providers"))
+def _plugin_dir_stamps_settled(stamps: tuple) -> bool:
+ """Whether every stamp is at least one mtime tick away from now (either side: clock skew)."""
+ now = time.time_ns()
+ return all(s is None or abs(now - s) >= _PLUGIN_DIR_MTIME_TICK_NS for s in stamps)
+
+
def _user_plugins_dir() -> Path | None:
"""Return ``$HERMES_HOME/plugins/model-providers/`` if it exists."""
try:
diff --git a/tests/providers/test_profile_miss_ttl.py b/tests/providers/test_profile_miss_ttl.py
index aca1d524172..14c29e06b08 100644
--- a/tests/providers/test_profile_miss_ttl.py
+++ b/tests/providers/test_profile_miss_ttl.py
@@ -7,8 +7,10 @@ provider installed while the process runs is found on its first request.
from __future__ import annotations
+import os
import sys
import textwrap
+import time
import types
import pytest
@@ -36,7 +38,7 @@ def layer_env(tmp_path, monkeypatch):
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))
+ monkeypatch.setattr(providers, "time", types.SimpleNamespace(monotonic=lambda: clock.now, time_ns=time.time_ns))
stats = []
real = providers._plugin_dir_stamps
monkeypatch.setattr(providers, "_plugin_dir_stamps", lambda h: stats.append(1) or real(h))
@@ -92,3 +94,41 @@ def test_runtime_registration_is_found_despite_a_cached_miss(layer_env):
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
+
+
+def test_install_in_the_same_timestamp_tick_is_found_after_the_ttl(layer_env):
+ """A kernel with a coarse timestamp clock (or FAT/HFS+ rounding) gives every directory change
+ within one tick the same mtime, so the stamp read at the last scan can equal the stamp after an
+ install: the new plugin must still be found once the TTL has passed."""
+ import providers
+
+ home, clock, _stats = layer_env
+ _install(home, "tick-b")
+ assert providers.get_provider_profile("tick-b") is not None
+ model_providers = home / "plugins" / "model-providers"
+ before = model_providers.stat()
+
+ _install(home, "tick-a")
+ os.utime(model_providers, ns=(before.st_atime_ns, before.st_mtime_ns)) # same tick, same mtime
+ clock.now += providers._PLUGIN_DIR_STAMP_TTL_SECONDS
+ assert providers.get_provider_profile("tick-a") is not None
+
+
+def test_settled_unchanged_plugin_dirs_are_not_rescanned(layer_env, monkeypatch):
+ import providers
+
+ home, clock, _stats = layer_env
+ _install(home, "settled")
+ hour_ago = time.time_ns() - 3600 * 10**9
+ for path in (home / "plugins", home / "plugins" / "model-providers"):
+ os.utime(path, ns=(hour_ago, hour_ago))
+ scans = []
+ real_scan = providers._scan_home_layer
+ monkeypatch.setattr(providers, "_scan_home_layer", lambda *a: scans.append(1) or real_scan(*a))
+
+ assert providers.get_provider_profile("settled") is not None
+ for _ in range(3):
+ clock.now += providers._PLUGIN_DIR_STAMP_TTL_SECONDS
+ assert providers.get_provider_profile("settled") is not None
+ assert providers.get_provider_profile("no-profile-id") is None
+ assert len(scans) == 1 # stamps older than the filesystem's timestamp tick are trusted