diff --git a/bin/docker_command.py b/bin/docker_command.py index eb2004dc..d01b35bd 100644 --- a/bin/docker_command.py +++ b/bin/docker_command.py @@ -6,9 +6,11 @@ from shell_command import launched # The options of docker, docker buildx and docker compose that take a value, so the value is not read as a subcommand. DOCKER_VALUED = frozenset({"--ansi", "--builder", "--config", "--context", "--env-file", "--file", "--host", "--log-level", "--parallel", "--profile", "--progress", "--project-directory", "--project-name", "--tlscacert", "--tlscert", "--tlskey", "-c", "-f", "-H", "-l", "-p"}) # The subcommands that start a container, which takes the memory limit docker run's -m sets. -STARTS_CONTAINER = (["run"], ["create"], ["container", "run"], ["container", "create"]) +STARTS_CONTAINER = (["container", "create"], ["container", "run"], ["create"], ["run"]) +# A size as docker reads one, such as 4g, 512M or 1.5GiB: the pattern go-units' RAMInBytes parses. +SIZE = r"\d+(?:\.\d+)*[kKmMgGtTpP]?[iI]?[bB]?" # -m4g is -m with its value attached; --memory-swap alone sets no limit. -MEMORY_LIMIT = re.compile(r"-m.*|--memory(=.*)?") +ATTACHED_LIMIT = re.compile(rf"(?:-m|--memory=){SIZE}") def docker_subcommand(arguments: list[str]) -> list[str]: @@ -30,12 +32,24 @@ def docker_subcommand(arguments: list[str]) -> list[str]: def unlimited(arguments: list[str]) -> bool: """Whether docker's arguments start a container, with docker run or docker create, and set it no memory limit. - A --memory among the container's own arguments, past its image, reads as the limit too: telling it apart needs every value-taking option of docker run. + A -m or --memory with a size among the container's own arguments, past its image, reads as the limit too: telling it apart needs every value-taking option of docker run. Taking only a size keeps python -m pytest from reading as one. """ - return "--help" not in arguments and docker_subcommand(arguments) in STARTS_CONTAINER and not any(MEMORY_LIMIT.fullmatch(word) for word in arguments) + subcommand = docker_subcommand(arguments) + return subcommand in STARTS_CONTAINER and not limited(arguments) and not helped(arguments, subcommand) + + +def limited(arguments: list[str]) -> bool: + """Whether the arguments carry a memory limit: -m or --memory with a size, as its next word or attached.""" + return any(ATTACHED_LIMIT.fullmatch(word) or (word in ("-m", "--memory") and re.fullmatch(SIZE, value)) for word, value in zip(arguments, [*arguments[1:], ""])) def unlimited_container(words: list[str]) -> bool: """Whether the simple command, directly or through a launcher, starts a container that has no memory limit.""" docker = launched(words, {"docker"}) return docker is not None and unlimited(docker.arguments) + + +def helped(arguments: list[str], subcommand: list[str]) -> bool: + """Whether docker only prints its help: a --help before the subcommand or as the first word after it, not one the container's own command takes.""" + after = arguments.index(subcommand[-1]) + 1 + return "--help" in arguments[:after + 1] diff --git a/bin/rule_hook/rules.py b/bin/rule_hook/rules.py index 864b6e04..a90a037b 100644 --- a/bin/rule_hook/rules.py +++ b/bin/rule_hook/rules.py @@ -138,7 +138,7 @@ def command_breaches(command: str, simples: list[Simple], home: str) -> list[Bre if heavy(simple.words): breaches.append(Breach("run-heavy", f"run `{' '.join(simple.words)[:80]}` under `~/.claude/bin/run-heavy`, in the background", simple.directory)) if unlimited_container(simple.words): - breaches.append(Breach("docker-run-memory", f"give `{' '.join(simple.words)[:80]}` a memory limit as large as its work needs, such as `--memory 4g --memory-swap 4g`", simple.directory)) + breaches.append(Breach("docker-run-memory", f"give `{' '.join(simple.words)[:80]}` a memory limit as large as its work needs", simple.directory)) for commit in found: if commit.from_file: breaches.append(Breach("commit-one-line", "pass the message as one -m '', not from a file", commit.directory)) diff --git a/bin/rule_hook/test_rule_hook.py b/bin/rule_hook/test_rule_hook.py index 850dd441..b764aabf 100644 --- a/bin/rule_hook/test_rule_hook.py +++ b/bin/rule_hook/test_rule_hook.py @@ -117,12 +117,12 @@ class CommandBreaches(unittest.TestCase): self.assertEqual(checked(command), []) def test_a_docker_run_or_create_without_a_memory_limit_breaks_docker_run_memory(self): - for command in ["docker run --rm node:24.22.1-alpine3.24 node x.js", "cd /w && docker run -v /w:/w alpine:3.22.2 true", "docker create alpine:3.22.2", "docker container run alpine:3.22.2 true", "docker container create alpine:3.22.2", "nice -n 19 docker run alpine:3.22.2 true", "docker -H unix:///d.sock run alpine:3.22.2 true", "docker run --memory-swap 4g alpine:3.22.2 true", "~/.claude/bin/run-heavy docker run alpine:3.22.2 true"]: + for command in ["docker run --rm node:24.22.1-alpine3.24 node x.js", "cd /w && docker run -v /w:/w alpine:3.22.2 true", "docker create alpine:3.22.2", "docker container run alpine:3.22.2 true", "docker container create alpine:3.22.2", "nice -n 19 docker run alpine:3.22.2 true", "docker -H unix:///d.sock run alpine:3.22.2 true", "docker run --memory-swap 4g alpine:3.22.2 true", "~/.claude/bin/run-heavy docker run alpine:3.22.2 true", "docker run --rm python:3.13.1 python -m pytest", "docker run alpine:3.22.2 git commit -m x", "timeout 60 docker run alpine:3.22.2 true", "docker run node:24.22.1-alpine3.24 node --help"]: with self.subTest(command): self.assertEqual(checked(command), ["docker-run-memory"]) def test_a_docker_run_with_a_memory_limit_or_another_docker_call_passes_docker_run_memory(self): - for command in ["docker run -m 4g alpine:3.22.2 true", "docker run -m4g alpine:3.22.2 true", "docker run --memory 4g alpine:3.22.2 true", "docker run --rm --memory=4g --memory-swap=4g alpine:3.22.2 true", "docker container create -m 1g alpine:3.22.2", "docker run --help", "docker ps", "docker images", "docker container ls", "docker compose run --rm app go vet", "docker exec box true", "echo docker run alpine:3.22.2"]: + for command in ["docker run -m 4g alpine:3.22.2 true", "docker run -m4g alpine:3.22.2 true", "docker run --memory 4g alpine:3.22.2 true", "docker run -m 512M alpine:3.22.2 true", "docker run -m 1073741824 alpine:3.22.2 true", "timeout 60 docker run -m 4g alpine:3.22.2 true", "docker run --rm --memory=4g --memory-swap=4g alpine:3.22.2 true", "docker container create -m 1g alpine:3.22.2", "docker run --help", "docker container run --help", "docker --help", "docker ps", "docker images", "docker container ls", "docker compose run --rm app go vet", "docker exec box true", "echo docker run alpine:3.22.2"]: with self.subTest(command): self.assertEqual(checked(command), []) diff --git a/bin/shell_command.py b/bin/shell_command.py index 3be290ac..69061760 100644 --- a/bin/shell_command.py +++ b/bin/shell_command.py @@ -6,7 +6,7 @@ import shlex import typing # 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", "run-heavy", "time", "uvx", "yarn"}) +LAUNCHERS = frozenset({"bunx", "env", "nice", "npx", "pipx", "pnpm", "run-heavy", "time", "timeout", "uvx", "yarn"}) HEREDOC = re.compile(r"<<-?[ \t]*([\"']?)([A-Za-z_]\w*)\1[^\n]*\n.*?^[ \t]*\2[ \t]*$", re.DOTALL | re.MULTILINE) ASSIGNMENT = re.compile(r"[A-Za-z_][A-Za-z0-9_]*=") OPERATOR = set("\n();&|<>") diff --git a/docs/decisions.md b/docs/decisions.md index bcf2a971..7bde67ce 100644 --- a/docs/decisions.md +++ b/docs/decisions.md @@ -78,7 +78,7 @@ The cap is 5, `bin/todo_table.py`'s `BUNDLE_MAX_R`, from the same chunks: by the ## machine load: every container a session starts with `docker run` or `docker create` sets a memory limit as large as its work needs, which rule-hook's `docker-run-memory` enforces, and no daemon-wide cap limits all containers together -2026-10-24, lilleman. Serves goal 1.5. On 2026-10-23 ai-sandbox, with 8 CPUs and 7.6 GiB, hung until a hard reset, although its one heavy command held a slot: beside that mutation run, a reviewer's stress probes ran in `docker run` with no memory limit, each to 2 GiB. A limit makes a runaway container fail alone instead of taking the machine with it. The session sizes the limit, since lilleman wants a project to run heavier work where it needs to. rule-hook's rule reads only the commands a session types (todo item 228): a container a script or docker compose starts is out of its sight. lilleman turned down a daemon-wide cap, a dockerd `cgroup-parent` with a memory limit, which would see every container, on 2026-10-24: it is host config, and it would cap a project's heavy work too. Valid while sessions start their work in containers on a machine they share. +2026-10-24, lilleman. Serves goal 1.5. On 2026-10-23 ai-sandbox, with 8 CPUs and 7.6 GiB, hung until a hard reset, although its one heavy command held a slot: beside that mutation run, a reviewer's stress probes ran in `docker run` with no memory limit, each to 2 GiB. A limit makes a runaway container fail alone instead of taking the machine with it. The session sizes the limit, since lilleman wants a project to run heavier work where it needs to. rule-hook's rule reads only the commands a session types: a container a script or docker compose starts is out of its sight, and todo item 227 caps the ones a heavy command starts under run-heavy. lilleman turned down a daemon-wide cap, a dockerd `cgroup-parent` with a memory limit, which would see every container, on 2026-10-24: it is host config, and it would cap a project's heavy work too. Valid while sessions start their work in containers on a machine they share. ## 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