Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

test(resource-governance): drive the governor with pids no process can hold (#2389)

Call White379c476df8d9
diff --git a/tests/test_resource_governance.py b/tests/test_resource_governance.py
index 47a769dbc..c39f5ca2f 100644
--- a/tests/test_resource_governance.py
+++ b/tests/test_resource_governance.py
@@ -18,6 +18,14 @@ from core.resource_governance import (
tenant_memory_limit_bytes,
tenant_pid_limit,
)
+from tests.fake_pid_helpers import fake_pid
+
+# The governor reads the host's /proc for every pid it moves (children, owner,
+# command line) and writes its oom_score_adj, so a literal pid reaches whatever
+# process holds that number on the machine running the test.
+AGENT, NEXT_AGENT, AGENT_CHILD, AGENT_GRANDCHILD, RUNTIME, RUNTIME_SIBLING, FOREIGN = (
+ fake_pid(index) for index in range(7)
+)
def test_derive_agent_limits_uses_single_aggregate_budget() -> None:
@@ -411,7 +419,7 @@ def test_governor_disabled_mode_does_not_create_group(tmp_path: Path) -> None:
base.mkdir()
governor = AgentResourceGovernor({"mode": "disabled"}, root=tmp_path, base_cgroup=base)
- assert governor.apply_to_pid(123, label="test") is False
+ assert governor.apply_to_pid(AGENT, label="test") is False
assert not (base / "avibe-agents").exists()
@@ -437,14 +445,14 @@ def test_governor_update_config_resets_cached_group(tmp_path: Path, monkeypatch:
monkeypatch.setattr(Path, "mkdir", mkdir_with_controller_files)
- assert governor.apply_to_pid(4321, label="test") is True
+ assert governor.apply_to_pid(AGENT, label="test") is True
assert governor.group_path == group
governor.update_config({"mode": "disabled"})
# Existing members remain observable, but new processes are not adopted.
assert governor.group_path == group
- assert governor.apply_to_pid(4322, label="test") is False
+ assert governor.apply_to_pid(NEXT_AGENT, label="test") is False
def test_governor_configures_group_and_moves_pid(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
@@ -456,7 +464,7 @@ def test_governor_configures_group_and_moves_pid(tmp_path: Path, monkeypatch: py
(base / "pids.max").write_text("4096\n", encoding="utf-8")
(base / "cgroup.controllers").write_text("memory cpu io pids\n", encoding="utf-8")
(base / "cgroup.subtree_control").write_text("", encoding="utf-8")
- (base / "cgroup.procs").write_text("1001\n1002\n", encoding="utf-8")
+ (base / "cgroup.procs").write_text(f"{RUNTIME}\n{RUNTIME_SIBLING}\n", encoding="utf-8")
governor = AgentResourceGovernor({"mode": "enabled"}, root=root, base_cgroup=base)
group = base / "avibe-agents"
@@ -488,8 +496,8 @@ def test_governor_configures_group_and_moves_pid(tmp_path: Path, monkeypatch: py
def fake_write_cgroup_value(path: Path, value: str) -> None:
if path == runtime_group / "cgroup.procs":
runtime_writes.append(value)
- if value in {"1001", "1002"}:
- remaining = "1002\n" if value == "1001" else ""
+ if value in {str(RUNTIME), str(RUNTIME_SIBLING)}:
+ remaining = f"{RUNTIME_SIBLING}\n" if value == str(RUNTIME) else ""
(base / "cgroup.procs").write_text(remaining, encoding="utf-8")
return
if path == group / "cgroup.procs":
@@ -497,13 +505,13 @@ def test_governor_configures_group_and_moves_pid(tmp_path: Path, monkeypatch: py
path.write_text(f"{value}\n", encoding="utf-8")
monkeypatch.setattr("core.resource_governance._write_cgroup_value", fake_write_cgroup_value)
- monkeypatch.setattr("core.resource_governance._runtime_process_tree_pids", lambda pid=None: {1001, 1002})
+ monkeypatch.setattr("core.resource_governance._runtime_process_tree_pids", lambda pid=None: {RUNTIME, RUNTIME_SIBLING})
- assert governor.apply_to_pid(4321, label="test") is True
+ assert governor.apply_to_pid(AGENT, label="test") is True
- assert runtime_writes == ["1001", "1002"]
+ assert runtime_writes == [str(RUNTIME), str(RUNTIME_SIBLING)]
assert (base / "cgroup.subtree_control").read_text(encoding="utf-8") == "+memory +cpu +io +pids\n"
- assert (group / "cgroup.procs").read_text(encoding="utf-8") == "4321\n"
+ assert (group / "cgroup.procs").read_text(encoding="utf-8") == f"{AGENT}\n"
assert (group / "memory.max").read_text(encoding="utf-8").strip() == str(1382 * MIB)
assert (group / "memory.high").read_text(encoding="utf-8").strip() == str(1174 * MIB)
assert (group / "memory.oom.group").read_text(encoding="utf-8").strip() == "1"
@@ -533,7 +541,7 @@ def test_existing_agent_group_baseline_precedes_migrated_pid(
monkeypatch.setattr(governor, "_prepare_base_cgroup", migrate)
monkeypatch.setattr(governor, "_enable_subtree_controllers", lambda _base: None)
- assert governor._ensure_group(known_agent_pids={4321}) == group
+ assert governor._ensure_group(known_agent_pids={AGENT}) == group
assert governor.observe_resource_pressure().event_delta == 1
@@ -561,7 +569,7 @@ def test_governor_falls_back_when_memory_controller_is_missing(
monkeypatch.setattr(Path, "mkdir", mkdir_with_minimal_files)
- assert governor.apply_to_pid(4321, label="test") is False
+ assert governor.apply_to_pid(AGENT, label="test") is False
assert governor.group_path is None
@@ -572,11 +580,11 @@ def test_governor_falls_back_when_runtime_leaf_cannot_be_created(tmp_path: Path)
(base / "memory.max").write_text(str(2 * 1024 * MIB), encoding="utf-8")
(base / "cgroup.controllers").write_text("memory cpu io pids\n", encoding="utf-8")
(base / "cgroup.subtree_control").write_text("", encoding="utf-8")
- (base / "cgroup.procs").write_text("1001\n", encoding="utf-8")
+ (base / "cgroup.procs").write_text(f"{RUNTIME}\n", encoding="utf-8")
governor = AgentResourceGovernor({"mode": "auto"}, root=root, base_cgroup=base)
- assert governor.apply_to_pid(4321, label="test") is False
+ assert governor.apply_to_pid(AGENT, label="test") is False
assert governor.group_path is None
@@ -588,7 +596,7 @@ def test_governor_falls_back_when_base_has_foreign_member_pids(
base = root / "service"
base.mkdir(parents=True)
(base / "memory.max").write_text(str(512 * MIB), encoding="utf-8")
- (base / "cgroup.procs").write_text("1001\n2002\n", encoding="utf-8")
+ (base / "cgroup.procs").write_text(f"{RUNTIME}\n{FOREIGN}\n", encoding="utf-8")
governor = AgentResourceGovernor({"mode": "auto"}, root=root, base_cgroup=base)
runtime_group = base / "avibe-runtime"
@@ -602,13 +610,13 @@ def test_governor_falls_back_when_base_has_foreign_member_pids(
return result
monkeypatch.setattr(Path, "mkdir", mkdir_with_runtime_file)
- monkeypatch.setattr("core.resource_governance._runtime_process_tree_pids", lambda pid=None: {1001})
+ monkeypatch.setattr("core.resource_governance._runtime_process_tree_pids", lambda pid=None: {RUNTIME})
monkeypatch.setattr(
"core.resource_governance._write_cgroup_value",
lambda path, value: writes.append(value),
)
- assert governor.apply_to_pid(4321, label="test") is False
+ assert governor.apply_to_pid(AGENT, label="test") is False
assert governor.group_path is None
assert writes == []
@@ -621,7 +629,7 @@ def test_governor_allows_known_runtime_sibling_pids_during_base_migration(
base = root / "service"
base.mkdir(parents=True)
(base / "memory.max").write_text(str(512 * MIB), encoding="utf-8")
- (base / "cgroup.procs").write_text("1001\n1002\n5001\n", encoding="utf-8")
+ (base / "cgroup.procs").write_text(f"{RUNTIME}\n{RUNTIME_SIBLING}\n{AGENT}\n", encoding="utf-8")
governor = AgentResourceGovernor({"mode": "enabled"}, root=root, base_cgroup=base)
group = base / "avibe-agents"
@@ -654,13 +662,13 @@ def test_governor_allows_known_runtime_sibling_pids_during_base_migration(
return [pid for pid in text.split() if pid]
monkeypatch.setattr(Path, "mkdir", mkdir_with_cgroup_procs)
- monkeypatch.setattr("core.resource_governance._runtime_process_tree_pids", lambda pid=None: {1001})
- monkeypatch.setattr("core.resource_governance._known_avibe_runtime_sibling_pids", lambda pids: {1002})
+ monkeypatch.setattr("core.resource_governance._runtime_process_tree_pids", lambda pid=None: {RUNTIME})
+ monkeypatch.setattr("core.resource_governance._known_avibe_runtime_sibling_pids", lambda pids: {RUNTIME_SIBLING})
monkeypatch.setattr("core.resource_governance._write_cgroup_value", fake_write_cgroup_value)
- assert governor.apply_to_pid(5001, label="test") is True
- assert runtime_writes == ["1001", "1002"]
- assert agent_writes == ["5001", "5001"]
+ assert governor.apply_to_pid(AGENT, label="test") is True
+ assert runtime_writes == [str(RUNTIME), str(RUNTIME_SIBLING)]
+ assert agent_writes == [str(AGENT), str(AGENT)]
assert governor.group_path == group
@@ -672,7 +680,7 @@ def test_governor_allows_known_agent_pids_during_base_migration(
base = root / "service"
base.mkdir(parents=True)
(base / "memory.max").write_text(str(512 * MIB), encoding="utf-8")
- (base / "cgroup.procs").write_text("1001\n5001\n5002\n", encoding="utf-8")
+ (base / "cgroup.procs").write_text(f"{RUNTIME}\n{AGENT}\n{AGENT_CHILD}\n", encoding="utf-8")
(base / "cgroup.controllers").write_text("cpu io pids\n", encoding="utf-8")
(base / "cgroup.subtree_control").write_text("", encoding="utf-8")
@@ -710,13 +718,13 @@ def test_governor_allows_known_agent_pids_during_base_migration(
return [pid for pid in text.split() if pid]
monkeypatch.setattr(Path, "mkdir", mkdir_with_controller_files)
- monkeypatch.setattr("core.resource_governance._runtime_process_tree_pids", lambda pid=None: {1001})
- monkeypatch.setattr("core.resource_governance._descendant_pids", lambda pid: [5002])
+ monkeypatch.setattr("core.resource_governance._runtime_process_tree_pids", lambda pid=None: {RUNTIME})
+ monkeypatch.setattr("core.resource_governance._descendant_pids", lambda pid: [AGENT_CHILD])
monkeypatch.setattr("core.resource_governance._write_cgroup_value", fake_write_cgroup_value)
- assert governor.apply_to_pid(5001, label="test") is True
- assert runtime_writes == ["1001"]
- assert agent_writes == ["5001", "5002", "5001", "5002"]
+ assert governor.apply_to_pid(AGENT, label="test") is True
+ assert runtime_writes == [str(RUNTIME)]
+ assert agent_writes == [str(AGENT), str(AGENT_CHILD), str(AGENT), str(AGENT_CHILD)]
assert governor.group_path == group
assert governor.observe_resource_pressure().kind == "pids"
@@ -750,7 +758,7 @@ def test_governor_falls_back_when_subtree_control_enable_fails(
monkeypatch.setattr(Path, "mkdir", mkdir_with_runtime_file)
monkeypatch.setattr("core.resource_governance._write_cgroup_value", fake_write_cgroup_value)
- assert governor.apply_to_pid(4321, label="test") is False
+ assert governor.apply_to_pid(AGENT, label="test") is False
assert governor.group_path is None
assert not (base / "avibe-agents").exists()
@@ -781,7 +789,7 @@ def test_governor_uses_parent_when_current_cgroup_is_runtime_leaf(
monkeypatch.setattr(Path, "mkdir", mkdir_with_controller_files)
- assert governor.apply_to_pid(4321, label="test") is True
+ assert governor.apply_to_pid(AGENT, label="test") is True
assert governor.group_path == group
assert not (runtime_group / "avibe-agents").exists()
assert (base / "cgroup.subtree_control").read_text(encoding="utf-8") == "+cpu +io +pids\n"
@@ -818,8 +826,8 @@ def test_governor_moves_existing_descendant_pids(
path.write_text(f"{value}\n", encoding="utf-8")
monkeypatch.setattr(Path, "mkdir", mkdir_with_controller_files)
- monkeypatch.setattr("core.resource_governance._descendant_pids", lambda pid: [5002, 5003])
+ monkeypatch.setattr("core.resource_governance._descendant_pids", lambda pid: [AGENT_CHILD, AGENT_GRANDCHILD])
monkeypatch.setattr("core.resource_governance._write_cgroup_value", fake_write_cgroup_value)
- assert governor.apply_to_pid(5001, label="test") is True
- assert writes == ["5001", "5002", "5003"]
+ assert governor.apply_to_pid(AGENT, label="test") is True
+ assert writes == [str(AGENT), str(AGENT_CHILD), str(AGENT_GRANDCHILD)]