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
+24 -14
View File
@@ -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 <file> [<pattern>] --timeout <seconds>`"
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: