Repository navigation
fix(agent): surface silent pre-tool-call hook stalls (#32460) #32613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
34d1c87
9b08923
7c03e36
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ | |
| import logging | ||
| import os | ||
| import re | ||
| import time | ||
| from pathlib import Path | ||
| from typing import Any, Dict, List, Optional | ||
|
|
||
|
|
@@ -36,6 +37,63 @@ | |
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| # Pre-tool dispatch latency threshold (#32460). The agent loop calls | ||
| # ``get_pre_tool_call_block_message()`` for every tool call before the | ||
| # tool starts measuring its own duration, so a slow plugin / shell hook | ||
| # here produces a silent wall-clock gap between an LLM response and the | ||
| # resulting tool execution. When the call exceeds this threshold, emit | ||
| # a single WARNING that names the tool — the operator no longer has to | ||
| # guess whether the agent was idle or one of their hooks was hung. | ||
| _PRE_TOOL_DISPATCH_SLOW_THRESHOLD_SECONDS = 2.0 | ||
|
|
||
|
|
||
| def pre_tool_call_block_message_with_latency( | ||
| tool_name: str, | ||
| function_args: Optional[Dict[str, Any]], | ||
| *, | ||
| task_id: str = "", | ||
| session_id: str = "", | ||
| tool_call_id: str = "", | ||
| ) -> Optional[str]: | ||
| """Call ``get_pre_tool_call_block_message`` and warn on slow dispatch. | ||
|
|
||
| Equivalent to invoking | ||
| :func:`hermes_cli.plugins.get_pre_tool_call_block_message` directly, | ||
| but wraps the call in a ``time.monotonic`` window so dispatch latency | ||
| above ``_PRE_TOOL_DISPATCH_SLOW_THRESHOLD_SECONDS`` is reported via a | ||
| WARNING that names the offending tool. | ||
|
|
||
| The pre-tool dispatch path is silent in the agent log under normal | ||
| conditions, so a hook or plugin that blocks here for tens of seconds | ||
| appears to the user as the agent "freezing" between an LLM response | ||
| and the next tool execution — exactly the symptom reported in | ||
| #32460. Surfacing the latency immediately lets the operator chase | ||
| down the responsible hook / plugin without having to enable debug | ||
| logging or instrument by hand. | ||
| """ | ||
| from hermes_cli.plugins import get_pre_tool_call_block_message | ||
|
|
||
| t0 = time.monotonic() | ||
| try: | ||
| return get_pre_tool_call_block_message( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Current main makes |
||
| tool_name, | ||
| function_args, | ||
| task_id=task_id, | ||
| session_id=session_id, | ||
| tool_call_id=tool_call_id, | ||
| ) | ||
| finally: | ||
| elapsed = time.monotonic() - t0 | ||
| if elapsed >= _PRE_TOOL_DISPATCH_SLOW_THRESHOLD_SECONDS: | ||
| logger.warning( | ||
| "pre_tool_call dispatch took %.1fs for tool %r — a slow " | ||
| "shell hook or plugin is stalling the agent loop. " | ||
| "Check ~/.hermes/config.yaml 'hooks.pre_tool_call' " | ||
| "entries and recently installed plugins.", | ||
| elapsed, | ||
| tool_name, | ||
| ) | ||
|
|
||
| # Tools that must never run concurrently (interactive / user-facing). | ||
| # When any of these appear in a batch, we fall back to sequential execution. | ||
| _NEVER_PARALLEL_TOOLS = frozenset({"clarify"}) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_spawn()is shared by every shell-hook event, includingpre_llm_call, so this wording is inaccurate outsidepre_tool_call. Either limit this watchdog to pre-tool hooks or describe the specific event that is currently blocked.