Refuse in rule-hook a Bash or Monitor wait that has no end
CI / tests (pull_request) Failing after 3m3s
CI / tests (pull_request) Failing after 3m3s
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
"""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).
|
||||
"""
|
||||
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", "{"})
|
||||
LOOPS = frozenset({"for", "until", "while"})
|
||||
SHELLS = frozenset({"bash", "dash", "sh", "zsh"})
|
||||
# A word with which a loop counts its rounds or compares a clock: `[ "$n" -lt 40 ]`, `$SECONDS`.
|
||||
COUNTERS = frozenset({"-ge", "-gt", "-le", "-lt"})
|
||||
# `for ((;;))` has no condition. The lexer splits at its parentheses, so the check reads the command's text.
|
||||
FOREVER = re.compile(r"\bfor\s*\(\(\s*[^;)]*;\s*;")
|
||||
# pgrep's and pkill's options that take a value, as short letters and as long names.
|
||||
VALUED = frozenset("dFgGPrstuU")
|
||||
VALUED_LONG = frozenset({"--delimiter", "--euid", "--group", "--ns", "--nslist", "--parent", "--pgroup", "--pidfile", "--runstates", "--session", "--signal", "--terminal", "--uid"})
|
||||
HOW = "use `~/.claude/bin/wait-for <file> [<pattern>] --timeout <seconds>`"
|
||||
|
||||
|
||||
def endless_waits(command: str, simples: list[Simple]) -> list[tuple[str, str]]:
|
||||
"""Each endless wait the command starts, as its detail and the directory it runs in."""
|
||||
return waits(command, simples, False)
|
||||
|
||||
|
||||
def waits(command: str, simples: list[Simple], capped: bool) -> list[tuple[str, str]]:
|
||||
"""capped is whether a `timeout` outside the simple commands ends them all, as for the inside of `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`, `timeout`, or a count 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 (script := shell_script(simple.words)) is not None:
|
||||
found += 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."""
|
||||
start = next((i for i, word in enumerate(words) if word not in KEYWORDS), len(words))
|
||||
return words[start:]
|
||||
|
||||
|
||||
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]] = []
|
||||
closed = []
|
||||
for simple in simples:
|
||||
words = core(simple.words)
|
||||
for loop in open_loops:
|
||||
loop.append(Simple(words, simple.directory))
|
||||
if words[:1] and words[0] in LOOPS:
|
||||
open_loops.append([Simple(words, simple.directory)])
|
||||
elif words[:1] == ["done"] and open_loops:
|
||||
closed.append(open_loops.pop())
|
||||
return closed + open_loops
|
||||
|
||||
|
||||
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):
|
||||
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.
|
||||
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 head == [head[0]]:
|
||||
return False
|
||||
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", "timeout"}) for simple in loop))
|
||||
|
||||
|
||||
def shell_script(words: list[str]) -> str | None:
|
||||
"""The script of `bash -c <script>`, whose inner commands the lexer keeps as one word; None for any other command."""
|
||||
shell = launched(words, SHELLS)
|
||||
if shell is None:
|
||||
return None
|
||||
flags = [i for i, argument in enumerate(shell.arguments) if re.fullmatch(r"-[a-z]*c[a-z]*", argument)]
|
||||
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"})
|
||||
if launch is None:
|
||||
return None
|
||||
full, patterns, rest = False, [], iter(launch.arguments)
|
||||
for word in rest:
|
||||
if word.startswith("--"):
|
||||
full = full or word == "--full"
|
||||
if "=" not in word and word in VALUED_LONG:
|
||||
next(rest, None)
|
||||
elif word.startswith("-") and len(word) > 1:
|
||||
# A signal, such as -9 or -KILL, isn't a cluster of options.
|
||||
if word[1:].isdigit() or (word[1:].isupper() and len(word) > 2):
|
||||
continue
|
||||
for at, letter in enumerate(word[1:], 1):
|
||||
full = full or letter == "f"
|
||||
if letter in VALUED:
|
||||
# `-u root` takes the next word; `-uroot` has it in the cluster.
|
||||
if at == len(word) - 1:
|
||||
next(rest, None)
|
||||
break
|
||||
else:
|
||||
patterns.append(word)
|
||||
return next((pattern for pattern in patterns if full and matches(pattern, command)), None)
|
||||
|
||||
|
||||
def matches(pattern: str, text: str) -> bool:
|
||||
"""Whether pgrep's regular expression finds a match in text; an expression Python cannot read is taken as plain text."""
|
||||
try:
|
||||
return re.search(pattern, text) is not None
|
||||
except re.error:
|
||||
return pattern in text
|
||||
@@ -3,14 +3,14 @@
|
||||
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, MultiEdit, Read, Skill and Write call, in every session and
|
||||
subagent. The rules:
|
||||
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:
|
||||
|
||||
{rules}
|
||||
|
||||
A call overrides a rule with `# rule-override <rule>: <reason>`:
|
||||
|
||||
- anywhere in a Bash command, such as a trailing comment
|
||||
- anywhere in a Bash or Monitor command, such as a trailing comment
|
||||
- in a file, as a comment line in the text the edit writes
|
||||
- in Markdown, as `<!-- rule-override <rule>: <reason> -->`
|
||||
|
||||
@@ -64,6 +64,10 @@ from rule_hook.todo_check import todo_breaches
|
||||
from rule_hook.transcript_read import transcript_breaches
|
||||
|
||||
|
||||
# The tools whose `command` is a shell command.
|
||||
COMMAND_TOOLS = ("Bash", "Monitor")
|
||||
|
||||
|
||||
def main(argv: list[str], stdin: str) -> int:
|
||||
if argv in (["-h"], ["--help"]) or not argv and not stdin:
|
||||
print(__doc__.strip().replace("{rules}", rules_help()))
|
||||
@@ -84,7 +88,7 @@ def main(argv: list[str], stdin: str) -> int:
|
||||
if found is None:
|
||||
return 0
|
||||
written, what, breaches = found
|
||||
return answer(check(breaches, written, text(call.given, "file_path").endswith(".md"), call.tool == "Bash"), what, time.time())
|
||||
return answer(check(breaches, written, text(call.given, "file_path").endswith(".md"), call.tool in COMMAND_TOOLS), what, time.time())
|
||||
|
||||
|
||||
def breaches_of(call: Call, home: str, environ: typing.Mapping[str, str]) -> tuple[str, str, list[Breach]] | None:
|
||||
@@ -93,7 +97,7 @@ def breaches_of(call: Call, home: str, environ: typing.Mapping[str, str]) -> tup
|
||||
return text(call.given, "prompt"), text(call.given, "subagent_type"), reviewer_breaches(call, environ, home)
|
||||
if call.tool == "Skill":
|
||||
return text(call.given, "args"), text(call.given, "skill"), skill_breaches(call, environ, home)
|
||||
if call.tool == "Bash":
|
||||
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)
|
||||
if call.tool == "Read":
|
||||
|
||||
@@ -6,13 +6,14 @@ 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 rule_hook.heavy_command import heavy
|
||||
from command_lines.shell_command import launched, LAUNCHERS, Simple
|
||||
from review_rules.review_folder import REPORTS
|
||||
from project_config import templates
|
||||
from command_lines.docker_command import unlimited
|
||||
|
||||
RuleName = typing.Literal["action-pin", "commit-one-line", "commit-title", "docker-run-memory", "env-file-read", "ignorefile-sorted", "image-pin", "no-ai-attribution", "repeat-read", "reviewer-report", "reviewer-rung", "round-cap", "round-title-counts", "run-heavy", "todo-rank-check", "transcript-read"]
|
||||
RuleName = typing.Literal["action-pin", "commit-one-line", "commit-title", "docker-run-memory", "endless-wait", "env-file-read", "ignorefile-sorted", "image-pin", "no-ai-attribution", "repeat-read", "reviewer-report", "reviewer-rung", "round-cap", "round-title-counts", "run-heavy", "todo-rank-check", "transcript-read"]
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
@@ -40,6 +41,10 @@ RULES: dict[RuleName, Rule] = {
|
||||
"a container made with docker run or docker create has a memory limit and a swap limit, as large as its work needs, such as `--memory 4g --memory-swap 4g`, so a runaway container fails alone instead of starving the machine; a swap limit equal to the memory limit keeps it out of swap",
|
||||
"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 <file> [<pattern>] --timeout <seconds>`, 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 `timeout`, 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 marker would let the credentials into the transcript all the same, so no marker waives it.
|
||||
"env-file-read": Rule(
|
||||
"an agent never reads ~/.claude/.env or a project's env-file, the file of credentials the project's config names, since a value the agent reads lands in its transcript, which loop-claude archives and the backups copy; it leaves a tool that needs a credential to read the file itself, gets the file's path from `~/.claude/bin/project-config env-file`, and spells the path another way, such as \"the .env in ~/.claude\", in a command that must mention it without reading it, such as a commit's title, and searches only a folder without it",
|
||||
@@ -163,6 +168,7 @@ def command_breaches(command: str, simples: list[Simple]) -> list[Breach]:
|
||||
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)]
|
||||
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))
|
||||
|
||||
@@ -144,6 +144,32 @@ class CommandBreaches(unittest.TestCase):
|
||||
with self.subTest(command):
|
||||
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"]:
|
||||
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", "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'"]:
|
||||
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", "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)
|
||||
|
||||
def test_a_loop_that_waits_on_its_own_pgrep_breaks_endless_wait_twice(self):
|
||||
command = 'until [ -s /tmp/merge-402.log ] && ! pgrep -f "chunk-pr merg"; do sleep 15; done'
|
||||
self.assertEqual(checked(command), ["endless-wait", "endless-wait"])
|
||||
self.assertEqual(checked(command.replace("-f ", "-x ")), ["endless-wait"])
|
||||
|
||||
def test_a_pgrep_f_on_a_pattern_the_command_lacks_or_a_pgrep_without_f_passes_endless_wait(self):
|
||||
for command in ["pgrep -f '[c]hunk-pr merg'", "pgrep -f \"$name\"", "pgrep -f '^python3 .*loop-claude$'", "pgrep -x chunk-pr", "pgrep chunk-pr", "pgrep -u me -x chunk-pr", "pkill -f -u me '[l]oop-claude'", "echo pgrep -f chunk-pr", "git commit -m 'Refuse pgrep -f chunk-pr'", "pgrep -f"]:
|
||||
with self.subTest(command):
|
||||
self.assertEqual(checked(command), [])
|
||||
|
||||
def test_an_attribution_breaks_only_in_a_git_gh_tea_or_chunk_pr_command(self):
|
||||
self.assertEqual(checked(f"tea pr create --description '{TRAILER}'"), ["no-ai-attribution"])
|
||||
self.assertEqual(checked(f"~/.claude/bin/chunk-pr open 'Do it' '{TRAILER}'"), ["no-ai-attribution"])
|
||||
@@ -955,6 +981,20 @@ class Hook(unittest.TestCase):
|
||||
self.assertIn("Blocked by rule image-pin: ", ran.stderr)
|
||||
self.assertIn("`# rule-override image-pin: <reason>`", 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)
|
||||
self.assertEqual(ran.returncode, 2)
|
||||
self.assertIn("Blocked by rule endless-wait: ", ran.stderr)
|
||||
self.assertIn("`# rule-override endless-wait: <reason>` 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)
|
||||
|
||||
def test_settings_hooks_a_monitor_call(self):
|
||||
settings = json.loads((HOOK.parent.parent / "settings.json").read_text())
|
||||
matchers = [entry["matcher"].split("|") for entry in settings["hooks"]["PreToolUse"] if "rule-hook" in entry["hooks"][0]["command"]]
|
||||
self.assertIn("Monitor", matchers[0])
|
||||
|
||||
def test_an_edit_of_a_fifo_or_a_device_finishes(self):
|
||||
fifo = f"{self.repo}/pipe"
|
||||
os.mkfifo(fifo)
|
||||
|
||||
Reference in New Issue
Block a user