Skip to content

Commit bca2fa1

Browse files
sourcehawkclaude
andauthored
fix(skills/bounding-heavy-commands): stop first measurements from running together (#50)
* fix(skills/bounding-heavy-commands): run a first measurement alone in the queue (#49) A measurement with no budget took the default budget, a quarter of the machine, while the peak of the command was not known. Several sessions could measure several commands together, and with no hard cap nothing stopped them. Such a measurement now takes each slot of the queue. It waits for the free memory of the default budget only, because the memory of the full queue is seldom free. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * fix(skills/bounding-heavy-commands): send SIGQUIT to the command (#49) The quit signal of the terminal ended the wrapper and not the command. On a system with no parent-death signal the command lived on, and the queue counted its slots as free. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * refactor(skills/bounding-heavy-commands): remove test comments that describe the next line (#49) Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * docs(README): say what a session with no project memory costs (#49) The text said that the queue still protects the machine with memory off. For a command that was never measured, on a machine with no hard cap, the default budget can be less than the command needs. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * fix(skills/bounding-heavy-commands): record a budget from a full run only, and dispatch in sequence with no wrapper (#49) An agent recorded the budget of a test suite that stopped at its first failure, in the third of about 200 files. The budget was too small and stayed in the record. The skill now takes a budget only from a run that did its full work, and says that a first measurement takes the whole queue. With no wrapper, no lock makes subagents take turns, and the skills gave the orchestrator no means to keep two heavy commands apart. The subagents that run a heavy command are now dispatched one after the other. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * fix(skills/bounding-heavy-commands): keep a stop signal that comes before the capped command runs (#49) The process inside the cap took a stop signal and did nothing with it. A signal that came before the command existed was lost, and the command ran to its end. The process now sends that signal to the command after the start, and SIGKILL when more than one came. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
1 parent 0e68383 commit bca2fa1

5 files changed

Lines changed: 97 additions & 29 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, 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.
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.
121121

122122
## Notes
123123

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ python3 "${CLAUDE_PLUGIN_ROOT}/skills/bounding-heavy-commands/scripts/bounded_ru
3636

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

4444
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).
4545

@@ -90,10 +90,10 @@ A part of a recorded command (one test file, one target) uses the bounded comman
9090
python3 "${CLAUDE_PLUGIN_ROOT}/skills/bounding-heavy-commands/scripts/bounded_run.py" --measure -- <command>
9191
```
9292

93-
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.
93+
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.
9494
6. Save the record.
9595

96-
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.
96+
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.
9797

9898
## Rules for the wait
9999

@@ -127,7 +127,7 @@ On a machine on which the wrapper can run, every dispatch prompt for a subagent
127127
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."
128128
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.
129129

130-
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).
130+
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.
131131

132132
## When the wrapper cannot run
133133

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

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

140-
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.
140+
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.
141141

142142
## Red flags
143143

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ def forward(signum: int, frame: object) -> None:
655655
except (ProcessLookupError, PermissionError):
656656
pass
657657

658-
previous = {signum: signal.signal(signum, forward) for signum in (signal.SIGINT, signal.SIGTERM, signal.SIGHUP)}
658+
previous = {signum: signal.signal(signum, forward) for signum in (signal.SIGINT, signal.SIGTERM, signal.SIGHUP, signal.SIGQUIT)}
659659
try:
660660
try:
661661
process = subprocess.Popen(list(command), env=dict(environ), start_new_session=True, preexec_fn=parent_death_signal())
@@ -701,16 +701,27 @@ def inside_cap(
701701
The cgroup exists for as long as this process is in it. Thus the peak is readable after the
702702
command stops, which is not possible from outside the cgroup.
703703
"""
704+
early: List[int] = []
705+
started: List[int] = []
706+
704707
def keep_running(signum: int, frame: object) -> None:
705-
pass
708+
# The wrapper sends a signal to the process group, so the command gets its own copy once it runs.
709+
if not started:
710+
early.append(signum)
706711

707-
previous = {signum: signal.signal(signum, keep_running) for signum in (signal.SIGINT, signal.SIGTERM, signal.SIGHUP)}
712+
previous = {signum: signal.signal(signum, keep_running) for signum in (signal.SIGINT, signal.SIGTERM, signal.SIGHUP, signal.SIGQUIT)}
708713
try:
709714
try:
710715
process = subprocess.Popen(list(command), preexec_fn=parent_death_signal())
711716
except OSError as error:
712717
log("cannot start '%s': %s" % (command[0], error))
713718
return EXIT_WRAPPER
719+
started.append(process.pid)
720+
if early:
721+
try:
722+
os.kill(process.pid, early[0] if len(early) == 1 else signal.SIGKILL)
723+
except (ProcessLookupError, PermissionError):
724+
pass
714725
_, status = os.waitpid(process.pid, 0)
715726
process.returncode = exit_code_of(status)
716727
# After the kernel kills a process of the scope for memory, systemd can stop the scope with SIGTERM.
@@ -785,6 +796,7 @@ def bounded(options: Options, environ: Mapping[str, str]) -> int:
785796
budget = parse_size_mib(options.memory)
786797
else:
787798
budget = default_budget_mib(settings.total_mib, settings.slot_mib)
799+
wait_budget = budget
788800
command = list(options.command)
789801
if shutil.which(command[0], path=environ.get("PATH")) is None:
790802
raise WrapperError("cannot start '%s': the command does not exist" % command[0])
@@ -794,9 +806,13 @@ def bounded(options: Options, environ: Mapping[str, str]) -> int:
794806
if settings.reserve_mib >= settings.total_mib:
795807
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))
796808
most = queue_mib(settings.total_mib, settings.slot_mib, settings.reserve_mib)
809+
if options.measure and options.memory is None:
810+
# The peak of this command is not known yet, so no other command runs beside it.
811+
budget, needed = most, count
797812
if budget > most:
798813
log("WARNING: the budget of %d MiB is more than the %d MiB of the queue; the budget is %d MiB" % (budget, most, most))
799814
budget = most
815+
wait_budget = min(wait_budget, budget)
800816
uid = os.getuid()
801817
directory = lock_directory(environ, uid)
802818
ensure_lock_directory(directory, uid)
@@ -842,7 +858,7 @@ def read_sample() -> Optional[int]:
842858
reported: Optional[int] = None
843859
reservation = reserve(directory, count, needed, exclusive_files, settings.poll_seconds)
844860
try:
845-
wait_for_memory(budget, settings.memory_wait_seconds, settings.poll_seconds)
861+
wait_for_memory(wait_budget, settings.memory_wait_seconds, settings.poll_seconds)
846862
code, maxrss = run_command(command, child_environ, start_tracker)
847863
tracked = trackers[0].finish() if trackers else None
848864
finally:

0 commit comments

Comments
 (0)