Skip to content

The agent that grows with you

fix(plugins): stream observer hooks and event subscribers fail-report once, not per event

teknium13f80dc9a5a2c
diff --git a/agent/plugin_stream_hooks.py b/agent/plugin_stream_hooks.py
index 47bfa3ad5bc..0cb5d6a4d28 100644
--- a/agent/plugin_stream_hooks.py
+++ b/agent/plugin_stream_hooks.py
@@ -67,9 +67,11 @@ def _worker(dispatcher: _ConsumerDispatcher) -> None:
try:
dispatcher.callback(**payload)
except Exception as exc:
- logger.warning(
- "Hook '%s' callback %s raised: %s", dispatcher.hook_name, _callback_name(dispatcher.callback), exc
- )
+ # Fires once per streaming delta: a mis-declared callback fails identically every
+ # time, so it goes through the manager's warn-once reporter (#111922).
+ from hermes_cli.plugins import get_plugin_manager
+
+ get_plugin_manager()._report_hook_failure(dispatcher.hook_name, dispatcher.callback, payload, exc)
finally:
dispatcher.events.task_done()
diff --git a/hermes_cli/plugins_dispatch.py b/hermes_cli/plugins_dispatch.py
index 73406eea4c4..bdbd4a9bba2 100644
--- a/hermes_cli/plugins_dispatch.py
+++ b/hermes_cli/plugins_dispatch.py
@@ -398,9 +398,8 @@ class PluginDispatchMixin:
# Fresh deep copy per subscriber: no callback can mutate what the next sees.
resolve_plugin_command_result(callback(**copy.deepcopy(item.payload)))
except Exception as exc:
- logger.warning(
- "Event '%s' subscriber %s raised: %s", item.event,
- getattr(callback, "__name__", repr(callback)), exc)
+ # A subscriber that fails identically on every emit is reported once (#111922).
+ self._report_hook_failure(item.event, callback, item.payload, exc, surface="Event")
finally:
self._emit_depth.value = previous_depth
diff --git a/tests/hermes_cli/test_plugin_hook_failure_reporting.py b/tests/hermes_cli/test_plugin_hook_failure_reporting.py
index f4296501384..365d3e82dfa 100644
--- a/tests/hermes_cli/test_plugin_hook_failure_reporting.py
+++ b/tests/hermes_cli/test_plugin_hook_failure_reporting.py
@@ -98,3 +98,53 @@ def test_execution_chain_middleware_failure_warns_once(manager, caplog, monkeypa
assert len(warnings) == 1
assert len(debugs) == 3
assert "Middleware 'tool_execution'" in warnings[0].getMessage()
+
+
+def test_stream_observer_hook_failure_warns_once(manager, caplog, monkeypatch):
+ """Stream observer hooks fire once per streaming delta (far more often than per tool call);
+ the per-consumer worker reports a mis-declared callback through the same warn-once path."""
+ from agent import plugin_stream_hooks as psh
+
+ monkeypatch.setattr("hermes_cli.plugins._plugin_manager", manager)
+
+ def on_stream_delta(tool_data, **kwargs): # core sends delta, never tool_data
+ return None
+
+ monkeypatch.setattr(psh, "_registered_callbacks", lambda name: (on_stream_delta,))
+ psh.shutdown_plugin_stream_hook_dispatcher()
+ try:
+ with caplog.at_level(logging.DEBUG):
+ for i in range(20):
+ assert psh.enqueue_plugin_stream_hook("on_stream_delta", delta=f"d{i}")
+ for dispatcher in psh._dispatchers_for("on_stream_delta"):
+ dispatcher.events.join()
+ finally:
+ psh.shutdown_plugin_stream_hook_dispatcher()
+
+ warnings = [r for r in caplog.records if r.levelno == logging.WARNING and "on_stream_delta" in r.getMessage()]
+ debugs = [r for r in caplog.records if r.levelno == logging.DEBUG and "on_stream_delta" in r.getMessage()]
+ assert len(warnings) == 1
+ assert len(debugs) == 19
+ assert "Hook 'on_stream_delta'" in warnings[0].getMessage()
+ assert "delta" in warnings[0].getMessage()
+
+
+def test_event_subscriber_failure_warns_once(manager, caplog):
+ """Plugin event subscribers deliver on the host worker; one that raises identically on every
+ emit is reported once with the ``Event`` surface label."""
+ manager._discovered = True
+
+ def on_event(tool_data, **kwargs): # the emitter sends its own payload, never tool_data
+ return None
+
+ manager._subscribe_event("listener", "emitter:tick", on_event)
+ with caplog.at_level(logging.DEBUG, logger="hermes_cli.plugins"):
+ for i in range(4):
+ assert manager._dispatch_event("emitter:tick", {"n": i}) == 1
+ assert manager._wait_for_event_dispatch(timeout=2.0)
+
+ warnings = [r for r in caplog.records if r.levelno == logging.WARNING and "on_event" in r.getMessage()]
+ debugs = [r for r in caplog.records if r.levelno == logging.DEBUG and "on_event" in r.getMessage()]
+ assert len(warnings) == 1
+ assert len(debugs) == 3
+ assert "Event 'emitter:tick'" in warnings[0].getMessage()