Skip to content

The agent that grows with you

fix(gateway): publish plugin injector across profiles

JoaoMarcos448a462159c115
diff --git a/gateway/run_inbound.py b/gateway/run_inbound.py
index e7647ceb5b2..4fb6c2176d2 100644
--- a/gateway/run_inbound.py
+++ b/gateway/run_inbound.py
@@ -1839,18 +1839,16 @@ class GatewayInboundMixin:
delattr(event, attr)
def _install_plugin_message_injector(self) -> None:
- """Publish this live gateway's plugin message scheduler."""
- from hermes_cli.plugins import get_plugin_manager
+ """Publish this live gateway's plugin message scheduler process-wide."""
+ from hermes_cli.plugins import publish_gateway_message_host
- get_plugin_manager().set_gateway_message_injector(
- self, self._schedule_plugin_message_injection
- )
+ publish_gateway_message_host(self, self._schedule_plugin_message_injection)
def _clear_plugin_message_injector(self) -> None:
"""Remove this runner's scheduler without clobbering a newer owner."""
- from hermes_cli.plugins import get_plugin_manager
+ from hermes_cli.plugins import clear_published_gateway_message_host
- get_plugin_manager().clear_gateway_message_injector(self)
+ clear_published_gateway_message_host(self)
def _schedule_plugin_message_injection(
self, *, session_key: str, content: str, plugin_id: str
diff --git a/hermes_cli/plugins.py b/hermes_cli/plugins.py
index 189174ee86e..693da25b8e9 100644
--- a/hermes_cli/plugins.py
+++ b/hermes_cli/plugins.py
@@ -1607,6 +1607,11 @@ _plugin_manager: Optional[PluginManager] = None
_plugin_managers_by_home: Dict[Path, PluginManager] = {}
_plugin_managers_lock = threading.RLock()
+# Process-wide messaging-gateway host. A multiplexed gateway owns one scheduler while plugins are
+# isolated in per-profile managers, so every manager in this process must see the same live host.
+_published_gateway_message_injector: tuple[object, Callable] | None = None
+_published_gateway_host_lock = threading.Lock()
+
# Process-wide Ink TUI / desktop host. Not the messaging-gateway slot. Stamped onto
# each profile's manager so a multiplexed desktop process does not drop injects
# aimed at a non-launch profile. ``None`` until the TUI/desktop process installs it.
@@ -1647,6 +1652,36 @@ def _known_plugin_managers() -> list[PluginManager]:
return managers
+def publish_gateway_message_host(owner: object, injector: Callable[..., bool]) -> None:
+ """Remember the process gateway host and stamp managers that already exist."""
+ global _published_gateway_message_injector
+ # Keep publication + stamping atomic with owner-safe clear. Managers created concurrently are
+ # registered before _attach_published_gateway_host(), which takes this same lock.
+ with _published_gateway_host_lock:
+ _published_gateway_message_injector = (owner, injector)
+ for manager in _known_plugin_managers():
+ manager.set_gateway_message_injector(owner, injector)
+
+
+def clear_published_gateway_message_host(owner: object) -> None:
+ """Forget this owner's process gateway host without clobbering a newer runner."""
+ global _published_gateway_message_injector
+ with _published_gateway_host_lock:
+ if (_published_gateway_message_injector is not None
+ and _published_gateway_message_injector[0] is owner):
+ _published_gateway_message_injector = None
+ for manager in _known_plugin_managers():
+ manager.clear_gateway_message_injector(owner)
+
+
+def _attach_published_gateway_host(manager: PluginManager) -> None:
+ """Give a newly resolved profile manager the live process gateway host, if any."""
+ with _published_gateway_host_lock:
+ host = _published_gateway_message_injector
+ if host is not None and manager._gateway_message_injector is None:
+ manager.set_gateway_message_injector(*host)
+
+
def publish_tui_message_host(owner: object, injector: Callable[..., bool]) -> None:
"""Remember the process TUI/desktop host and stamp managers that already exist.
@@ -1695,13 +1730,14 @@ def get_plugin_manager() -> PluginManager:
manager = PluginManager(scope_key=hermes_home_key(current_home))
_plugin_managers_by_home[current_home] = manager
_plugin_manager = manager
+ _attach_published_gateway_host(manager)
_attach_published_tui_host(manager)
return manager
def _reset_plugin_managers_for_tests() -> None:
"""Test-only: drop every cached manager and its submodules for a fully clean slate."""
- global _plugin_manager, _published_tui_message_injector
+ global _plugin_manager, _published_gateway_message_injector, _published_tui_message_injector
with _plugin_managers_lock:
managers = list(dict.fromkeys(_plugin_managers_by_home.values()))
if _plugin_manager is not None and _plugin_manager not in managers:
@@ -1714,6 +1750,8 @@ def _reset_plugin_managers_for_tests() -> None:
logger.debug("test plugin-manager unload failed", exc_info=True)
_plugin_managers_by_home.clear()
_plugin_manager = None
+ with _published_gateway_host_lock:
+ _published_gateway_message_injector = None
with _published_tui_host_lock:
_published_tui_message_injector = None
# Dashboard-auth providers are persistent and survive a routine unload, so the clean-slate
diff --git a/tests/gateway/test_plugin_message_injection.py b/tests/gateway/test_plugin_message_injection.py
index b24b9760a7c..1621ba747cf 100644
--- a/tests/gateway/test_plugin_message_injection.py
+++ b/tests/gateway/test_plugin_message_injection.py
@@ -119,7 +119,9 @@ async def test_plugin_context_routes_through_live_gateway_to_existing_session(
manager,
)
- with patch("hermes_cli.plugins.get_plugin_manager", return_value=manager):
+ # The runner publishes process-wide; expose this standalone manager through the legacy slot so
+ # the real publisher stamps it while this integration test still exercises the scheduler.
+ with patch("hermes_cli.plugins._plugin_manager", manager):
runner._install_plugin_message_injector()
assert (
context.inject_message(
@@ -431,24 +433,15 @@ def test_scheduler_rejects_submission_failure():
)
-def test_install_and_clear_gateway_injector_preserves_newer_owner():
+def test_install_and_clear_gateway_injector_use_process_host():
runner = _runner(_entry())
- manager = PluginManager()
-
- with patch("hermes_cli.plugins.get_plugin_manager", return_value=manager):
- runner._install_plugin_message_injector()
- assert manager.has_gateway_message_injector is True
-
- runner._clear_plugin_message_injector()
- assert manager.has_gateway_message_injector is False
+ with (
+ patch("hermes_cli.plugins.publish_gateway_message_host") as publish,
+ patch("hermes_cli.plugins.clear_published_gateway_message_host") as clear,
+ ):
runner._install_plugin_message_injector()
-
- newer_owner = MagicMock()
- newer_injector = MagicMock(return_value=True)
- manager.set_gateway_message_injector(newer_owner, newer_injector)
runner._clear_plugin_message_injector()
- assert manager.has_gateway_message_injector is True
- assert manager.inject_gateway_message(value="kept") is True
- newer_injector.assert_called_once_with(value="kept")
+ publish.assert_called_once_with(runner, runner._schedule_plugin_message_injection)
+ clear.assert_called_once_with(runner)
diff --git a/tests/hermes_cli/test_plugin_message_injection.py b/tests/hermes_cli/test_plugin_message_injection.py
index 5346db792d2..0474655a91b 100644
--- a/tests/hermes_cli/test_plugin_message_injection.py
+++ b/tests/hermes_cli/test_plugin_message_injection.py
@@ -6,6 +6,8 @@ from unittest.mock import MagicMock, patch
import hermes_yaml as yaml
+import hermes_cli.plugins as plugins_mod
+from hermes_constants import reset_hermes_home_override, set_hermes_home_override
from hermes_cli.plugins import PluginContext, PluginManager, PluginManifest
@@ -181,3 +183,87 @@ def test_gateway_injection_fails_closed_on_host_exception(tmp_path, monkeypatch)
)
is False
)
+
+
+def test_published_gateway_host_reaches_existing_and_future_profile_managers(tmp_path, monkeypatch):
+ homes = [tmp_path / name for name in ("launch", "secondary", "late")]
+ for home in homes:
+ home.mkdir()
+ (home / "config.yaml").write_text(
+ yaml.safe_dump({
+ "plugins": {"entries": {"notify-plugin": {"allow_gateway_injection": True}}}
+ })
+ )
+ monkeypatch.setenv("HERMES_HOME", str(homes[0]))
+ plugins_mod._reset_plugin_managers_for_tests()
+
+ def manager_for(home):
+ token = set_hermes_home_override(str(home))
+ try:
+ return plugins_mod.get_plugin_manager()
+ finally:
+ reset_hermes_home_override(token)
+
+ owner = object()
+ injector = MagicMock(return_value=True)
+ try:
+ launch_manager = manager_for(homes[0])
+ secondary_manager = manager_for(homes[1])
+ assert secondary_manager is not launch_manager
+ assert secondary_manager.has_gateway_message_injector is False
+
+ plugins_mod.publish_gateway_message_host(owner, injector)
+ assert launch_manager.has_gateway_message_injector is True
+ assert secondary_manager.has_gateway_message_injector is True
+
+ late_manager = manager_for(homes[2])
+ assert late_manager.has_gateway_message_injector is True
+
+ token = set_hermes_home_override(str(homes[1]))
+ try:
+ context = PluginContext(
+ PluginManifest(name="notify-plugin", key="notify-plugin", source="user"),
+ secondary_manager,
+ )
+ assert context.inject_message(
+ "continue", session_key="agent:secondary:telegram:dm:42"
+ ) is True
+ finally:
+ reset_hermes_home_override(token)
+
+ injector.assert_called_once_with(
+ session_key="agent:secondary:telegram:dm:42",
+ content="continue",
+ plugin_id="notify-plugin",
+ )
+
+ plugins_mod.clear_published_gateway_message_host(owner)
+ assert launch_manager.has_gateway_message_injector is False
+ assert secondary_manager.has_gateway_message_injector is False
+ assert late_manager.has_gateway_message_injector is False
+ finally:
+ plugins_mod.clear_published_gateway_message_host(owner)
+ plugins_mod._reset_plugin_managers_for_tests()
+
+
+def test_published_gateway_host_clear_preserves_newer_owner(tmp_path, monkeypatch):
+ home = tmp_path / "hermes"
+ home.mkdir()
+ monkeypatch.setenv("HERMES_HOME", str(home))
+ plugins_mod._reset_plugin_managers_for_tests()
+ manager = plugins_mod.get_plugin_manager()
+ older_owner, newer_owner = object(), object()
+ older = MagicMock(return_value=False)
+ newer = MagicMock(return_value=True)
+ try:
+ plugins_mod.publish_gateway_message_host(older_owner, older)
+ plugins_mod.publish_gateway_message_host(newer_owner, newer)
+ plugins_mod.clear_published_gateway_message_host(older_owner)
+
+ assert manager.has_gateway_message_injector is True
+ assert manager.inject_gateway_message(session_key="kept") is True
+ newer.assert_called_once_with(session_key="kept")
+ older.assert_not_called()
+ finally:
+ plugins_mod.clear_published_gateway_message_host(newer_owner)
+ plugins_mod._reset_plugin_managers_for_tests()