diff --git a/bin/rule_hook/rules.py b/bin/rule_hook/rules.py index f51a1399..da983542 100644 --- a/bin/rule_hook/rules.py +++ b/bin/rule_hook/rules.py @@ -62,7 +62,7 @@ RULES: dict[RuleName, Rule] = { ), "run-heavy": Rule( "a heavy command, which loads the machine for minutes, runs under ~/.claude/bin/run-heavy, so sessions running at once cannot starve the machine", - "a docker build, docker buildx build or docker compose build, and a run of mutmut, cargo mutants, Stryker or Infection, the mutation tools stability-reviewer names; a gate script it cannot tell from a light one", + "a docker build, docker buildx build or docker compose build, and a run of mutmut, cargo mutants, Stryker or Infection, the mutation tools stability-reviewer names, whether direct, through a launcher such as uvx, or inside docker run, docker exec or docker compose run or exec; a gate script it cannot tell from a light one", ), "todo-rank-check": Rule( "while todo.md has changes, staged or not, a git commit waits until ~/.claude/bin/rank_todo.py --check passes on it", @@ -101,17 +101,49 @@ class Outcome: # Launchers that run the tool named later in their words, such as uvx --with pytest mutmut run. LAUNCHERS = frozenset({"bunx", "env", "nice", "npx", "pipx", "pnpm", "time", "uvx", "yarn"}) -# Each heavy tool, and the words of which one must follow it within two words for its call to be heavy; an empty set makes every call heavy. -HEAVY = {"cargo": {"mutants"}, "docker": {"build"}, "infection": set(), "mutmut": {"run"}, "stryker": {"run"}} +# Each mutation tool, and the word of which one must follow it within two words for its call to be a mutation run; an empty set makes every call one. +MUTATORS = {"cargo": {"mutants"}, "infection": set(), "mutmut": {"run"}, "stryker": {"run"}} +# The options of docker and docker compose that take a value, so the value is not read as a subcommand. +DOCKER_VALUED = frozenset({"--ansi", "--config", "--context", "--env-file", "--file", "--host", "--log-level", "--parallel", "--profile", "--progress", "--project-directory", "--project-name", "-c", "-f", "-H", "-l", "-p"}) + + +def tool_name(word: str) -> str: + """The tool a word names, such as mutmut for `mutmut>=3,<4` or `.venv/bin/mutmut`; empty for a word that names none, such as a file name.""" + named = re.fullmatch(r"([A-Za-z_-]+)(?:[<>=!~@].*)?", posixpath.basename(word)) + return named.group(1) if named else "" def heavy(words: list[str]) -> bool: - """Whether the simple command runs a heavy tool, itself or through a launcher, outside run-heavy.""" - names = [re.sub(r"[^A-Za-z_-].*", "", posixpath.basename(word)) for word in words] - if names[0] == "run-heavy": + """Whether the simple command runs a docker build, or a mutation run directly, through a launcher or in a container, outside run-heavy.""" + names = [tool_name(word) for word in words] + if "run-heavy" in names: return False - at = range(len(names)) if names[0] in LAUNCHERS else range(1) - return any(names[i] in HEAVY and (not HEAVY[names[i]] or HEAVY[names[i]] & set(names[i + 1:i + 3])) for i in at) + start = next((i for i, name in enumerate(names) if name not in LAUNCHERS), len(names)) + if names[start:start + 1] == ["docker"]: + subcommand = docker_subcommand(words[start + 1:]) + return subcommand in (["build"], ["buildx", "build"], ["compose", "build"]) or (subcommand[-1:] in (["run"], ["exec"]) and mutation_run(names[start + 1:])) + # Past a launcher, the tool may follow the launcher's own options, as in uvx --with pytest mutmut run; without one, the command itself is the tool. + return mutation_run(names[start:]) if start else bool(names) and names[0] in MUTATORS and mutation_run(names) + + +def mutation_run(names: list[str]) -> bool: + return any(name in MUTATORS and (not MUTATORS[name] or MUTATORS[name] & set(names[i + 1:i + 3])) for i, name in enumerate(names)) + + +def docker_subcommand(words: list[str]) -> list[str]: + """The subcommand docker's words name, past its options: such as ["build"], ["compose", "run"] or ["buildx", "build"].""" + positional: list[str] = [] + skip = False + for word in words: + if skip: + skip = False + elif word.startswith("-"): + skip = word in DOCKER_VALUED + else: + positional.append(word) + if positional[0] not in ("buildx", "compose") or len(positional) == 2: + break + return positional def overrides(written: str) -> tuple[dict[str, str], list[str]]: diff --git a/bin/rule_hook/test_rule_hook.py b/bin/rule_hook/test_rule_hook.py index 6afdab3b..b1164390 100644 --- a/bin/rule_hook/test_rule_hook.py +++ b/bin/rule_hook/test_rule_hook.py @@ -106,12 +106,12 @@ class CommandBreaches(unittest.TestCase): self.assertEqual(checked("git commit -m 'Answer the question in the README'"), []) def test_a_docker_build_or_a_mutation_run_outside_run_heavy_breaks_run_heavy(self): - for command in ["docker build -t x .", "cd /w && docker buildx build .", "docker compose build", "uvx --with pytest 'mutmut>=3,<4' run", "cargo mutants --in-diff d", "npx stryker run", "vendor/bin/infection --git-diff-lines"]: + for command in ["docker build -t x .", "cd /w && docker buildx build .", "docker compose build", "docker compose -f x.yml --verbose build app", "uvx --with pytest 'mutmut>=3,<4' run", "cargo mutants --in-diff d", "npx stryker run", "vendor/bin/infection --git-diff-lines", "docker compose run --rm app mutmut run", "docker run --rm -v /w:/w rust:1.90.0 cargo mutants", "docker compose exec app npx stryker run"]: with self.subTest(command): self.assertEqual(checked(command), ["run-heavy"]) def test_a_heavy_command_under_run_heavy_or_a_light_docker_or_mutmut_call_passes(self): - for command in ["~/.claude/bin/run-heavy docker build -t x .", "/home/me/.claude/bin/run-heavy uvx mutmut run", "docker images", "docker compose run --rm app go vet", "uvx 'mutmut>=3,<4' results", "echo docker build"]: + for command in ["~/.claude/bin/run-heavy docker build -t x .", "/home/me/.claude/bin/run-heavy uvx mutmut run", "nice -n 19 ~/.claude/bin/run-heavy docker build .", "env X=1 run-heavy docker compose run app mutmut run", "docker images", "docker compose run --rm app go vet", "docker compose run app make build", "uvx 'mutmut>=3,<4' results", "echo docker build", "npx prettier --check infection.json5"]: with self.subTest(command): self.assertEqual(checked(command), []) diff --git a/docs/decisions.md b/docs/decisions.md index 92bbb631..a882384c 100644 --- a/docs/decisions.md +++ b/docs/decisions.md @@ -64,7 +64,7 @@ The cap is 5, `bin/todo_table.py`'s `BUNDLE_MAX_R`, from the same chunks: by the ## machine load: a heavy command waits for one of a slot per 4 CPUs that every session on the machine shares, and loop-claude starts a worker only while a quarter of the machine's memory is available -2026-10-23, lilleman. Serves goal 1.5. On 2026-10-22 ai-sandbox, with 8 CPUs and 16 GiB, thrashed for hours: four to six loops ran mutation tests, full gates, `docker build`s and QEMU-booting e2e tests at once, and nothing told one loop how busy the machine was. The human chose both limits; no standard sets either, and GNU parallel's `--memfree`, which does the memory wait's job, has no default. A slot is a file lock, so a session that dies frees its slot. A heavy command run inside a slot already held runs at once, so a gate that wraps its own steps cannot wait on itself. The memory wait reads `/proc/meminfo` only, and off Linux a worker starts at once. `bin/machine_load/` holds the slots and the memory share, `bin/run-heavy` takes a slot, `CLAUDE.md` → "Working with the codebase" has every session use it, and loop-claude's worker waits for the share. rule-hook's rule `run-heavy` blocks the heavy commands a program can tell apart, a `docker build` and a run of the mutation tools stability-reviewer names, outside run-heavy; a gate script it cannot tell from a light one, so that part of the rule rests on `CLAUDE.md`. Valid while one machine runs several loops at once. +2026-10-23, lilleman. Serves goal 1.5. On 2026-10-22 ai-sandbox, with 8 CPUs and 16 GiB, thrashed for hours: four to six loops ran mutation tests, full gates, `docker build`s and QEMU-booting e2e tests at once, and nothing told one loop how busy the machine was. The human chose both limits; no standard sets either, and GNU parallel's `--memfree`, which does the memory wait's job, has no default. A slot is a file lock, so a session that dies frees its slot. A heavy command run inside a slot already held runs at once, so a gate that wraps its own steps cannot wait on itself. The memory wait reads `/proc/meminfo` only, and off Linux a worker starts at once. `bin/machine_load/` holds the slots and the memory share, `bin/run-heavy` takes a slot, `CLAUDE.md` → "Working with the codebase" has every session use it, and loop-claude's worker waits for the share. rule-hook's rule `run-heavy` blocks the heavy commands a program can tell apart, a `docker build` and a run of the mutation tools stability-reviewer names, direct or inside a container, outside run-heavy; a gate script it cannot tell from a light one, so that part of the rule rests on `CLAUDE.md`. Valid while one machine runs several loops at once. ## principles: about seven things per level, four or five where they act on each other, more where a reader finds them by name; crossing a limit asks for a grouping or a recorded reason