Skip to content

The agent that grows with you

fix(providers): a home rescan never re-imports a hosted provider plugin it already loaded

Tekniume5a9a33ff068
diff --git a/providers/__init__.py b/providers/__init__.py
index 75921076e64..f24c98fed28 100644
--- a/providers/__init__.py
+++ b/providers/__init__.py
@@ -88,6 +88,9 @@ class _HomeLayer:
misses: dict[str, float] = field(default_factory=dict)
# plugin dir name -> the error its import raised (``plugin_load_error``), until an import succeeds
errors: dict[str, str] = field(default_factory=dict)
+ # module names of plugins whose profiles this layer already holds as plugin-host proxies: the
+ # hosted path's ``sys.modules`` check, so a rescan never re-imports, re-warns or swaps proxies
+ hosted: set[str] = field(default_factory=set)
_HOME_LAYERS: dict[str, _HomeLayer] = {}
@@ -616,6 +619,9 @@ def _import_hosted_plugin_dir(plugin_dir: Path, source: str, module_name: str) -
so it never takes its hosted siblings down with it."""
global _current_source
from hermes_cli.plugin_host_profiles import load_hosted_profiles
+ layer = _REGISTRATION_TARGET.get()
+ if layer is not None and module_name in layer.hosted:
+ return # already imported into this home's layer
_current_source = source
try:
hosted, in_process = load_hosted_profiles(plugin_dir, module_name)
@@ -627,16 +633,19 @@ def _import_hosted_plugin_dir(plugin_dir: Path, source: str, module_name: str) -
return
finally:
_current_source = None
- if not in_process:
- return
- for name, reason in in_process.items():
- logger.warning("Provider plugin %s: profile %r cannot run in the plugin host (%s); loading it "
- "in-process, NOT isolated by plugins.isolation: host", plugin_dir.name, name, reason)
- token = _REGISTRATION_ONLY.set(frozenset(in_process))
- try:
- _exec_plugin_module(plugin_dir, source, module_name)
- finally:
- _REGISTRATION_ONLY.reset(token)
+ if in_process:
+ for name, reason in in_process.items():
+ logger.warning("Provider plugin %s: profile %r cannot run in the plugin host (%s); loading it "
+ "in-process, NOT isolated by plugins.isolation: host", plugin_dir.name, name, reason)
+ token = _REGISTRATION_ONLY.set(frozenset(in_process))
+ try:
+ _exec_plugin_module(plugin_dir, source, module_name)
+ finally:
+ _REGISTRATION_ONLY.reset(token)
+ if module_name not in sys.modules:
+ return # the in-process import failed: the next scan retries the plugin
+ if layer is not None:
+ layer.hosted.add(module_name)
def _exec_plugin_module(plugin_dir: Path, source: str, module_name: str) -> None:
diff --git a/tests/hermes_cli/test_plugin_host_mixed_profiles.py b/tests/hermes_cli/test_plugin_host_mixed_profiles.py
index 8108969c2b1..66003c88484 100644
--- a/tests/hermes_cli/test_plugin_host_mixed_profiles.py
+++ b/tests/hermes_cli/test_plugin_host_mixed_profiles.py
@@ -58,3 +58,35 @@ def test_create_client_profile_loads_in_process_and_its_sibling_stays_hosted(tmp
host.shutdown()
for name in [m for m in sys.modules if m.endswith("_mixpair")]:
sys.modules.pop(name, None)
+
+
+def test_a_rescan_keeps_the_hosted_plugin_it_already_loaded(tmp_path, monkeypatch, caplog):
+ """Another plugin dir landing while the process runs rescans the home: the plugin already loaded
+ is not imported again (no second warning, the same hosted profile object)."""
+ import providers
+
+ home = tmp_path / ".hermes"
+ plugin_dir = home / "plugins" / "model-providers" / "mixpair"
+ plugin_dir.mkdir(parents=True)
+ (plugin_dir / "plugin.yaml").write_text("name: mixpair\nkind: model-provider\n", encoding="utf-8")
+ (plugin_dir / "__init__.py").write_text(TWO_PROFILE_PLUGIN, encoding="utf-8")
+ (home / "config.yaml").write_text(yaml.safe_dump({"plugins": {"isolation": "host"}}), encoding="utf-8")
+ monkeypatch.setenv("HOME", str(tmp_path / "os-home"))
+ monkeypatch.setenv("HERMES_HOME", str(home))
+
+ try:
+ with caplog.at_level(logging.WARNING, logger="providers"):
+ first = providers.get_provider_profile("mixhosted")
+ assert providers.get_provider_profile("mixlocal") is not None
+ (home / "plugins" / "model-providers" / "other").mkdir()
+ providers.refresh_bound_home()
+ again = providers.get_provider_profile("mixhosted")
+ assert first is not None and again is first
+ assert len([r for r in caplog.records if "mixlocal" in r.getMessage()]) == 1
+ finally:
+ host = getattr(plugins_mod.get_plugin_manager(), "_plugin_host_instance", None)
+ if host is not None:
+ host.shutdown()
+ for name in [m for m in sys.modules if m.endswith("_mixpair")]:
+ sys.modules.pop(name, None)