fix(terminal): align worker limit with local guard

This commit is contained in:
Dominic Bejar
2026-08-06 02:06:54 +00:00
committed by kshitij
parent 5f93083221
commit b0346ba42a
2 changed files with 14 additions and 13 deletions
+3 -3
View File
@@ -1793,10 +1793,10 @@ class TestSystemdCgroupIsolation:
assert argv[-3:] == ["/bin/bash", "-lic", "set +m; codex"]
assert session.systemd_unit == f"hermes-worker-{session.id}.scope"
def test_worker_memory_limit_accepts_valid_byte_override(self, monkeypatch):
def test_worker_memory_limit_honors_local_guard_mb_override(self, monkeypatch):
import tools.process_registry as pr
monkeypatch.setenv("HERMES_WORKER_MEMORY_MAX_BYTES", "123456789")
monkeypatch.setenv("TERMINAL_LOCAL_MEMORY_MAX_MB", "123")
monkeypatch.setattr("shutil.which", lambda name: "/usr/bin/systemd-run")
argv = pr._build_systemd_scope_argv(
@@ -1804,7 +1804,7 @@ class TestSystemdCgroupIsolation:
unit_suffix="test",
)
assert "MemoryMax=123456789" in argv
assert f"MemoryMax={123 * 1024 * 1024}" in argv
def test_kill_recovered_detached_already_exited_stops_persisted_scope(
self, registry, monkeypatch
+11 -10
View File
@@ -108,25 +108,26 @@ _WORKER_MEMORY_MAX_CAP_BYTES = 4 * 1024 * 1024 * 1024
def _worker_memory_max_bytes() -> int:
"""Return a finite per-worker cgroup limit without widening host risk.
An explicit byte override is useful for operators with known workload
requirements. Otherwise retain the tighter of the gateway's current
cgroup-v2 ``memory.max`` and half of physical RAM, capped at 4 GiB. This
keeps the sibling worker outside the gateway cgroup while ensuring the
worker cannot consume memory up to the enclosing user slice or host limit.
The proposed local-memory-guard environment override is honored so this
isolation composes with PR #57121 instead of inventing a second knob.
Otherwise retain the tighter of the gateway's current cgroup-v2
``memory.max`` and half of physical RAM, capped at 4 GiB. This keeps the
sibling worker outside the gateway cgroup while ensuring the worker cannot
consume memory up to the enclosing user slice or host limit.
"""
override = os.getenv("HERMES_WORKER_MEMORY_MAX_BYTES", "").strip()
override = os.getenv("TERMINAL_LOCAL_MEMORY_MAX_MB", "").strip()
if override:
try:
parsed = int(override)
parsed = int(override) * 1024 * 1024
if parsed >= _MIN_WORKER_MEMORY_MAX_BYTES:
return parsed
except ValueError:
pass
logger.warning(
"Ignoring invalid HERMES_WORKER_MEMORY_MAX_BYTES=%r; "
"expected an integer of at least %d bytes",
"Ignoring invalid TERMINAL_LOCAL_MEMORY_MAX_MB=%r; "
"expected an integer representing at least %d MiB",
override,
_MIN_WORKER_MEMORY_MAX_BYTES,
_MIN_WORKER_MEMORY_MAX_BYTES // (1024 * 1024),
)
candidates: List[int] = []