The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(hooks): block failed hooks without directives
fangliquanflq65f3d08b1783
diff --git a/agent/shell_hooks.py b/agent/shell_hooks.pyindex 70fae831091..ca56bcc3ff0 100644--- a/agent/shell_hooks.py+++ b/agent/shell_hooks.py@@ -381,10 +381,28 @@ def _fail_closed_block(spec: ShellHookSpec, reason: str) -> Dict[str, Any]:return {"action": "block", "message": f"hook {spec.command} failed closed: {reason}"}-def _evaluate_result(spec: ShellHookSpec, r: Dict[str, Any]) -> Optional[Dict[str, Any]]:- """``_spawn`` result → hook contribution (live callback and ``run_once``). Spawn error/timeout fail- open unless fail_closed; exit 2 on a blocking event blocks (message: stdout JSON, then stderr, then- default); other non-zero exits warn then parse stdout; unparseable stdout on a fail_closed hook blocks."""+def _evaluate_result(+ spec: ShellHookSpec, r: Dict[str, Any],+) -> Optional[Dict[str, Any]]:+ """Turn a :func:`_spawn` diagnostic dict into the hook's contribution.++ Single place that encodes the failure semantics:++ * spawn error / timeout — fail open (log + ``None``) unless the spec+ is ``fail_closed`` on a blocking-capable event, in which case a+ canonical block shape is returned;+ * exit code 2 on a blocking-capable event — block, with the message+ taken from stdout block JSON, then stderr, then a default+ (Claude-Code / Cursor compatible);+ * other non-zero exits — warn, then parse stdout normally; a+ ``fail_closed`` hook blocks if no directive was produced;+ * non-JSON / unparseable stdout on a ``fail_closed`` blocking hook —+ block instead of silently contributing nothing.++ Shared by the live callback path (:func:`_make_callback`) and the CLI+ test helper (:func:`run_once`) so ``hermes hooks test`` reflects+ production behaviour exactly.+ """blocking_event = spec.event in _BLOCKING_EVENTSfail_closed = spec.fail_closed and blocking_eventif r["error"]:@@ -409,8 +427,15 @@ def _evaluate_result(spec: ShellHookSpec, r: Dict[str, Any]) -> Optional[Dict[str["returncode"], spec.event, spec.command, stderr[:_STDERR_MESSAGE_LIMIT])stdout = (r["stdout"] or "").strip()parsed = _parse_response(spec.event, stdout)+ if parsed is None and fail_closed and r["returncode"] != 0:+ return _fail_closed_block(+ spec, f"hook exited {r['returncode']} with no directive",+ )+if parsed is None and fail_closed and stdout and not _is_json_object(stdout):- # A fail-closed gate must not silently allow on garbage stdout (e.g. a stack trace).+ # The hook produced output we could not turn into a directive.+ # A fail-closed gate must not silently allow the action on+ # garbage output (e.g. a stack trace on stdout).return _fail_closed_block(spec, "unparseable stdout (expected a JSON object)")return parseddiff --git a/tests/agent/test_shell_hooks.py b/tests/agent/test_shell_hooks.pyindex bcb76fd6825..c12f559711c 100644--- a/tests/agent/test_shell_hooks.py+++ b/tests/agent/test_shell_hooks.py@@ -553,6 +553,14 @@ class TestEvaluateResult:assert r["action"] == "block"assert "unparseable stdout" in r["message"]+ def test_nonzero_exit_without_directive_fail_closed_blocks(self):+ r = shell_hooks._evaluate_result(+ self._spec(fail_closed=True),+ _spawn_result(returncode=1, stderr="Traceback: hook crashed"),+ )+ assert r["action"] == "block"+ assert "hook exited 1 with no directive" in r["message"]+def test_unparseable_stdout_fails_open_by_default(self):r = shell_hooks._evaluate_result(self._spec(),