Files
skills/tests/test_orchestrator_migration.py
T
Kevin Veen-BirkenbachandClaude Opus 5.5 0fad1bfae3 feat: hub and skills requirements 032 to 063
Hub: send in the field (032), coherent speak box (033), stream head band (034), Match at full width with its pile (035, 036), Home without the outer box (037), badge to Match (038), rejects that survive a changed or withdrawn card (039), the release card (040), code marks in the review diff (041), why Deny is off (042), the undo window clock (043), widgets without inner boxes (044), the Artifacts head on one line (045), accepting a red gate's risk (046), one code box (047), done rows greyed and folded (048), one control height (049), menus that stay open (050), radio settings in the Device card (051), bubbles beside the modes (052), agent settings on every face and name (053), pin only by menu (054), WASD in Match (055), the flowing card edge (056), Schedules in Controls (057) and the hub poll cost (059).

Skills: the coach leaves for its own repository kevinveenbirkenbach/coach (058; test_install pins its four skills in skills-lock.json and stays red until that repository is published and pinned), hooks and timers start fewer processes (060), council files on hot paths stay bounded and mirror by marker instead of mtime (061), scripts and hooks hand agents less output (062), and block.py builds the commit table and message block in one run (063). Requirement documents 032 to 063 with their implementation amendments.

MEDUSA false positives accepted after kvb-think-criticize (36 lines): MM-SCAN-058 (pla inside replace or plan), JB-SCAN-616/641 (the local clip helper), CODEGEN-SCAN-001 (drop-in paths), CGS-SCAN-029 (systemctl argv from constants), CGS-SCAN-040 (runner poll loop), FLA-SCAN-055 (feedback), WM-SCAN-354, AI-BACKDOOR-006, GENAI-SCAN-1270, B604 (runner shell path for passkey-verified commands), TC004, EXA002, EXA006, EXA010 and WEB-SSRF.

🎯 Fix: 92 %
💥 Break: 8 %
💸 Tokens: 89 %
⏱️ Velocity: –
🛡️ Security: ~100 %
🏆 Score: 95
🚦 Gate: ⛔ break 8 % > 5

Created-with: https://s.veen.world/skills
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 08:44:15 +00:00

261 lines
16 KiB
Python

