Description
When using context-mode with Gemini CLI (Node v24+), the AfterTool hook frequently fails with a Hook(s) failed for event AfterTool warning. This happens because the hook tries to read from stdin without a timeout, and processes potentially large tool responses (multi-MB) within the strict <20ms budget of the CLI.
Symptoms
- Constant
AfterTool failed warnings in Gemini CLI UI.
- Hook processes hanging or being killed by the system.
- No clear error in
aftertool-debug.log because the process is terminated mid-execution.
Root Cause
- Stdin Hang:
readStdin() lacks a timeout guard.
- Performance Budget: Extracting events from very large tool outputs (e.g., massive
run_shell_command output) exceeds the time limit.
Proposed Solution (Applied locally)
I have applied a patch to hooks/gemini-cli/aftertool.mjs with:
- A 10s
setTimeout guard on stdin to prevent permanent hangs.
- A size check: if
tool_response > 1MB, skip event extraction to preserve CLI responsiveness.
// Stdin timeout guard
const stdinTimeout = setTimeout(() => process.exit(0), 10000);
// ... readStdin ...
clearTimeout(stdinTimeout);
// Response size guard
const responseSize = typeof input.tool_response === 'string' ? input.tool_response.length : 0;
if (responseSize > 1024 * 1024) {
process.exit(0);
}
Environment
- OS: Darwin (macOS)
- Node: v24.4.1
- Gemini CLI: Latest
Description
When using
context-modewith Gemini CLI (Node v24+), theAfterToolhook frequently fails with aHook(s) failed for event AfterToolwarning. This happens because the hook tries to read fromstdinwithout a timeout, and processes potentially large tool responses (multi-MB) within the strict <20ms budget of the CLI.Symptoms
AfterTool failedwarnings in Gemini CLI UI.aftertool-debug.logbecause the process is terminated mid-execution.Root Cause
readStdin()lacks a timeout guard.run_shell_commandoutput) exceeds the time limit.Proposed Solution (Applied locally)
I have applied a patch to
hooks/gemini-cli/aftertool.mjswith:setTimeoutguard onstdinto prevent permanent hangs.tool_response> 1MB, skip event extraction to preserve CLI responsiveness.Environment