Refuse in rule-hook a Bash or Monitor wait that has no end
CI / tests (pull_request) Failing after 3m3s

This commit is contained in:
2026-11-18 19:07:42 +01:00
parent 90c7a2edbb
commit 1d411be20d
7 changed files with 192 additions and 8 deletions
+40
View File
@@ -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)