Watch
0
0
Fork
You've already forked hyperhive
0

subagent and bash daemons: the runs' memory ceiling sits on their cgroup node, not on the unit

This commit is contained in:
damocles 2026-11-26 14:22:57 +01:00
commit 897f8af77d
3 changed files with 109 additions and 33 deletions

View file

@ -458,8 +458,9 @@ in
# actually chose and leaves the daemon serving, which is also what
# lets it report the kill rather than vanish and be restarted.
OOMPolicy = "continue";
# `MemoryHigh=` is a drop-in from ./memory-cap.nix, sized at boot from
# the container's cap.
# The runs' memory ceiling is `HIVE_RUNS_MEMORY_HIGH`, a drop-in from
# ./memory-cap.nix sized at boot from the container's cap. Not
# `MemoryHigh=`: that would throttle the daemon with its runs.
User = userName;
Group = userName;
}

View file

@ -19,15 +19,14 @@
# it in login shells that no unit starts (`nixos-container root-login`).
# `hive-agent` and `hive-subagent-daemon` spawn `claude` as a plain child,
# which inherits it, so neither unit may set or unset it.
# - `MemoryHigh=` on `hive-subagent-daemon` and on `hive-bash-daemon`, two
# thirds of the cap each, as generator drop-ins. Each daemon's cgroup holds
# its children (nested `claude` sessions, bash tasks), so a `cargo` or
# `nix build` storm throttles itself there before the container's
# `MemoryMax=`. `High`, not `Max`: past it the kernel reclaims hard and the
# work slows down, so the last third stays free for the agent's own turn.
#
# Tested by `checks.script-test-agent-memory-cap` and
# `checks.module-eval-agent-memory`.
# - `HIVE_RUNS_MEMORY_HIGH`, two thirds of the cap, in `hive-subagent-daemon`'s
# and `hive-bash-daemon`'s environment (generator drop-ins). Each daemon
# writes it to `memory.high` of the cgroup node holding its runs
# (`run_cgroup` in `hive-agent-sock`), so a `cargo` or `nix build` storm
# throttles itself before the container's `MemoryMax=`. Not `MemoryHigh=` on
# the unit, which would throttle the daemon beside its runs too. `High`, not
# `Max`, so the last third stays free for the agent's own turn.
# Tested by `checks.script-test-agent-memory-cap`, `checks.module-eval-agent-memory`.
{ pkgs, ... }:
let
# Sets `bytes`. Exits for `infinity`, and runs `noCap` when there is no file
@ -55,7 +54,7 @@ let
for unit in hive-subagent-daemon hive-bash-daemon; do
dir="$1/$unit.service.d"
${pkgs.coreutils}/bin/mkdir -p "$dir"
printf '[Service]\nMemoryHigh=%s\n' "$((bytes * 2 / 3))" > "$dir/50-memory-high.conf"
printf '[Service]\nEnvironment=HIVE_RUNS_MEMORY_HIGH=%s\n' "$((bytes * 2 / 3))" > "$dir/50-runs-memory-high.conf"
done
'';
@ -69,7 +68,7 @@ let
else
why="is missing"
fi
echo "<4>/run/hive/memory-max-bytes $why: no BUN_JSC_forceRAMSize heap ceiling and no MemoryHigh= on hive-subagent-daemon or hive-bash-daemon; the host's MemoryMax= on the container still applies." >&2
echo "<4>/run/hive/memory-max-bytes $why: no BUN_JSC_forceRAMSize heap ceiling and no memory ceiling on the subagent and bash daemons' runs; the host's MemoryMax= on the container still applies." >&2
exit 0
''}
'';

View file

@ -31,7 +31,7 @@ if ! printf '%s\n' "$EXTRA_INIT" | grep -qxF '. /etc/hyperhive/memory-cap.sh'; t
fail "/etc/set-environment does not source the snippet"
fi
# check <description> <expected BUN_JSC_forceRAMSize or ""> <expected MemoryHigh or "">
# check <description> <expected BUN_JSC_forceRAMSize or ""> <expected HIVE_RUNS_MEMORY_HIGH or "">
# <why the boot check warns, or "">
check() {
case=$1
@ -53,8 +53,8 @@ check() {
# The subagent daemon and the bash daemon each get one, and nothing else is written.
local unit
for unit in hive-subagent-daemon hive-bash-daemon; do
got=$(cat "$out/normal/$unit.service.d/50-memory-high.conf" 2>&1)
if [ "$got" != "[Service]"$'\n'"MemoryHigh=$want_high" ]; then
got=$(cat "$out/normal/$unit.service.d/50-runs-memory-high.conf" 2>&1)
if [ "$got" != "[Service]"$'\n'"Environment=HIVE_RUNS_MEMORY_HIGH=$want_high" ]; then
fail "unit generator's drop-in for $unit:"$'\n'"$got"
fi
done
@ -67,7 +67,7 @@ check() {
got=$(env -i "$dir/check" 2>&1)
local want_warn=
if [ -n "$want_why" ]; then
want_warn="<4>$cap $want_why: no BUN_JSC_forceRAMSize heap ceiling and no MemoryHigh= on hive-subagent-daemon or hive-bash-daemon; the host's MemoryMax= on the container still applies."
want_warn="<4>$cap $want_why: no BUN_JSC_forceRAMSize heap ceiling and no memory ceiling on the subagent and bash daemons' runs; the host's MemoryMax= on the container still applies."
fi
if [ "$got" != "$want_warn" ]; then
fail "boot check printed:"$'\n'"$got"$'\n'"wanted:"$'\n'"$want_warn"