Skip to content

The agent that grows with you

fix(gateway): register secondary profiles' shell hooks and outbound webhooks

chelsealong29640e3b5ae5
diff --git a/agent/outbound_webhooks.py b/agent/outbound_webhooks.py
index f437b809f35..dfdc4ccac37 100644
--- a/agent/outbound_webhooks.py
+++ b/agent/outbound_webhooks.py
@@ -98,8 +98,12 @@ _TOOL_SCOPED_EVENTS = {"pre_tool_call", "post_tool_call"}
# kwargs promoted to top-level payload keys (mirrors shell hooks wire).
_TOP_LEVEL_PAYLOAD_KEYS = {"tool_name", "args", "session_id", "parent_session_id"}
-# (event, url) pairs already wired to the plugin manager in this process.
-_registered: Set[Tuple[str, str]] = set()
+# (home, event, url) triples already wired to the plugin manager in this
+# process. Home is part of the key so a multiplexed gateway's secondary
+# profiles — each with their own plugin manager (see
+# hermes_cli.plugins.get_plugin_manager) — can register identical webhook
+# targets without the first profile's registration shadowing the rest.
+_registered: Set[Tuple[str, str, str]] = set()
_registered_lock = threading.Lock()
_delivery_queue: "queue.Queue[Optional[Dict[str, Any]]]" = queue.Queue(
@@ -180,15 +184,17 @@ def register_from_config(cfg: Optional[Dict[str, Any]]) -> List[WebhookTarget]:
return []
from hermes_cli.plugins import get_plugin_manager
+ from hermes_constants import get_hermes_home
manager = get_plugin_manager()
+ home_key = str(get_hermes_home().expanduser().resolve())
registered: List[WebhookTarget] = []
with _registered_lock:
for target in targets:
wired_any = False
for event in target.events:
- key = (event, target.url)
+ key = (home_key, event, target.url)
if key in _registered:
continue
manager._hooks.setdefault(event, []).append(
diff --git a/agent/shell_hooks.py b/agent/shell_hooks.py
index 8751aeb6fd9..289cf39d04d 100644
--- a/agent/shell_hooks.py
+++ b/agent/shell_hooks.py
@@ -182,13 +182,17 @@ _BLOCKING_EVENTS = frozenset({"pre_tool_call"})
_STDERR_MESSAGE_LIMIT = 400
-# (event, matcher, command) triples that have been wired to the plugin
+# (home, event, matcher, command) tuples that have been wired to the plugin
# manager in the current process. Matcher is part of the key because
# the same script can legitimately register for different matchers under
-# the same event (e.g. one entry per tool the user wants to gate).
-# Second registration attempts for the exact same triple become no-ops
+# the same event (e.g. one entry per tool the user wants to gate). Home is
+# part of the key so a multiplexed gateway's secondary profiles — each with
+# their own plugin manager (see hermes_cli.plugins.get_plugin_manager) — can
+# register identical hook triples without the first profile's registration
+# silently shadowing the rest.
+# Second registration attempts for the exact same tuple become no-ops
# so the CLI and gateway can both call register_from_config() safely.
-_registered: Set[Tuple[str, Optional[str], str]] = set()
+_registered: Set[Tuple[str, str, Optional[str], str]] = set()
_registered_lock = threading.Lock()
# Intra-process lock for allowlist read-modify-write on platforms that
@@ -289,13 +293,14 @@ def register_from_config(
from hermes_cli.plugins import get_plugin_manager
manager = get_plugin_manager()
+ home_key = str(get_hermes_home().expanduser().resolve())
# Idempotence + allowlist read happen under the lock; the TTY
# prompt runs outside so other threads aren't parked on a blocking
# input(). Mutation re-takes the lock with a defensive idempotence
# re-check in case two callers ever race through the prompt.
for spec in specs:
- key = (spec.event, spec.matcher, spec.command)
+ key = (home_key, spec.event, spec.matcher, spec.command)
with _registered_lock:
if key in _registered:
continue
diff --git a/gateway/run.py b/gateway/run.py
index fe194f1e413..c65af2e8aba 100644
--- a/gateway/run.py
+++ b/gateway/run.py
@@ -17038,6 +17038,34 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew
from hermes_cli.plugins import discover_plugins
discover_plugins()
+
+ # Register this profile's own declarative shell hooks and
+ # outbound webhooks. The startup-time registration in
+ # start() only ever sees the root/default profile's config
+ # (it runs before any profile scope exists), so without this
+ # a secondary profile's `hooks:` block is silently inert —
+ # its turns run under this profile's own plugin manager
+ # (hermes_cli.plugins.get_plugin_manager keys by resolved
+ # home), which never received the callbacks.
+ try:
+ from hermes_cli.config import load_config as _load_profile_config
+ from agent.shell_hooks import (
+ register_from_config as _register_shell_hooks,
+ )
+ from agent.outbound_webhooks import (
+ register_from_config as _register_outbound_webhooks,
+ )
+
+ _profile_hooks_cfg = _load_profile_config()
+ _register_shell_hooks(_profile_hooks_cfg, accept_hooks=False)
+ _register_outbound_webhooks(_profile_hooks_cfg)
+ except Exception:
+ logger.warning(
+ "shell-hook/webhook registration failed for profile '%s'",
+ profile_name,
+ exc_info=True,
+ )
+
profile_cfg = load_gateway_config()
violation = _own_policy_open_startup_violation(profile_cfg)
self._snapshot_profile_busy_modes(profile_name, profile_runtime_cfg)
diff --git a/tests/gateway/test_multiplex_adapter_registry.py b/tests/gateway/test_multiplex_adapter_registry.py
index 812c4f7dae8..84b539cdf2a 100644
--- a/tests/gateway/test_multiplex_adapter_registry.py
+++ b/tests/gateway/test_multiplex_adapter_registry.py
@@ -1012,6 +1012,53 @@ class TestSecondaryProfileConfigHandling:
assert runner._profile_adapters["later"][photon] is later
+class TestSecondaryProfileHookRegistration:
+ """A secondary profile's own `hooks:` block must register on ITS
+ plugin manager, not just the root/default profile's (#92672).
+
+ Startup only calls agent.shell_hooks/outbound_webhooks
+ register_from_config() once, against the root config, before any
+ profile scope exists. Without a matching call inside
+ _start_one_profile_adapters, a secondary profile's config.yaml
+ `hooks:` block (shell hooks and outbound webhooks) never registers.
+ """
+
+ @pytest.mark.asyncio
+ async def test_registers_shell_hooks_and_webhooks_for_secondary_profile(
+ self, monkeypatch
+ ):
+ runner = _secondary_recovery_runner()
+ config = GatewayConfig(multiplex_profiles=True, platforms={})
+ monkeypatch.setattr("gateway.config.load_gateway_config", lambda: config)
+
+ profile_cfg = {
+ "hooks": {
+ "pre_tool_call": [
+ {"matcher": "write_file", "command": "~/.hermes/deny.sh"}
+ ],
+ "outbound": [
+ {"url": "http://127.0.0.1:9000/hook", "events": ["on_session_end"]}
+ ],
+ }
+ }
+ monkeypatch.setattr("hermes_cli.config.load_config", lambda: profile_cfg)
+
+ seen = []
+ monkeypatch.setattr(
+ "agent.shell_hooks.register_from_config",
+ lambda cfg, **kwargs: seen.append(("shell", cfg)) or [],
+ )
+ monkeypatch.setattr(
+ "agent.outbound_webhooks.register_from_config",
+ lambda cfg: seen.append(("webhook", cfg)) or [],
+ )
+
+ await runner._start_one_profile_adapters("second", "/tmp/second", {})
+
+ assert ("shell", profile_cfg) in seen
+ assert ("webhook", profile_cfg) in seen
+
+
class TestFeishuPortBindingConditional:
"""Feishu websocket mode does NOT bind a port; only webhook mode does (#52563)."""