Skip to content

Commit 52152f7

Browse files
sourcehawkclaude
andcommitted
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 <noreply@anthropic.com>
1 parent 2d86e2b commit 52152f7

4 files changed

Lines changed: 80 additions & 21 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ A local path also works for development: `/plugin marketplace add /path/to/featu
117117

118118
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).
119119

120-
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.
120+
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.
121121

122122
## Notes
123123

‎skills/bounding-heavy-commands/SKILL.md‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ python3 "${CLAUDE_PLUGIN_ROOT}/skills/bounding-heavy-commands/scripts/bounded_ru
4040

4141
| Option | Meaning |
4242
| --- | --- |
43-
| `--memory <budget>` | 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. |
43+
| `--memory <budget>` | 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. |
4444
| `--cpus <n>` | Processor limit, as a whole number. Taken from the record. |
4545
| `--exclusive <name>` | 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. |
46-
| `--measure` | Also print a suggested budget. Discovery uses it. With no `--memory`, the command runs alone in the queue. |
47-
| `--measure-rows <n>,<n>,...` | 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. |
46+
| `--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. |
47+
| `--measure-rows <n>,<n>,...` | 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. |
4848
| `--label <name>` | 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. |
4949
| `--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. |
5050

@@ -131,14 +131,14 @@ A part of a recorded command (one test file, one target) uses the bounded comman
131131
python3 "${CLAUDE_PLUGIN_ROOT}/skills/bounding-heavy-commands/scripts/bounded_run.py" --measure --cpus <n> -- <bounded command>
132132
```
133133

134-
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.
134+
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.
135135
6. Save the record.
136136

137137
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.
138138

139139
### Measure the whole project
140140

141-
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.
141+
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.
142142

143143
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.
144144
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
147147
python3 "<path of the wrapper>" --measure-rows 16,8,4 --label unit-tests -- <bounded command> ; python3 "<path of the wrapper>" --measure-rows 16,8,4 --label lint -- <bounded command>
148148
```
149149

150-
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 `! <the line>` in this session, or run it in a terminal at the root of the project, or tell you to run it.
150+
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 `! <the line>` in this session, or run it in a terminal at the root of the project, or tell you to run it.
151151
4. Act on the answer:
152152
- **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.
153153
- **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,
224224
| "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. |
225225
| "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. |
226226
| "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. |
227-
| "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. |
228-
| "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. |
227+
| "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. |
228+
| "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. |
229229
| "Four subagents is too many for this machine, I'll dispatch two" | The queue limits the commands. Fewer subagents only makes the work slower. |
230230
| "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. |
231231
| "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. |

‎skills/bounding-heavy-commands/scripts/bounded_run.py‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414
1515
budget is the suggested budget. A '-' stands for an option that the call did
1616
not give. --measure-rows measures the command at each processor limit of the
17-
list, the largest first. The largest runs with --memory, or alone in the queue.
18-
Each smaller one runs with the suggested budget of the one before it, or with
19-
the budget of the one before it when that run did not exit 0. The exit code
20-
is the first exit code that is not 0.
17+
list, the largest first. The largest runs with --memory. Without it, the
18+
largest runs with the default budget where the hard cap is available, and
19+
alone in the queue where it is not. Each smaller one runs with the suggested
20+
budget of the one before it, or with the budget of the one before it when that
21+
run did not exit 0. The exit code is the first exit code that is not 0.
2122
2223
--check runs no command and holds no lock when it returns. Its exit code is 0.
2324
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
11991200
budget = parse_size_mib(options.memory)
12001201
else:
12011202
budget = default_budget_mib(settings.total_mib, settings.slot_mib)
1202-
wait_budget = budget
12031203
command = list(options.command)
12041204
if shutil.which(command[0], path=environ.get("PATH")) is None:
12051205
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
12091209
if settings.reserve_mib >= settings.total_mib:
12101210
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))
12111211
most = queue_mib(settings.total_mib, settings.slot_mib, settings.reserve_mib)
1212-
if options.measure and options.memory is None:
1213-
# The peak of this command is not known yet, so no other command runs beside it.
1214-
budget, needed = most, count
12151212
if budget > most:
12161213
log("WARNING: the budget of %d MiB is more than the %d MiB of the queue; the budget is %d MiB" % (budget, most, most))
12171214
budget = most
1218-
wait_budget = min(wait_budget, budget)
1215+
wait_budget = budget
12191216
uid = os.getuid()
12201217
directory = lock_directory(environ, uid)
12211218
ensure_lock_directory(directory, uid)
@@ -1231,6 +1228,9 @@ def bounded_once(options: Options, environ: Mapping[str, str]) -> Tuple[int, int
12311228
choice = None if settings.no_cap else choose_cap_prefix(budget, options.cpus, unit, probe_cap)
12321229
prefix = choice[0] if choice is not None else None
12331230
capped = prefix is not None
1231+
if options.measure and options.memory is None and not capped:
1232+
# The peak of this command is not known yet, and without the cap only the queue keeps it from the other commands.
1233+
budget, needed = most, count
12341234
# Every scope of the user that systemd-run makes with no slice option goes to the slice of the probe.
12351235
scopes = scope_parent_directory(choice[1]) if choice is not None else None
12361236
# A wrapper that runs inside a bounded scope does its work in the budget of that scope.

0 commit comments

Comments
 (0)