| author | |
| committer | |
| log | 3dc3f61c5d3bbda749756a50026ab3694f886b5d |
| tree | 66bb28d89c34f0f64be3032f071125db4e145060 |
| parent | c584c4d8a4cca56a74abd108b2f6595ac50fd5ec |
| signature | Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU |
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.512 files changed, 223 insertions(+), 54 deletions(-)
nixos/mac-vm.nix+5-2| ... | ... | @@ -1,16 +1,19 @@ |
| 1 | 1 | { ... }: |
| 2 | 2 | let |
| 3 | 3 | workspace = builtins.fromJSON (builtins.readFile ../config/mac-studio-sandbox.json); |
| 4 | owner = "8c909fab-98b0-4472-9171-9606ccebe9d5"; | |
| 4 | 5 | in { |
| 5 | 6 | systemd.services.snowglobe-host = { |
| 6 | 7 | environment.SNOWGLOBE_MAC_VM = builtins.toJSON { |
| 7 | name = "m3-devbox"; | |
| 8 | owner = "8c909fab-98b0-4472-9171-9606ccebe9d5"; | |
| 9 | 8 | address = workspace.clientAddress; |
| 10 | 9 | port = 22; |
| 11 | 10 | user = "clo"; |
| 12 | 11 | identity = "/var/lib/snowglobe/mac-vm/ssh_key"; |
| 13 | 12 | knownHosts = "/var/lib/snowglobe/mac-vm/known_hosts"; |
| 13 | vms = [ | |
| 14 | { name = "m3-devbox"; inherit owner; console = true; suspend = true; } | |
| 15 | { name = "m3-phoenix-box"; inherit owner; console = false; suspend = false; } | |
| 16 | ]; | |
| 14 | 17 | }; |
| 15 | 18 | serviceConfig.IPAddressAllow = [ "${workspace.clientAddress}/32" ]; |
| 16 | 19 | }; |
tools/dashboard-host-test.py+1-1| ... | ... | @@ -213,7 +213,7 @@ class Host: |
| 213 | 213 | while not (root / 'release').exists(): time.sleep(.01) |
| 214 | 214 | return {'operation': request['operation'], 'active_snapshot': active_snapshot} |
| 215 | 215 | m.Host = Host |
| 216 | m.vms.mac_vms.CONFIG = {'name': 'managed-mac'} | |
| 216 | m.vms.mac_vms.CONFIG = {'vms': [{'name': 'managed-mac'}]} | |
| 217 | 217 | with m.vms.computer_lock('fixture', 'exec') as active, m.vms.computer_lock('managed-mac', 'exec'): |
| 218 | 218 | (root / 'active.json').write_text(json.dumps(active)) |
| 219 | 219 | m.main() |
tools/dashboard-host.py+3-3| ... | ... | @@ -217,7 +217,7 @@ class Host: |
| 217 | 217 | except ValueError as error: |
| 218 | 218 | raise Rejected(str(error)) from error |
| 219 | 219 | if action == "clipboard": |
| 220 | if not vms.mac_vms.CONFIG or payload["name"] != vms.mac_vms.CONFIG["name"]: | |
| 220 | if not vms.mac_vms.owns(payload["name"]): | |
| 221 | 221 | raise Rejected("This VM does not support this guest operation.") |
| 222 | 222 | try: |
| 223 | 223 | return vms.mac_vms.handle(action, payload) |
| ... | ... | @@ -385,7 +385,7 @@ def stream_console(connection, host, request): |
| 385 | 385 | screen.connect(details["path"]) |
| 386 | 386 | screen.setblocking(False) |
| 387 | 387 | elif details.get("remote"): |
| 388 | _, screen = resources.enter_context(vms.mac_vms.connect({"action": "console"}, timeout=20)) | |
| 388 | _, screen = resources.enter_context(vms.mac_vms.connect(details["name"], {"action": "console"}, timeout=20)) | |
| 389 | 389 | screen.setblocking(False) |
| 390 | 390 | else: |
| 391 | 391 | identity = details["uuid"] |
| ... | ... | @@ -533,7 +533,7 @@ def serve_connection(connection, host, allowed_uid, allowed_operations=None): |
| 533 | 533 | operation_handle = action.get("action") == "process" and isinstance(identity, str) and identity.startswith("operation:") |
| 534 | 534 | control = operation_handle and action.get("operation") == "status" |
| 535 | 535 | if (action.get("action") == "status" or operation_handle and action.get("operation") == "stop") and ( |
| 536 | not vms.mac_vms.CONFIG or payload["name"] != vms.mac_vms.CONFIG["name"] | |
| 536 | not vms.mac_vms.owns(payload["name"]) | |
| 537 | 537 | ): |
| 538 | 538 | active_snapshot = vms.computer_activity(payload["name"]) |
| 539 | 539 | control = active_snapshot is not None |
tools/dashboard-stage.py+6-3| ... | ... | @@ -148,9 +148,11 @@ def stage(computer=None): |
| 148 | 148 | selected = saved["computer"].split(",") |
| 149 | 149 | for name in selected: |
| 150 | 150 | boundary.vms.ensure_name(name) |
| 151 | if not mac or name != mac["name"]: | |
| 151 | if not mac or all(entry["name"] != name for entry in mac["vms"]): | |
| 152 | 152 | snowglobe.command("virsh", "domuuid", name, capture=True) |
| 153 | if mac and mac["name"] not in selected: | |
| 153 | if mac: | |
| 154 | mac["vms"] = [entry for entry in mac["vms"] if entry["name"] in selected] | |
| 155 | if mac and not mac["vms"]: | |
| 154 | 156 | mac = None |
| 155 | 157 | nix = ["nix", "--extra-experimental-features", "nix-command flakes"] |
| 156 | 158 | package = "path:" + str(snowglobe.REPO) + "#dashboard-image" |
| ... | ... | @@ -229,7 +231,8 @@ def stage(computer=None): |
| 229 | 231 | owner = accounts.execute("SELECT id FROM users WHERE username='preview'").fetchone() |
| 230 | 232 | if not owner: |
| 231 | 233 | raise ValueError("The preview account is unavailable.") |
| 232 | mac["owner"] = owner[0] | |
| 234 | for entry in mac["vms"]: | |
| 235 | entry["owner"] = owner[0] | |
| 233 | 236 | computer_environment.append("--setenv=SNOWGLOBE_MAC_VM=" + json.dumps(mac)) |
| 234 | 237 | 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() |
| 235 | 238 | subprocess.run(["systemctl", "stop", "snowglobe-preview-screen-" + identity], capture_output=True) |
tools/install-mac-computer.py+7-5| ... | ... | @@ -1,17 +1,19 @@ |
| 1 | 1 | #!/usr/bin/env python3 |
| 2 | """Install computer-use support in the existing managed Mac guest and bridge.""" | |
| 2 | """Install computer-use support in a managed Mac guest (default: the devbox) and refresh the shared bridge.""" | |
| 3 | 3 | from importlib import import_module |
| 4 | 4 | import json |
| 5 | 5 | import os |
| 6 | 6 | from pathlib import Path |
| 7 | import sys | |
| 7 | 8 | import tempfile |
| 8 | 9 | |
| 9 | 10 | vm = import_module("mac-agent-vm") |
| 10 | 11 | root = Path(__file__).resolve().parent |
| 11 | identity = vm.tart("exec", vm.NAME, "/bin/hostname", capture_output=True, text=True, timeout=10).stdout.strip() | |
| 12 | guest = sys.argv[1] if len(sys.argv) > 1 else vm.NAME | |
| 13 | identity = vm.tart("exec", guest, "/bin/hostname", capture_output=True, text=True, timeout=10).stdout.strip() | |
| 12 | 14 | print("Installing guest tools in " + identity, flush=True) |
| 13 | 15 | source = {name: (root / name).read_text() for name in ["mac-computer.py", "mac-agent-gui.swift"]} |
| 14 | vm.tart("exec", "-i", vm.NAME, "/Library/Developer/CommandLineTools/usr/bin/python3", "-c", ''' | |
| 16 | vm.tart("exec", "-i", guest, "/Library/Developer/CommandLineTools/usr/bin/python3", "-c", ''' | |
| 15 | 17 | import json, pathlib, subprocess, sys, tempfile |
| 16 | 18 | files = json.load(sys.stdin) |
| 17 | 19 | with tempfile.TemporaryDirectory(prefix="snowglobe-computer-install-") as temporary: |
| ... | ... | @@ -23,7 +25,7 @@ with tempfile.TemporaryDirectory(prefix="snowglobe-computer-install-") as tempor |
| 23 | 25 | for source, destination in [("mac-computer.py", "/usr/local/libexec/snowglobe-computer.py"), ("agent-gui", "/usr/local/bin/agent-gui")]: |
| 24 | 26 | subprocess.run(["sudo", "-n", "install", "-m", "755", str(root / source), destination], check=True) |
| 25 | 27 | ''', input=json.dumps(source).encode(), timeout=180) |
| 26 | for name in ["mac-agent-vm.py", "mac-vm-bridge.py"]: | |
| 28 | for name in ["mac-agent-vm.py", "mac-vm-bridge.py"] if guest == vm.NAME else []: | |
| 27 | 29 | target = vm.ROOT / "runtime/control" / name |
| 28 | 30 | with tempfile.NamedTemporaryFile(dir=target.parent, delete=False) as outgoing: |
| 29 | 31 | pending = Path(outgoing.name) |
| ... | ... | @@ -33,7 +35,7 @@ for name in ["mac-agent-vm.py", "mac-vm-bridge.py"]: |
| 33 | 35 | os.replace(pending, target) |
| 34 | 36 | finally: |
| 35 | 37 | pending.unlink(missing_ok=True) |
| 36 | result = vm.tart("exec", "-i", vm.NAME, "/Library/Developer/CommandLineTools/usr/bin/python3", "/usr/local/libexec/snowglobe-computer.py", | |
| 38 | result = vm.tart("exec", "-i", guest, "/Library/Developer/CommandLineTools/usr/bin/python3", "/usr/local/libexec/snowglobe-computer.py", | |
| 37 | 39 | input=b'{"action":"status"}', capture_output=True, timeout=15) |
| 38 | 40 | value = json.loads(result.stdout) |
| 39 | 41 | if not all(value.get("permissions", {}).get(name) for name in ["accessibility", "screenCapture", "postEvent"]): |
tools/mac-computer-test.py+1-1| ... | ... | @@ -71,7 +71,7 @@ class ComputerTests(unittest.TestCase): |
| 71 | 71 | tart.write_text("#!/usr/bin/python3\n" + output + "\n") |
| 72 | 72 | tart.chmod(0o700) |
| 73 | 73 | with patch.object(bridge.vm, "TART", tart), self.assertRaisesRegex(ValueError, message): |
| 74 | bridge.computer({"action": "status"}) | |
| 74 | bridge.computer("gui-agent", {"action": "status"}) | |
| 75 | 75 | |
| 76 | 76 | |
| 77 | 77 | if __name__ == "__main__": |
tools/mac-vm-bridge.py+44-8| ... | ... | @@ -1,5 +1,5 @@ |
| 1 | 1 | #!/usr/bin/env python3 |
| 2 | """Restricted SSH entry point for the existing Mac development VM.""" | |
| 2 | """Restricted SSH entry point for the managed Mac VMs.""" | |
| 3 | 3 | import fcntl |
| 4 | 4 | from importlib import import_module |
| 5 | 5 | import json |
| ... | ... | @@ -13,6 +13,10 @@ import tempfile |
| 13 | 13 | import time |
| 14 | 14 | |
| 15 | 15 | vm = import_module("mac-agent-vm") |
| 16 | # Dashboard names mapped to Tart VMs. The first is managed end to end by mac-agent-vm.py; the others are | |
| 17 | # network-locked guests with their own start script, reached only through Tart's guest agent. | |
| 18 | VMS = {"m3-devbox": vm.NAME, "m3-phoenix-box": "m3-phoenix-box"} | |
| 19 | LOCKED_START = vm.ROOT / "runtime/control/start-m3-phoenix-box" | |
| 16 | 20 | |
| 17 | 21 | |
| 18 | 22 | def status(): |
| ... | ... | @@ -38,18 +42,27 @@ def status(): |
| 38 | 42 | "agent": agent, "pendingAction": pending, "mac": current["MACAddress"]} |
| 39 | 43 | |
| 40 | 44 | |
| 45 | def locked_status(name): | |
| 46 | current = json.loads(vm.tart("get", name, "--format", "json", capture_output=True, text=True, timeout=5).stdout) | |
| 47 | disk = vm.ROOT / "state/vms" / name / "disk.img" | |
| 48 | return {"state": {"running": "running", "stopped": "shutoff", "suspended": "pmsuspended"}[current["State"]], | |
| 49 | "vcpus": current["CPU"], "memory": current["Memory"] * 2**20, | |
| 50 | "disk": {"capacity": disk.stat().st_size, "allocation": disk.stat().st_blocks * 512}, | |
| 51 | "agent": None, "pendingAction": None, "mac": current["MACAddress"]} | |
| 52 | ||
| 53 | ||
| 41 | 54 | def respond(value): |
| 42 | 55 | data = json.dumps(value).encode() |
| 43 | 56 | sys.stdout.buffer.write(struct.pack("!I", len(data)) + data) |
| 44 | 57 | sys.stdout.buffer.flush() |
| 45 | 58 | |
| 46 | 59 | |
| 47 | def computer(request): | |
| 60 | def computer(name, request): | |
| 48 | 61 | with tempfile.TemporaryFile() as source, selectors.DefaultSelector() as selector: |
| 49 | 62 | source.write(json.dumps(request).encode()) |
| 50 | 63 | source.seek(0) |
| 51 | 64 | # Recovery must work when the selected Xcode is unavailable. |
| 52 | process = subprocess.Popen([str(vm.TART), "exec", "-i", vm.NAME, "/Library/Developer/CommandLineTools/usr/bin/python3", | |
| 65 | process = subprocess.Popen([str(vm.TART), "exec", "-i", name, "/Library/Developer/CommandLineTools/usr/bin/python3", | |
| 53 | 66 | "/usr/local/libexec/snowglobe-computer.py"], stdin=source, stdout=subprocess.PIPE, |
| 54 | 67 | stderr=subprocess.PIPE, start_new_session=True, env={**os.environ, "TART_HOME": str(vm.ROOT / "state")}) |
| 55 | 68 | output = bytearray() |
| ... | ... | @@ -108,6 +121,18 @@ def lifecycle(action): |
| 108 | 121 | process.wait() |
| 109 | 122 | |
| 110 | 123 | |
| 124 | def locked_power(name, action): | |
| 125 | if action in {"stop", "reboot"}: | |
| 126 | vm.tart("stop", name, timeout=120) | |
| 127 | if action in {"start", "reboot"}: | |
| 128 | subprocess.run([str(LOCKED_START)], check=True, stdout=subprocess.DEVNULL, timeout=30) | |
| 129 | for _ in range(60): | |
| 130 | if json.loads(vm.tart("get", name, "--format", "json", capture_output=True, text=True, timeout=5).stdout)["Running"]: | |
| 131 | return | |
| 132 | time.sleep(1) | |
| 133 | raise ValueError("The VM did not start.") | |
| 134 | ||
| 135 | ||
| 111 | 136 | def main(): |
| 112 | 137 | line = sys.stdin.buffer.readline(1048577) |
| 113 | 138 | if len(line) > 1048576 or not line.endswith(b"\n"): |
| ... | ... | @@ -115,25 +140,36 @@ def main(): |
| 115 | 140 | request = json.loads(line) |
| 116 | 141 | if not isinstance(request, dict): |
| 117 | 142 | raise ValueError("Choose a supported VM operation.") |
| 143 | # Hosts that predate multiple VMs omit the name. | |
| 144 | requested = request.pop("vm", "m3-devbox") | |
| 145 | name = VMS.get(requested) if isinstance(requested, str) else None | |
| 146 | if name is None: | |
| 147 | raise ValueError("Choose a managed VM.") | |
| 148 | primary = name == vm.NAME | |
| 118 | 149 | action = request.get("action") |
| 119 | 150 | if action == "status" and set(request) == {"action"}: |
| 120 | respond({"value": status()}) | |
| 151 | respond({"value": {**(status() if primary else locked_status(name)), "name": name}}) | |
| 121 | 152 | elif action in {"start", "shutdown", "reboot", "suspend", "resume"} and set(request) == {"action"}: |
| 153 | if not primary and action in {"suspend", "resume"}: | |
| 154 | raise ValueError("This operation is unavailable for this Mac VM.") | |
| 122 | 155 | action = {"shutdown": "stop", "resume": "start"}.get(action, action) |
| 123 | lifecycle(action) | |
| 156 | if primary: | |
| 157 | lifecycle(action) | |
| 158 | else: | |
| 159 | locked_power(name, action) | |
| 124 | 160 | respond({"value": None}) |
| 125 | 161 | elif action == "clipboard" and set(request) == {"action", "text"}: |
| 126 | 162 | value = request["text"] |
| 127 | 163 | if not isinstance(value, str) or len(value.encode()) > 262144: |
| 128 | 164 | raise ValueError("Shorten the clipboard text, then send it again.") |
| 129 | vm.tart("exec", "-i", vm.NAME, "/usr/bin/env", "LC_CTYPE=en_US.UTF-8", "/usr/bin/pbcopy", | |
| 165 | vm.tart("exec", "-i", name, "/usr/bin/env", "LC_CTYPE=en_US.UTF-8", "/usr/bin/pbcopy", | |
| 130 | 166 | input=value.encode(), capture_output=True, timeout=15) |
| 131 | 167 | respond({"value": None}) |
| 132 | 168 | elif action == "computer" and set(request) == {"action", "request"}: |
| 133 | 169 | if not isinstance(request["request"], dict): |
| 134 | 170 | raise ValueError("Send a computer operation.") |
| 135 | respond({"value": computer(request["request"])}) | |
| 136 | elif action == "console" and set(request) == {"action"}: | |
| 171 | respond({"value": computer(name, request["request"])}) | |
| 172 | elif action == "console" and set(request) == {"action"} and primary: | |
| 137 | 173 | with vm.guest_screen() as upstream: |
| 138 | 174 | respond({"value": None}) |
| 139 | 175 | try: |
tools/mac-vm-test.py+76-5| ... | ... | @@ -19,11 +19,16 @@ bridge = import_module("mac-vm-bridge") |
| 19 | 19 | backend = import_module("mac-vms") |
| 20 | 20 | |
| 21 | 21 | |
| 22 | FIXTURE = {"vms": [{"name": "mac-fixture", "owner": "fixture-owner", "console": True, "suspend": True}, | |
| 23 | {"name": "locked-fixture", "owner": "fixture-owner", "console": False, "suspend": False}]} | |
| 24 | ||
| 25 | ||
| 22 | 26 | class MacVMTests(unittest.TestCase): |
| 23 | 27 | def test_protocol_rejects_arbitrary_commands_and_unbounded_clipboards(self): |
| 24 | 28 | for request in [{"action": "exec", "command": "id"}, {"action": "status", "name": "other"}, |
| 25 | 29 | {"action": "clipboard", "text": "a" * 262145}, ["status"], |
| 26 | {"action": "clipboard", "text": 42}]: | |
| 30 | {"action": "clipboard", "text": 42}, {"action": "status", "vm": "other"}, | |
| 31 | {"action": "status", "vm": ["m3-devbox"]}]: | |
| 27 | 32 | with self.subTest(request=str(request)[:80]), \ |
| 28 | 33 | patch.object(sys, "stdin", io.TextIOWrapper(io.BytesIO(json.dumps(request).encode() + b"\n"))), \ |
| 29 | 34 | patch.object(bridge.vm, "tart") as tart, patch.object(bridge, "lifecycle") as lifecycle: |
| ... | ... | @@ -49,7 +54,7 @@ class MacVMTests(unittest.TestCase): |
| 49 | 54 | "pendingAction": None, "disk": {"capacity": 128 * 2**30, "allocation": 42 * 2**30}, |
| 50 | 55 | "mac": "00:11:22:33:44:55"}, None |
| 51 | 56 | with tempfile.TemporaryDirectory() as directory, \ |
| 52 | patch.object(backend, "CONFIG", {"name": "mac-fixture", "owner": "fixture-owner"}), \ | |
| 57 | patch.object(backend, "CONFIG", FIXTURE), \ | |
| 53 | 58 | patch.object(backend, "CACHE", Path(directory) / "cache.json"): |
| 54 | 59 | with patch.object(backend, "connect", side_effect=ValueError("offline")): |
| 55 | 60 | first = backend.domain()[0] |
| ... | ... | @@ -66,21 +71,87 @@ class MacVMTests(unittest.TestCase): |
| 66 | 71 | self.assertEqual(offline["name"], "mac-fixture") |
| 67 | 72 | self.assertEqual(offline["owner"], "fixture-owner") |
| 68 | 73 | self.assertTrue(offline["ownerOnly"]) |
| 69 | self.assertEqual(backend.handle("owner", {}), {"owner": "fixture-owner", "ownerOnly": True}) | |
| 74 | self.assertEqual(backend.handle("owner", {"name": "mac-fixture"}), {"owner": "fixture-owner", "ownerOnly": True}) | |
| 70 | 75 | self.assertEqual(offline["memory"], live["memory"]) |
| 71 | 76 | self.assertEqual(offline["state"], "unreachable") |
| 72 | 77 | self.assertIsNone(offline["agent"]) |
| 73 | 78 | self.assertIsNone(offline["pendingAction"]) |
| 74 | 79 | |
| 75 | 80 | def test_unsupported_operations_never_contact_mac(self): |
| 76 | with patch.object(backend, "connect") as connect: | |
| 81 | with patch.object(backend, "CONFIG", FIXTURE), patch.object(backend, "connect") as connect: | |
| 77 | 82 | for action in ["delete", "update", "preset", "media"]: |
| 78 | 83 | with self.subTest(action=action), self.assertRaises(ValueError): |
| 79 | 84 | backend.handle(action, {"name": "mac-fixture"}) |
| 80 | 85 | with self.assertRaises(ValueError): |
| 81 | backend.handle("act", {"action": "destroy"}) | |
| 86 | backend.handle("act", {"name": "mac-fixture", "action": "destroy"}) | |
| 82 | 87 | connect.assert_not_called() |
| 83 | 88 | |
| 89 | def test_each_vm_keeps_its_own_features_and_requests_name_it(self): | |
| 90 | @contextlib.contextmanager | |
| 91 | def connected(name, request, timeout=10): | |
| 92 | yield {"state": "running", "vcpus": 8, "memory": 2**34, "agent": None, "pendingAction": None, | |
| 93 | "disk": {"capacity": 2**36, "allocation": 2**35}, "mac": "00:11:22:33:44:55", "name": name}, None | |
| 94 | with tempfile.TemporaryDirectory() as directory, patch.object(backend, "CONFIG", FIXTURE), \ | |
| 95 | patch.object(backend, "CACHE", Path(directory) / "cache.json"): | |
| 96 | with patch.object(backend, "connect", connected): | |
| 97 | full, locked = backend.domain() | |
| 98 | self.assertEqual((full["name"], full["console"], "suspend" in full["capabilities"]), ("mac-fixture", True, True)) | |
| 99 | self.assertEqual((locked["name"], locked["console"], "suspend" in locked["capabilities"]), ("locked-fixture", False, False)) | |
| 100 | self.assertIn("computer", locked["capabilities"]) | |
| 101 | self.assertTrue(backend.owns("locked-fixture") and not backend.owns("other")) | |
| 102 | with patch.object(backend, "connect") as connect: | |
| 103 | for action, payload in [("console", {}), ("act", {"action": "suspend"}), ("act", {"action": "resume"})]: | |
| 104 | with self.subTest(operation=action, payload=payload), self.assertRaises(ValueError): | |
| 105 | backend.handle(action, {"name": "locked-fixture", **payload}) | |
| 106 | connect.assert_not_called() | |
| 107 | self.assertEqual(backend.handle("console", {"name": "mac-fixture"}), {"remote": True, "name": "mac-fixture"}) | |
| 108 | with patch.object(backend, "connect", connected), patch.object(backend, "CACHE", Path(directory) / "cache.json"): | |
| 109 | self.assertEqual(backend.handle("computer", {"name": "locked-fixture", "request": {"action": "status"}})["name"], "locked-fixture") | |
| 110 | with patch.object(backend, "connect", side_effect=ValueError("offline")) as offline: | |
| 111 | self.assertEqual([value["state"] for value in backend.domain()], ["unreachable", "unreachable"]) | |
| 112 | self.assertEqual([value["memory"] for value in backend.domain()], [2**34, 2**34]) | |
| 113 | self.assertEqual({call.args[0] for call in offline.call_args_list}, {"mac-fixture", "locked-fixture"}) | |
| 114 | ||
| 115 | def test_bridge_serves_only_named_vms_and_hosts_without_a_name_get_the_devbox(self): | |
| 116 | def run(request): | |
| 117 | with patch.object(sys, "stdin", io.TextIOWrapper(io.BytesIO(json.dumps(request).encode() + b"\n"))), \ | |
| 118 | patch.object(bridge.vm, "tart") as tart, patch.object(bridge, "respond") as respond, \ | |
| 119 | patch.object(bridge, "status", return_value={"state": "devbox"}) as status, \ | |
| 120 | patch.object(bridge, "locked_status", return_value={"state": "locked"}) as locked_status, \ | |
| 121 | patch.object(bridge, "lifecycle") as lifecycle, patch.object(bridge, "locked_power") as locked_power: | |
| 122 | bridge.main() | |
| 123 | return tart, respond, status, locked_status, lifecycle, locked_power | |
| 124 | _, respond, status, locked_status, *_ = run({"action": "status"}) | |
| 125 | respond.assert_called_once_with({"value": {"state": "devbox", "name": "gui-agent"}}) | |
| 126 | _, respond, status, locked_status, *_ = run({"action": "status", "vm": "m3-phoenix-box"}) | |
| 127 | respond.assert_called_once_with({"value": {"state": "locked", "name": "m3-phoenix-box"}}) | |
| 128 | locked_status.assert_called_once_with("m3-phoenix-box") | |
| 129 | status.assert_not_called() | |
| 130 | tart, *_ = run({"action": "clipboard", "text": "x", "vm": "m3-phoenix-box"}) | |
| 131 | self.assertEqual(tart.call_args.args[:3], ("exec", "-i", "m3-phoenix-box")) | |
| 132 | *_, lifecycle, locked_power = run({"action": "reboot", "vm": "m3-phoenix-box"}) | |
| 133 | locked_power.assert_called_once_with("m3-phoenix-box", "reboot") | |
| 134 | lifecycle.assert_not_called() | |
| 135 | *_, lifecycle, locked_power = run({"action": "shutdown", "vm": "m3-devbox"}) | |
| 136 | lifecycle.assert_called_once_with("stop") | |
| 137 | locked_power.assert_not_called() | |
| 138 | for request in [{"action": "suspend", "vm": "m3-phoenix-box"}, {"action": "resume", "vm": "m3-phoenix-box"}, | |
| 139 | {"action": "console", "vm": "m3-phoenix-box"}]: | |
| 140 | with self.subTest(request=request), self.assertRaises(ValueError): | |
| 141 | run(request) | |
| 142 | ||
| 143 | def test_locked_vm_start_waits_until_tart_reports_it_running(self): | |
| 144 | states = iter([False, False, True]) | |
| 145 | def tart(*args, **kwargs): | |
| 146 | if args[0] == "get": | |
| 147 | return subprocess.CompletedProcess(args, 0, stdout=json.dumps({"Running": next(states)})) | |
| 148 | with patch.object(bridge.vm, "tart", side_effect=tart) as run, patch.object(bridge.subprocess, "run") as launch, \ | |
| 149 | patch.object(bridge.time, "sleep"): | |
| 150 | bridge.locked_power("m3-phoenix-box", "reboot") | |
| 151 | self.assertEqual(run.call_args_list[0].args, ("stop", "m3-phoenix-box")) | |
| 152 | launch.assert_called_once() | |
| 153 | self.assertEqual(sum(call.args[0] == "get" for call in run.call_args_list), 3) | |
| 154 | ||
| 84 | 155 | def test_lifecycle_timeout_reaps_commands_but_preserves_detached_runner(self): |
| 85 | 156 | with tempfile.TemporaryDirectory() as directory: |
| 86 | 157 | root = Path(directory) |
tools/mac-vms.py+46-22| ... | ... | @@ -1,3 +1,4 @@ |
| 1 | import concurrent.futures | |
| 1 | 2 | import contextlib |
| 2 | 3 | import json |
| 3 | 4 | import os |
| ... | ... | @@ -9,12 +10,19 @@ import tempfile |
| 9 | 10 | |
| 10 | 11 | |
| 11 | 12 | CONFIG = json.loads(os.environ.get("SNOWGLOBE_MAC_VM", "null")) |
| 12 | CAPABILITIES = ["start", "shutdown", "reboot", "suspend", "resume", "clipboard", "computer"] | |
| 13 | 13 | CACHE = Path(os.environ.get("SNOWGLOBE_MAC_VM_CACHE", "/var/lib/snowglobe/host/mac-vm.json")) |
| 14 | 14 | |
| 15 | 15 | |
| 16 | def vm(name): | |
| 17 | return next((entry for entry in CONFIG["vms"] if entry["name"] == name), None) if CONFIG else None | |
| 18 | ||
| 19 | ||
| 20 | def owns(name): | |
| 21 | return vm(name) is not None | |
| 22 | ||
| 23 | ||
| 16 | 24 | @contextlib.contextmanager |
| 17 | def connect(request, timeout=10): | |
| 25 | def connect(name, request, timeout=10): | |
| 18 | 26 | if not CONFIG: |
| 19 | 27 | raise ValueError("The Mac VM is not configured on this host.") |
| 20 | 28 | parent, child = socket.socketpair() |
| ... | ... | @@ -30,7 +38,7 @@ def connect(request, timeout=10): |
| 30 | 38 | ], stdin=child, stdout=child, stderr=subprocess.DEVNULL) |
| 31 | 39 | child.close() |
| 32 | 40 | parent.settimeout(timeout) |
| 33 | parent.sendall(json.dumps(request).encode() + b"\n") | |
| 41 | parent.sendall(json.dumps({**request, "vm": name}).encode() + b"\n") | |
| 34 | 42 | |
| 35 | 43 | def read(size): |
| 36 | 44 | data = bytearray() |
| ... | ... | @@ -68,56 +76,72 @@ def connect(request, timeout=10): |
| 68 | 76 | process.wait() |
| 69 | 77 | |
| 70 | 78 | |
| 71 | def ownership(): | |
| 72 | return {"owner": CONFIG["owner"], "ownerOnly": True} | |
| 79 | def ownership(entry): | |
| 80 | return {"owner": entry["owner"], "ownerOnly": True} | |
| 73 | 81 | |
| 74 | 82 | |
| 75 | def domain(): | |
| 76 | if not CONFIG: | |
| 77 | return [] | |
| 78 | value = {"name": CONFIG["name"], **ownership(), "host": "Mac Studio", "arch": "aarch64", | |
| 83 | def power(entry): | |
| 84 | return ["start", "shutdown", "reboot"] + (["suspend", "resume"] if entry["suspend"] else []) | |
| 85 | ||
| 86 | ||
| 87 | def observe(entry): | |
| 88 | value = {"name": entry["name"], **ownership(entry), "host": "Mac Studio", "arch": "aarch64", | |
| 79 | 89 | "state": "unreachable", "reason": None, |
| 80 | 90 | "os": "macOS", "vcpus": None, "memory": None, "balloon": None, "pinned": None, |
| 81 | 91 | "autostart": False, "startedAt": None, "agent": None, "setup": None, "pendingAction": None, |
| 82 | "disks": [], "media": [], "firmware": "uefi", "platform": "macos", "console": True, | |
| 83 | "serial": False, "interfaces": [], "hostdevs": [], "capabilities": CAPABILITIES} | |
| 92 | "disks": [], "media": [], "firmware": "uefi", "platform": "macos", "console": entry["console"], | |
| 93 | "serial": False, "interfaces": [], "hostdevs": [], "capabilities": power(entry) + ["clipboard", "computer"]} | |
| 84 | 94 | try: |
| 85 | with connect({"action": "status"}, timeout=8) as (status, _): | |
| 95 | with connect(entry["name"], {"action": "status"}, timeout=8) as (status, _): | |
| 86 | 96 | value.update({key: status[key] for key in ("state", "vcpus", "memory", "agent", "pendingAction")}) |
| 87 | 97 | value["balloon"] = value["memory"] |
| 88 | value["disks"] = [{"target": "disk", "pool": "Mac Studio", "source": "gui-agent", **status["disk"]}] | |
| 98 | value["disks"] = [{"target": "disk", "pool": "Mac Studio", "source": status.get("name", entry["name"]), **status["disk"]}] | |
| 89 | 99 | value["interfaces"] = [{"source": "Mac Studio NAT", "mac": status["mac"], "addresses": []}] |
| 90 | 100 | except (ValueError, OSError, KeyError, TypeError): |
| 91 | 101 | try: |
| 92 | previous = json.loads(CACHE.read_text()) | |
| 102 | previous = json.loads(CACHE.read_text())[entry["name"]] | |
| 93 | 103 | for key in ("vcpus", "memory", "balloon", "disks", "interfaces"): |
| 94 | 104 | value[key] = previous[key] |
| 95 | 105 | except (ValueError, OSError, KeyError, TypeError): |
| 96 | 106 | pass |
| 97 | 107 | value.update(state="unreachable", agent=None, pendingAction=None) |
| 98 | else: | |
| 108 | return value, value["state"] != "unreachable" | |
| 109 | ||
| 110 | ||
| 111 | def domain(): | |
| 112 | if not CONFIG: | |
| 113 | return [] | |
| 114 | with concurrent.futures.ThreadPoolExecutor(len(CONFIG["vms"])) as pool: | |
| 115 | observed = list(pool.map(observe, CONFIG["vms"])) | |
| 116 | live = {value["name"]: value for value, reachable in observed if reachable} | |
| 117 | if live: | |
| 99 | 118 | try: |
| 100 | 119 | CACHE.parent.mkdir(mode=0o700, parents=True, exist_ok=True) |
| 120 | try: | |
| 121 | live = {**json.loads(CACHE.read_text()), **live} | |
| 122 | except (ValueError, OSError): | |
| 123 | pass | |
| 101 | 124 | descriptor, temporary = tempfile.mkstemp(dir=CACHE.parent, prefix=".mac-vm-") |
| 102 | 125 | try: |
| 103 | 126 | with os.fdopen(descriptor, "w") as outgoing: |
| 104 | json.dump(value, outgoing) | |
| 127 | json.dump(live, outgoing) | |
| 105 | 128 | os.replace(temporary, CACHE) |
| 106 | 129 | finally: |
| 107 | 130 | Path(temporary).unlink(missing_ok=True) |
| 108 | 131 | except OSError: |
| 109 | 132 | pass |
| 110 | return [value] | |
| 133 | return [value for value, _ in observed] | |
| 111 | 134 | |
| 112 | 135 | |
| 113 | 136 | def handle(action, payload): |
| 137 | entry = vm(payload["name"]) | |
| 114 | 138 | if action == "owner": |
| 115 | return ownership() | |
| 139 | return ownership(entry) | |
| 116 | 140 | if action == "access": |
| 117 | 141 | return None |
| 118 | if action == "console": | |
| 119 | return {"remote": True} | |
| 120 | if action == "act" and payload["action"] in {"start", "shutdown", "reboot", "suspend", "resume"}: | |
| 142 | if action == "console" and entry["console"]: | |
| 143 | return {"remote": True, "name": entry["name"]} | |
| 144 | if action == "act" and payload["action"] in power(entry): | |
| 121 | 145 | request = {"action": payload["action"]} |
| 122 | 146 | elif action == "clipboard": |
| 123 | 147 | request = {"action": "clipboard", "text": payload["text"]} |
| ... | ... | @@ -125,5 +149,5 @@ def handle(action, payload): |
| 125 | 149 | request = {"action": "computer", "request": payload["request"]} |
| 126 | 150 | else: |
| 127 | 151 | raise ValueError("This operation is unavailable for this Mac VM.") |
| 128 | with connect(request, timeout=220) as (value, _): | |
| 152 | with connect(entry["name"], request, timeout=220) as (value, _): | |
| 129 | 153 | return value |
tools/phoenix-box.md created+30| ... | ... | @@ -0,0 +1,30 @@ |
| 1 | # Phoenix box | |
| 2 | ||
| 3 | `m3-phoenix-box` is a macOS 15 Tart VM beside `gui-agent`, in the same VM home under | |
| 4 | `/Volumes/Documents/VM/macOS Code Sandbox`. It holds Discord Phoenix and runs it against | |
| 5 | Spacebar, so its network is locked from outside the guest. | |
| 6 | ||
| 7 | Start it with `runtime/control/start-m3-phoenix-box` in that VM home. The script owns the | |
| 8 | flags: headless, `--vnc-experimental`, and Softnet with every destination blocked except | |
| 9 | `100.64.0.1/32`, Snow Globe's tailnet address. This Mac's NAT only leaves through Tailscale | |
| 10 | (its own routes do not reach the internet), and Snow Globe's Caddy answers on that address. | |
| 11 | Softnet filters by address, not port, so the VM can reach every port Snow Globe accepts | |
| 12 | on the tailnet. | |
| 13 | ||
| 14 | - **Names:** the guest has no DNS (its gateway is blocked), so `/etc/hosts` maps | |
| 15 | `spacebar.paperclover.net` to `100.64.0.1`. Go ignores `/etc/hosts` on macOS unless | |
| 16 | `GODEBUG=netdns=go`, which `run-phoenix-spacebar` sets for the Caddy relay. | |
| 17 | - **Access:** Softnet also blocks host to guest, so there is no SSH or Screen Sharing. | |
| 18 | Tart's own VNC server (random password per run, printed to `run.log` in | |
| 19 | `private/m3-phoenix-box`, listening on every interface of this Mac) and `tart exec` are | |
| 20 | the ways in. Console login is `admin` / `admin`; SSH accepts only the key in that | |
| 21 | private directory. | |
| 22 | - **Softnet:** 0.23.0 at `/usr/local/bin/softnet`, owned by root with the setuid bit. | |
| 23 | Releases from 0.24 need macOS 26. | |
| 24 | - **Dashboard:** `nixos/mac-vm.nix` lists both Mac VMs. The Mac-side bridge | |
| 25 | (`runtime/control/mac-vm-bridge.py`) maps each dashboard name to a Tart VM, so Computer | |
| 26 | MCP connections can be granted `m3-phoenix-box` like any VM. It has no dashboard | |
| 27 | console or suspend; power operations run `start-m3-phoenix-box` and `tart stop`. | |
| 28 | ||
| 29 | To manage another Mac VM, add it to `VMS` in the bridge and to `nixos/mac-vm.nix`, then run | |
| 30 | `python3 tools/install-mac-computer.py <tart name>` to install the guest worker. |
tools/vm-computer.py+2-2| ... | ... | @@ -536,7 +536,7 @@ def handle(name, request, *, active_snapshot=None): |
| 536 | 536 | validate(request) |
| 537 | 537 | vms.ensure_name(name) |
| 538 | 538 | if request['action'] == 'process' and request['id'].startswith('operation:'): |
| 539 | if vms.mac_vms.CONFIG and name == vms.mac_vms.CONFIG['name']: | |
| 539 | if vms.mac_vms.owns(name): | |
| 540 | 540 | raise ValueError('This VM does not provide active operation handles.') |
| 541 | 541 | identity = request['id'] |
| 542 | 542 | with vms.computer_control(name) as control: |
| ... | ... | @@ -568,7 +568,7 @@ def handle(name, request, *, active_snapshot=None): |
| 568 | 568 | return {'running': False} |
| 569 | 569 | finish_operation(name, unfinished) |
| 570 | 570 | return {'running': False, 'cancelled': True, 'id': identity} |
| 571 | if vms.mac_vms.CONFIG and name == vms.mac_vms.CONFIG['name']: | |
| 571 | if vms.mac_vms.owns(name): | |
| 572 | 572 | return vms.mac_vms.handle('computer', {'name':name,'request':request}) |
| 573 | 573 | token = DEADLINE.set(time.monotonic() + 210) |
| 574 | 574 | operation_token = None |
tools/vms.py+2-2| ... | ... | @@ -203,7 +203,7 @@ def validate(action, payload): |
| 203 | 203 | for field, minimum, maximum in [("vcpus", 1, host["cpus"]), ("memory", 2**29, host["memory"]), ("disk", 2**30, 2**63 - 1)]: |
| 204 | 204 | if type(payload[field]) is not int or not minimum <= payload[field] <= maximum: |
| 205 | 205 | raise ValueError(f"Choose {field} within the host's supported range.") |
| 206 | if action == "create" and mac_vms.CONFIG and payload["name"] == mac_vms.CONFIG["name"]: | |
| 206 | if action == "create" and mac_vms.owns(payload["name"]): | |
| 207 | 207 | raise ValueError("A VM already has this name. Choose another name.") |
| 208 | 208 | if DISKS.is_symlink() or (DISKS / payload["name"]).is_symlink(): |
| 209 | 209 | raise ValueError("The VM disk directory is a symbolic link. Remove the link before continuing.") |
| ... | ... | @@ -1106,7 +1106,7 @@ def main(): |
| 1106 | 1106 | return result |
| 1107 | 1107 | if action == "library": |
| 1108 | 1108 | return library() |
| 1109 | if mac_vms.CONFIG and isinstance(payload, dict) and payload.get("name") == mac_vms.CONFIG["name"]: | |
| 1109 | if isinstance(payload, dict) and mac_vms.owns(payload.get("name")): | |
| 1110 | 1110 | return mac_vms.handle(action, payload) |
| 1111 | 1111 | if action == "console": |
| 1112 | 1112 | return console_target(payload["name"]) |