The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
test(hooks): cover crashed fail-closed process
fangliquanflq942dd4fb0fa7
diff --git a/tests/agent/test_shell_hooks.py b/tests/agent/test_shell_hooks.pyindex c12f559711c..62e9c75b389 100644--- a/tests/agent/test_shell_hooks.py+++ b/tests/agent/test_shell_hooks.py@@ -9,6 +9,7 @@ covered in ``test_shell_hooks_consent.py``.from __future__ import annotationsimport json+import sysfrom pathlib import Pathimport pytest@@ -595,6 +596,20 @@ class TestEvaluateResult:class TestFailSemanticsEndToEnd:+ def test_fail_closed_crashed_process_blocks(self, tmp_path):+ script = _write_script(+ tmp_path, "crash.py", 'raise RuntimeError("hook crashed")\n',+ )+ spec = shell_hooks.ShellHookSpec(+ event="pre_tool_call",+ command=f'"{sys.executable}" "{script}"',+ fail_closed=True,+ )+ cb = shell_hooks._make_callback(spec)+ result = cb(tool_name="terminal", args={"command": "rm -rf /"})+ assert result is not None and result["action"] == "block"+ assert "hook exited 1 with no directive" in result["message"]+@pytest.mark.platforms("linux")def test_exit_2_script_blocks(self, tmp_path):script = _write_script(diff --git a/website/docs/user-guide/features/hooks.md b/website/docs/user-guide/features/hooks.mdindex 59975d14edf..0f48e8cb85d 100644--- a/website/docs/user-guide/features/hooks.md+++ b/website/docs/user-guide/features/hooks.md@@ -1767,7 +1767,7 @@ profile's `HERMES_HOME`. `tool_name` and `tool_input` are `null` for non-tool ev// Silent no-op — any empty / non-matching output is fine:```-Malformed JSON, non-zero exit codes, and timeouts log a warning but never abort the agent loop.+Except for `pre_tool_call` exit code 2 described below, malformed JSON, ordinary non-zero exit codes, and timeouts fail open by default: they log a warning but do not abort the agent loop. A `fail_closed` hook changes the behavior as described below.### Exit code 2 = block (Claude Code / Cursor compatible)@@ -1808,6 +1808,7 @@ With `fail_closed: true`, each of these now **blocks** the tool call with `hook|---------|--------------------|--------------------|| Command not found / not executable | warn, proceed | **block** || Timeout | warn, proceed | **block** |+| Non-zero exit with no recognized directive | warn, proceed | **block** || Non-JSON stdout (e.g. a stack trace) | warn, proceed | **block** || Clean exit, valid no-op JSON (`{}`) | proceed | proceed |