Skip to content

The agent that grows with you

fix(plugins): rescan a secondary profile at most once per window for an unknown plugin

unsupportedpastelsd190ec94e82a
diff --git a/hermes_cli/web_server_dashboard.py b/hermes_cli/web_server_dashboard.py
index d1321d29ad2..b7d2ba355de 100644
--- a/hermes_cli/web_server_dashboard.py
+++ b/hermes_cli/web_server_dashboard.py
@@ -865,10 +865,11 @@ def _plugin_api_request_refused(name: str) -> bool:
counts as a user plugin (safe default: refused).
"""
from hermes_cli.web_server import _get_dashboard_plugins
- from hermes_cli.web_server_plugin_profiles import secondary_profile_home
+ from hermes_cli.web_server_plugin_profiles import rescanned_plugin, secondary_profile_home
plugin = next((p for p in _get_dashboard_plugins() if p.get("name") == name), None)
- if plugin is None and secondary_profile_home() is not None: # installed since the last scan
- plugin = next((p for p in _get_dashboard_plugins(force_rescan=True) if p.get("name") == name), None)
+ home = secondary_profile_home()
+ if plugin is None and home is not None: # installed since the last scan (rate-limited per name)
+ plugin = rescanned_plugin(home, name)
return _plugin_consent_withheld(plugin or {"name": name, "source": "user"})
diff --git a/hermes_cli/web_server_plugin_profiles.py b/hermes_cli/web_server_plugin_profiles.py
index 2c6c497e1f9..8e873b24283 100644
--- a/hermes_cli/web_server_plugin_profiles.py
+++ b/hermes_cli/web_server_plugin_profiles.py
@@ -49,6 +49,10 @@ _HOSTED_REQUEST_THREADS = 4
# has no cancel, so the host may still finish the abandoned handler; the gateway's thread is freed.
HOSTED_REQUEST_TIMEOUT_SECS = 60.0
_hosted_executors: dict[str, ThreadPoolExecutor] = {}
+# A name a secondary profile lacks rescans its plugin dirs (it may be newly installed), then not again
+# for this long, so 404 probes can't force a disk scan per request. An explicit rescan clears it.
+_UNKNOWN_PLUGIN_RESCAN_SECS = 10.0
+_unknown_plugins: dict[tuple[str, str], float] = {}
def secondary_profile_home() -> Optional[Path]:
@@ -68,6 +72,9 @@ def profile_dashboard_plugins(home: Path, force_rescan: bool = False) -> list:
from hermes_constants import hermes_home_key
key = hermes_home_key(home)
with _profile_plugins_lock:
+ if force_rescan:
+ for unknown in [k for k in _unknown_plugins if k[0] == key]:
+ del _unknown_plugins[unknown]
cached = _profile_plugins.get(key)
if cached is None or force_rescan or any(not Path(p["_dir"]).is_dir() for p in cached):
bundled, own = get_bundled_plugins_dir(), Path(home) / "plugins"
@@ -82,6 +89,23 @@ def _named(plugins: list, name: str) -> Optional[dict]:
return next((p for p in plugins if p.get("name") == name), None)
+def rescanned_plugin(home: Path, name: str) -> Optional[dict]:
+ """The profile at ``home``'s entry for ``name`` after a rescan, or None; a name found missing
+ within the last :data:`_UNKNOWN_PLUGIN_RESCAN_SECS` is answered without one."""
+ from hermes_constants import hermes_home_key
+ key, now = (hermes_home_key(home), name), time.monotonic()
+ with _profile_plugins_lock:
+ if _unknown_plugins.get(key, now) > now:
+ return None
+ plugin = _named(profile_dashboard_plugins(home, force_rescan=True), name)
+ if plugin is None:
+ with _profile_plugins_lock:
+ for expired in [k for k, until in _unknown_plugins.items() if until <= now]:
+ del _unknown_plugins[expired]
+ _unknown_plugins[key] = now + _UNKNOWN_PLUGIN_RESCAN_SECS
+ return plugin
+
+
def plugin_api_path(plugin: Optional[dict]) -> Optional[Path]:
"""The resolved backend file of a discovered plugin entry, or None."""
if not plugin or not plugin.get("_api_file"):
diff --git a/tests/hermes_cli/test_plugin_secondary_profile_host.py b/tests/hermes_cli/test_plugin_secondary_profile_host.py
index b3b38655f75..aa7e796d497 100644
--- a/tests/hermes_cli/test_plugin_secondary_profile_host.py
+++ b/tests/hermes_cli/test_plugin_secondary_profile_host.py
@@ -19,6 +19,7 @@ import sys
import threading
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
+from types import SimpleNamespace
from urllib.parse import parse_qsl, urljoin
import pytest
@@ -538,3 +539,29 @@ def test_launch_profiles_hosted_mount_answers_504_too(daemon, monkeypatch):
assert response.status_code == 504 and homes == [daemon.launch]
+
+def test_unknown_plugin_names_do_not_rescan_the_profile_on_every_request(daemon, monkeypatch):
+ """A 404 probe rescans a secondary profile's plugin dirs once per short window, not on every
+ request; an explicit rescan, or the end of the window, still finds a plugin installed meanwhile."""
+ from hermes_cli import web_server_dashboard, web_server_plugin_profiles
+ _beta_plugin(daemon)
+ _plugins(daemon.beta, enabled=[NAME, "later", "latest"])
+ selected = {"params": {"profile": "beta"}, "headers": daemon.owner}
+ status = lambda name: daemon.client.get(f"/api/plugins/{name}/ping", **selected).status_code
+ assert status(NAME) == 200
+ scans, discover, clock = [], web_server_dashboard._discover_dashboard_plugins, [1000.0]
+ monkeypatch.setattr(web_server_dashboard, "_discover_dashboard_plugins",
+ lambda *a, **k: scans.append(1) or discover(*a, **k))
+ monkeypatch.setattr(web_server_plugin_profiles, "time", SimpleNamespace(monotonic=lambda: clock[0]),
+ raising=False)
+
+ assert [status("later") for _ in range(3)] == [404] * 3
+ assert len(scans) == 1
+ _install(daemon.beta, "later")
+ daemon.client.get("/api/dashboard/plugins/rescan", **selected)
+ assert status("later") == 200
+
+ assert status("latest") == 404
+ _install(daemon.beta, "latest")
+ clock[0] += 60
+ assert status("latest") == 200