From 75ccc2eaafe87892aca16188889987d529e96cfd Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Wed, 25 Nov 2026 14:49:05 -0800 Subject: [PATCH] fix(plugins): load a platform plugin's new dependencies without a restart; no false catalog notice A platform plugin whose Python dependencies are new (Feishu: lark-oapi) lands them in a new PM dependency generation. The gateway that installed it at start, or saw it installed while running (Desktop / dashboard save, `hermes plugins install`), kept importing its boot generation: discovery logged "restart Hermes" and the adapter failed with "lark-oapi is not installed" until a restart. PM already adopts a published generation in place when nothing the process imported would change (pm.environments_adopt.adopt / adopt_selected, used by pm.extras.ensure_import). Plugin discovery and the platform dependency probe (PlatformRegistry.ensure_deps) now use it through one helper, pm.environments_adopt.adopt_published; when PM refuses (a loaded package would change) the restart remedy stays, unchanged. catalog_platforms.pending_entries no longer names an entry whose required env a platform plugin the home already has also requires (the community hermes-tag overrides Feishu with the same FEISHU_*), so a working Feishu user is not told on every start that a plugin they do not need is missing. --- gateway/platform_registry.py | 6 +++ hermes_cli/catalog_platforms.py | 36 +++++++++++-- hermes_cli/plugins_loader.py | 22 +++++++- pm/environments_adopt.py | 9 ++++ .../test_platform_plugin_install_race.py | 23 ++++++++ .../test_plugin_dependency_adoption.py | 52 +++++++++++++++++++ 6 files changed, 142 insertions(+), 6 deletions(-) create mode 100644 tests/hermes_cli/test_plugin_dependency_adoption.py diff --git a/gateway/platform_registry.py b/gateway/platform_registry.py index e25041d3ab..fe5bf18811 100644 --- a/gateway/platform_registry.py +++ b/gateway/platform_registry.py @@ -550,6 +550,12 @@ class PlatformRegistry: logger.warning(failure_msg, entry.label, e) return False deps_ok = _probe(entry.check_fn, "Platform '%s' check_fn raised: %s") + if not deps_ok: + from hermes_cli.plugins_loader import adopt_published_dependencies + if adopt_published_dependencies(): + # The plugin's dependencies were published after this process booted (a gateway-start or + # mid-run install): its adapter can be built now, without a restart. + deps_ok = _probe(entry.check_fn, "Platform '%s' check_fn raised: %s") if not deps_ok and entry.ensure_deps_fn is not None: # The ONE place the active installer runs: the platform is enabled+configured # and about to connect, so an install is what the user wants. diff --git a/hermes_cli/catalog_platforms.py b/hermes_cli/catalog_platforms.py index 6c73462663..37752e217c 100644 --- a/hermes_cli/catalog_platforms.py +++ b/hermes_cli/catalog_platforms.py @@ -35,17 +35,43 @@ def _configured(entry, home: Path) -> bool: return bool(names) and all(_home_env(home, name, process_env=True) for name in names) +def _served_env(home: Path) -> set[str]: + """Env names a platform plugin *home* already has (bundled or installed, not disabled) requires: read + from the manifests, never by loading a plugin, so it holds before *home*'s first discovery.""" + from hermes_cli.left_core_migration import _plugins_listed + from hermes_cli.plugins_discovery import collect_directory_manifests + from agent.runtime_scope import bind_profile_scope + from hermes_cli.plugins_manifest import manifest_key + with bind_profile_scope(home, hydrate_secrets=False): + manifests = collect_directory_manifests() + served: set[str] = set() + for manifest in manifests: + if manifest.kind != "platform" or _plugins_listed(home, (manifest.name, manifest_key(manifest)))[1]: + continue + for item in manifest.requires_env: + name = item.get("name") if isinstance(item, dict) else item + if isinstance(name, str): + served.add(name) + return served + + def pending_entries(home: Path) -> list: - """Platform entries *home* has the credentials for and has never had installed. Read-only.""" + """Platform entries *home* has the credentials for and has never had installed. Read-only. + + An entry whose required env a platform plugin *home* already has also requires is not pending: those + credentials are served (``hermes-tag`` overrides Feishu with the same ``FEISHU_*``), so naming it would + tell a working Feishu user on every start that a plugin they do not need is missing.""" from hermes_cli.default_plugins import _installed_dir, _recorded from hermes_cli.left_core_migration import LEFT_CORE, _plugins_listed from hermes_cli.plugin_catalog import load_catalog_live owned = {feature.plugin for feature in LEFT_CORE} done = _recorded(home) - return [entry for entry in load_catalog_live() - if entry.category == "platform" and entry.name not in owned and entry.name not in done - and _configured(entry, home) and _installed_dir(home, entry) is None - and not _plugins_listed(home, (entry.name,))[1]] + candidates = [entry for entry in load_catalog_live() + if entry.category == "platform" and entry.name not in owned and entry.name not in done + and _configured(entry, home) and _installed_dir(home, entry) is None + and not _plugins_listed(home, (entry.name,))[1]] + served = _served_env(home) if candidates else set() + return [entry for entry in candidates if not set(entry.capabilities.requires_env) <= served] def recover_in(home: Path, *, report: Callable[[str], None]) -> list[str]: diff --git a/hermes_cli/plugins_loader.py b/hermes_cli/plugins_loader.py index 157b2f225a..8c8f6ffa9e 100644 --- a/hermes_cli/plugins_loader.py +++ b/hermes_cli/plugins_loader.py @@ -298,6 +298,17 @@ def installed_in_selected_environment(requirements: list[str]) -> bool: return False +def adopt_published_dependencies() -> bool: + """Whether this process just moved onto a dependency generation published after it booted.""" + from pm.environments_adopt import adopt_published + from pm.paths import repo_root + try: + return adopt_published(repo_root()) + except (OSError, RuntimeError, ValueError): # unreadable selection: keep the restart remedy + logger.debug("dependency generation adoption failed", exc_info=True) + return False + + class PluginLoaderMixin: def on_plugin_loaded(self, callback: Callable[[list[dict[str, Any]]], Any]) -> Callable[[], None]: """Subscribe to "a discovery sweep loaded plugins this process did not have": fires from INSIDE @@ -514,12 +525,21 @@ class PluginLoaderMixin: def _warn_python_dependencies(self, manifest: PluginManifest) -> None: """Report missing dependencies without installing during discovery. - Plugin admission and PM repair own dependency changes. + Plugin admission and PM repair own dependency changes. When the generation PM selected since this + process booted carries them (a platform plugin installed at gateway start, or by Desktop while the + gateway runs), this process adopts it first (``pm.environments_adopt.adopt_published``: only when + nothing it already imported would change), so the plugin loads now instead of after a restart. """ missing = missing_python_dependencies(manifest) if not missing: return key = manifest_key(manifest) + if installed_in_selected_environment(missing) and adopt_published_dependencies(): + missing = missing_python_dependencies(manifest) + if not missing: + logger.info("Plugin %s: loaded its Python dependencies from the dependency environment " + "published after this process started.", key) + return if installed_in_selected_environment(missing): logger.warning( "Plugin %s's Python dependencies were installed after this process started: %s. " diff --git a/pm/environments_adopt.py b/pm/environments_adopt.py index 4e48efd630..7eb38d4fee 100644 --- a/pm/environments_adopt.py +++ b/pm/environments_adopt.py @@ -132,3 +132,12 @@ def adopt_selected(project_root: Path) -> bool: or runs from no generation of this install at all (there is nothing to adopt).""" pair = _running_and_selected(project_root) return pair is None or adopt(pair[0], pair[1], pair[0]) + + +def adopt_published(project_root: Path) -> bool: + """Move this process onto a generation published after it booted (a plugin's dependencies installed + by another process or by this one). True only when this process now imports from a generation it did + not before; False when nothing was published since, when it runs from no generation of this install, + or when :func:`adopt` refuses (something already loaded would change: only a restart loads that).""" + return restart_needed(project_root) is not None and adopt_selected(project_root) \ + and restart_needed(project_root) is None diff --git a/tests/hermes_cli/test_platform_plugin_install_race.py b/tests/hermes_cli/test_platform_plugin_install_race.py index 580e9034e5..88ad43d801 100644 --- a/tests/hermes_cli/test_platform_plugin_install_race.py +++ b/tests/hermes_cli/test_platform_plugin_install_race.py @@ -239,3 +239,26 @@ def test_catalog_platform_credentials_install_a_default_entry_and_point_to_any_o told = [line for line in said if "chattwo" in line] assert len(told) == 1 and "`hermes plugins install chattwo`" in told[0] and "CHATTWO_TOKEN" in told[0] assert not any("chatthree" in line for line in said) + + +def test_a_catalog_platform_whose_credentials_another_platform_serves_is_not_reported(tmp_path, monkeypatch): + """The real catalog's community ``hermes-tag`` requires ``FEISHU_APP_ID``/``FEISHU_APP_SECRET``, which the + Feishu platform plugin this home has also requires: those credentials are served, so a working Feishu + user is not told on every start that 'hermes-tag' is missing. A platform nothing serves still is.""" + import hermes_cli.plugin_catalog as catalog + from hermes_cli import catalog_platforms + + home = tmp_path / "home" + home.mkdir() + (home / ".env").write_text("FEISHU_APP_ID=a\nFEISHU_APP_SECRET=b\nFLUXER_BOT_TOKEN=c\n", encoding="utf-8") + for name in ("FEISHU_APP_ID", "FEISHU_APP_SECRET", "FLUXER_BOT_TOKEN"): + monkeypatch.delenv(name, raising=False) + monkeypatch.setenv("HERMES_HOME", str(home)) + monkeypatch.setattr(catalog, "fetch_live_catalog", lambda **kwargs: None) + names = {entry.name for entry in catalog.load_catalog()} + assert {"hermes-tag", "fluxer-platform"} <= names # the real in-tree catalog file + + pending = {entry.name for entry in catalog_platforms.pending_entries(home)} + + assert "hermes-tag" not in pending + assert "fluxer-platform" in pending diff --git a/tests/hermes_cli/test_plugin_dependency_adoption.py b/tests/hermes_cli/test_plugin_dependency_adoption.py new file mode 100644 index 0000000000..b09af79baa --- /dev/null +++ b/tests/hermes_cli/test_plugin_dependency_adoption.py @@ -0,0 +1,52 @@ +"""A plugin whose Python dependencies PM published after this process booted loads without a restart. + +A platform plugin with new dependencies (Feishu: ``lark-oapi``) is installed into a NEW dependency +generation: at gateway start (credentials present, plugin missing) or while the gateway runs (Desktop / +dashboard save, ``hermes plugins install``). Activation is a boot decision, so the gateway that did or saw +the install kept importing its boot generation: discovery logged "restart Hermes" and the adapter failed +with "lark-oapi is not installed" until somebody restarted it. PM already adopts a published generation +in place when nothing this process imported would change (``pm.environments_adopt``); discovery and the +platform dependency probe now use it. Real uv, offline wheels, temp HERMES_HOME. +""" +from __future__ import annotations + +import importlib.util +import logging + +from tests.hermes_cli.plugin_worker_support import ( + boot as boot, isolated_python as isolated_python, plugin_world as plugin_world, publish_plugins) # noqa: PLC0414 -- the self-alias is load-bearing: it suppresses F811 for the pytest fixture parameter shadowing this import + + +def test_discovery_loads_a_plugin_whose_dependencies_were_published_after_boot(plugin_world, boot, caplog): + from hermes_cli import plugins + + world = plugin_world + boot(publish_plugins(world, {"base": ["plugin-proof-dep==1.0"]})) + publish_plugins(world, {"base": ["plugin-proof-dep==1.0"], "adds": ["plugin-proof-other"]}) + plugins._reset_plugin_managers_for_tests() + try: + with caplog.at_level(logging.WARNING, logger="hermes_cli.plugins"): + plugins.discover_plugins(force=True) + loaded = plugins.get_plugin_manager()._plugins["adds"] + finally: + plugins._reset_plugin_managers_for_tests() + + assert loaded.error is None and (world.home / "plugins" / "adds" / "imported").is_file(), loaded.error + assert not [r.getMessage() for r in caplog.records if "restart" in r.getMessage()] + + +def test_a_platform_adapter_finds_dependencies_published_after_boot(plugin_world, boot): + """The platform was loaded (its check imports lazily) before the generation carrying its SDK + was published: building its adapter adopts that generation instead of reporting the SDK missing.""" + from gateway.platform_registry import PlatformEntry, PlatformRegistry + + world = plugin_world + boot(publish_plugins(world, {"base": ["plugin-proof-dep==1.0"]})) + registry = PlatformRegistry() + registry.register(PlatformEntry( + name="proofchat", label="Proof Chat", adapter_factory=lambda cfg: object(), source="plugin", + check_fn=lambda: importlib.util.find_spec("plugin_proof_other") is not None)) + assert importlib.util.find_spec("plugin_proof_other") is None + publish_plugins(world, {"base": ["plugin-proof-dep==1.0"], "adds": ["plugin-proof-other"]}) + + assert registry.ensure_deps("proofchat") is True