You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(skills/bounding-heavy-commands): stop the queue from giving out more memory than the machine has - #58
A laptop froze under memory pressure while every heavy command of every session ran through the wrapper, because the queue could give out more memory than the machine had. The wrapper now counts the part of each running command's budget that the command has not used yet, keeps a headroom of free memory, and never starts a command when the memory is not free, not even after a long wait. A command can now wait longer on a busy machine: this is the intended cost, and #54 (next in this stack) keeps that wait fair.
Changes
Before it starts, the wrapper counts memory.max - memory.current of each running bounded-run-* scope as memory that is already promised, and starts only when the free memory minus that sum minus a headroom covers the budget.
The headroom is a tenth of the memory, at least 1024 MiB, for the desktop and the agent sessions that are not in the queue. The budget and the headroom together never need more than the memory of the queue, so a budget of the whole queue can still start: the queue's own reserve is the floor for what stays outside.
One wrapper at a time checks the memory and starts its command, under a lock file that all wrappers share. It keeps the lock until the scope of its command is in the slice with its memory limit, so that the next check counts it, or for at most 5 seconds. Without the lock, two wrappers that hold different slots could both pass the check before either scope existed. A wrapper that waits frees the lock between two readings.
The wait for free memory has no time limit. BOUNDED_RUN_MEMORY_WAIT_SECONDS is now the time between two "still waiting" lines.
The skill treats a server that a heavy command talks to (a development server, a database, a container) as a part of that command: the run gets a port of its own so that the runner starts the server inside the bounded command, or the server runs through the wrapper. A new red-flag row covers "the server is already running, the tests can reuse it".
Challenges
The wrapper finds the running scopes with no state of its own. The cap probe already starts a real scope with the same systemd-run --user --scope options as the command, and it now prints its own cgroup. The parent of that cgroup is the slice where the user manager puts every bounded-run-* scope of the user, because the wrapper never passes --slice. Where there is no cap or no cgroup version 2, nothing is counted, and the wait line says so.
TDD for each change, with fake cgroup trees in temp directories and injected readers. The new tests cover: a running command that uses little of a large budget holds back the next one; the time limit no longer starts a command; probe scopes, the own unit, and max limits are skipped; nothing is counted without a cgroup tree; and the next wrapper does not check the memory before the scope of a started command counts. The full suite (227 tests) passes on Python 3.14 through the queue. The tests that start the real wrapper set a test-only BOUNDED_RUN_FREE_MIB, so they read neither the machine's free memory nor other sessions' scopes, and the suite takes the same time on a busy machine as on an idle one. The counting itself is tested with fake cgroup trees.
The skill rule went through a RED and GREEN check. In the baseline, an agent ran an e2e suite against a development server that another session had started without the wrapper. It noted that the growth was outside the budget and went ahead anyway. With the rule, two fresh agents gave the run a port of its own, left the other server alone, and one wrote the port into the record.
Review round on 59df696 (review 5360298684). No suppressed findings.
Release memory.lock before finite memory.max is applied: fixed in d316e4c, with a test.
Comment gate against the review base 1fb65ef, self-evaluated because this session could not dispatch an agent:
Declaration
Lines at base, now
Sentence
Verdict
Source
Caller code
Judge
wait_for_memory
5, 6
the lock sentence from the first round
done, KEEP, unchanged
self-evaluated
wait_for_scope (added)
0, 2
"Returns True when the scope of the unit is in parent with its memory limit, as unused_budget_mib counts it, or False when it is not there after limit_seconds."
kept (result meaning)
_scope_counts
the caller logs on False
self
inline comment in wait_for_scope (added)
0, 1
"systemd makes the directory of the scope before it writes the limit."
This limitation also applies when a hard cap works on cgroup v1: probe_cap succeeds, but scope_parent_directory returns None without a cgroup v2 0:: entry, so unused budgets are not counted. Document both conditions so agents do not assume every capped Linux run receives this protection.
Use a coherent snapshot for memory admission checks: fixed in 95b971c, with a test.
Previously missed, on SKILL.md:50, "Cgroup v1 caps omit unused-budget protection" (no thread): applied in 9ad86b7. The sentence now reads: "Without the cap, and on a system with cgroup version 1, the wait does not count the unused budgets of the running commands."
Comment gate against the review base 1fb65ef, self-evaluated because this session could not dispatch an agent. One new block: an inline comment at the two reads in wait_for_memory, "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." It stays, because it gives the reason for the order, which the code cannot show. The wait_for_memory flag is unchanged since its KEEP verdict.
Review round on 9ad86b7 (review 5360416046). No suppressed findings.
Use coherent memory snapshot before admitting commands: fixed in 4f831ac, with a test.
Comment gate against 1fb65ef, self-evaluated because this session could not dispatch an agent: the inline comment at the reads in wait_for_memory changed to "A running command that grows or shrinks during the reads moves both values. The larger of the two unused counts around the free memory is the one that matches it or errs on the safe side." It stays, because it gives the reason for the two reads, which the code cannot show.
This advances the reporting interval by requested sleep durations rather than real elapsed time. Under the memory pressure this code handles, descheduling or an oversleep can make BOUNDED_RUN_MEMORY_WAIT_SECONDS=300 produce lines much more than 300 seconds apart and understate the value in “still waiting after …”, contrary to the variable's new meaning. Use a monotonic clock/deadline, as wait_for_scope does, and cover an oversleeping clock in the test.
Prevent unsafe admission during scope shrink-then-grow races: declined, no change. See the thread. This is the third round on the atomicity of two file reads. The realistic cases are closed, and an exact scheme would add state that the queue avoids on purpose.
Previously missed, on bounded_run.py:507, "Use monotonic elapsed time for memory wait reporting" (no thread): fixed in 6a506a3. wait_for_memory takes a clock (default time.monotonic) and spaces its lines by elapsed time. New test: test_the_time_between_two_lines_is_real_time, with a sleep that takes ten times as long as asked. It failed before the fix.
Comment gate against 1fb65ef, self-evaluated: no comment changed in this round.
…tart on a full machine
The wait for free memory compared the free memory now with the new budget. It ignored the part of their budgets that running bounded commands had not used yet, it trusted a reserve fixed at the start, and after BOUNDED_RUN_MEMORY_WAIT_SECONDS it started the command anyway. Together these let the queue give out more memory than the machine had (#55).
- The cap probe now prints its own cgroup. Every scope that systemd-run makes for this user with no slice option lands in the same slice, so the probe finds the siblings with no state file. The wait subtracts the sum of memory.max - memory.current over the bounded-run scopes there, skipping probe scopes, the new unit, the scope the wrapper runs in, and scopes with no limit. Without a cap or cgroup v2 nothing is subtracted, and the log says so.
- The wait also keeps a headroom of max(1 GiB, a tenth of the memory) for the programs outside the queue.
- The wait has no time limit. BOUNDED_RUN_MEMORY_WAIT_SECONDS is now the time between two "still waiting" lines. A machine whose free memory cannot be read still starts the command.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…and uses
A test suite that talks to a server outside the queue moves its load to that server. A runner that reuses a development server already listening on its port makes that server compile and grow, and no budget or cap covers it. In a baseline test, an agent saw that the growth was outside the budget and ran the suite anyway, because the skill had no rule for it.
The skill now treats such a server as a part of the heavy command: give the run a port of its own so that the runner starts the server inside the bounded command, or start the server through the wrapper. Never run against a server outside the queue, and never stop it.
Refs #55
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nd 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>
…the memory and start
Two wrappers that hold different slots could both read the free memory before either command had a scope, both pass the check, and together promise more memory than the machine has. The check now runs under a lock file that all wrappers share. The wrapper keeps the lock until the scope of its command is in the slice where the next check counts it, or for at most 5 seconds. A wrapper that waits for memory frees the lock between two readings, so a command that fits can still start.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ew scope has its limit
systemd makes the directory of a scope before it writes memory.max, and the count of unused budgets skips a scope whose limit is still max. The wrapper freed the memory lock when the directory appeared, so the next check could run before the new budget counted. The wrapper now waits until memory.max holds a number and memory.current can be read, which is what the count needs.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…he free memory
A running command that grows between the two reads lowers both values. With the free memory read first, the check could pair an old, high free memory with a new, low unused budget, and start a command that neither true state lets start. In the new order, growth between the reads can only make the free memory look smaller.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…sed budget on cgroup version 1
The count of unused budgets reads the cgroup version 2 tree. On a system with cgroup version 1 the hard cap can work while the wait counts nothing, and the skill named only the case with no cap.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…udget 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>
…al-time deadline
The wait added up the sleep time that it asked for. On a machine under memory pressure a sleep can take much longer, so the memory lock, which blocks every wrapper, could stay held far past its 5 seconds. The wait now ends at a monotonic deadline.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…it by real time
BOUNDED_RUN_MEMORY_WAIT_SECONDS is now the time between two "still waiting" lines, but the wait added up the sleep time that it asked for. Under memory pressure a sleep can take much longer, so the lines came further apart and understated the time waited. The wait now reads a monotonic clock.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #55
A laptop froze under memory pressure while every heavy command of every session ran through the wrapper, because the queue could give out more memory than the machine had. The wrapper now counts the part of each running command's budget that the command has not used yet, keeps a headroom of free memory, and never starts a command when the memory is not free, not even after a long wait. A command can now wait longer on a busy machine: this is the intended cost, and #54 (next in this stack) keeps that wait fair.
Changes
memory.max - memory.currentof each runningbounded-run-*scope as memory that is already promised, and starts only when the free memory minus that sum minus a headroom covers the budget.BOUNDED_RUN_MEMORY_WAIT_SECONDSis now the time between two "still waiting" lines.Challenges
The wrapper finds the running scopes with no state of its own. The cap probe already starts a real scope with the same
systemd-run --user --scopeoptions as the command, and it now prints its own cgroup. The parent of that cgroup is the slice where the user manager puts everybounded-run-*scope of the user, because the wrapper never passes--slice. Where there is no cap or no cgroup version 2, nothing is counted, and the wait line says so.Related
Testing
TDD for each change, with fake cgroup trees in temp directories and injected readers. The new tests cover: a running command that uses little of a large budget holds back the next one; the time limit no longer starts a command; probe scopes, the own unit, and
maxlimits are skipped; nothing is counted without a cgroup tree; and the next wrapper does not check the memory before the scope of a started command counts. The full suite (227 tests) passes on Python 3.14 through the queue. The tests that start the real wrapper set a test-onlyBOUNDED_RUN_FREE_MIB, so they read neither the machine's free memory nor other sessions' scopes, and the suite takes the same time on a busy machine as on an idle one. The counting itself is tested with fake cgroup trees.The skill rule went through a RED and GREEN check. In the baseline, an agent ran an e2e suite against a development server that another session had started without the wrapper. It noted that the growth was outside the budget and went ahead anyway. With the rule, two fresh agents gave the run a port of its own, left the other server alone, and one wrote the port into the record.
🤖 Generated with Claude Code