From 3dc3f61c5d3bbda749756a50026ab3694f886b5d Mon Sep 17 00:00:00 2001 From: clover caruso Date: Sat, 21 Nov 2026 19:59:51 -0800 Subject: [PATCH] Mac VMs: manage m3-phoenix-box beside m3-devbox The dashboard knew one Mac VM, so Computer MCP connections could not be granted the network-locked Phoenix guest. SNOWGLOBE_MAC_VM now lists the VMs, requests to the Mac bridge name the VM, and the bridge maps each name to a Tart VM (power through the locked start script, status without probing the guest agent). install-mac-computer.py takes the guest to install into. Assisted-by: claude-sonnet-5.5 --- nixos/mac-vm.nix | 7 ++- tools/dashboard-host-test.py | 2 +- tools/dashboard-host.py | 6 +-- tools/dashboard-stage.py | 9 ++-- tools/install-mac-computer.py | 12 +++--- tools/mac-computer-test.py | 2 +- tools/mac-vm-bridge.py | 52 ++++++++++++++++++---- tools/mac-vm-test.py | 81 ++++++++++++++++++++++++++++++++--- tools/mac-vms.py | 68 +++++++++++++++++++---------- tools/phoenix-box.md | 30 +++++++++++++ tools/vm-computer.py | 4 +- tools/vms.py | 4 +- 12 files changed, 223 insertions(+), 54 deletions(-) create mode 100644 tools/phoenix-box.md diff --git a/nixos/mac-vm.nix b/nixos/mac-vm.nix index 144c8a0bfb67d96ba0b11524774d2b12804d093b..b584af2d9578735c0eae16ccb1ab1916b08f1192 100644 --- a/nixos/mac-vm.nix +++ b/nixos/mac-vm.nix @@ -1,16 +1,19 @@ { ... }: let workspace = builtins.fromJSON (builtins.readFile ../config/mac-studio-sandbox.json); + owner = "8c909fab-98b0-4472-9171-9606ccebe9d5"; in { systemd.services.snowglobe-host = { environment.SNOWGLOBE_MAC_VM = builtins.toJSON { - name = "m3-devbox"; - owner = "8c909fab-98b0-4472-9171-9606ccebe9d5"; address = workspace.clientAddress; port = 22; user = "clo"; identity = "/var/lib/snowglobe/mac-vm/ssh_key"; knownHosts = "/var/lib/snowglobe/mac-vm/known_hosts"; + vms = [ + { name = "m3-devbox"; inherit owner; console = true; suspend = true; } + { name = "m3-phoenix-box"; inherit owner; console = false; suspend = false; } + ]; }; serviceConfig.IPAddressAllow = [ "${workspace.clientAddress}/32" ]; }; diff --git a/tools/dashboard-host-test.py b/tools/dashboard-host-test.py index 91721f32c5875951b3e767e30087a160714a4774..2ca7a9fbd19f786ba88efe858c1e13917f904a2e 100644 --- a/tools/dashboard-host-test.py +++ b/tools/dashboard-host-test.py @@ -213,7 +213,7 @@ class Host: while not (root / 'release').exists(): time.sleep(.01) return {'operation': request['operation'], 'active_snapshot': active_snapshot} m.Host = Host -m.vms.mac_vms.CONFIG = {'name': 'managed-mac'} +m.vms.mac_vms.CONFIG = {'vms': [{'name': 'managed-mac'}]} with m.vms.computer_lock('fixture', 'exec') as active, m.vms.computer_lock('managed-mac', 'exec'): (root / 'active.json').write_text(json.dumps(active)) m.main() diff --git a/tools/dashboard-host.py b/tools/dashboard-host.py index a150cc43d5b4aa25a0cf4531eeabfe3013cab157..69e167d2990d685ec8fdfaf960d76caf7fccaf27 100644 --- a/tools/dashboard-host.py +++ b/tools/dashboard-host.py @@ -217,7 +217,7 @@ class Host: except ValueError as error: raise Rejected(str(error)) from error if action == "clipboard": - if not vms.mac_vms.CONFIG or payload["name"] != vms.mac_vms.CONFIG["name"]: + if not vms.mac_vms.owns(payload["name"]): raise Rejected("This VM does not support this guest operation.") try: return vms.mac_vms.handle(action, payload) @@ -385,7 +385,7 @@ def stream_console(connection, host, request): screen.connect(details["path"]) screen.setblocking(False) elif details.get("remote"): - _, screen = resources.enter_context(vms.mac_vms.connect({"action": "console"}, timeout=20)) + _, screen = resources.enter_context(vms.mac_vms.connect(details["name"], {"action": "console"}, timeout=20)) screen.setblocking(False) else: identity = details["uuid"] @@ -533,7 +533,7 @@ def serve_connection(connection, host, allowed_uid, allowed_operations=None): operation_handle = action.get("action") == "process" and isinstance(identity, str) and identity.startswith("operation:") control = operation_handle and action.get("operation") == "status" if (action.get("action") == "status" or operation_handle and action.get("operation") == "stop") and ( - not vms.mac_vms.CONFIG or payload["name"] != vms.mac_vms.CONFIG["name"] + not vms.mac_vms.owns(payload["name"]) ): active_snapshot = vms.computer_activity(payload["name"]) control = active_snapshot is not None diff --git a/tools/dashboard-stage.py b/tools/dashboard-stage.py index c7545ee3f39cabaf9c370710d66b2fd4649c3924..28af941551a0e62b5aff913ff5be529b991584c1 100644 --- a/tools/dashboard-stage.py +++ b/tools/dashboard-stage.py @@ -148,9 +148,11 @@ def stage(computer=None): selected = saved["computer"].split(",") for name in selected: boundary.vms.ensure_name(name) - if not mac or name != mac["name"]: + if not mac or all(entry["name"] != name for entry in mac["vms"]): snowglobe.command("virsh", "domuuid", name, capture=True) - if mac and mac["name"] not in selected: + if mac: + mac["vms"] = [entry for entry in mac["vms"] if entry["name"] in selected] + if mac and not mac["vms"]: mac = None nix = ["nix", "--extra-experimental-features", "nix-command flakes"] package = "path:" + str(snowglobe.REPO) + "#dashboard-image" @@ -229,7 +231,8 @@ def stage(computer=None): owner = accounts.execute("SELECT id FROM users WHERE username='preview'").fetchone() if not owner: raise ValueError("The preview account is unavailable.") - mac["owner"] = owner[0] + for entry in mac["vms"]: + entry["owner"] = owner[0] computer_environment.append("--setenv=SNOWGLOBE_MAC_VM=" + json.dumps(mac)) host_python = snowglobe.command(*nix, "build", "--out-link", str(root / "host-python"), "--print-out-paths", "path:" + str(snowglobe.REPO) + "#host-python", capture=True).stdout.strip() subprocess.run(["systemctl", "stop", "snowglobe-preview-screen-" + identity], capture_output=True) diff --git a/tools/install-mac-computer.py b/tools/install-mac-computer.py index 5c5db8f751e270c863924b75738eec395d094cfa..df9b2ab2f730db4f6720a4544004a63b71f22fbc 100644 --- a/tools/install-mac-computer.py +++ b/tools/install-mac-computer.py @@ -1,17 +1,19 @@ #!/usr/bin/env python3 -"""Install computer-use support in the existing managed Mac guest and bridge.""" +"""Install computer-use support in a managed Mac guest (default: the devbox) and refresh the shared bridge.""" from importlib import import_module import json import os from pathlib import Path +import sys import tempfile vm = import_module("mac-agent-vm") root = Path(__file__).resolve().parent -identity = vm.tart("exec", vm.NAME, "/bin/hostname", capture_output=True, text=True, timeout=10).stdout.strip() +guest = sys.argv[1] if len(sys.argv) > 1 else vm.NAME +identity = vm.tart("exec", guest, "/bin/hostname", capture_output=True, text=True, timeout=10).stdout.strip() print("Installing guest tools in " + identity, flush=True) source = {name: (root / name).read_text() for name in ["mac-computer.py", "mac-agent-gui.swift"]} -vm.tart("exec", "-i", vm.NAME, "/Library/Developer/CommandLineTools/usr/bin/python3", "-c", ''' +vm.tart("exec", "-i", guest, "/Library/Developer/CommandLineTools/usr/bin/python3", "-c", ''' import json, pathlib, subprocess, sys, tempfile files = json.load(sys.stdin) with tempfile.TemporaryDirectory(prefix="snowglobe-computer-install-") as temporary: @@ -23,7 +25,7 @@ with tempfile.TemporaryDirectory(prefix="snowglobe-computer-install-") as tempor for source, destination in [("mac-computer.py", "/usr/local/libexec/snowglobe-computer.py"), ("agent-gui", "/usr/local/bin/agent-gui")]: subprocess.run(["sudo", "-n", "install", "-m", "755", str(root / source), destination], check=True) ''', input=json.dumps(source).encode(), timeout=180) -for name in ["mac-agent-vm.py", "mac-vm-bridge.py"]: +for name in ["mac-agent-vm.py", "mac-vm-bridge.py"] if guest == vm.NAME else []: target = vm.ROOT / "runtime/control" / name with tempfile.NamedTemporaryFile(dir=target.parent, delete=False) as outgoing: pending = Path(outgoing.name) @@ -33,7 +35,7 @@ for name in ["mac-agent-vm.py", "mac-vm-bridge.py"]: os.replace(pending, target) finally: pending.unlink(missing_ok=True) -result = vm.tart("exec", "-i", vm.NAME, "/Library/Developer/CommandLineTools/usr/bin/python3", "/usr/local/libexec/snowglobe-computer.py", +result = vm.tart("exec", "-i", guest, "/Library/Developer/CommandLineTools/usr/bin/python3", "/usr/local/libexec/snowglobe-computer.py", input=b'{"action":"status"}', capture_output=True, timeout=15) value = json.loads(result.stdout) if not all(value.get("permissions", {}).get(name) for name in ["accessibility", "screenCapture", "postEvent"]): diff --git a/tools/mac-computer-test.py b/tools/mac-computer-test.py index 565633939c6a9e027d2e151ef7f828cbef68ef3b..4d7b87adf08549d1c21ea1c753d33588cfb35dec 100644 --- a/tools/mac-computer-test.py +++ b/tools/mac-computer-test.py @@ -71,7 +71,7 @@ class ComputerTests(unittest.TestCase): tart.write_text("#!/usr/bin/python3\n" + output + "\n") tart.chmod(0o700) with patch.object(bridge.vm, "TART", tart), self.assertRaisesRegex(ValueError, message): - bridge.computer({"action": "status"}) + bridge.computer("gui-agent", {"action": "status"}) if __name__ == "__main__": diff --git a/tools/mac-vm-bridge.py b/tools/mac-vm-bridge.py index c9dcfdeb0a6399fc565dde6508a9b085b161036b..e3a5bdec36b3291d25e36c7eaf896f3ce4ec3015 100644 --- a/tools/mac-vm-bridge.py +++ b/tools/mac-vm-bridge.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -"""Restricted SSH entry point for the existing Mac development VM.""" +"""Restricted SSH entry point for the managed Mac VMs.""" import fcntl from importlib import import_module import json @@ -13,6 +13,10 @@ import tempfile import time vm = import_module("mac-agent-vm") +# Dashboard names mapped to Tart VMs. The first is managed end to end by mac-agent-vm.py; the others are +# network-locked guests with their own start script, reached only through Tart's guest agent. +VMS = {"m3-devbox": vm.NAME, "m3-phoenix-box": "m3-phoenix-box"} +LOCKED_START = vm.ROOT / "runtime/control/start-m3-phoenix-box" def status(): @@ -38,18 +42,27 @@ def status(): "agent": agent, "pendingAction": pending, "mac": current["MACAddress"]} +def locked_status(name): + current = json.loads(vm.tart("get", name, "--format", "json", capture_output=True, text=True, timeout=5).stdout) + disk = vm.ROOT / "state/vms" / name / "disk.img" + return {"state": {"running": "running", "stopped": "shutoff", "suspended": "pmsuspended"}[current["State"]], + "vcpus": current["CPU"], "memory": current["Memory"] * 2**20, + "disk": {"capacity": disk.stat().st_size, "allocation": disk.stat().st_blocks * 512}, + "agent": None, "pendingAction": None, "mac": current["MACAddress"]} + + def respond(value): data = json.dumps(value).encode() sys.stdout.buffer.write(struct.pack("!I", len(data)) + data) sys.stdout.buffer.flush() -def computer(request): +def computer(name, request): with tempfile.TemporaryFile() as source, selectors.DefaultSelector() as selector: source.write(json.dumps(request).encode()) source.seek(0) # Recovery must work when the selected Xcode is unavailable. - process = subprocess.Popen([str(vm.TART), "exec", "-i", vm.NAME, "/Library/Developer/CommandLineTools/usr/bin/python3", + process = subprocess.Popen([str(vm.TART), "exec", "-i", name, "/Library/Developer/CommandLineTools/usr/bin/python3", "/usr/local/libexec/snowglobe-computer.py"], stdin=source, stdout=subprocess.PIPE, stderr=subprocess.PIPE, start_new_session=True, env={**os.environ, "TART_HOME": str(vm.ROOT / "state")}) output = bytearray() @@ -108,6 +121,18 @@ def lifecycle(action): process.wait() +def locked_power(name, action): + if action in {"stop", "reboot"}: + vm.tart("stop", name, timeout=120) + if action in {"start", "reboot"}: + subprocess.run([str(LOCKED_START)], check=True, stdout=subprocess.DEVNULL, timeout=30) + for _ in range(60): + if json.loads(vm.tart("get", name, "--format", "json", capture_output=True, text=True, timeout=5).stdout)["Running"]: + return + time.sleep(1) + raise ValueError("The VM did not start.") + + def main(): line = sys.stdin.buffer.readline(1048577) if len(line) > 1048576 or not line.endswith(b"\n"): @@ -115,25 +140,36 @@ def main(): request = json.loads(line) if not isinstance(request, dict): raise ValueError("Choose a supported VM operation.") + # Hosts that predate multiple VMs omit the name. + requested = request.pop("vm", "m3-devbox") + name = VMS.get(requested) if isinstance(requested, str) else None + if name is None: + raise ValueError("Choose a managed VM.") + primary = name == vm.NAME action = request.get("action") if action == "status" and set(request) == {"action"}: - respond({"value": status()}) + respond({"value": {**(status() if primary else locked_status(name)), "name": name}}) elif action in {"start", "shutdown", "reboot", "suspend", "resume"} and set(request) == {"action"}: + if not primary and action in {"suspend", "resume"}: + raise ValueError("This operation is unavailable for this Mac VM.") action = {"shutdown": "stop", "resume": "start"}.get(action, action) - lifecycle(action) + if primary: + lifecycle(action) + else: + locked_power(name, action) respond({"value": None}) elif action == "clipboard" and set(request) == {"action", "text"}: value = request["text"] if not isinstance(value, str) or len(value.encode()) > 262144: raise ValueError("Shorten the clipboard text, then send it again.") - vm.tart("exec", "-i", vm.NAME, "/usr/bin/env", "LC_CTYPE=en_US.UTF-8", "/usr/bin/pbcopy", + vm.tart("exec", "-i", name, "/usr/bin/env", "LC_CTYPE=en_US.UTF-8", "/usr/bin/pbcopy", input=value.encode(), capture_output=True, timeout=15) respond({"value": None}) elif action == "computer" and set(request) == {"action", "request"}: if not isinstance(request["request"], dict): raise ValueError("Send a computer operation.") - respond({"value": computer(request["request"])}) - elif action == "console" and set(request) == {"action"}: + respond({"value": computer(name, request["request"])}) + elif action == "console" and set(request) == {"action"} and primary: with vm.guest_screen() as upstream: respond({"value": None}) try: diff --git a/tools/mac-vm-test.py b/tools/mac-vm-test.py index 309bc951cee21a6de4bccc69d854d5db175d38ce..e4c1785251b1490e62b024e7d2be9be998a5290e 100644 --- a/tools/mac-vm-test.py +++ b/tools/mac-vm-test.py @@ -19,11 +19,16 @@ bridge = import_module("mac-vm-bridge") backend = import_module("mac-vms") +FIXTURE = {"vms": [{"name": "mac-fixture", "owner": "fixture-owner", "console": True, "suspend": True}, + {"name": "locked-fixture", "owner": "fixture-owner", "console": False, "suspend": False}]} + + class MacVMTests(unittest.TestCase): def test_protocol_rejects_arbitrary_commands_and_unbounded_clipboards(self): for request in [{"action": "exec", "command": "id"}, {"action": "status", "name": "other"}, {"action": "clipboard", "text": "a" * 262145}, ["status"], - {"action": "clipboard", "text": 42}]: + {"action": "clipboard", "text": 42}, {"action": "status", "vm": "other"}, + {"action": "status", "vm": ["m3-devbox"]}]: with self.subTest(request=str(request)[:80]), \ patch.object(sys, "stdin", io.TextIOWrapper(io.BytesIO(json.dumps(request).encode() + b"\n"))), \ patch.object(bridge.vm, "tart") as tart, patch.object(bridge, "lifecycle") as lifecycle: @@ -49,7 +54,7 @@ class MacVMTests(unittest.TestCase): "pendingAction": None, "disk": {"capacity": 128 * 2**30, "allocation": 42 * 2**30}, "mac": "00:11:22:33:44:55"}, None with tempfile.TemporaryDirectory() as directory, \ - patch.object(backend, "CONFIG", {"name": "mac-fixture", "owner": "fixture-owner"}), \ + patch.object(backend, "CONFIG", FIXTURE), \ patch.object(backend, "CACHE", Path(directory) / "cache.json"): with patch.object(backend, "connect", side_effect=ValueError("offline")): first = backend.domain()[0] @@ -66,21 +71,87 @@ class MacVMTests(unittest.TestCase): self.assertEqual(offline["name"], "mac-fixture") self.assertEqual(offline["owner"], "fixture-owner") self.assertTrue(offline["ownerOnly"]) - self.assertEqual(backend.handle("owner", {}), {"owner": "fixture-owner", "ownerOnly": True}) + self.assertEqual(backend.handle("owner", {"name": "mac-fixture"}), {"owner": "fixture-owner", "ownerOnly": True}) self.assertEqual(offline["memory"], live["memory"]) self.assertEqual(offline["state"], "unreachable") self.assertIsNone(offline["agent"]) self.assertIsNone(offline["pendingAction"]) def test_unsupported_operations_never_contact_mac(self): - with patch.object(backend, "connect") as connect: + with patch.object(backend, "CONFIG", FIXTURE), patch.object(backend, "connect") as connect: for action in ["delete", "update", "preset", "media"]: with self.subTest(action=action), self.assertRaises(ValueError): backend.handle(action, {"name": "mac-fixture"}) with self.assertRaises(ValueError): - backend.handle("act", {"action": "destroy"}) + backend.handle("act", {"name": "mac-fixture", "action": "destroy"}) connect.assert_not_called() + def test_each_vm_keeps_its_own_features_and_requests_name_it(self): + @contextlib.contextmanager + def connected(name, request, timeout=10): + yield {"state": "running", "vcpus": 8, "memory": 2**34, "agent": None, "pendingAction": None, + "disk": {"capacity": 2**36, "allocation": 2**35}, "mac": "00:11:22:33:44:55", "name": name}, None + with tempfile.TemporaryDirectory() as directory, patch.object(backend, "CONFIG", FIXTURE), \ + patch.object(backend, "CACHE", Path(directory) / "cache.json"): + with patch.object(backend, "connect", connected): + full, locked = backend.domain() + self.assertEqual((full["name"], full["console"], "suspend" in full["capabilities"]), ("mac-fixture", True, True)) + self.assertEqual((locked["name"], locked["console"], "suspend" in locked["capabilities"]), ("locked-fixture", False, False)) + self.assertIn("computer", locked["capabilities"]) + self.assertTrue(backend.owns("locked-fixture") and not backend.owns("other")) + with patch.object(backend, "connect") as connect: + for action, payload in [("console", {}), ("act", {"action": "suspend"}), ("act", {"action": "resume"})]: + with self.subTest(operation=action, payload=payload), self.assertRaises(ValueError): + backend.handle(action, {"name": "locked-fixture", **payload}) + connect.assert_not_called() + self.assertEqual(backend.handle("console", {"name": "mac-fixture"}), {"remote": True, "name": "mac-fixture"}) + with patch.object(backend, "connect", connected), patch.object(backend, "CACHE", Path(directory) / "cache.json"): + self.assertEqual(backend.handle("computer", {"name": "locked-fixture", "request": {"action": "status"}})["name"], "locked-fixture") + with patch.object(backend, "connect", side_effect=ValueError("offline")) as offline: + self.assertEqual([value["state"] for value in backend.domain()], ["unreachable", "unreachable"]) + self.assertEqual([value["memory"] for value in backend.domain()], [2**34, 2**34]) + self.assertEqual({call.args[0] for call in offline.call_args_list}, {"mac-fixture", "locked-fixture"}) + + def test_bridge_serves_only_named_vms_and_hosts_without_a_name_get_the_devbox(self): + def run(request): + with patch.object(sys, "stdin", io.TextIOWrapper(io.BytesIO(json.dumps(request).encode() + b"\n"))), \ + patch.object(bridge.vm, "tart") as tart, patch.object(bridge, "respond") as respond, \ + patch.object(bridge, "status", return_value={"state": "devbox"}) as status, \ + patch.object(bridge, "locked_status", return_value={"state": "locked"}) as locked_status, \ + patch.object(bridge, "lifecycle") as lifecycle, patch.object(bridge, "locked_power") as locked_power: + bridge.main() + return tart, respond, status, locked_status, lifecycle, locked_power + _, respond, status, locked_status, *_ = run({"action": "status"}) + respond.assert_called_once_with({"value": {"state": "devbox", "name": "gui-agent"}}) + _, respond, status, locked_status, *_ = run({"action": "status", "vm": "m3-phoenix-box"}) + respond.assert_called_once_with({"value": {"state": "locked", "name": "m3-phoenix-box"}}) + locked_status.assert_called_once_with("m3-phoenix-box") + status.assert_not_called() + tart, *_ = run({"action": "clipboard", "text": "x", "vm": "m3-phoenix-box"}) + self.assertEqual(tart.call_args.args[:3], ("exec", "-i", "m3-phoenix-box")) + *_, lifecycle, locked_power = run({"action": "reboot", "vm": "m3-phoenix-box"}) + locked_power.assert_called_once_with("m3-phoenix-box", "reboot") + lifecycle.assert_not_called() + *_, lifecycle, locked_power = run({"action": "shutdown", "vm": "m3-devbox"}) + lifecycle.assert_called_once_with("stop") + locked_power.assert_not_called() + for request in [{"action": "suspend", "vm": "m3-phoenix-box"}, {"action": "resume", "vm": "m3-phoenix-box"}, + {"action": "console", "vm": "m3-phoenix-box"}]: + with self.subTest(request=request), self.assertRaises(ValueError): + run(request) + + def test_locked_vm_start_waits_until_tart_reports_it_running(self): + states = iter([False, False, True]) + def tart(*args, **kwargs): + if args[0] == "get": + return subprocess.CompletedProcess(args, 0, stdout=json.dumps({"Running": next(states)})) + with patch.object(bridge.vm, "tart", side_effect=tart) as run, patch.object(bridge.subprocess, "run") as launch, \ + patch.object(bridge.time, "sleep"): + bridge.locked_power("m3-phoenix-box", "reboot") + self.assertEqual(run.call_args_list[0].args, ("stop", "m3-phoenix-box")) + launch.assert_called_once() + self.assertEqual(sum(call.args[0] == "get" for call in run.call_args_list), 3) + def test_lifecycle_timeout_reaps_commands_but_preserves_detached_runner(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory) diff --git a/tools/mac-vms.py b/tools/mac-vms.py index ca0aed41c1300b2591e31e85a7b725ebe35405f7..01a34289f9b1761693e268300fd44a05652a9006 100644 --- a/tools/mac-vms.py +++ b/tools/mac-vms.py @@ -1,3 +1,4 @@ +import concurrent.futures import contextlib import json import os @@ -9,12 +10,19 @@ import tempfile CONFIG = json.loads(os.environ.get("SNOWGLOBE_MAC_VM", "null")) -CAPABILITIES = ["start", "shutdown", "reboot", "suspend", "resume", "clipboard", "computer"] CACHE = Path(os.environ.get("SNOWGLOBE_MAC_VM_CACHE", "/var/lib/snowglobe/host/mac-vm.json")) +def vm(name): + return next((entry for entry in CONFIG["vms"] if entry["name"] == name), None) if CONFIG else None + + +def owns(name): + return vm(name) is not None + + @contextlib.contextmanager -def connect(request, timeout=10): +def connect(name, request, timeout=10): if not CONFIG: raise ValueError("The Mac VM is not configured on this host.") parent, child = socket.socketpair() @@ -30,7 +38,7 @@ def connect(request, timeout=10): ], stdin=child, stdout=child, stderr=subprocess.DEVNULL) child.close() parent.settimeout(timeout) - parent.sendall(json.dumps(request).encode() + b"\n") + parent.sendall(json.dumps({**request, "vm": name}).encode() + b"\n") def read(size): data = bytearray() @@ -68,56 +76,72 @@ def connect(request, timeout=10): process.wait() -def ownership(): - return {"owner": CONFIG["owner"], "ownerOnly": True} +def ownership(entry): + return {"owner": entry["owner"], "ownerOnly": True} -def domain(): - if not CONFIG: - return [] - value = {"name": CONFIG["name"], **ownership(), "host": "Mac Studio", "arch": "aarch64", +def power(entry): + return ["start", "shutdown", "reboot"] + (["suspend", "resume"] if entry["suspend"] else []) + + +def observe(entry): + value = {"name": entry["name"], **ownership(entry), "host": "Mac Studio", "arch": "aarch64", "state": "unreachable", "reason": None, "os": "macOS", "vcpus": None, "memory": None, "balloon": None, "pinned": None, "autostart": False, "startedAt": None, "agent": None, "setup": None, "pendingAction": None, - "disks": [], "media": [], "firmware": "uefi", "platform": "macos", "console": True, - "serial": False, "interfaces": [], "hostdevs": [], "capabilities": CAPABILITIES} + "disks": [], "media": [], "firmware": "uefi", "platform": "macos", "console": entry["console"], + "serial": False, "interfaces": [], "hostdevs": [], "capabilities": power(entry) + ["clipboard", "computer"]} try: - with connect({"action": "status"}, timeout=8) as (status, _): + with connect(entry["name"], {"action": "status"}, timeout=8) as (status, _): value.update({key: status[key] for key in ("state", "vcpus", "memory", "agent", "pendingAction")}) value["balloon"] = value["memory"] - value["disks"] = [{"target": "disk", "pool": "Mac Studio", "source": "gui-agent", **status["disk"]}] + value["disks"] = [{"target": "disk", "pool": "Mac Studio", "source": status.get("name", entry["name"]), **status["disk"]}] value["interfaces"] = [{"source": "Mac Studio NAT", "mac": status["mac"], "addresses": []}] except (ValueError, OSError, KeyError, TypeError): try: - previous = json.loads(CACHE.read_text()) + previous = json.loads(CACHE.read_text())[entry["name"]] for key in ("vcpus", "memory", "balloon", "disks", "interfaces"): value[key] = previous[key] except (ValueError, OSError, KeyError, TypeError): pass value.update(state="unreachable", agent=None, pendingAction=None) - else: + return value, value["state"] != "unreachable" + + +def domain(): + if not CONFIG: + return [] + with concurrent.futures.ThreadPoolExecutor(len(CONFIG["vms"])) as pool: + observed = list(pool.map(observe, CONFIG["vms"])) + live = {value["name"]: value for value, reachable in observed if reachable} + if live: try: CACHE.parent.mkdir(mode=0o700, parents=True, exist_ok=True) + try: + live = {**json.loads(CACHE.read_text()), **live} + except (ValueError, OSError): + pass descriptor, temporary = tempfile.mkstemp(dir=CACHE.parent, prefix=".mac-vm-") try: with os.fdopen(descriptor, "w") as outgoing: - json.dump(value, outgoing) + json.dump(live, outgoing) os.replace(temporary, CACHE) finally: Path(temporary).unlink(missing_ok=True) except OSError: pass - return [value] + return [value for value, _ in observed] def handle(action, payload): + entry = vm(payload["name"]) if action == "owner": - return ownership() + return ownership(entry) if action == "access": return None - if action == "console": - return {"remote": True} - if action == "act" and payload["action"] in {"start", "shutdown", "reboot", "suspend", "resume"}: + if action == "console" and entry["console"]: + return {"remote": True, "name": entry["name"]} + if action == "act" and payload["action"] in power(entry): request = {"action": payload["action"]} elif action == "clipboard": request = {"action": "clipboard", "text": payload["text"]} @@ -125,5 +149,5 @@ def handle(action, payload): request = {"action": "computer", "request": payload["request"]} else: raise ValueError("This operation is unavailable for this Mac VM.") - with connect(request, timeout=220) as (value, _): + with connect(entry["name"], request, timeout=220) as (value, _): return value diff --git a/tools/phoenix-box.md b/tools/phoenix-box.md new file mode 100644 index 0000000000000000000000000000000000000000..1767a8f0dc7be1c684915dde052a42f6baa7d857 --- /dev/null +++ b/tools/phoenix-box.md @@ -0,0 +1,30 @@ +# Phoenix box + +`m3-phoenix-box` is a macOS 15 Tart VM beside `gui-agent`, in the same VM home under +`/Volumes/Documents/VM/macOS Code Sandbox`. It holds Discord Phoenix and runs it against +Spacebar, so its network is locked from outside the guest. + +Start it with `runtime/control/start-m3-phoenix-box` in that VM home. The script owns the +flags: headless, `--vnc-experimental`, and Softnet with every destination blocked except +`100.64.0.1/32`, Snow Globe's tailnet address. This Mac's NAT only leaves through Tailscale +(its own routes do not reach the internet), and Snow Globe's Caddy answers on that address. +Softnet filters by address, not port, so the VM can reach every port Snow Globe accepts +on the tailnet. + +- **Names:** the guest has no DNS (its gateway is blocked), so `/etc/hosts` maps + `spacebar.paperclover.net` to `100.64.0.1`. Go ignores `/etc/hosts` on macOS unless + `GODEBUG=netdns=go`, which `run-phoenix-spacebar` sets for the Caddy relay. +- **Access:** Softnet also blocks host to guest, so there is no SSH or Screen Sharing. + Tart's own VNC server (random password per run, printed to `run.log` in + `private/m3-phoenix-box`, listening on every interface of this Mac) and `tart exec` are + the ways in. Console login is `admin` / `admin`; SSH accepts only the key in that + private directory. +- **Softnet:** 0.23.0 at `/usr/local/bin/softnet`, owned by root with the setuid bit. + Releases from 0.24 need macOS 26. +- **Dashboard:** `nixos/mac-vm.nix` lists both Mac VMs. The Mac-side bridge + (`runtime/control/mac-vm-bridge.py`) maps each dashboard name to a Tart VM, so Computer + MCP connections can be granted `m3-phoenix-box` like any VM. It has no dashboard + console or suspend; power operations run `start-m3-phoenix-box` and `tart stop`. + +To manage another Mac VM, add it to `VMS` in the bridge and to `nixos/mac-vm.nix`, then run +`python3 tools/install-mac-computer.py ` to install the guest worker. diff --git a/tools/vm-computer.py b/tools/vm-computer.py index afcf4e3ff4c01925370b8eba9dac745f2d6d11e8..46545fbee4631cd36e66090d1f8d9039e0b07de9 100644 --- a/tools/vm-computer.py +++ b/tools/vm-computer.py @@ -536,7 +536,7 @@ def handle(name, request, *, active_snapshot=None): validate(request) vms.ensure_name(name) if request['action'] == 'process' and request['id'].startswith('operation:'): - if vms.mac_vms.CONFIG and name == vms.mac_vms.CONFIG['name']: + if vms.mac_vms.owns(name): raise ValueError('This VM does not provide active operation handles.') identity = request['id'] with vms.computer_control(name) as control: @@ -568,7 +568,7 @@ def handle(name, request, *, active_snapshot=None): return {'running': False} finish_operation(name, unfinished) return {'running': False, 'cancelled': True, 'id': identity} - if vms.mac_vms.CONFIG and name == vms.mac_vms.CONFIG['name']: + if vms.mac_vms.owns(name): return vms.mac_vms.handle('computer', {'name':name,'request':request}) token = DEADLINE.set(time.monotonic() + 210) operation_token = None diff --git a/tools/vms.py b/tools/vms.py index d82b077e84e0fa98dcaea6f9122ff45b4ac817fa..fd4d3d4a34f3a06613335d4acb7ccda78a06ad57 100644 --- a/tools/vms.py +++ b/tools/vms.py @@ -203,7 +203,7 @@ def validate(action, payload): for field, minimum, maximum in [("vcpus", 1, host["cpus"]), ("memory", 2**29, host["memory"]), ("disk", 2**30, 2**63 - 1)]: if type(payload[field]) is not int or not minimum <= payload[field] <= maximum: raise ValueError(f"Choose {field} within the host's supported range.") - if action == "create" and mac_vms.CONFIG and payload["name"] == mac_vms.CONFIG["name"]: + if action == "create" and mac_vms.owns(payload["name"]): raise ValueError("A VM already has this name. Choose another name.") if DISKS.is_symlink() or (DISKS / payload["name"]).is_symlink(): raise ValueError("The VM disk directory is a symbolic link. Remove the link before continuing.") @@ -1106,7 +1106,7 @@ def main(): return result if action == "library": return library() - if mac_vms.CONFIG and isinstance(payload, dict) and payload.get("name") == mac_vms.CONFIG["name"]: + if isinstance(payload, dict) and mac_vms.owns(payload.get("name")): return mac_vms.handle(action, payload) if action == "console": return console_target(payload["name"]) -- 2.54.1