diff --git a/bin/rule_hook/endless_wait.py b/bin/rule_hook/endless_wait.py index bdca2335..afe5d1f2 100644 --- a/bin/rule_hook/endless_wait.py +++ b/bin/rule_hook/endless_wait.py @@ -1,7 +1,7 @@ """The waits that cannot end in a Bash or Monitor command, for the `endless-wait` rule: a loop that sleeps with no time or count cap, and a `pgrep -f` or `pkill -f` whose pattern matches the command line that holds it. -Both kinds hang the session that waits on them: `until [ -s f ] && ! pgrep -f "chunk-pr merg"; do sleep 15; done` matches its own shell, so its condition never holds. -Shell forms it does not read: a loop inside a function, a variable or a script file, and a count or deadline it does not recognise (it refuses the loop, and an override waives that). +A sleeping loop with no cap never ends. A `pgrep -f` or `pkill -f` finds the shell that runs it when its pattern matches that shell's command line, so a loop that waits for the process to disappear, as in `until [ -s f ] && ! pgrep -f "chunk-pr merg"; do sleep 15; done`, never ends either. A session that waits on one hangs. +It reads the command's own text. A loop in a script file or in a variable's value isn't read, and a counter or deadline it doesn't recognise leaves the loop refused, which an override waives. """ import itertools import re @@ -9,7 +9,9 @@ import re from command_lines.shell_command import launched, segments, Simple # What stands before a command in a segment without being part of it, as in `do sleep 15` or `! pgrep -f x`. -KEYWORDS = frozenset({"!", "do", "else", "then", "{"}) +KEYWORDS = frozenset({"!", "do", "elif", "else", "if", "then", "{"}) +# The keywords of a loop whose condition is a command, as in `while sleep 30`. +CONDITIONS = frozenset({"until", "while"}) LOOPS = frozenset({"for", "until", "while"}) SHELLS = frozenset({"bash", "dash", "sh", "zsh"}) # A word with which a loop counts its rounds: `[ "$n" -lt 40 ]`. @@ -25,21 +27,28 @@ HOW = "use `~/.claude/bin/wait-for [] --timeout `" def endless_waits(command: str, simples: list[Simple], capped: bool = False) -> list[tuple[str, str]]: - """Each endless wait the command starts, as its detail and the directory it runs in. capped is whether a `timeout` ends every loop in the simple commands, as it does inside `timeout 60 bash -c '…'`.""" + """Each endless wait the command starts, as its detail and the directory it runs in. capped is whether something ends every loop in the simple commands: a Monitor call's timeout_ms, or a `timeout` around the shell, as in `timeout 60 bash -c '…'`.""" found = [] if capped else [(f"a loop that sleeps, `{' '.join(loop[0].words)[:60]}`, has neither a time limit nor a count cap; {HOW}, or give the loop a deadline compared with `date` or $SECONDS, or a counter, as in `for i in $(seq 1 40)`", loop[0].directory) for loop in loops(simples) if endless(loop, command)] for simple in simples: - if (pattern := self_match(simple.words, command)) is not None: - found.append((f"`{pattern}` is matched by the command line that holds this `pgrep -f` or `pkill -f`, so it finds its own shell; {HOW}, a `pgrep -x` on the process name, or a pattern the command doesn't contain, such as `[c]hunk` for `chunk`", simple.directory)) + if (found_match := self_match(simple.words, command)) is not None: + tool, pattern = found_match + instead = "a `pgrep -x` on the process name" if tool == "pgrep" else "a `pkill -x` on the process name" + found.append((f"`{pattern}` matches the command line that holds this `{tool} -f`, so `{tool}` finds the shell that runs the command; {HOW + ', ' if tool == 'pgrep' else 'use '}{instead}, or a pattern the command doesn't contain, such as `[c]hunk` for `chunk`", simple.directory)) if (script := shell_script(simple.words)) is not None: found += endless_waits(command, [Simple(words, simple.directory) for words in segments(script)], capped or launched(simple.words, {"timeout"}) is not None) return found def core(words: list[str]) -> list[str]: - """The words of a segment without the keywords before its command.""" + """The words of a segment without the keywords before its command or its loop keyword.""" return list(itertools.dropwhile(KEYWORDS.__contains__, words)) +def invoked(words: list[str]) -> list[str]: + """The command a segment runs, which a loop's condition may be: `until ! pgrep -f x` runs pgrep.""" + return list(itertools.dropwhile((KEYWORDS | CONDITIONS).__contains__, words)) + + def loops(simples: list[Simple]) -> list[list[Simple]]: """Each loop as the segments from its keyword to its `done`, those of a loop nested in it included.""" open_loops: list[list[Simple]] = [] @@ -57,17 +66,18 @@ def loops(simples: list[Simple]) -> list[list[Simple]]: def endless(loop: list[Simple], command: str) -> bool: """Whether the loop sleeps and nothing bounds it.""" - if not any(launched(simple.words, {"sleep"}) for simple in loop): + if not any(launched(invoked(simple.words), {"sleep"}) for simple in loop): return False head = loop[0].words if head[0] == "for": - # A list ends the loop; an arithmetic for, whose words end at the lexer's split at `((`, does only where it has a condition. + # A list ends the loop. An arithmetic `for` ends it only with a condition; its words stop at the lexer's split at `((`. return head == ["for"] and FOREVER.search(command) is not None # `while ((i < 10))` splits the same way, so a bare keyword reads as a count. if len(head) == 1: return False + # A counter or a clock is judged by its words alone: a stricter test would refuse the bounded loops people write, and none can tell that the count advances or the clock is compared. So `-ge` on a file's line count, or a `date` that only logs, passes. words = {word for simple in loop for word in simple.words} - return not (COUNTERS & words or any("SECONDS" in word for word in words) or any(launched(simple.words, {"date"}) for simple in loop)) + return not (COUNTERS & words or any("SECONDS" in word for word in words) or any(launched(invoked(simple.words), {"date"}) for simple in loop)) def shell_script(words: list[str]) -> str | None: @@ -79,9 +89,9 @@ def shell_script(words: list[str]) -> str | None: return shell.arguments[flags[0] + 1] if flags and flags[0] + 1 < len(shell.arguments) else None -def self_match(words: list[str], command: str) -> str | None: - """The pattern of a `pgrep -f` or `pkill -f` that the command's own text matches, the shell's command line being that text; None where there is none.""" - launch = launched(core(words), {"pgrep", "pkill"}) +def self_match(words: list[str], command: str) -> tuple[str, str] | None: + """The tool and the pattern of a `pgrep -f` or `pkill -f` whose pattern matches the command's own text, which is the shell's command line; None where there is none.""" + launch = launched(invoked(words), {"pgrep", "pkill"}) if launch is None: return None full, patterns, rest = False, [], iter(launch.arguments) @@ -100,7 +110,7 @@ def self_match(words: list[str], command: str) -> str | None: break elif not word.startswith("-") or len(word) == 1: patterns.append(word) - return next((pattern for pattern in patterns if full and matches(pattern, command)), None) + return next(((launch.tool, pattern) for pattern in patterns if full and matches(pattern, command)), None) def matches(pattern: str, text: str) -> bool: diff --git a/bin/rule_hook/main.py b/bin/rule_hook/main.py index 27dec5e4..24d75366 100644 --- a/bin/rule_hook/main.py +++ b/bin/rule_hook/main.py @@ -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]: diff --git a/bin/rule_hook/rules.py b/bin/rule_hook/rules.py index 44a48b27..4890ac8f 100644 --- a/bin/rule_hook/rules.py +++ b/bin/rule_hook/rules.py @@ -5,8 +5,8 @@ import typing from review_rules.round_titles import CAP, COUNTS, EXAMPLE, lacks_counts, OPENING, RELEASE_REVIEW_CAP from git_cli.git_command import CommitCall -from run_heavy.capped_docker import finds_this_docker from rule_hook.endless_wait import endless_waits +from run_heavy.capped_docker import finds_this_docker from rule_hook.heavy_command import heavy from command_lines.shell_command import launched, LAUNCHERS, Simple from review_rules.review_folder import REPORTS @@ -42,8 +42,8 @@ RULES: dict[RuleName, Rule] = { "docker run, docker create, docker container run and docker container create, whether direct or through a launcher such as nice or timeout. One under run-heavy passes, as run-heavy's docker adds the limits on Linux, unless it names docker by its path, or changes or clears PATH, as env -i, sudo or doas do. A container a script or docker compose starts is not checked", ), "endless-wait": Rule( - "a wait ends: a loop that sleeps has a time limit or a count cap, and a `pgrep -f` or `pkill -f` has a pattern its own command line doesn't match, since such a pattern finds the shell that runs the command and the wait never ends, and the session that waits on it hangs; wait with `~/.claude/bin/wait-for [] --timeout `, or find a process by name with `pgrep -x`", - "a Bash or Monitor command with an `until`, `while` or `for ((;;))` loop that runs `sleep` and has no `date` or $SECONDS comparison and no counter compared with -lt, -le, -gt or -ge, or a `pgrep -f` or `pkill -f` whose pattern, read as a regular expression, matches the command's text; the same inside the script of a `bash -c`, unless a `timeout` runs the shell. A loop over a list, such as `for i in $(seq 1 40)`, is a count and passes", + "a wait ends: a loop that sleeps has a time limit or a count cap, and a `pgrep -f` or `pkill -f` has a pattern that its own command line doesn't match, since a pattern that line matches finds the shell that runs the command, and a loop that waits for that process to end never does; wait with `~/.claude/bin/wait-for [] --timeout `, or find a process by name with `pgrep -x`", + "a Bash command with an `until`, `while` or `for ((;;))` loop that runs `sleep` and has no `date` or $SECONDS comparison and no counter compared with -lt, -le, -gt or -ge, so a loop over a list, such as `for i in $(seq 1 40)`, passes; a Bash or Monitor command with a `pgrep -f` or `pkill -f` whose pattern, read as a regular expression, matches the command's text; the same inside the script of a shell's -c, where a `timeout` around the shell lifts only the loop check. A Monitor call's `timeout_ms` ends its loops, so only the `pgrep -f` and `pkill -f` check applies to it", ), # A marker would let the credentials into the transcript all the same, so no marker waives it. "env-file-read": Rule( @@ -163,12 +163,13 @@ def check(breaches: list[Breach], written: str, markdown: bool, bash: bool) -> C return Checked(messages, bool(blocking), [(breach, grant[breach.rule]) for breach, grant in zip(breaches, grants) if breach.rule in grant]) -def command_breaches(command: str, simples: list[Simple]) -> list[Breach]: +def command_breaches(command: str, simples: list[Simple], monitor: bool = False) -> list[Breach]: + """The breaches of the command's own words; monitor is whether a Monitor call runs it, which ends every loop at its timeout_ms.""" breaches = [] publishing = [simple for simple in simples if posixpath.basename(simple.words[0]) in ("chunk-pr", "gh", "git", "tea")] if publishing and ATTRIBUTION.search(command): breaches.append(Breach("no-ai-attribution", "the command carries an attribution", publishing[0].directory)) - breaches += [Breach("endless-wait", detail, directory) for detail, directory in endless_waits(command, simples)] + breaches += [Breach("endless-wait", detail, directory) for detail, directory in endless_waits(command, simples, monitor)] for simple in simples: if heavy(simple.words): breaches.append(Breach("run-heavy", f"run `{' '.join(simple.words)[:80]}` under `~/.claude/bin/run-heavy`, in the background", simple.directory)) diff --git a/bin/rule_hook/test_rule_hook.py b/bin/rule_hook/test_rule_hook.py index fd47fb90..e25f23de 100644 --- a/bin/rule_hook/test_rule_hook.py +++ b/bin/rule_hook/test_rule_hook.py @@ -145,22 +145,24 @@ class CommandBreaches(unittest.TestCase): self.assertEqual(checked(command), []) def test_a_loop_that_sleeps_without_a_time_or_count_cap_breaks_endless_wait(self): - for command in ["until [ -s /tmp/x.log ]; do sleep 15; done", "while true; do sleep 1; done", "while ! curl -sf localhost:80; do sleep 2; done", "until [ -s /tmp/merge.log ] && ! pgrep -x chunk-pr; do sleep 15; done", "for ((;;)); do sleep 1; done", "echo a && until test -f f; do sleep 1; done", "bash -c 'until [ -s f ]; do sleep 1; done'", "if x; then while true; do sleep 1; done; fi", "for i in 1 2; do until [ -s f ]; do sleep 1; done; done", "tail -f x | while read l; do sleep 1; done", "until timeout 5 curl -sf x; do sleep 1; done", "until [ -s f ]; do echo seconds; sleep 1; done", "until [ -s f ]\ndo\n sleep 1\ndone", "bash -x -c 'until [ -s f ]; do sleep 1; done'"]: + for command in ["until [ -s /tmp/x.log ]; do sleep 15; done", "while true; do sleep 1; done", "while ! curl -sf localhost:80; do sleep 2; done", "until [ -s /tmp/merge.log ] && ! pgrep -x chunk-pr; do sleep 15; done", "for ((;;)); do sleep 1; done", "echo a && until test -f f; do sleep 1; done", "bash -c 'until [ -s f ]; do sleep 1; done'", "if x; then while true; do sleep 1; done; fi", "for i in 1 2; do until [ -s f ]; do sleep 1; done; done", "tail -f x | while read l; do sleep 1; done", "until timeout 5 curl -sf x; do sleep 1; done", "until [ -s f ]; do echo seconds; sleep 1; done", "until [ -s f ]\ndo\n sleep 1\ndone", "bash -x -c 'until [ -s f ]; do sleep 1; done'", "while sleep 30; do echo x; done", "until ! pgrep -x chunk-pr; do sleep 5; done"]: with self.subTest(command): self.assertEqual(checked(command), ["endless-wait"]) def test_a_loop_with_a_deadline_a_count_or_no_sleep_passes_endless_wait(self): - for command in ["until [ -s f ]; do sleep 15; [ $((n+=1)) -ge 40 ] && break; done", "end=$((SECONDS+600)); until [ -s f ] || [ $SECONDS -gt $end ]; do sleep 5; done", "until [ -s f ] || [ $(date +%s) -ge $end ]; do sleep 5; done", "n=0; while [ $n -lt 40 ]; do sleep 1; n=$((n+1)); done", "while ((n < 40)); do sleep 1; done", "until [ -s f ] || [ \"$SECONDS\" = 600 ]; do sleep 5; done", "until [ -s f ] || [ $(date +%s) = $end ]; do sleep 5; done", "while read l; do echo $l; done < f; sleep 5", "bash script.sh", "bash -c", "bash -x", "for i in $(seq 1 40); do [ -s f ] && break; sleep 15; done", "for f in a b c; do sleep 1; done", "for ((i=0; i<40; i++)); do sleep 1; done", "timeout 600 bash -c 'until [ -s f ]; do sleep 1; done'", "~/.claude/bin/wait-for /tmp/x.log 'merged' --timeout 600", "while read l; do echo $l; done < f", "until [ -s f ]; do echo waiting; done", "echo 'until x; do sleep 1; done'"]: + for command in ["until [ -s f ]; do sleep 15; [ $((n+=1)) -ge 40 ] && break; done", "end=$((SECONDS+600)); until [ -s f ] || [ $SECONDS -gt $end ]; do sleep 5; done", "until [ -s f ] || [ $(date +%s) -ge $end ]; do sleep 5; done", "n=0; while [ $n -lt 40 ]; do sleep 1; n=$((n+1)); done", "while ((n < 40)); do sleep 1; done", "until [ -s f ] || [ \"$SECONDS\" = 600 ]; do sleep 5; done", "until [ -s f ] || [ $(date +%s) = $end ]; do sleep 5; done", "while read l; do echo $l; done < f; sleep 5", "bash script.sh", "bash -c", "bash -x", "for i in $(seq 1 40); do [ -s f ] && break; sleep 15; done", "for f in a b c; do sleep 1; done", "for ((i=0; i<40; i++)); do sleep 1; done", "timeout 600 bash -c 'until [ -s f ]; do sleep 1; done'", "~/.claude/bin/wait-for /tmp/x.log 'merged' --timeout 600", "while read l; do echo $l; done < f", "until [ -s f ]; do echo waiting; done", "echo 'until x; do sleep 1; done'", "until ! pgrep -x chunk-pr || [ $SECONDS -gt 600 ]; do sleep 15; done"]: with self.subTest(command): self.assertEqual(checked(command), []) def test_a_pgrep_or_pkill_f_whose_pattern_the_command_contains_breaks_endless_wait(self): - for command in ["pgrep -f chunk-pr", "pkill -f 'loop-claude'", "pgrep -fa chunk-pr", "pgrep -u me -f chunk-pr", "pgrep --full chunk-pr", "pkill -9 -f loop-claude", "pkill -KILL -f loop-claude", "pgrep -f chunk-pr -u", "pkill --full chunk-pr --signal", "pgrep -f -", "pgrep -U me -f chunk-pr", "pgrep -fUME chunk-pr", "pgrep -f9 chunk-pr", "pgrep --signal TERM --full chunk-pr", "pgrep -f 'chunk[pr'", "pgrep -f 'chunk-pr .*merg'", "nice pgrep -f chunk-pr", "bash -c 'pgrep -f chunk-pr'"]: + for command in ["pgrep -f chunk-pr", "pkill -f 'loop-claude'", "pgrep -fa chunk-pr", "pgrep -u me -f chunk-pr", "pgrep --full chunk-pr", "pkill -9 -f loop-claude", "pkill -KILL -f loop-claude", "pgrep -f chunk-pr -u", "pkill --full chunk-pr --signal", "pgrep -f -", "if pgrep -f loop-claude; then echo up; fi", "until ! pgrep -f \"chunk-pr merg\" || [ $SECONDS -gt 600 ]; do sleep 15; done", "pgrep -U me -f chunk-pr", "pgrep -fUME chunk-pr", "pgrep -f9 chunk-pr", "pgrep --signal TERM --full chunk-pr", "pgrep -f 'chunk[pr'", "pgrep -f 'chunk-pr .*merg'", "nice pgrep -f chunk-pr", "bash -c 'pgrep -f chunk-pr'"]: with self.subTest(command): self.assertEqual(checked(command), ["endless-wait"]) self.assertIn("pgrep -x", breaches("pgrep -f chunk-pr")[0].detail) + self.assertIn("pkill -x", breaches("pkill -f chunk-pr")[0].detail) + self.assertNotIn("wait-for", breaches("pkill -f chunk-pr")[0].detail) - def test_an_endless_wait_names_its_loop_and_the_folder_it_runs_in(self): + def test_an_endless_wait_names_its_loop_and_the_directory_it_runs_in(self): head = "until [ -s /tmp/a-file-whose-name-takes-the-head-past-sixty-characters-by-itself ]" found = breaches(f"cd /w && {head}; do sleep 1; done") self.assertEqual([breach.directory for breach in found], ["/w"]) @@ -990,14 +992,15 @@ class Hook(unittest.TestCase): self.assertIn("Blocked by rule image-pin: ", ran.stderr) self.assertIn("`# rule-override image-pin: `", ran.stderr) - def test_a_monitor_command_is_checked_as_a_bash_command(self): - endless = {"command": "until [ -s f ]; do sleep 15; done", "description": "d", "timeout_ms": 60000} - ran = hook({"cwd": self.repo, "tool_input": endless, "tool_name": "Monitor"}, self.repo) + def test_a_monitor_command_is_checked_for_a_self_matching_pgrep_but_not_for_a_loop_its_timeout_ends(self): + monitor = {"command": "pgrep -f chunk-pr", "description": "d", "timeout_ms": 60000} + ran = hook({"cwd": self.repo, "tool_input": monitor, "tool_name": "Monitor"}, self.repo) self.assertEqual(ran.returncode, 2) self.assertIn("Blocked by rule endless-wait: ", ran.stderr) self.assertIn("`# rule-override endless-wait: ` to the command", ran.stderr) - bounded = endless | {"command": "~/.claude/bin/wait-for f --timeout 60"} - self.assertEqual(hook({"cwd": self.repo, "tool_input": bounded, "tool_name": "Monitor"}, self.repo).returncode, 0) + looping = monitor | {"command": "until [ -s f ]; do sleep 15; done"} + self.assertEqual(hook({"cwd": self.repo, "tool_input": looping, "tool_name": "Monitor"}, self.repo).returncode, 0) + self.assertEqual(hook({"cwd": self.repo, "tool_input": looping | {"command": looping["command"] + " # x"}, "tool_name": "Bash"}, self.repo).returncode, 2) def test_settings_hooks_a_monitor_call(self): settings = json.loads((HOOK.parent.parent / "settings.json").read_text())