The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(plugins): host isolation loads a create_client profile in-process instead of dropping its siblings
Teknium3c0011c4044e
diff --git a/hermes_cli/plugin_host_child.py b/hermes_cli/plugin_host_child.pyindex f17e153c17d..ffc903e07cf 100644--- a/hermes_cli/plugin_host_child.py+++ b/hermes_cli/plugin_host_child.py@@ -527,7 +527,6 @@ def _take_protocol_streams():def capture_profiles(path: str, module_name: str) -> list[Any]:"""Import a model-provider plugin and return the profiles it passes to ``register_provider``."""import providers- from providers.base import ProviderProfileproviders.list_providers() # bundled + known profiles register before the capture windowcaptured: list[Any] = []original = providers.register_provider@@ -536,14 +535,24 @@ def capture_profiles(path: str, module_name: str) -> list[Any]:_import_plugin({"path": path, "module_name": module_name, "name": Path(path).name})finally:providers.register_provider = original # type: ignore[assignment]- for profile in captured:- if type(profile).create_client is not ProviderProfile.create_client:- raise PluginHostUnsupported(- f"provider profile {getattr(profile, 'name', '?')!r} overrides create_client(), which returns "- "a live SDK client; it runs only in-process")return captured+def _host_unsupported_reason(profile: Any) -> str | None:+ """Why *profile* cannot run behind the plugin host (it then loads in-process), or None."""+ from providers.base import ProviderProfile+ if type(profile).create_client is not ProviderProfile.create_client:+ return "it overrides create_client(), which returns a live SDK client"+ return None+++def _extraction_entry(profile: Any) -> dict[str, Any]:+ reason = _host_unsupported_reason(profile)+ if reason is not None:+ return {"name": profile.name, "in_process": reason}+ return describe_profile(profile)++def describe_profile(profile: Any) -> dict[str, Any]:"""Profile data (instance fields) plus the methods and callable fields that must run here."""from providers.base import ProviderProfile@@ -577,7 +586,7 @@ def extract_profiles_main(path: str, module_name: str) -> int:del readerimport hermes_bootstraptry:- payload = {"profiles": [describe_profile(p) for p in capture_profiles(path, module_name)]}+ payload = {"profiles": [_extraction_entry(p) for p in capture_profiles(path, module_name)]}except Exception as exc:payload = {"error": f"{type(exc).__name__}: {exc}"}writer.write(json.dumps(payload).encode("utf-8"))diff --git a/hermes_cli/plugin_host_profiles.py b/hermes_cli/plugin_host_profiles.pyindex 16df810ec84..27791f3f51d 100644--- a/hermes_cli/plugin_host_profiles.py+++ b/hermes_cli/plugin_host_profiles.py@@ -29,14 +29,19 @@ _EXTRACT_TIMEOUT_SECS = 60.0_CACHE_VERSION = 1-def load_hosted_profiles(plugin_dir: Path, module_name: str) -> list[Any]:- """ProviderProfile proxies for a model-provider plugin whose code runs in the plugin host."""+def load_hosted_profiles(plugin_dir: Path, module_name: str) -> tuple[list[Any], dict[str, str]]:+ """ProviderProfile proxies for a model-provider plugin whose code runs in the plugin host, plus+ ``{profile name: reason}`` for its profiles that cannot cross the host boundary (the caller+ loads those in-process)."""from providers.base import ProviderProfilepayload = _cached_extraction(Path(plugin_dir), module_name)if payload.get("error"):raise PluginHostUnavailable(str(payload["error"]))- return [_profile_proxy(ProviderProfile, str(plugin_dir), module_name, entry)- for entry in payload.get("profiles") or []]+ entries = payload.get("profiles") or []+ in_process = {str(e["name"]): str(e["in_process"]) for e in entries if e.get("in_process")}+ hosted = [_profile_proxy(ProviderProfile, str(plugin_dir), module_name, e)+ for e in entries if not e.get("in_process")]+ return hosted, in_processdef _fingerprint(plugin_dir: Path) -> str:diff --git a/providers/__init__.py b/providers/__init__.pyindex 8f72a081543..57ab4354ae3 100644--- a/providers/__init__.py+++ b/providers/__init__.py@@ -92,6 +92,9 @@ _HOME_LAYERS_LOCK = threading.Lock()# across the import itself — a thread mid-``import hermes_cli.auth`` (whose import calls# ``list_providers()``) would block on it while the scanning thread waits on that module's import lock._REGISTRATION_TARGET: ContextVar[_HomeLayer | None] = ContextVar("_provider_registration_target", default=None)+# Profile names an in-process import may register (None = all). Set while a hosted plugin's+# profiles that cannot cross the host boundary are imported here: its hosted siblings stay hosted.+_REGISTRATION_ONLY: ContextVar[frozenset[str] | None] = ContextVar("_provider_registration_only", default=None)# Repo-root ``plugins/model-providers/`` — populated at discovery time._BUNDLED_PLUGINS_DIR = (@@ -132,6 +135,9 @@ def register_provider(profile: ProviderProfile) -> None:``$HERMES_HOME`` plugin is being imported lands in that home's layer."""global _PROVIDER_LIST_CACHE, _REGISTRY_GENERATION+ only = _REGISTRATION_ONLY.get()+ if only is not None and profile.name not in only:+ return_REGISTRY_GENERATION += 1layer = _REGISTRATION_TARGET.get()if layer is not None:@@ -558,23 +564,12 @@ def _import_plugin_dir(plugin_dir: Path, source: str, *, home_key: str = "") ->``source`` is "bundled" or "user"; it is recorded per registered profile (``_SOURCES``)."""- global _current_source- init_file = plugin_dir / "__init__.py"- if not init_file.exists():+ if not (plugin_dir / "__init__.py").exists():returnif source != "bundled":from hermes_cli.plugin_isolation import ISOLATION_HOST, isolation_modeif isolation_mode() == ISOLATION_HOST: # the plugin's code runs in the plugin host- from hermes_cli.plugin_host_profiles import load_hosted_profiles- _current_source = source- try:- for profile in load_hosted_profiles(plugin_dir, _user_module_name(plugin_dir, home_key)):- register_provider(profile)- except Exception as exc:- logger.warning("Failed to load user provider plugin %s in the plugin host: %s",- plugin_dir.name, exc)- finally:- _current_source = None+ _import_hosted_plugin_dir(plugin_dir, source, _user_module_name(plugin_dir, home_key))return# Give bundled plugins a stable import path (``plugins.model_providers.<name>``)@@ -585,21 +580,53 @@ def _import_plugin_dir(plugin_dir: Path, source: str, *, home_key: str = "") ->module_name = f"plugins.model_providers.{plugin_dir.name.replace('-', '_')}"else:module_name = _user_module_name(plugin_dir, home_key)+ _exec_plugin_module(plugin_dir, source, module_name)+++def _import_hosted_plugin_dir(plugin_dir: Path, source: str, module_name: str) -> None:+ """Register a plugin's profiles as plugin-host proxies; a profile that cannot cross the host+ boundary (e.g. it overrides ``create_client``) is imported in-process ALONE, with a warning,+ so it never takes its hosted siblings down with it."""+ global _current_source+ from hermes_cli.plugin_host_profiles import load_hosted_profiles+ _current_source = source+ try:+ hosted, in_process = load_hosted_profiles(plugin_dir, module_name)+ for profile in hosted:+ register_provider(profile)+ except Exception as exc: # health: allow BLE001 -- plugin load boundary (moved from _import_plugin_dir): one broken plugin never breaks discovery+ logger.warning("Failed to load user provider plugin %s in the plugin host: %s", plugin_dir.name, exc)+ 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)++def _exec_plugin_module(plugin_dir: Path, source: str, module_name: str) -> None:+ global _current_sourceif module_name in sys.modules:return # already imported_current_source = sourcetry:spec = importlib.util.spec_from_file_location(- module_name, init_file, submodule_search_locations=[str(plugin_dir)]+ module_name, plugin_dir / "__init__.py", submodule_search_locations=[str(plugin_dir)])if spec is None or spec.loader is None:returnmodule = importlib.util.module_from_spec(spec)sys.modules[module_name] = modulespec.loader.exec_module(module)- except Exception as exc:+ except Exception as exc: # health: allow BLE001 -- plugin import boundary (moved from _import_plugin_dir): one broken plugin never breaks discoverylogger.warning("Failed to load %s provider plugin %s: %s", source, plugin_dir.name, exc)diff --git a/tests/hermes_cli/test_plugin_host_mixed_profiles.py b/tests/hermes_cli/test_plugin_host_mixed_profiles.pynew file mode 100644index 00000000000..8108969c2b1--- /dev/null+++ b/tests/hermes_cli/test_plugin_host_mixed_profiles.py@@ -0,0 +1,60 @@+"""plugins.isolation: host — a profile that cannot cross the host boundary never takes its siblings down."""++import logging+import os+import sys++import hermes_yaml as yaml+import pytest++from hermes_cli import plugins as plugins_mod++TWO_PROFILE_PLUGIN = '''+import os+from providers import register_provider+from providers.base import ProviderProfile++class Hosted(ProviderProfile):+ def build_extra_body(self, *, session_id=None, **context):+ return {"pid": os.getpid()}++class Local(ProviderProfile):+ def create_client(self, **kwargs):+ return {"pid": os.getpid()}++register_provider(Hosted(name="mixhosted", base_url="https://mixhosted.example/v1"))+register_provider(Local(name="mixlocal", base_url="acp://mixlocal"))+'''++[email protected]("any") # the host is a child process: its env/home resolution is per-OS+def test_create_client_profile_loads_in_process_and_its_sibling_stays_hosted(tmp_path, monkeypatch, caplog):+ 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))++ with caplog.at_level(logging.WARNING, logger="providers"):+ hosted = providers.get_provider_profile("mixhosted")+ local = providers.get_provider_profile("mixlocal")+ try:+ assert hosted is not None and local is not None+ assert local.create_client()["pid"] == os.getpid()+ assert hosted.build_extra_body(session_id="s")["pid"] != os.getpid()+ # The in-process import registered ONLY the profile that needs it.+ assert type(hosted).__module__ == "hermes_cli.plugin_host_profiles"+ notices = [r.getMessage() for r in caplog.records if "mixlocal" in r.getMessage()]+ assert len(notices) == 1 and "create_client" in notices[0]+ assert not any("mixhosted" in r.getMessage() for r in caplog.records)+ 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)