Skip to content

Commit 1fb65ef

Browse files
sourcehawkclaude
andcommitted
fix(skills/bounding-heavy-commands): let a whole-queue budget start and keep the tests off the machine's load
A budget of the whole queue could never start. The wait needed the budget plus a headroom of max(1 GiB, a tenth of the memory) free, and the queue already leaves a reserve of max(2 GiB, a quarter of the memory) outside it. On a 27703 MiB machine a budget of the 20480 MiB queue needed about 23250 MiB free, which a machine with a desktop never has, so the command waited forever. headroom_mib now takes the budget and the memory of the queue and never lets the two together need more than the queue, so the headroom shrinks to zero as the budget nears the whole queue and the reserve is the floor for what stays outside. The tests depended on the load of the real machine. The wrappers that the process tests start, and the in-process bounded() tests, read the real free memory, and with the hard cap they counted the unused budgets of every real bounded-run scope of the user. On a busy machine they waited past their timeouts. The new test-only variable BOUNDED_RUN_FREE_MIB, listed in the usage text with the other values for the tests, gives the wait a fixed free memory and counts no unused budgets. The tests of the counting itself still use fake cgroup trees and do not set it. Refs #55 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent afc119d commit 1fb65ef

3 files changed

Lines changed: 81 additions & 11 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ python3 "${CLAUDE_PLUGIN_ROOT}/skills/bounding-heavy-commands/scripts/bounded_ru
4343
| `--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. |
4444
| `--measure` | Also print a suggested budget. Discovery uses it. With no `--memory`, the command runs alone in the queue. |
4545

46-
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. For the wait, the free memory is the memory that the machine has free now, less the part of their budgets that the running bounded commands do not use yet, less a headroom for the programs outside the queue. The headroom is a tenth of the memory of the machine, and at least 1 GiB. The wait has no time limit: the command does not start before its memory is free, and the wrapper prints a line at intervals while it waits. 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).
46+
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. For the wait, the free memory is the memory that the machine has free now, less the part of their budgets that the running bounded commands do not use yet, less a headroom for the programs outside the queue. The headroom is a tenth of the memory of the machine, and at least 1 GiB. The budget and the headroom together never need more than the memory of the queue, so a budget of the whole queue does not wait for a headroom. The wait has no time limit: the command does not start before its memory is free, and the wrapper prints a line at intervals while it waits. 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).
4747

4848
The wrapper sets `BOUNDED_RUN_CPUS` and `BOUNDED_RUN_MEMORY_MIB` in the environment of the command, from `--cpus` and `--memory`. A recorded command line can read them and hand them to the toolchain's own parallelism and memory settings. You do not set them: you change them with the two options.
4949

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
Values for the tests of this script, and for its maintainers:
2626
2727
BOUNDED_RUN_TOTAL_MIB memory of the machine, in place of the measured value
28+
BOUNDED_RUN_FREE_MIB free memory for the wait, in place of the measured free memory less the unused budgets
2829
BOUNDED_RUN_POLL_SECONDS time between two tries for the slots (default 2)
2930
BOUNDED_RUN_SAMPLE_SECONDS time between two memory samples (default 1)
3031
BOUNDED_RUN_LOCK_DIR directory of the lock files, as an absolute path
@@ -188,9 +189,13 @@ def default_reserve_mib(total_mib: int) -> int:
188189
return max(MINIMUM_RESERVE_MIB, total_mib // 4)
189190

190191

191-
def headroom_mib(total_mib: int) -> int:
192-
"""Returns the memory that the wait for free memory keeps free for the programs outside the queue."""
193-
return max(MINIMUM_HEADROOM_MIB, total_mib // 10)
192+
def headroom_mib(total_mib: int, budget_mib: int, queue_mib: int) -> int:
193+
"""Returns the memory that the wait for free memory keeps free beside the budget for the programs outside the queue.
194+
195+
The budget and the headroom together never need more than queue_mib, so a budget of the whole queue gets no headroom.
196+
"""
197+
# The reserve of the queue already keeps memory for the programs outside it.
198+
return max(0, min(max(MINIMUM_HEADROOM_MIB, total_mib // 10), queue_mib - budget_mib))
194199

195200

196201
def slot_count(total_mib: int, slot_mib: int, reserve_mib: int) -> int:
@@ -225,6 +230,8 @@ def __init__(self, environ: Mapping[str, str], read_total: Callable[[], int] = t
225230
self.memory_wait_seconds = _number(environ, "BOUNDED_RUN_MEMORY_WAIT_SECONDS", MEMORY_WAIT_SECONDS)
226231
self.sample_seconds = _number(environ, "BOUNDED_RUN_SAMPLE_SECONDS", SAMPLE_SECONDS)
227232
self.no_cap = bool(environ.get("BOUNDED_RUN_NO_CAP"))
233+
free = environ.get("BOUNDED_RUN_FREE_MIB")
234+
self.free_mib = int(_number(environ, "BOUNDED_RUN_FREE_MIB", 0)) if free else None
228235
if self.slot_mib <= 0:
229236
raise WrapperError("BOUNDED_RUN_SLOT_MIB must be more than zero")
230237
# A loop that sleeps for zero seconds never ends its wait and takes a full processor.
@@ -903,8 +910,13 @@ def bounded(options: Options, environ: Mapping[str, str]) -> int:
903910
# A wrapper that runs inside a bounded scope does its work in the budget of that scope.
904911
skip = [name for name in (unit + ".scope", own_scope_name()) if name is not None]
905912

913+
def read_available() -> Optional[int]:
914+
return available_memory_mib() if settings.free_mib is None else settings.free_mib
915+
906916
def read_outstanding() -> Optional[int]:
907-
return None if scopes is None else unused_budget_mib(scopes, skip)
917+
if settings.free_mib is not None or scopes is None:
918+
return None
919+
return unused_budget_mib(scopes, skip)
908920

909921
peak_file = os.path.join(directory, "peak-%s" % unit)
910922
if prefix is not None:
@@ -939,7 +951,8 @@ def read_sample() -> Optional[int]:
939951
try:
940952
wait_for_memory(
941953
wait_budget, settings.memory_wait_seconds, settings.poll_seconds,
942-
headroom_mib=headroom_mib(settings.total_mib), read_outstanding=read_outstanding,
954+
headroom_mib=headroom_mib(settings.total_mib, wait_budget, most),
955+
read_available=read_available, read_outstanding=read_outstanding,
943956
)
944957
code, maxrss = run_command(command, child_environ, start_tracker)
945958
tracked = trackers[0].finish() if trackers else None

‎skills/bounding-heavy-commands/scripts/tests/test_bounded_run.py‎

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ def test_says_who_the_test_values_are_for(self):
6060
self.assertIn("Values for the tests of this script, and for its maintainers:", text)
6161
self.assertNotIn("diagnosis", text)
6262

63+
def test_lists_the_free_memory_for_the_tests(self):
64+
values = bounded_run.__doc__.split("Values for the tests of this script, and for its maintainers:")[1]
65+
self.assertIn("BOUNDED_RUN_FREE_MIB", values)
66+
6367

6468
class SizeTest(unittest.TestCase):
6569
def test_reads_gibibytes(self):
@@ -228,6 +232,10 @@ def test_defaults(self):
228232
self.assertEqual(settings.poll_seconds, 2.0)
229233
self.assertEqual(settings.memory_wait_seconds, 300.0)
230234
self.assertFalse(settings.no_cap)
235+
self.assertIsNone(settings.free_mib)
236+
237+
def test_a_set_free_memory(self):
238+
self.assertEqual(bounded_run.Settings({"BOUNDED_RUN_FREE_MIB": "3000"}, read_total=lambda: 1).free_mib, 3000)
231239

232240
def test_overrides(self):
233241
settings = bounded_run.Settings({
@@ -805,10 +813,16 @@ def test_a_line_of_the_wait_says_when_the_unused_budgets_are_not_counted(self):
805813

806814
class HeadroomTest(unittest.TestCase):
807815
def test_a_tenth_of_the_memory(self):
808-
self.assertEqual(bounded_run.headroom_mib(27703), 2770)
816+
self.assertEqual(bounded_run.headroom_mib(27703, 2048, 20480), 2770)
809817

810818
def test_never_less_than_the_minimum(self):
811-
self.assertEqual(bounded_run.headroom_mib(4096), 1024)
819+
self.assertEqual(bounded_run.headroom_mib(4096, 1024, 4096), 1024)
820+
821+
def test_no_headroom_for_a_budget_of_the_whole_queue(self):
822+
self.assertEqual(bounded_run.headroom_mib(27703, 20480, 20480), 0)
823+
824+
def test_the_budget_and_the_headroom_never_need_more_than_the_queue(self):
825+
self.assertEqual(bounded_run.headroom_mib(27703, 19000, 20480), 1480)
812826

813827

814828
class UnusedBudgetTest(unittest.TestCase):
@@ -1372,6 +1386,7 @@ def setUp(self):
13721386
"BOUNDED_RUN_POLL_SECONDS": "0.05",
13731387
"BOUNDED_RUN_MEMORY_WAIT_SECONDS": "0",
13741388
"BOUNDED_RUN_SAMPLE_SECONDS": "0.1",
1389+
"BOUNDED_RUN_FREE_MIB": "1048576",
13751390
})
13761391

13771392
def test_the_peak_file_is_removed_when_the_run_fails_after_the_start(self):
@@ -1417,20 +1432,61 @@ def fake_run_command(command, environ, on_start=None):
14171432
self.assertEqual(waits, [4096])
14181433
self.assertIn("MemoryMax=4096M", commands[0])
14191434

1420-
def wait_arguments(self, probe_output):
1435+
def wait_arguments(self, probe_output, environ=None):
14211436
waits = []
14221437
options = bounded_run.Options()
14231438
options.memory = "1G"
14241439
options.command = [sys.executable, "-c", "pass"]
1440+
if environ is None:
1441+
environ = dict(self.environ)
1442+
del environ["BOUNDED_RUN_FREE_MIB"]
14251443
with mock.patch.object(bounded_run, "probe_cap", return_value=probe_output), \
14261444
mock.patch.object(bounded_run, "own_scope_name", return_value="bounded-run-2-111111.scope"), \
14271445
mock.patch.object(bounded_run, "wait_for_memory", side_effect=lambda *arguments, **keywords: waits.append(keywords)), \
14281446
mock.patch.object(bounded_run, "run_command", return_value=(0, 0)):
1429-
self.assertEqual(bounded_run.bounded(options, self.environ), 0)
1447+
self.assertEqual(bounded_run.bounded(options, environ), 0)
14301448
return waits[0]
14311449

14321450
def test_the_wait_keeps_a_headroom_of_the_memory_of_the_machine(self):
1433-
self.assertEqual(self.wait_arguments(None)["headroom_mib"], bounded_run.headroom_mib(4096))
1451+
self.assertEqual(self.wait_arguments(None)["headroom_mib"], bounded_run.headroom_mib(4096, 1024, 4096))
1452+
1453+
def run_with_free_memory(self, memory, total, free):
1454+
options = bounded_run.Options()
1455+
options.memory = memory
1456+
options.command = [sys.executable, "-c", "pass"]
1457+
environ = dict(self.environ, BOUNDED_RUN_TOTAL_MIB=str(total), BOUNDED_RUN_RESERVE_MIB="", BOUNDED_RUN_FREE_MIB=str(free))
1458+
1459+
real_wait = bounded_run.wait_for_memory
1460+
1461+
def sleep(seconds):
1462+
raise AssertionError("the command waits")
1463+
1464+
def wait(*arguments, **keywords):
1465+
return real_wait(*arguments, sleep=sleep, **keywords)
1466+
1467+
with mock.patch.object(bounded_run, "probe_cap", return_value=None), \
1468+
mock.patch.object(bounded_run, "wait_for_memory", wait), \
1469+
mock.patch.object(bounded_run, "run_command", return_value=(0, 0)):
1470+
return bounded_run.bounded(options, environ)
1471+
1472+
def test_a_budget_of_the_whole_queue_starts_when_the_memory_of_the_queue_is_free(self):
1473+
queue = bounded_run.queue_mib(27703, 2048, bounded_run.default_reserve_mib(27703))
1474+
self.assertEqual(self.run_with_free_memory("%dM" % queue, 27703, queue), 0)
1475+
1476+
def test_a_small_budget_waits_for_its_headroom(self):
1477+
with self.assertRaisesRegex(AssertionError, "the command waits"):
1478+
self.run_with_free_memory("2G", 27703, 2048 + bounded_run.headroom_mib(27703, 2048, 20480) - 1)
1479+
1480+
def test_a_small_budget_starts_with_its_headroom_free(self):
1481+
self.assertEqual(self.run_with_free_memory("2G", 27703, 2048 + bounded_run.headroom_mib(27703, 2048, 20480)), 0)
1482+
1483+
def test_a_set_free_memory_replaces_the_free_memory_and_the_unused_budgets(self):
1484+
output = "0::/user.slice/app.slice/bounded-run-1-000000-probe.scope\n"
1485+
keywords = self.wait_arguments(output, dict(self.environ, BOUNDED_RUN_FREE_MIB="3000"))
1486+
with mock.patch.object(bounded_run, "unused_budget_mib", side_effect=AssertionError("unused")), \
1487+
mock.patch.object(bounded_run, "available_memory_mib", side_effect=AssertionError("available")):
1488+
self.assertEqual(keywords["read_available"](), 3000)
1489+
self.assertIsNone(keywords["read_outstanding"]())
14341490

14351491
def test_the_wait_counts_the_unused_budgets_beside_the_scope_of_the_probe(self):
14361492
calls = []
@@ -1473,6 +1529,7 @@ def setUp(self):
14731529
"BOUNDED_RUN_MEMORY_WAIT_SECONDS": "0",
14741530
"BOUNDED_RUN_SAMPLE_SECONDS": "0.1",
14751531
"BOUNDED_RUN_NO_CAP": "1",
1532+
"BOUNDED_RUN_FREE_MIB": "1048576",
14761533
"EVENTS": self.events,
14771534
})
14781535
self.processes = []

0 commit comments

Comments
 (0)