From 39de100a3436d20718601127c3f8408bd0dcef1d Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Wed, 18 Nov 2026 20:39:10 +0100 Subject: [PATCH] Kill the endless-wait message mutants --- bin/rule_hook/endless_wait.py | 7 ++++--- bin/rule_hook/test_rule_hook.py | 6 +++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/bin/rule_hook/endless_wait.py b/bin/rule_hook/endless_wait.py index afe5d1f2..e4486d93 100644 --- a/bin/rule_hook/endless_wait.py +++ b/bin/rule_hook/endless_wait.py @@ -24,16 +24,17 @@ SIGNAL = re.compile(r"-(?:\d+|[A-Z]{2,})") 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 [] --timeout `" +# What the refusal of a self-matching pgrep or pkill suggests first. Only a pgrep waits, so only it is pointed to wait-for. +INSTEAD = {"pgrep": f"{HOW}, a `pgrep -x` on the process name", "pkill": "use a `pkill -x` on the process name"} -def endless_waits(command: str, simples: list[Simple], capped: bool = False) -> list[tuple[str, str]]: +def endless_waits(command: str, simples: list[Simple], capped: bool) -> list[tuple[str, str]]: """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 (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)) + found.append((f"`{pattern}` matches the command line that holds this `{tool} -f`, so `{tool}` finds the shell that runs the command; {INSTEAD[tool]}, 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 diff --git a/bin/rule_hook/test_rule_hook.py b/bin/rule_hook/test_rule_hook.py index e25f23de..74a6b862 100644 --- a/bin/rule_hook/test_rule_hook.py +++ b/bin/rule_hook/test_rule_hook.py @@ -158,9 +158,9 @@ class CommandBreaches(unittest.TestCase): 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) + ending = ", or a pattern the command doesn't contain, such as `[c]hunk` for `chunk`" + self.assertEqual(breaches("pgrep -f chunk-pr")[0].detail, "`chunk-pr` matches the command line that holds this `pgrep -f`, so `pgrep` finds the shell that runs the command; use `~/.claude/bin/wait-for [] --timeout `, a `pgrep -x` on the process name" + ending) + self.assertEqual(breaches("pkill -f chunk-pr")[0].detail, "`chunk-pr` matches the command line that holds this `pkill -f`, so `pkill` finds the shell that runs the command; use a `pkill -x` on the process name" + ending) 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 ]"