Skip to content

The agent that grows with you

fix(plugins): execution-chain middleware failures are reported once, not per call

teknium10f98d5a1d901
diff --git a/hermes_cli/middleware.py b/hermes_cli/middleware.py
index 897e4afc07b..e5d42c3a2c4 100644
--- a/hermes_cli/middleware.py
+++ b/hermes_cli/middleware.py
@@ -162,7 +162,8 @@ def _run_execution_chain(kind: str, terminal_call: Callable[[Any], Any], **kwarg
from hermes_cli.plugins import get_plugin_manager
payload_key = "request" if "request" in kwargs else "args"
- callbacks = list(get_plugin_manager()._middleware.get(kind, []))
+ manager = get_plugin_manager()
+ callbacks = list(manager._middleware.get(kind, []))
if not callbacks:
return terminal_call(kwargs[payload_key])
@@ -201,9 +202,9 @@ def _run_execution_chain(kind: str, terminal_call: Callable[[Any], Any], **kwarg
except _DownstreamExecutionError as exc:
raise exc.original
except Exception as exc:
- logger.warning(
- "Middleware '%s' callback %s raised: %s",
- kind, getattr(callback, "__name__", repr(callback)), exc)
+ # Runs once per tool/LLM call: a mis-declared callback fails identically every time,
+ # so it goes through the manager's warn-once reporter (#111922).
+ manager._report_hook_failure(kind, callback, call_kwargs, exc, surface="Middleware")
if next_succeeded:
return next_result
if next_called:
diff --git a/tests/hermes_cli/test_plugin_hook_failure_reporting.py b/tests/hermes_cli/test_plugin_hook_failure_reporting.py
index b93dc948460..f4296501384 100644
--- a/tests/hermes_cli/test_plugin_hook_failure_reporting.py
+++ b/tests/hermes_cli/test_plugin_hook_failure_reporting.py
@@ -72,3 +72,29 @@ def test_middleware_failure_warns_once_and_unload_forgets_it(manager, caplog):
warnings = [r for r in caplog.records if r.levelno == logging.WARNING and "on_exec" in r.getMessage()]
assert len(warnings) == 2
assert "Middleware 'agent_tool_execution'" in warnings[0].getMessage()
+
+
+def test_execution_chain_middleware_failure_warns_once(manager, caplog, monkeypatch):
+ """The execution chain (``tool_execution``/``llm_execution``, one frame per tool or LLM call)
+ reports a mis-declared callback through the same warn-once path as hooks — and still skips the
+ frame and runs the tool."""
+ from hermes_cli import middleware as mw
+
+ monkeypatch.setattr("hermes_cli.plugins._plugin_manager", manager)
+
+ def on_exec(tool_data, next_call): # core sends tool_name/args, never tool_data
+ return next_call()
+
+ manager._middleware.setdefault(mw.TOOL_EXECUTION_MIDDLEWARE, []).append(on_exec)
+ with caplog.at_level(logging.DEBUG):
+ results = [
+ mw.run_tool_execution_middleware("read_file", {"path": f"/p{i}"}, lambda args: "ran")
+ for i in range(4)
+ ]
+
+ assert results == ["ran"] * 4
+ warnings = [r for r in caplog.records if r.levelno == logging.WARNING and "on_exec" in r.getMessage()]
+ debugs = [r for r in caplog.records if r.levelno == logging.DEBUG and "on_exec" in r.getMessage()]
+ assert len(warnings) == 1
+ assert len(debugs) == 3
+ assert "Middleware 'tool_execution'" in warnings[0].getMessage()
diff --git a/website/docs/developer-guide/middleware.md b/website/docs/developer-guide/middleware.md
index ad1764f904c..5469af351cf 100644
--- a/website/docs/developer-guide/middleware.md
+++ b/website/docs/developer-guide/middleware.md
@@ -79,7 +79,11 @@ def on_tool_execution(**kwargs):
If multiple plugins register the same execution middleware kind, Hermes runs
them as a nested chain in registration order. Middleware failures are fail-open:
Hermes logs a warning and continues with the next middleware or the base
-runtime path.
+runtime path. A callback that fails the same way on every call (typically a
+signature naming a field the middleware does not send) is reported **once** at
+WARNING — the message lists the fields it does provide — and identical repeats go
+to DEBUG, so a mis-declared middleware cannot flood the log; a plugin reload
+resets the report.
## Execution Order