Answer principles round 2: 0 🔴, 2 🟡; have rule-hook read docker's subcommand and the tool inside docker run or exec, and pass a run-heavy behind a launcher

This commit is contained in:
2026-10-23 11:06:55 +02:00
parent c7dbd56f39
commit dde9caa172
3 changed files with 43 additions and 11 deletions
+40 -8
View File
@@ -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]]: