From 578de9231ff4fcf487d788112aac9fa3a59b0fb9 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Fri, 23 Oct 2026 14:35:06 +0200 Subject: [PATCH] Move the run-heavy rule's command parsing into its own module and pick docker's position without a nested conditional --- bin/rule_hook/heavy_command.py | 53 ++++++++++++++++++++++++++++++++++ bin/rule_hook/rules.py | 51 +------------------------------- todo.md | 5 ---- 3 files changed, 54 insertions(+), 55 deletions(-) create mode 100644 bin/rule_hook/heavy_command.py diff --git a/bin/rule_hook/heavy_command.py b/bin/rule_hook/heavy_command.py new file mode 100644 index 00000000..87951195 --- /dev/null +++ b/bin/rule_hook/heavy_command.py @@ -0,0 +1,53 @@ +"""Whether a simple command is a heavy one, for the `run-heavy` rule: a docker build, or a run of a mutation tool.""" +import posixpath +import re + +# 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 mutation tool, and the words that make its call a mutation run when one of them follows it within two words; with no words, every call is one. +MUTATORS = {"cargo": {"mutants"}, "infection": set(), "mutmut": {"run"}, "stryker": {"run"}} +# Flags with which a tool only prints its help, version or list, so a call that carries one is light. Not -h, which docker run reads as the container's hostname. +LIGHT_FLAGS = frozenset({"--help", "--list", "--version"}) +# 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 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 not names or "run-heavy" in names or LIGHT_FLAGS & set(words): + return False + # Past a launcher, the tool may follow the launcher's own options and assignments, as in nice -n 19 docker build or uvx --with pytest mutmut run; without one, the command itself is the tool. + launched = names[0] in LAUNCHERS + reachable = names if launched else names[:1] + docker = reachable.index("docker") if "docker" in reachable else None + if docker is not None: + subcommand = docker_subcommand(words[docker + 1:]) + return subcommand in (["build"], ["buildx", "build"], ["compose", "build"]) or (subcommand[-1:] in (["run"], ["exec"]) and mutation_run(names[docker + 1:])) + return (launched or 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 diff --git a/bin/rule_hook/rules.py b/bin/rule_hook/rules.py index 6cd9d81c..971754c0 100644 --- a/bin/rule_hook/rules.py +++ b/bin/rule_hook/rules.py @@ -6,6 +6,7 @@ import typing from round_titles import CAP, lacks_counts, past_cap from git_command import commits from review_folder import REPORTS +from rule_hook.heavy_command import heavy from shell_command import Simple RuleName = typing.Literal["action-pin", "commit-one-line", "ignorefile-sorted", "image-pin", "no-ai-attribution", "reviewer-report", "reviewer-rung", "round-cap", "round-title-counts", "run-heavy", "todo-rank-check"] @@ -100,56 +101,6 @@ class Checked: overridden: list[tuple[Breach, str]] -# 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 mutation tool, and the words that make its call a mutation run when one of them follows it within two words; with no words, every call is one. -MUTATORS = {"cargo": {"mutants"}, "infection": set(), "mutmut": {"run"}, "stryker": {"run"}} -# Flags with which a tool only prints its help, version or list, so a call that carries one is light. Not -h, which docker run reads as the container's hostname. -LIGHT_FLAGS = frozenset({"--help", "--list", "--version"}) -# 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 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 not names or "run-heavy" in names or LIGHT_FLAGS & set(words): - return False - # Past a launcher, the tool may follow the launcher's own options and assignments, as in nice -n 19 docker build or uvx --with pytest mutmut run; without one, the command itself is the tool. - launched = names[0] in LAUNCHERS - docker = next((i for i, name in enumerate(names) if name == "docker"), None) if launched else (0 if names[0] == "docker" else None) - if docker is not None: - subcommand = docker_subcommand(words[docker + 1:]) - return subcommand in (["build"], ["buildx", "build"], ["compose", "build"]) or (subcommand[-1:] in (["run"], ["exec"]) and mutation_run(names[docker + 1:])) - return (launched or 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]]: """The rules the text overrides with their reasons, and the markers that name no rule or give no reason; a marker for a rule that takes no override is neither.""" given = [(rule, reason.strip().removesuffix("-->").strip()) for rule, reason in OVERRIDE.findall(written)] diff --git a/todo.md b/todo.md index 7b2d06f5..6472e725 100644 --- a/todo.md +++ b/todo.md @@ -12,7 +12,6 @@ | ID | Release | Blocked by | Exempt | Item | L | E | I | R | S | Moves | Score | |---|---|---|---|---|---|---|---|---|---|---|---| -| 223 | 0.4.0 | | | **Move the `run-heavy` rule's command parsing out of `bin/rule_hook/rules.py` into a module of its own, as `reviewer_rung.py` holds its rule, and pick docker's position in `heavy` without a nested conditional.** | 2 | 1 | 2 | 3 | 2 | code health by 2 | 12.0 | | 59 | 0.4.0 | | | **Bound a run of unfinished verdicts on one chunk, so a worker and judge that disagree cannot cycle unattended without end.** | 2 | 1 | 3 | 4 | 3 | 1.4 by 6 | 6.0 | | 167 | 0.4.0 | | | **Refuse a `/security-review` or `/claude-security` Skill call below the project's rung, as rule-hook refuses such an Agent call.** | 3 | 2 | 3 | 6 | 2 | 1.1 by 4 | 5.6 | | 6 | 0.4.0 | | decision | **Move what `loop-claude` writes out of `~/.claude.json`, and ask the human about each write that must stay.** | 3 | 3 | 3 | 7 | 3 | 1.1 by 5 | 5.3 | @@ -70,10 +69,6 @@ ## Details -### 223. Move the `run-heavy` rule's command parsing out of `bin/rule_hook/rules.py` into a module of its own, as `reviewer_rung.py` holds its rule, and pick docker's position in `heavy` without a nested conditional. - -PR #366's paired ruling, at e05c54a against 4a0ba59, had Mid A and the inherited architect both rule Shape worse at `bin/rule_hook/rules.py:103` `heavy`. About 50 lines of command-word parsing (`LAUNCHERS`, `MUTATORS`, `DOCKER_VALUED`, `LIGHT_FLAGS`, `tool_name`, `heavy`, `mutation_run`, `docker_subcommand`) joined the module that holds the rule table, while the other non-trivial rules each have a sibling module. Both also named line 126, where a conditional nested in another picks docker's index. Make that place easier to read. - ### 59. Bound a run of unfinished verdicts on one chunk, so a worker and judge that disagree cannot cycle unattended without end. Raised by the product-owner review of PR #211. The resumed worker gets the judge's quote of the work remaining, but a worker that still thinks the chunk done can answer each cycle the same way, and each cycle costs a worker and a judge run (goal 1.4).