"""Run orchestrator.sh install twice in a scratch HOME with stub docker, claude and systemctl."""
from __future__ import annotations
import hashlib
import json
import os
import shutil
import subprocess
import tempfile
import unittest
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parents[1]
SCRIPT = REPO_ROOT / "scripts" / "orchestrator.sh"
HOST_SCRIPTS = (
"kvb-org-organize/scripts/council.py", "kvb-org-organize/scripts/nofollow.py", "kvb-org-organize/scripts/councilroot.py",
"kvb-org-rasci/scripts/rasci.py", "kvb-org-operator/scripts/operator.py", "kvb-org-radio/scripts/radio.py",
)
OLD = "kvb-hub"
NEW = "kvb-orchestrator"
STUB = r'''#!/usr/bin/env python3
import json, os, sys
from pathlib import Path
tool, args = Path(sys.argv[0]).name, sys.argv[1:]
file = Path(os.environ["STUB_STATE"])
state = json.loads(file.read_text())
state["calls"].append([tool, *args])
code = 0
if tool == "docker" and args[:2] == ["image", "inspect"]:
code = 0 if args[2] in state["images"] else 1
elif tool == "docker" and args[:2] == ["image", "rm"]:
code = 0 if args[2] in state["images"] else 1
state["images"] = [image for image in state["images"] if image != args[2]]
elif tool == "docker" and args[0] == "compose" and "build" in args:
code = 1 if state.get("fail_build") else 0
elif tool == "docker" and args[0] == "compose" and "up" in args:
state["images"] = sorted({*state["images"], "kvb-orchestrator:local"})
elif tool == "claude" and args[:2] == ["mcp", "get"]:
code = 0 if args[2] in state["mcp"] else 1
elif tool == "claude" and args[:2] == ["mcp", "remove"]:
code = 0 if args[-1] in state["mcp"] else 1
state["mcp"].pop(args[-1], None)
elif tool == "claude" and args[:2] == ["mcp", "add"]:
state["mcp"][args[-2]] = args[-1]
elif tool == "systemctl":
verb, unit = args[1], args[-1]
if verb == "is-enabled":
print("enabled" if unit in state["enabled"] else "disabled")
code = 0 if unit in state["enabled"] else 1
elif verb == "disable":
state["enabled"] = [name for name in state["enabled"] if name != unit]
elif verb == "enable":
state["enabled"] = sorted({*state["enabled"], unit})
elif verb == "is-active":
print("active" if unit in state["enabled"] else "inactive")
code = 0 if unit in state["enabled"] else 3
file.write_text(json.dumps(state))
sys.exit(code)
'''
def snapshot(root: Path) -> dict:
found = {}
for path in sorted(root.rglob("*")):
key = str(path.relative_to(root))
if path.is_symlink():
found[key] = ("link", os.readlink(path))
elif path.is_dir():
found[key] = ("dir", path.stat().st_mode & 0o777)
else:
found[key] = ("file", path.stat().st_mode & 0o777, hashlib.sha256(path.read_bytes()).hexdigest())
return found
@unittest.skipUnless(shutil.which("bash") and shutil.which("python3"), "bash or python3 missing")
class TestOrchestratorMigration(unittest.TestCase):
def setUp(self):
tmp = tempfile.TemporaryDirectory()
self.addCleanup(tmp.cleanup)
self.root = Path(tmp.name)
self.home = self.root / "home"
stubs = self.root / "bin"
stubs.mkdir()
(stubs / "stub").write_text(STUB, encoding="utf-8")
(stubs / "stub").chmod(0o755)
for tool in ("docker", "claude", "systemctl"):
(stubs / tool).symlink_to("stub")
self.state = self.root / "stub.json"
self.state.write_text(json.dumps({"calls": [], "images": [f"{OLD}:local"], "mcp": {OLD: "http://127.0.0.1:8737/mcp"}, "enabled": [f"{OLD}-runner.service"]}), encoding="utf-8")
self.env = {**os.environ, "HOME": str(self.home), "PATH": f"{stubs}:{os.environ['PATH']}", "STUB_STATE": str(self.state), "AGENTS_COUNCIL_ROOT": str(self.root / "council"), "PKGMGR_REPO": str(self.root / "no-pkgmgr")}
self.env.pop("XDG_STATE_HOME", None)
share, local, config = self.home / ".local" / "share", self.home / ".local" / "state", self.home / ".config"
self.write(share / OLD / "compose.yml", f"name: {OLD}\n")
self.write(share / OLD / ".env", "KVB_HUB_PORT=8737\n")
self.write(local / OLD / "rules.json", '{"rule": 1}\n')
self.write(local / f"{OLD}-approvals" / "runner.json", '{"used": {}}\n')
self.write(config / OLD / "approvers.json", '{"credentials": []}\n', 0o600)
(config / OLD).chmod(0o700)
units = config / "systemd" / "user"
self.write(units / f"{OLD}-runner.service", "[Unit]\n")
(units / "default.target.wants").mkdir()
(units / "default.target.wants" / f"{OLD}-runner.service").symlink_to(units / f"{OLD}-runner.service")
self.other = {"matcher": "Bash", "hooks": [{"type": "command", "command": "other-hook"}]}
legacy = {"matcher": "Bash", "hooks": [{"type": "command", "command": f"python3 {share}/{OLD}/client/permission_hook.py --port 8737"}]}
self.write(self.home / ".claude" / "settings.json", json.dumps({"model": "x", "hooks": {"PermissionRequest": [self.other, legacy], "Stop": [self.other]}}), 0o640)
for script in HOST_SCRIPTS:
self.write(self.home / ".claude" / "skills" / script, (REPO_ROOT / "skills" / script).read_text(encoding="utf-8"))
def write(self, path: Path, text: str, mode: int = 0o644) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(text, encoding="utf-8")
path.chmod(mode)
def install(self) -> dict:
done = subprocess.run(["bash", str(SCRIPT), "install"], env=self.env, capture_output=True, text=True, timeout=120, check=False)
self.assertEqual(done.returncode, 0, done.stderr)
return json.loads(self.state.read_text(encoding="utf-8"))
def test_install_migrates_every_trace_once(self):
first = self.install()
share, local, config = self.home / ".local" / "share", self.home / ".local" / "state", self.home / ".config"
units = config / "systemd" / "user"
for gone in (share / OLD, local / OLD, local / f"{OLD}-approvals", config / OLD, units / f"{OLD}-runner.service", units / "default.target.wants" / f"{OLD}-runner.service"):
with self.subTest(gone=gone):
self.assertFalse(gone.exists() or gone.is_symlink())
self.assertEqual((local / NEW / "rules.json").read_text(encoding="utf-8"), '{"rule": 1}\n')
self.assertEqual((local / f"{NEW}-approvals" / "runner.json").read_text(encoding="utf-8"), '{"used": {}}\n')
self.assertEqual(((config / NEW).stat().st_mode & 0o777, (config / NEW / "approvers.json").stat().st_mode & 0o777), (0o700, 0o600))
self.assertTrue((units / f"{NEW}-runner.service").is_file())
self.assertTrue((units / f"{NEW}-sessions.timer").is_file())
self.assertTrue((units / f"{NEW}-resources.timer").is_file())
self.assertIn(f"KVB_ORCHESTRATOR_STATE={local / NEW}", (share / NEW / ".env").read_text(encoding="utf-8"))
settings = self.home / ".claude" / "settings.json"
data = json.loads(settings.read_text(encoding="utf-8"))
hooks = data["hooks"]["PermissionRequest"]
self.assertEqual((data["model"], data["hooks"]["Stop"], hooks[0], len(hooks)), ("x", [self.other], self.other, 2))
self.assertIn(f"{share / NEW}/client/permission_hook.py", hooks[1]["hooks"][0]["command"])
self.assertEqual(settings.stat().st_mode & 0o777, 0o640)
self.assertIn(f"{OLD}/client", (self.home / ".claude" / f"settings.json.before-{NEW}-migration").read_text(encoding="utf-8"))
self.assertEqual((first["images"], first["mcp"], first["enabled"]), ([f"{NEW}:local"], {NEW: "http://127.0.0.1:8737/mcp"}, [f"{NEW}-firewall.path", f"{NEW}-firewall.timer", f"{NEW}-offices.path", f"{NEW}-resources.timer", f"{NEW}-runner.service", f"{NEW}-sessions.timer", f"{NEW}-tokens.timer"]))
self.assertIn(["docker", "compose", "--file", str(share / OLD / "compose.yml"), "down", "--remove-orphans"], first["calls"])
before = snapshot(self.home)
second = self.install()
self.assertEqual(snapshot(self.home), before)
self.assertEqual({key: second[key] for key in ("images", "mcp", "enabled")}, {key: first[key] for key in ("images", "mcp", "enabled")})
legacy_calls = [call for call in second["calls"][len(first["calls"]) :] if any(OLD in arg for arg in call)]
self.assertEqual(legacy_calls, [["docker", "image", "inspect", f"{OLD}:local"], ["claude", "mcp", "get", OLD]])
def test_install_hands_the_hub_the_agents_root_and_the_host_only_root_apart(self):
del self.env["AGENTS_COUNCIL_ROOT"]
self.install()
state = self.home / ".local" / "state"
env = (self.home / ".local" / "share" / NEW / ".env").read_text(encoding="utf-8").splitlines()
self.assertLessEqual({f"KVB_ORCHESTRATOR_COUNCIL={state / 'agents-shared'}", f"KVB_ORCHESTRATOR_KEPT={state / 'agents-council'}"}, set(env))
self.assertTrue((state / "agents-council" / "resources").is_dir())
def test_the_hub_is_built_before_it_is_replaced_and_a_failed_build_leaves_it_down_and_fails(self):
self.install()
hub = str(self.home / ".local" / "share" / NEW / "compose.yml")
first = json.loads(self.state.read_text(encoding="utf-8"))
self.assertEqual([call[4:] for call in first["calls"] if call[:4] == ["docker", "compose", "--file", hub]], [["build"], ["up", "-d"]])
self.state.write_text(json.dumps({**first, "calls": [], "fail_build": True}), encoding="utf-8")
done = subprocess.run(["bash", str(SCRIPT), "install"], env=self.env, capture_output=True, text=True, timeout=120, check=False)
self.assertEqual(done.returncode, 1, done.stderr)
self.assertIn("building the Agent Orchestrator failed; it stays down until 'bash scripts/orchestrator.sh install' succeeds", done.stderr)
after = json.loads(self.state.read_text(encoding="utf-8"))
self.assertEqual([call[4:] for call in after["calls"] if call[:4] == ["docker", "compose", "--file", hub]], [["build"], ["stop"]])
self.assertIn(f"{NEW}-resources.timer", after["enabled"])
installer = (REPO_ROOT / "scripts" / "install.sh").read_text(encoding="utf-8")
self.assertIn('if ! bash "${REPO_ROOT}/scripts/orchestrator.sh" install; then', installer)
def test_check_reports_the_sessions_timer_the_install_enabled_as_active(self):
for tool in ("curl", "journalctl"):
(self.root / "bin" / tool).symlink_to("stub")
timer = f"{NEW}-sessions.timer"
self.assertIn(timer, self.install()["enabled"])
checked = subprocess.run(["bash", str(SCRIPT), "check"], env=self.env, capture_output=True, text=True, timeout=120, check=False)
self.assertIn(f"ok {timer} active\n", checked.stdout, checked.stderr)
state = json.loads(self.state.read_text(encoding="utf-8"))
self.state.write_text(json.dumps({**state, "enabled": [unit for unit in state["enabled"] if unit != timer]}), encoding="utf-8")
stopped = subprocess.run(["bash", str(SCRIPT), "check"], env=self.env, capture_output=True, text=True, timeout=120, check=False)
self.assertIn(f"todo {timer} off: agent states wait for make orchestrator-install\n", stopped.stdout, stopped.stderr)
self.assertNotIn(f"{timer} active", stopped.stdout)
def test_the_offices_unit_waits_for_the_installed_skill_scripts_of_this_tree(self):
self.install()
stale = self.home / ".claude" / "skills" / HOST_SCRIPTS[2]
stale.write_text(stale.read_text(encoding="utf-8") + "\n", encoding="utf-8")
done = subprocess.run(["bash", str(SCRIPT), "install"], env=self.env, capture_output=True, text=True, timeout=120, check=False)
self.assertEqual(done.returncode, 0, done.stderr)
self.assertIn("run 'make install' to enable", done.stderr)
self.assertNotIn(f"{NEW}-offices.path", json.loads(self.state.read_text(encoding="utf-8"))["enabled"])
def test_the_runner_unit_waits_for_the_installed_skill_scripts_of_this_tree(self):
self.assertIn(f"{NEW}-runner.service", self.install()["enabled"])
runner = f"{NEW}-runner.service"
for script in HOST_SCRIPTS:
with self.subTest(script=script):
installed = self.home / ".claude" / "skills" / script
kept = installed.read_text(encoding="utf-8")
installed.write_text(kept + "\n", encoding="utf-8")
state = json.loads(self.state.read_text(encoding="utf-8"))
self.state.write_text(json.dumps({**state, "calls": [], "enabled": sorted({*state["enabled"], runner})}), encoding="utf-8")
done = subprocess.run(["bash", str(SCRIPT), "install"], env=self.env, capture_output=True, text=True, timeout=120, check=False)
self.assertEqual(done.returncode, 0, done.stderr)
self.assertIn(f"run 'make install' to enable {runner}", done.stderr)
after = json.loads(self.state.read_text(encoding="utf-8"))
self.assertNotIn(runner, after["enabled"])
self.assertNotIn(["systemctl", "--user", "restart", runner], after["calls"])
refused = subprocess.run(["bash", str(SCRIPT), "runner"], env=self.env, capture_output=True, text=True, timeout=120, check=False)
self.assertEqual(refused.returncode, 1, refused.stderr)
self.assertNotIn(runner, json.loads(self.state.read_text(encoding="utf-8"))["enabled"])
installed.write_text(kept, encoding="utf-8")
def test_install_refreshes_and_restarts_only_an_enabled_runner_unit(self):
units = self.home / ".config" / "systemd" / "user"
(units / "default.target.wants" / f"{OLD}-runner.service").unlink()
(units / f"{OLD}-runner.service").unlink()
shipped = (REPO_ROOT / "orchestrator" / "systemd" / f"{NEW}-runner.service").read_text(encoding="utf-8")
restart = ["systemctl", "--user", "restart", f"{NEW}-runner.service"]
for enabled in (False, True):
with self.subTest(enabled=enabled):
self.write(units / f"{NEW}-runner.service", "[Unit]\nstale\n")
state = json.loads(self.state.read_text(encoding="utf-8"))
self.state.write_text(json.dumps({**state, "calls": [], "enabled": [f"{NEW}-runner.service"] if enabled else []}), encoding="utf-8")
calls = self.install()["calls"]
self.assertEqual((units / f"{NEW}-runner.service").read_text(encoding="utf-8") == shipped, enabled)
self.assertEqual(restart in calls, enabled)
def test_every_shipped_service_pins_the_path(self):
units = sorted((REPO_ROOT / "orchestrator" / "systemd").glob("*.service"))
self.assertIn(f"{NEW}-runner.service", [unit.name for unit in units])
for unit in units:
with self.subTest(unit=unit.name):
self.assertIn("Environment=PATH=/usr/bin:/bin", unit.read_text(encoding="utf-8").splitlines())
def test_install_ships_no_bytecode_from_the_tree(self):
client = REPO_ROOT / "orchestrator" / "client"
if not (client / "__pycache__").exists():
(client / "__pycache__").mkdir()
self.addCleanup(shutil.rmtree, client / "__pycache__", True)
for planted in (client / "__pycache__" / "planted.cpython-0.pyc", client / "planted.pyc", client / "planted.pyo"):
planted.write_bytes(b"planted")
self.addCleanup(planted.unlink, True)
self.install()
hub = self.home / ".local" / "share" / NEW
self.assertEqual(sorted(path for path in hub.rglob("*") if path.name == "__pycache__" or path.suffix in (".pyc", ".pyo")), [])
self.assertTrue((hub / "client" / "permission_hook.py").is_file())
def test_old_names_live_only_in_the_migration(self):
done = subprocess.run(["git", "grep", "-niE", "kvb[-_]hub", "--", ".", ":!docs/requirements", f":!{Path(__file__).relative_to(REPO_ROOT)}"], cwd=REPO_ROOT, capture_output=True, text=True, check=False)
hits = sorted(line.split(":", 2)[0] + ":" + line.split(":", 2)[2].strip() for line in done.stdout.splitlines())
self.assertEqual(hits, ['orchestrator/templates/page-state.js:let userViews = validViews(stored(STORE.views, stored("kvb-hub.views", [])));', 'scripts/orchestrator.sh:LEGACY="kvb-hub"'])
if __name__ == "__main__":
unittest.main()