Skip to content

Commit 4f831ac

Browse files
sourcehawkclaude
andcommitted
fix(skills/bounding-heavy-commands): count the larger of two unused-budget reads around the free memory
The order of the reads made the check safe only while running commands grew. A command that freed memory between the two reads raised the free memory and left the unused count too small. The wait now reads the unused budgets before and after the free memory and counts the larger value, which is safe in both directions. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 9ad86b7 commit 4f831ac

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,12 +485,15 @@ def wait_for_memory(
485485
next_line = 0.0
486486
while True:
487487
lock()
488-
# A running command that grows between the two reads lowers both values. In this order the growth can only make the free memory look smaller.
489-
outstanding = read_outstanding()
488+
# A running command that grows or shrinks during the reads moves both values. The larger of the two unused
489+
# counts around the free memory is the one that matches it or errs on the safe side.
490+
before = read_outstanding()
490491
available = read_available()
491492
if available is None:
492493
log("cannot read the free memory; starting")
493494
return
495+
after = read_outstanding()
496+
outstanding = None if before is None or after is None else max(before, after)
494497
counted = "unused running budgets not counted here" if outstanding is None else "%d MiB of running budgets unused" % outstanding
495498
state = "%d MiB free, %s, %d MiB headroom, budget %d MiB" % (available, counted, headroom_mib, budget_mib)
496499
if available - (outstanding or 0) - headroom_mib >= budget_mib:

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ def read():
808808
)
809809
self.assertEqual(events, ["lock", "read", "unlock", "sleep", "lock", "read"])
810810

811-
def test_reads_the_unused_budgets_before_the_free_memory(self):
811+
def test_reads_the_unused_budgets_before_and_after_the_free_memory(self):
812812
events = []
813813

814814
def read_available():
@@ -822,7 +822,19 @@ def read_outstanding():
822822
bounded_run.wait_for_memory(
823823
4096, 300, 5.0, read_available=read_available, read_outstanding=read_outstanding, sleep=lambda seconds: None,
824824
)
825-
self.assertEqual(events, ["unused", "free"])
825+
self.assertEqual(events, ["unused", "free", "unused"])
826+
827+
def test_counts_the_larger_of_the_unused_budgets_read_before_and_after_the_free_memory(self):
828+
for first, second in ((1000, 3000), (3000, 1000)):
829+
with self.subTest(first=first, second=second):
830+
unused = [first, second, 0, 0]
831+
readings = [4000, 8000]
832+
sleeps = []
833+
bounded_run.wait_for_memory(
834+
2048, 300, 5.0, read_available=lambda: readings.pop(0), read_outstanding=lambda: unused.pop(0),
835+
sleep=sleeps.append,
836+
)
837+
self.assertEqual(sleeps, [5.0])
826838

827839
def test_the_headroom_is_not_given_out(self):
828840
self.assertEqual(self.wait([5000, 5200], 300, headroom=1024), [5.0])

0 commit comments

Comments
 (0)