Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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, the queue still protects the machine, but each session measures the commands again and uses the default budget for a command that it did not measure.
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.

## Notes

Expand Down
12 changes: 6 additions & 6 deletions skills/bounding-heavy-commands/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ python3 "${CLAUDE_PLUGIN_ROOT}/skills/bounding-heavy-commands/scripts/bounded_ru

| Option | Meaning |
| --- | --- |
| `--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. |
| `--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. |
| `--cpus <n>` | Processor limit, as a whole number. Taken from the record. |
| `--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. |
| `--measure` | Also print a suggested budget. Discovery uses it. |
| `--measure` | Also print a suggested budget. Discovery uses it. With no `--memory`, the command runs alone in the queue. |

The wrapper does four things in sequence. It waits for its turn in the queue. It waits until the memory of its budget is free. It runs the command. It prints the budget and the measured peak on the error stream. Each line that the wrapper prints starts with `bounded-run:`. Its exit code is the command's exit code. Exit code 125, with a `bounded-run:` line that names a fault and with no line for the budget and the peak, means that the wrapper itself failed (see §When the wrapper cannot run).

Expand Down Expand Up @@ -90,10 +90,10 @@ 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 -- <command>
```

5. Record the suggested budget that the wrapper prints. It is the peak plus a margin, and the margin is larger where the measurement is approximate. The measurement runs under the default budget. If the hard cap stopped the command during the measurement, run it again with `--memory` at two times the default budget that the wrapper printed.
5. Record the suggested budget that the wrapper prints, but only from a run in which the command did its full work. It 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. 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.
6. Save the record.

The measured run is a real run of the command. Its result counts, so do not run the command a second time to get the result.
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.

## Rules for the wait

Expand Down Expand Up @@ -127,7 +127,7 @@ On a machine on which the wrapper can run, every dispatch prompt for a subagent
2. This rule, in these words: "Run the bounded commands as written. You can make two changes: a narrower argument at the end of the line, such as one test file, and one more `--exclusive <name>` option after a collision on a port or a lock, which you name in your report. Do not run the plain form of a bounded command or of a part of it. For a heavy command that is not in this prompt, use the same wrapper with no `--memory` option. If the file of the wrapper does not exist, stop and report that. Set no environment variable whose name starts with `BOUNDED_RUN_`. A command can wait in the queue for minutes before it starts, so run it in the background and do not put a short timeout around it. A long wait means that the machine is full; it is not a fault to repair. Say in your report how long you waited."
3. Each command that an instruction exempts from the wrapper, in its plain form, in a list of its own, with the instruction that exempts it. The rule of item 2 does not apply to that list. An instruction of the user that covers one run goes to the one subagent that does that run, and to no other.

Do not dispatch fewer subagents to protect the machine. The queue protects it. Subagents that wait in the queue cost time, not memory. The one exception is a machine on which the wrapper cannot run (see the next section).
Do not dispatch fewer subagents to protect the machine. The queue protects it. Subagents that wait in the queue cost time, not memory. On a machine on which the wrapper cannot run, the next section says how to dispatch.

## When the wrapper cannot run

Expand All @@ -137,7 +137,7 @@ When `python3` reports that it cannot open the file of the wrapper, the path in

When the wrapper ends with exit code 125, read its `bounded-run:` message. A fault of your call (an option, a name, a command that does not exist) is yours to correct. A fault of the machine (the wrapper cannot make or lock its files) means that the wrapper cannot run here: tell the user once per session, with the message.

When Python is missing or too old, and when the fault is a fault of the machine, continue under these fallback rules. Run one heavy command at a time, never two in parallel, and set the toolchain's parallelism to half the machine's processors or fewer. Your subagents count as your session: no queue protects the machine now, so dispatch them so that only one of them runs a heavy command at a time. Their prompts carry the plain commands and these fallback rules, in place of the three items of §Dispatching subagents. The fallback protects the machine from your session only, not from the other sessions. Tell the user that in your report.
When Python is missing or too old, and when the fault is a fault of the machine, continue under these fallback rules. Run one heavy command at a time, never two in parallel, and set the toolchain's parallelism to half the machine's processors or fewer. Your subagents count as your session: no queue protects the machine now, and no lock makes them take turns. So dispatch one after the other the subagents that run a heavy command, and start the next one only after the one before it gave its report. Subagents that run no heavy command can run together. Their prompts carry the plain commands and these fallback rules, in place of the three items of §Dispatching subagents. The fallback protects the machine from your session only, not from the other sessions. Tell the user that in your report.

## Red flags

Expand Down
24 changes: 20 additions & 4 deletions skills/bounding-heavy-commands/scripts/bounded_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ def forward(signum: int, frame: object) -> None:
except (ProcessLookupError, PermissionError):
pass

previous = {signum: signal.signal(signum, forward) for signum in (signal.SIGINT, signal.SIGTERM, signal.SIGHUP)}
previous = {signum: signal.signal(signum, forward) for signum in (signal.SIGINT, signal.SIGTERM, signal.SIGHUP, signal.SIGQUIT)}
try:
try:
process = subprocess.Popen(list(command), env=dict(environ), start_new_session=True, preexec_fn=parent_death_signal())
Expand Down Expand Up @@ -701,16 +701,27 @@ def inside_cap(
The cgroup exists for as long as this process is in it. Thus the peak is readable after the
command stops, which is not possible from outside the cgroup.
"""
early: List[int] = []
started: List[int] = []

def keep_running(signum: int, frame: object) -> None:
pass
# The wrapper sends a signal to the process group, so the command gets its own copy once it runs.
if not started:
early.append(signum)

previous = {signum: signal.signal(signum, keep_running) for signum in (signal.SIGINT, signal.SIGTERM, signal.SIGHUP)}
previous = {signum: signal.signal(signum, keep_running) for signum in (signal.SIGINT, signal.SIGTERM, signal.SIGHUP, signal.SIGQUIT)}
Comment thread
sourcehawk marked this conversation as resolved.
Resolved
try:
try:
process = subprocess.Popen(list(command), preexec_fn=parent_death_signal())
except OSError as error:
log("cannot start '%s': %s" % (command[0], error))
return EXIT_WRAPPER
started.append(process.pid)
if early:
try:
os.kill(process.pid, early[0] if len(early) == 1 else signal.SIGKILL)
except (ProcessLookupError, PermissionError):
pass
_, status = os.waitpid(process.pid, 0)
process.returncode = exit_code_of(status)
# After the kernel kills a process of the scope for memory, systemd can stop the scope with SIGTERM.
Expand Down Expand Up @@ -785,6 +796,7 @@ def bounded(options: Options, environ: Mapping[str, str]) -> 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])
Expand All @@ -794,9 +806,13 @@ def bounded(options: Options, environ: Mapping[str, str]) -> 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)
uid = os.getuid()
directory = lock_directory(environ, uid)
ensure_lock_directory(directory, uid)
Expand Down Expand Up @@ -842,7 +858,7 @@ def read_sample() -> Optional[int]:
reported: Optional[int] = None
reservation = reserve(directory, count, needed, exclusive_files, settings.poll_seconds)
try:
wait_for_memory(budget, settings.memory_wait_seconds, settings.poll_seconds)
wait_for_memory(wait_budget, settings.memory_wait_seconds, settings.poll_seconds)
code, maxrss = run_command(command, child_environ, start_tracker)
tracked = trackers[0].finish() if trackers else None
finally:
Expand Down
Loading
Loading