Skip to content

The agent that grows with you

fix(plugins): only mark a timed-out hook worker abandoned while it still holds its token

kshitijk4poor73f808e47f9d
diff --git a/hermes_cli/plugins.py b/hermes_cli/plugins.py
index 699ef70f5e6..a979c394918 100644
--- a/hermes_cli/plugins.py
+++ b/hermes_cli/plugins.py
@@ -1162,8 +1162,8 @@ class PluginManager(PluginLoaderMixin, PluginDispatchMixin, PluginLedgerMixin):
self._event_queue: queue.Queue[Any] = queue.Queue(maxsize=_EVENT_PENDING_CAP)
self._event_worker: Optional[threading.Thread] = None
self._emit_depth = threading.local()
- # In-flight / recently-timed-out hook callbacks keyed by (hook_name, id(cb)) so a stuck
- # policy hook cannot spawn a new abandoned thread on every fire.
+ # In-flight / recently-timed-out hook callbacks keyed by (hook_name, id(cb), call_identity)
+ # so a stuck policy hook cannot spawn a new abandoned thread on every fire.
self._hook_running_callbacks: Dict[tuple, object] = {}
self._hook_abandoned: Dict[tuple, set] = {}
self._hook_timeout_suppressed_until: Dict[tuple, float] = {}
diff --git a/hermes_cli/plugins_dispatch.py b/hermes_cli/plugins_dispatch.py
index 996e1b77b44..d10b449893f 100644
--- a/hermes_cli/plugins_dispatch.py
+++ b/hermes_cli/plugins_dispatch.py
@@ -285,7 +285,11 @@ class PluginDispatchMixin:
# See #6622.
self._hook_timeout_suppressed_until[suppression_key] = (
time.monotonic() + self._hook_timeout_suppression_seconds)
- self._hook_abandoned.setdefault(suppression_key, set()).add(gate_key)
+ # The worker may have finished (and released its token) between the wait
+ # expiring and this lock; recording it as abandoned then would block the
+ # callback for that call id until reload with no thread behind it.
+ if self._hook_running_callbacks.get(gate_key) is token:
+ self._hook_abandoned.setdefault(suppression_key, set()).add(gate_key)
logger.warning(
"Hook '%s' callback %s timed out after %gs — skipping", hook_name, callback_name, timeout)
return _HOOK_SKIPPED
diff --git a/tests/hermes_cli/test_plugins.py b/tests/hermes_cli/test_plugins.py
index e0dfb03b78c..52cc99a1afb 100644
--- a/tests/hermes_cli/test_plugins.py
+++ b/tests/hermes_cli/test_plugins.py
@@ -1279,6 +1279,43 @@ class TestForceReloadSymmetry:
assert len(starts) == 1
hold.set()
+ def test_worker_finishing_at_timeout_does_not_leave_phantom_abandoned_entry(self, monkeypatch):
+ """If the worker completes between the wait expiring and the timeout branch taking the
+ lock, it has already released its token; recording it as abandoned anyway would block
+ every later call id for that callback until reload. A fresh call must still run."""
+ import hermes_cli.plugins_dispatch as dispatch
+
+ class _RacingEvent(threading.Event):
+ def wait(self, timeout=None):
+ super().wait(timeout=10.0) # the worker really finishes first...
+ return False # ...but the caller observes a timeout
+
+ class _Threading:
+ Event = _RacingEvent
+
+ def __getattr__(self, name):
+ return getattr(threading, name)
+
+ monkeypatch.setattr(dispatch, "threading", _Threading())
+ monkeypatch.setattr(
+ "hermes_cli.plugins._resolve_hook_callback_timeout", lambda: 0.1
+ )
+ starts = []
+
+ def quick(**_kwargs):
+ starts.append(1)
+ return "done"
+
+ mgr = PluginManager()
+ mgr._hook_timeout_suppression_seconds = 0.0 # isolate the gate from suppression
+ mgr._hooks["post_tool_call"] = [quick]
+
+ assert mgr.invoke_hook("post_tool_call", tool_name="read_file", tool_call_id="call-a") == []
+ assert mgr._hook_abandoned == {}
+ mgr.invoke_hook("post_tool_call", tool_name="read_file", tool_call_id="call-b")
+
+ assert len(starts) == 2
+
def test_pre_tool_call_timeout_fail_closed(self, monkeypatch):
"""Timed-out pre_tool_call must return a block directive, not allow."""
import time
diff --git a/tools/terminal_tool_result.py b/tools/terminal_tool_result.py
index b7917d303de..681d1fb0c19 100644
--- a/tools/terminal_tool_result.py
+++ b/tools/terminal_tool_result.py
@@ -139,12 +139,11 @@ def _apply_output_transform_hook(command, output, returncode, task_id, env_type)
with _quiet("transform_terminal_output hook"):
from hermes_cli.lifecycle import invoke_hook
from tools.approval_context import _approval_tool_call_id
- kwargs = dict(command=command, output=output, returncode=returncode,
- task_id=task_id or "", env_type=env_type)
- # Concurrent terminal calls in one turn must gate per call, not collapse into one.
- if _approval_tool_call_id.get():
- kwargs["tool_call_id"] = _approval_tool_call_id.get()
- results = invoke_hook("transform_terminal_output", **kwargs)
+ # Concurrent terminal calls in one turn must gate per call, not collapse into one;
+ # an empty id is treated as "no identity" by the hook gate.
+ results = invoke_hook("transform_terminal_output", command=command, output=output,
+ returncode=returncode, task_id=task_id or "", env_type=env_type,
+ tool_call_id=_approval_tool_call_id.get())
output = next((r for r in results if isinstance(r, str)), output)
return output