Answer stability round 1: 1 🔴, 0 🟡 (0 misread); read a loop condition as a command, and skip the loop check for a Monitor call, whose timeout_ms ends it

This commit is contained in:
2026-11-18 20:26:21 +01:00
parent ef552931df
commit fa14d1a4ac
4 changed files with 47 additions and 33 deletions
+5 -5
View File
@@ -4,7 +4,7 @@ usage: rule-hook (a PreToolUse hook: reads the call as JSON on stdin)
rule-hook overrides (prints this chunk's overrides)
settings.json runs it before each Agent, Bash, Edit, Grep, Monitor, MultiEdit, Read, Skill and Write call, in every session and
subagent. It reads a Monitor call's command as a Bash command; Claude Code ignores a hook's rewrite of a Monitor command, so there it can only block. The rules:
subagent. The rules:
{rules}
@@ -64,7 +64,7 @@ from rule_hook.todo_check import todo_breaches
from rule_hook.transcript_read import transcript_breaches
# The tools whose `command` is a shell command.
# The tools whose `command` is a shell command. Claude Code ignores a hook's rewrite of a Monitor command, but rule-hook only blocks, so it reads the two alike; its endless-wait loop check is the exception, since a Monitor call ends at its timeout_ms.
COMMAND_TOOLS = ("Bash", "Monitor")
@@ -99,7 +99,7 @@ def breaches_of(call: Call, home: str, environ: typing.Mapping[str, str]) -> tup
return text(call.given, "args"), text(call.given, "skill"), skill_breaches(call, environ, home)
if call.tool in COMMAND_TOOLS:
command = text(call.given, "command")
return command, command.split("\n", 1)[0], bash_breaches(command, call.cwd, home) + read_breaches(call, home) + env_file_breaches(call, home)
return command, command.split("\n", 1)[0], bash_breaches(command, call.cwd, home, call.tool == "Monitor") + read_breaches(call, home) + env_file_breaches(call, home)
if call.tool == "Read":
return "", text(call.given, "file_path"), read_breaches(call, home) + env_file_breaches(call, home) + transcript_breaches(call, home)
if call.tool == "Grep":
@@ -130,13 +130,13 @@ def answer(checked: Checked, what: str, now: float) -> int:
return 2 if unloggable else 0
def bash_breaches(command: str, cwd: str, home: str) -> list[Breach]:
def bash_breaches(command: str, cwd: str, home: str, monitor: bool = False) -> list[Breach]:
"""The breaches of the command's own words, then of each commit it makes, then of the files those commits may take."""
simples = simple_commands(command, cwd, home)
found = commits(simples, home)
in_commits = [breach for commit in found for breach in commit_breaches(commit, pathlib.Path(home))]
in_files = [breach for directory in dict.fromkeys(commit.directory for commit in found) for breach in committed_file_breaches(directory)]
return command_breaches(command, simples) + in_commits + in_files
return command_breaches(command, simples, monitor) + in_commits + in_files
def commit_breaches(commit: CommitCall, home: pathlib.Path) -> list[Breach]: