From e675fc296e7432ba5f367cdd02433125d9180169 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=86gir=20M=C3=A1ni=20Hauksson?= <54936225+sourcehawk@users.noreply.github.com> Date: Wed, 30 Sep 2026 01:37:26 +0100 Subject: [PATCH] fix(skills/bounding-heavy-commands): stop a first measurement from holding the whole queue where the hard cap protects the machine A measurement with no --memory reserved every slot of the queue, so no other heavy command on the machine could start while it ran. Where the hard cap is usable, that buys no safety: a command that passes its budget is stopped with exit code 137 and the machine does not swap. One observed first measurement held a 20 GiB queue while it used under 1 GiB. Where the cap is usable, a first measurement, and the largest row of --measure-rows with no --memory, now reserves the slots of the default budget and runs with that budget as its cap. Where there is no cap, it still takes the whole queue. The cap probe runs before this choice, with the default budget, which is the budget of the capped run. The skill and the README now say which case takes the whole queue, and a first measurement that the cap stopped is measured again at two times the default budget, up to the whole queue. Fixes #60 Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- skills/bounding-heavy-commands/SKILL.md | 16 ++--- .../scripts/bounded_run.py | 18 ++--- .../scripts/tests/test_bounded_run.py | 65 ++++++++++++++++++- 4 files changed, 80 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 1839f7a..ba3a47d 100644 --- a/README.md +++ b/README.md @@ -117,7 +117,7 @@ A local path also works for development: `/plugin marketplace add /path/to/featu No further setup is required; the skills derive your repo and build commands from context. Optionally, paste [`templates/project-CLAUDE.md`](templates/project-CLAUDE.md) into your project's `CLAUDE.md` to give every session the workflow overview and the operational rules (commit conventions, safe git staging, GitHub-mutation confirmation). -Keep project memory on to get the most from `bounding-heavy-commands`. In Claude Code that is auto memory, which is on by default. The skill saves the measured memory budget and the bounded command line of each heavy command there, so a later session starts with them. With memory off, each session measures the commands again, and each first measurement runs alone in the queue, so the sessions wait for each other more often. A command that a session did not measure runs under the default budget, which can be less than the command needs. +Keep project memory on to get the most from `bounding-heavy-commands`. In Claude Code that is auto memory, which is on by default. The skill saves the measured memory budget and the bounded command line of each heavy command there, so a later session starts with them. With memory off, each session measures the commands again, and each first measurement holds the default budget, or runs alone in the queue where there is no hard cap, so the sessions wait for each other more often. A command that a session did not measure runs under the default budget, which can be less than the command needs. ## Notes diff --git a/skills/bounding-heavy-commands/SKILL.md b/skills/bounding-heavy-commands/SKILL.md index ca2fad5..3b2387c 100644 --- a/skills/bounding-heavy-commands/SKILL.md +++ b/skills/bounding-heavy-commands/SKILL.md @@ -40,11 +40,11 @@ python3 "${CLAUDE_PLUGIN_ROOT}/skills/bounding-heavy-commands/scripts/bounded_ru | Option | Meaning | | --- | --- | -| `--memory ` | Memory the command may use, such as `6G` or `4096M`. Taken from the record. Without it the wrapper uses its default budget, a quarter of the machine's memory, or the whole queue for a measurement. | +| `--memory ` | Memory the command may use, such as `6G` or `4096M`. Taken from the record. Without it the wrapper uses its default budget, a quarter of the machine's memory. A measurement without it takes the whole queue where there is no hard cap. | | `--cpus ` | Processor limit, as a whole number. Taken from the record. | | `--exclusive ` | A resource that only one command may hold at a time, such as a fixed port. You choose the name. The wrapper keeps the names of each repository apart, and all worktrees of one repository share them. Repeat the option for each resource. | -| `--measure` | Also print a suggested budget. Discovery uses it. With no `--memory`, the command runs alone in the queue. | -| `--measure-rows ,,...` | Measure the command one time at each `--cpus` of the list, one after the other, the largest first. The largest runs with the `--memory` of the call. With no `--memory`, it runs alone in the queue. Each smaller one runs with `--memory` at the suggested budget of the one before it. When the one before it did not exit 0, the smaller one runs with the same `--memory` as that one. Use it in place of `--cpus` and `--measure`. A stop signal ends the list. The exit code is the first exit code of a row that is not 0. | +| `--measure` | Also print a suggested budget. Discovery uses it. With no `--memory`, the command runs with the default budget where the hard cap is available, and alone in the queue where it is not. | +| `--measure-rows ,,...` | Measure the command one time at each `--cpus` of the list, one after the other, the largest first. The largest runs with the `--memory` of the call. With no `--memory`, it runs with the default budget where the hard cap is available, and alone in the queue where it is not. Each smaller one runs with `--memory` at the suggested budget of the one before it. When the one before it did not exit 0, the smaller one runs with the same `--memory` as that one. Use it in place of `--cpus` and `--measure`. A stop signal ends the list. The exit code is the first exit code of a row that is not 0. | | `--label ` | A name for the row lines of a measurement, such as `unit-tests`. Use letters, digits, `.`, `_`, and `-`, and start with a letter or a digit. It changes nothing else. | | `--check` | Run no command. Print the state of the queue now, and for each `--memory`, whether a command of that budget would start now. It takes only `--memory`, which you can repeat. It holds no slot, and its exit code is 0. §The record says how to choose a row with it. | @@ -131,14 +131,14 @@ A part of a recorded command (one test file, one target) uses the bounded comman python3 "${CLAUDE_PLUGIN_ROOT}/skills/bounding-heavy-commands/scripts/bounded_run.py" --measure --cpus -- ``` -5. Record the suggested budget that the wrapper prints, with the peak and the date, as the row at the `--cpus` of the measurement. Record it only from a run in which the command did its full work. The suggested budget is the peak plus a margin, and the margin is larger where the measurement is approximate. A run that stopped early gives a peak that is too small: a suite that stops at its first failure, a command that failed at its start, a run that a signal or the hard cap stopped. Do not record the budget of such a run, and save no record for the command yet. Run the command with `--measure` again each time that you run it, until one run does its full work. A measurement with no `--memory` takes the whole queue, because the peak of the command is not known: it waits until no other bounded command runs, and no other starts while it runs. While it is at the head of the line, the commands behind it wait too (§Rules for the wait). When it waits, tell the user that a first measurement waits for the other bounded commands to end. If the hard cap stopped a measurement with `--memory`, measure again with two times that budget. If it stopped a measurement with no `--memory`, the command needs more memory than the queue has: tell the user, with the budget that the wrapper printed. +5. Record the suggested budget that the wrapper prints, with the peak and the date, as the row at the `--cpus` of the measurement. Record it only from a run in which the command did its full work. The suggested budget is the peak plus a margin, and the margin is larger where the measurement is approximate. A run that stopped early gives a peak that is too small: a suite that stops at its first failure, a command that failed at its start, a run that a signal or the hard cap stopped. Do not record the budget of such a run, and save no record for the command yet. Run the command with `--measure` again each time that you run it, until one run does its full work. A measurement with no `--memory` does not know the peak of the command. Where the hard cap is available, it runs with the default budget, and the cap stops it when it needs more. Where there is no hard cap, it takes the whole queue: it waits until no other bounded command runs, and no other starts while it runs. While it is at the head of the line, the commands behind it wait too (§Rules for the wait). When it waits for the whole queue, tell the user that a first measurement waits for the other bounded commands to end. If the hard cap stopped a measurement, measure again with `--memory` at two times the budget that the wrapper printed. For a measurement with no `--memory`, that is the default budget. The wrapper lowers a budget that is larger than the queue to the whole queue. If the cap stopped a measurement at the whole queue, the command needs more memory than the queue has: tell the user, with the budget that the wrapper printed. 6. Save the record. The measured run is a real run of the command. Its result counts, also when the run stopped early, so do not run the command a second time to get the result. ### Measure the whole project -When the record does not exist (§The record, case 5), do steps 1 to 3 for every heavy command of the project. Then measure them all in one run, in place of steps 4 and 5. Do not measure only the command that you need now. A command that gets its entry later gets its first measurement in the middle of someone's work, and that measurement holds the whole queue while other sessions wait. +When the record does not exist (§The record, case 5), do steps 1 to 3 for every heavy command of the project. Then measure them all in one run, in place of steps 4 and 5. Do not measure only the command that you need now. A command that gets its entry later gets its first measurement in the middle of someone's work. That measurement holds the default budget, and where there is no hard cap it holds the whole queue while other sessions wait. 1. Choose the rows. Measure each command at three `--cpus`: all the machine's processors, half of them, and a quarter of them. Round each one down, use at least 1, and drop a value that is the same as another. A command with `self-bound: none` gets the row at half only: nothing in it reads its `--cpus`, so each row would measure the same command. A command whose next run reuses the output of the run before, such as a build or a generator with a cache, also gets the row at half only: its later rows would do less work and measure a peak that is too small. 2. Write one command line. For each command, write one call of the wrapper with `--measure-rows` and the rows that step 1 chose for it, a `--label` that names the command and no other command of the line, the `--exclusive` options of the command, and its bounded command line. Join the calls with `;`, so that a measurement that fails does not stop the others. Write the path of the wrapper in full, so that the line runs in any shell. Run it from the root of the project: @@ -147,7 +147,7 @@ When the record does not exist (§The record, case 5), do steps 1 to 3 for every python3 "" --measure-rows 16,8,4 --label unit-tests -- ; python3 "" --measure-rows 16,8,4 --label lint -- ``` -3. Ask the user to run the line when the machine is quiet. Do not run it yourself unless the user asks you to. Your message names the commands and the rows that the line measures. It says that the line can take a long time, and that the first row of each command holds the whole queue, so that the heavy commands of the other sessions wait while it runs. It says how to run the line: type `! ` in this session, or run it in a terminal at the root of the project, or tell you to run it. +3. Ask the user to run the line when the machine is quiet. Do not run it yourself unless the user asks you to. Your message names the commands and the rows that the line measures. It says that the line can take a long time, and that the first row of each command holds the default budget, or the whole queue where there is no hard cap, so that the heavy commands of the other sessions can wait while it runs. It says how to run the line: type `! ` in this session, or run it in a terminal at the root of the project, or tell you to run it. 4. Act on the answer: - **The user ran the line.** Read its output into the record (step 5 of this list). When the user ran it in a terminal, you cannot see the output: ask the user for each line that starts with `bounded-run: row`, and for the end of the output of each command, which shows whether the run did its full work. - **The user tells you to run it.** Run it in the background, with no short timeout around it. Continue with work that runs no heavy command. A heavy command that you need before the line ends runs through the wrapper with no `--memory` option. When the line ends, read its output into the record (step 5 of this list). @@ -224,8 +224,8 @@ When Python is missing or too old, and when the fault is a fault of the machine, | "I measured the plain command, the peak is real" | It is the real peak of a command that nothing limits. Bound the command first, then measure the bounded command line at the `--cpus` that you record. | | "The new measurement at a different `--cpus` replaces the old entry" | A different `--cpus` is a different command. Add a row and keep the others. Only a measurement at the same `--cpus` replaces a row. | | "A smaller `--cpus` is a lower budget by another name" | A lower `--memory` on the same command is a guess. A row at a smaller `--cpus` is a smaller command with a measured budget. Use it to start sooner. | -| "I only need the unit tests now, the others get entries when an agent needs them" | Each later first measurement holds the whole queue in the middle of someone's work. When the record does not exist, measure every heavy command of the project with one command line. | -| "I'll run the measurements myself now, it saves the user a step" | The line holds the whole queue for a long time. The user chooses when the machine can give it that time. Ask, and run it only when the user tells you to. | +| "I only need the unit tests now, the others get entries when an agent needs them" | Each later first measurement holds memory in the middle of someone's work, and without the hard cap it holds the whole queue. When the record does not exist, measure every heavy command of the project with one command line. | +| "I'll run the measurements myself now, it saves the user a step" | The line holds memory of the queue for a long time, and without the hard cap it holds the whole queue. The user chooses when the machine can give it that time. Ask, and run it only when the user tells you to. | | "Four subagents is too many for this machine, I'll dispatch two" | The queue limits the commands. Fewer subagents only makes the work slower. | | "Project memory is off, so there is no record to keep" | Discover in this session and keep the result in context. The rule does not depend on the memory. | | "The wrapper cannot run here, so none of this applies" | The fallback applies: one heavy command at a time, toolchain parallelism at half or less, and say so. | diff --git a/skills/bounding-heavy-commands/scripts/bounded_run.py b/skills/bounding-heavy-commands/scripts/bounded_run.py index ad2b6c7..e71d9e8 100755 --- a/skills/bounding-heavy-commands/scripts/bounded_run.py +++ b/skills/bounding-heavy-commands/scripts/bounded_run.py @@ -14,10 +14,11 @@ budget is the suggested budget. A '-' stands for an option that the call did not give. --measure-rows measures the command at each processor limit of the -list, the largest first. The largest runs with --memory, or alone in the queue. -Each smaller one runs with the suggested budget of the one before it, or with -the budget of the one before it when that run did not exit 0. The exit code -is the first exit code that is not 0. +list, the largest first. The largest runs with --memory. Without it, the +largest runs with the default budget where the hard cap is available, and +alone in the queue where it is not. Each smaller one runs with the suggested +budget of the one before it, or with the budget of the one before it when that +run did not exit 0. The exit code is the first exit code that is not 0. --check runs no command and holds no lock when it returns. Its exit code is 0. It prints the state of the queue at that moment, one held line for each @@ -1199,7 +1200,6 @@ def bounded_once(options: Options, environ: Mapping[str, str]) -> Tuple[int, int budget = parse_size_mib(options.memory) else: budget = default_budget_mib(settings.total_mib, settings.slot_mib) - wait_budget = budget command = list(options.command) if shutil.which(command[0], path=environ.get("PATH")) is None: raise WrapperError("cannot start '%s': the command does not exist" % command[0]) @@ -1209,13 +1209,10 @@ def bounded_once(options: Options, environ: Mapping[str, str]) -> Tuple[int, int if settings.reserve_mib >= settings.total_mib: log("WARNING: the reserve of %d MiB leaves no memory of the %d MiB of the machine; the queue does not keep the reserve" % (settings.reserve_mib, settings.total_mib)) most = queue_mib(settings.total_mib, settings.slot_mib, settings.reserve_mib) - if options.measure and options.memory is None: - # The peak of this command is not known yet, so no other command runs beside it. - budget, needed = most, count if budget > most: log("WARNING: the budget of %d MiB is more than the %d MiB of the queue; the budget is %d MiB" % (budget, most, most)) budget = most - wait_budget = min(wait_budget, budget) + wait_budget = budget uid = os.getuid() directory = lock_directory(environ, uid) ensure_lock_directory(directory, uid) @@ -1231,6 +1228,9 @@ def bounded_once(options: Options, environ: Mapping[str, str]) -> Tuple[int, int choice = None if settings.no_cap else choose_cap_prefix(budget, options.cpus, unit, probe_cap) prefix = choice[0] if choice is not None else None capped = prefix is not None + if options.measure and options.memory is None and not capped: + # The peak of this command is not known yet, and without the cap only the queue keeps it from the other commands. + budget, needed = most, count # Every scope of the user that systemd-run makes with no slice option goes to the slice of the probe. scopes = scope_parent_directory(choice[1]) if choice is not None else None # A wrapper that runs inside a bounded scope does its work in the budget of that scope. diff --git a/skills/bounding-heavy-commands/scripts/tests/test_bounded_run.py b/skills/bounding-heavy-commands/scripts/tests/test_bounded_run.py index 7e9b38d..37340ac 100644 --- a/skills/bounding-heavy-commands/scripts/tests/test_bounded_run.py +++ b/skills/bounding-heavy-commands/scripts/tests/test_bounded_run.py @@ -1889,7 +1889,7 @@ def test_a_run_that_does_not_measure_prints_no_row_line(self): def test_rows_run_largest_first_and_each_smaller_row_gets_the_budget_of_the_row_before_it(self): code = self.run_bounded(["--measure-rows", "2,8,4", "--label", "build"], [(0, 1000), (0, 500), (0, 200)]) self.assertEqual(code, 0) - # The first row has no budget yet, so it holds the whole queue of 8192 MiB. + # The first row has no budget yet and no cap, so it holds the whole queue of 8192 MiB. self.assertEqual(self.runs, [(8, 8192), (4, 1536), (2, 768)]) self.assertEqual(self.row_lines(), [ "row label=build cpus=8 budget=1536M peak=1000M exact=no exit=0", @@ -1960,6 +1960,65 @@ def once(options, environ): self.assertEqual(calls, [8, 4]) +class FirstMeasurementTest(unittest.TestCase): + """Calls bounded() in this process with eight slots of 2 GiB, so the default budget of 4096 MiB needs two. + + reserve, run_command, and probe_cap are stubs: no lock is waited for and no command or probe runs. + """ + + def setUp(self): + patcher = mock.patch.object(bounded_run, "log") + patcher.start() + self.addCleanup(patcher.stop) + self.base = tempfile.TemporaryDirectory() + self.addCleanup(self.base.cleanup) + self.environ = dict(os.environ) + self.environ.pop("BOUNDED_RUN_ACTIVE", None) + self.environ.pop("BOUNDED_RUN_NO_CAP", None) + self.environ.update({ + "BOUNDED_RUN_LOCK_DIR": os.path.join(self.base.name, "locks"), + "BOUNDED_RUN_TOTAL_MIB": "16384", + "BOUNDED_RUN_RESERVE_MIB": "0", + "BOUNDED_RUN_SLOT_MIB": "2048", + "BOUNDED_RUN_POLL_SECONDS": "0.05", + "BOUNDED_RUN_MEMORY_WAIT_SECONDS": "0", + "BOUNDED_RUN_SAMPLE_SECONDS": "0.1", + "BOUNDED_RUN_FREE_MIB": "1048576", + }) + + def run_bounded(self, arguments, probe_output): + """Returns, for each run, the slots it reserved, the budget its command got, and the MemoryMax of its cap or None.""" + runs = [] + + def fake_reserve(directory, count, needed, exclusive_files, poll_seconds): + runs.append([needed]) + return bounded_run.Reservation([]) + + def fake_run_command(command, environ, on_start=None): + caps = [int(part[len("MemoryMax="):-1]) for part in command if part.startswith("MemoryMax=")] + runs[-1] += [int(environ["BOUNDED_RUN_MEMORY_MIB"]), caps[0] if caps else None] + return 0, 1024 + + options = bounded_run.parse_arguments(arguments + ["--", sys.executable, "-c", "pass"]) + with mock.patch.object(bounded_run, "probe_cap", return_value=probe_output), \ + mock.patch.object(bounded_run, "reserve", fake_reserve), \ + mock.patch.object(bounded_run, "run_command", fake_run_command): + self.assertEqual(bounded_run.bounded(options, self.environ), 0) + return runs + + def test_with_a_cap_a_first_measurement_takes_the_slots_of_the_default_budget(self): + self.assertEqual(self.run_bounded(["--measure"], ""), [[2, 4096, 4096]]) + + def test_without_a_cap_a_first_measurement_takes_the_whole_queue(self): + self.assertEqual(self.run_bounded(["--measure"], None), [[8, 16384, None]]) + + def test_with_a_cap_the_largest_measured_row_takes_the_slots_of_the_default_budget(self): + self.assertEqual(self.run_bounded(["--measure-rows", "4,2"], "")[0], [2, 4096, 4096]) + + def test_without_a_cap_the_largest_measured_row_takes_the_whole_queue(self): + self.assertEqual(self.run_bounded(["--measure-rows", "4,2"], None)[0], [8, 16384, None]) + + class CheckTest(unittest.TestCase): """Calls check() in this process with four slots of 2 GiB, a headroom of 1024 MiB, and no hard cap. @@ -2661,7 +2720,7 @@ def test_without_a_cap_the_output_says_so(self): output, errors = process.communicate(timeout=60) self.assertIn("no hard cap is available here", errors) - def test_a_first_measurement_runs_alone_in_the_queue(self): + def test_a_first_measurement_runs_alone_in_the_queue_without_a_cap(self): command = [sys.executable, "-c", WORKER, "measured"] first = self.wrapper(["--measure"], command, HOLD="1.0") self.wait_for_event("start", "measured") @@ -2671,7 +2730,7 @@ def test_a_first_measurement_runs_alone_in_the_queue(self): self.assertEqual(self.peak_concurrency(), 1) self.assertIn("bounded-run: budget 4096 MiB, peak ", self.stderr_of(first)) - def test_a_first_measurement_waits_for_the_commands_that_run(self): + def test_a_first_measurement_waits_for_the_commands_that_run_without_a_cap(self): first = self.worker("first", "2G", hold="1.0") self.wait_for_event("start", "first") second = self.wrapper(["--measure"], [sys.executable, "-c", WORKER, "measured"], HOLD="0.1")