Skip to content

The agent that grows with you

fix(plugins): dedupe middleware failures too, and let unload forget reported hook failures

teknium101d73210c2db
diff --git a/hermes_cli/plugins_dispatch.py b/hermes_cli/plugins_dispatch.py
index bb456f2a815..73406eea4c4 100644
--- a/hermes_cli/plugins_dispatch.py
+++ b/hermes_cli/plugins_dispatch.py
@@ -219,23 +219,29 @@ class PluginDispatchMixin:
self._report_hook_failure(hook_name, cb, kwargs, exc)
return results
- def _report_hook_failure(self, hook_name: str, cb: Callable, kwargs: Dict[str, Any], exc: Exception) -> None:
+ def _report_hook_failure(
+ self, hook_name: str, cb: Callable, kwargs: Dict[str, Any], exc: Exception, *, surface: str = "Hook"
+ ) -> None:
"""One WARNING per distinct (hook, callback, error); identical repeats at DEBUG.
A callback whose signature names a parameter the hook never sends (``tool_data`` instead
of ``tool_name``/``args``) fails identically on every tool call — ~1700 WARNING lines an
hour that bury real signals (#111922). The first report names the fields the hook does
- provide so the plugin author can fix the signature.
+ provide so the plugin author can fix the signature. The key names the callback by
+ module/qualname (not ``id()``, which CPython recycles across plugin reloads) and
+ truncates the message so a hook that embeds tool args in its error cannot grow the set
+ per call; the set is cleared on unload alongside the timeout-suppression map.
"""
callback_name = getattr(cb, "__name__", repr(cb))
- key = (hook_name, id(cb), repr(exc))
+ key = (hook_name, getattr(cb, "__module__", ""), getattr(cb, "__qualname__", callback_name),
+ type(exc).__name__, str(exc)[:200])
if key in self._hook_failures_reported:
- logger.debug("Hook '%s' callback %s raised again: %s", hook_name, callback_name, exc)
+ logger.debug("%s '%s' callback %s raised again: %s", surface, hook_name, callback_name, exc)
return
self._hook_failures_reported.add(key)
logger.warning(
- "Hook '%s' callback %s raised: %s (hook provides: %s; identical failures are logged at DEBUG from now on)",
- hook_name, callback_name, exc, ", ".join(sorted(kwargs)) or "no fields")
+ "%s '%s' callback %s raised: %s (%s provides: %s; identical failures are logged at DEBUG from now on)",
+ surface, hook_name, callback_name, exc, surface.lower(), ", ".join(sorted(kwargs)) or "no fields")
def _run_hook_callback_bounded(
self, hook_name: str, cb: Callable, kwargs: Dict[str, Any], timeout: float
@@ -519,6 +525,6 @@ class PluginDispatchMixin:
if ret is not None:
results.append(ret)
except Exception as exc:
- logger.warning(
- "Middleware '%s' callback %s raised: %s", kind, getattr(cb, "__name__", repr(cb)), exc)
+ # Runs once per tool call like a hook, so a mis-declared callback floods identically.
+ self._report_hook_failure(kind, cb, kwargs, exc, surface="Middleware")
return results
diff --git a/hermes_cli/plugins_ledger.py b/hermes_cli/plugins_ledger.py
index f259695b79d..e1603683e39 100644
--- a/hermes_cli/plugins_ledger.py
+++ b/hermes_cli/plugins_ledger.py
@@ -299,4 +299,5 @@ class PluginLedgerMixin:
self._hook_running_callbacks.clear()
self._hook_abandoned.clear()
self._hook_timeout_suppressed_until.clear()
+ self._hook_failures_reported.clear()
self._discovered = False
diff --git a/tests/hermes_cli/test_plugin_hook_failure_reporting.py b/tests/hermes_cli/test_plugin_hook_failure_reporting.py
index 768611eead7..b93dc948460 100644
--- a/tests/hermes_cli/test_plugin_hook_failure_reporting.py
+++ b/tests/hermes_cli/test_plugin_hook_failure_reporting.py
@@ -51,3 +51,24 @@ def test_distinct_hook_failures_each_warn(manager, caplog):
warnings = [r for r in caplog.records if r.levelno == logging.WARNING and "flaky" in r.getMessage()]
assert len(warnings) == 3
+
+
+def test_middleware_failure_warns_once_and_unload_forgets_it(manager, caplog):
+ """Middleware runs once per tool call like a hook, so it dedupes the same way; a plugin
+ reload (unload-all) forgets the reported failures so the reloaded callback's first failure
+ warns again."""
+ def on_exec(tool_data): # core sends tool_name/args, never tool_data
+ return None
+
+ manager._middleware.setdefault("agent_tool_execution", []).append(on_exec)
+ with caplog.at_level(logging.DEBUG, logger="hermes_cli.plugins"):
+ for i in range(3):
+ manager.invoke_middleware("agent_tool_execution", tool_name="x", args={"path": f"/p{i}"})
+ manager._reset_after_unload_all([])
+ assert not manager._hook_failures_reported
+ manager._middleware.setdefault("agent_tool_execution", []).append(on_exec)
+ manager.invoke_middleware("agent_tool_execution", tool_name="x", args={})
+
+ 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()