From 82710f4fa834ddb5a1094b9d10eac958caa3b909 Mon Sep 17 00:00:00 2001 From: damocles Date: Fri, 27 Nov 2026 17:08:19 +0100 Subject: [PATCH] memory-cap: give claude a heap ceiling of at most 3 GiB instead of 75% of the container cap --- docs/4-implementation/scheduler/coordinator.md | 2 +- nix/agent-modules/memory-cap.nix | 16 +++++++++++----- nix/script-tests/agent-memory-cap.sh | 7 +++++-- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/docs/4-implementation/scheduler/coordinator.md b/docs/4-implementation/scheduler/coordinator.md index 4aa17bd05..bb7f522c6 100644 --- a/docs/4-implementation/scheduler/coordinator.md +++ b/docs/4-implementation/scheduler/coordinator.md @@ -480,7 +480,7 @@ the last drop-in because it can't read the swarm caps file, the file carries that drop-in's `MemoryMax=`. At boot the container derives two values from a byte count: -`BUN_JSC_forceRAMSize` (75%, claude-code's heap ceiling) and +`BUN_JSC_forceRAMSize` (75%, but at most 3 GiB: claude-code's heap ceiling; it's a per-process hint while the agent's claude and every subagent's share one cap, so a share that grows with the cap would let a few processes together pass it) and `HIVE_RUNS_MEMORY_HIGH` in `hive-subagent-daemon`'s and `hive-bash-daemon`'s environment (two thirds each). Each daemon writes it to `memory.high` of the cgroup node its runs live in, so a build storm throttles before the diff --git a/nix/agent-modules/memory-cap.nix b/nix/agent-modules/memory-cap.nix index 846d92528..9cf700c94 100644 --- a/nix/agent-modules/memory-cap.nix +++ b/nix/agent-modules/memory-cap.nix @@ -10,10 +10,11 @@ # or malformed file sets neither too, and `hyperhive-memory-cap-check` logs a # warning at boot: # -# - `BUN_JSC_forceRAMSize`, 75% of the cap. Bun/JavaScriptCore, the runtime -# under claude-code, sizes its heap ceiling off `/proc/meminfo`, which under -# nspawn is the host's RAM, so without it the heap grows well past the real -# wall before GC kicks in hard and choom sessions hang. An environment +# - `BUN_JSC_forceRAMSize`, 75% of the cap, at most 3 GiB. Bun/JavaScriptCore, +# the runtime under claude-code, sizes its heap ceiling off `/proc/meminfo`, +# which under nspawn is the host's RAM, so without it the heap grows well past +# the real wall before GC kicks in hard and choom sessions hang. It is per +# process while the cap is shared, hence the fixed top. An environment # generator hands it to every unit of the container's systemd, `machinectl # shell` sessions (`hivectl choom`) included; `/etc/set-environment` exports # it in login shells that no unit starts (`nixos-container root-login`). @@ -42,9 +43,14 @@ let esac ''; + # What one claude process is told it may grow its heap toward, at most. + heapCeilingBytes = 3 * 1024 * 1024 * 1024; + envGenerator = pkgs.writeShellScript "hyperhive-memory-cap-env" '' ${readCap "exit 0"} - printf 'BUN_JSC_forceRAMSize=%s\n' "$((bytes * 75 / 100))" + heap=$((bytes * 75 / 100)) + [ "$heap" -gt ${toString heapCeilingBytes} ] && heap=${toString heapCeilingBytes} + printf 'BUN_JSC_forceRAMSize=%s\n' "$heap" ''; # `$1` is the generator's normal-priority output dir. A drop-in there merges diff --git a/nix/script-tests/agent-memory-cap.sh b/nix/script-tests/agent-memory-cap.sh index a62a24cb7..44e51c6d3 100644 --- a/nix/script-tests/agent-memory-cap.sh +++ b/nix/script-tests/agent-memory-cap.sh @@ -85,10 +85,13 @@ check() { } printf '8589934592\n' >"$cap" -check "an 8G cap" 6442450944 5726623061 +check "an 8G cap keeps the heap at the 3 GiB ceiling, not 75%" 3221225472 5726623061 + +printf '2147483648\n' >"$cap" +check "a 2G cap is below the ceiling and keeps 75%" 1610612736 1431655765 printf '4294967296' >"$cap" -check "a line without a newline is kept" 3221225472 2863311530 +check "a line without a newline is kept (75% is exactly the ceiling)" 3221225472 2863311530 printf '4294967296\nLD_PRELOAD=/x\n' >"$cap" check "only the first line is read" 3221225472 2863311530