From bd135e7d420ebca93413f25f49ed0b0256791a79 Mon Sep 17 00:00:00 2001 From: clover caruso Date: Tue, 17 Nov 2026 01:30:06 -0800 Subject: [PATCH] Build the Windows computer worker on the host Ship one compiled worker and install it atomically in guests, replacing repeated PowerShell compilation. Keep existing worker paths pinned for cleanup of active operations. Assisted-by: gpt-6.1-sol --- guest/computer-windows.cs | 10 ++++++- nixos/computer-windows.nix | 19 ++++++++++++++ nixos/configuration.nix | 4 ++- tools/computer-test.py | 34 ++++++++++++++++++++++-- tools/vm-computer.py | 54 ++++++++++++++++---------------------- 5 files changed, 85 insertions(+), 36 deletions(-) create mode 100644 nixos/computer-windows.nix diff --git a/guest/computer-windows.cs b/guest/computer-windows.cs index 0aa0dc5c79c8446193ec2887f6851e2f8515904d..11f52bf280fea71b2d10727b2932fdea1b277b78 100644 --- a/guest/computer-windows.cs +++ b/guest/computer-windows.cs @@ -123,9 +123,17 @@ public static class ComputerWorker { [DllImport("user32.dll")] static extern bool SetProcessDPIAware(); [DllImport("advapi32.dll",SetLastError=true)] static extern bool GetTokenInformation(IntPtr token,int informationClass,out int elevation,int length,out int returned); [STAThread] public static int Main(string[] args) { - if(args.Length==2 && (args[0]=="--prepare" || args[0]=="--cleanup" || args[0]=="--release")) { + if(args.Length==2 && (args[0]=="--prepare" || args[0]=="--cleanup" || args[0]=="--release" || args[0]=="--install")) { try { if(args[0]=="--prepare")System.IO.Directory.CreateDirectory(args[1]); + else if(args[0]=="--install") { + System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(args[1])); + string temporary=args[1]+"."+Guid.NewGuid().ToString("N")+".new"; + try { + System.IO.File.Copy(System.Reflection.Assembly.GetExecutingAssembly().Location,temporary,true); + if(!System.IO.File.Exists(args[1]))System.IO.File.Move(temporary,args[1]); + } finally {System.IO.File.Delete(temporary);} + } else Cleanup(args[1],args[0]=="--release"); return 0; } diff --git a/nixos/computer-windows.nix b/nixos/computer-windows.nix new file mode 100644 index 0000000000000000000000000000000000000000..cac9614bde03fcaf2351101d31d706c7f7fa223d --- /dev/null +++ b/nixos/computer-windows.nix @@ -0,0 +1,19 @@ +{ runCommand, fetchurl, mono, unzip }: +let + references = fetchurl { + url = "https://api.nuget.org/v3-flatcontainer/microsoft.netframework.referenceassemblies.net48/1.0.3/microsoft.netframework.referenceassemblies.net48.1.0.3.nupkg"; + hash = "sha256-in40hTjn65E1FpaRFon0nj1PY/i6tRdDK74Vm4sRBKI="; + }; +in runCommand "computer-windows" { + nativeBuildInputs = [ mono unzip ]; +} '' + unzip -q ${references} -d references + ref="$PWD/references/build/.NETFramework/v4.8" + mkdir -p "$out/bin" + mcs -noconfig -nostdlib -deterministic -optimize+ -target:winexe -platform:anycpu \ + -r:"$ref/mscorlib.dll" -r:"$ref/System.dll" -r:"$ref/System.Core.dll" \ + -r:"$ref/System.Management.dll" -r:"$ref/Microsoft.CSharp.dll" \ + -r:"$ref/System.Web.Extensions.dll" -r:"$ref/WindowsBase.dll" \ + -r:"$ref/UIAutomationClient.dll" -r:"$ref/UIAutomationTypes.dll" \ + -out:"$out/bin/worker.exe" ${../guest/computer-windows.cs} +'' diff --git a/nixos/configuration.nix b/nixos/configuration.nix index 2045d44dcc3a31aeeb29c2d9a12ca8d5406abb90..04491685e48de19fc0b03d6ea855664b57d89eab 100644 --- a/nixos/configuration.nix +++ b/nixos/configuration.nix @@ -6,9 +6,10 @@ let proxyToken = "/var/lib/snowglobe/dashboard-proxy.token"; hostTools = lib.fileset.toSource { root = ../.; - fileset = lib.fileset.unions [ ../tools/dashboard-host.py ../service/copyparty/manage.py ../service/copyparty/copyparty.conf ../tools/shale-labels.py ../tools/shale-access.py ../config/shale.json ../config/site.pkl ../dashboard/src/host-protocol.json ../tools/vms.py ../tools/vm-computer.py ../tools/vm-ai.py ../tools/vm-ai-limits.json ../tools/vm-rfb.py ../tools/mac-computer.py ../guest/computer-windows.cs ../guest/computer-legacy-mac.py ../guest/computer-linux.py ../guest/computer-linux-launch ../guest/computer-linux.nix ../tools/mac-vms.py ../tools/vm-ssh.py ../tools/dashboard-run.py ../tools/release.py ../guest/protocol.json ]; + fileset = lib.fileset.unions [ ../tools/dashboard-host.py ../service/copyparty/manage.py ../service/copyparty/copyparty.conf ../tools/shale-labels.py ../tools/shale-access.py ../config/shale.json ../config/site.pkl ../dashboard/src/host-protocol.json ../tools/vms.py ../tools/vm-computer.py ../tools/vm-ai.py ../tools/vm-ai-limits.json ../tools/vm-rfb.py ../tools/mac-computer.py ../guest/computer-legacy-mac.py ../guest/computer-linux.py ../guest/computer-linux-launch ../guest/computer-linux.nix ../tools/mac-vms.py ../tools/vm-ssh.py ../tools/dashboard-run.py ../tools/release.py ../guest/protocol.json ]; }; computerAhk = pkgs.callPackage ./computer-ahk.nix { }; + computerWindows = pkgs.callPackage ./computer-windows.nix { }; nativePkl = pkgs.callPackage ./pkl.nix { }; hostPython = pkgs.callPackage ./host-python.nix { }; screen = pkgs.callPackage ./vm-screen.nix { }; @@ -259,6 +260,7 @@ in SNOWGLOBE_POOL = lib.attrByPath [ "SNOWGLOBE_POOL" ] "snowglobe-demo" config.environment.variables; SNOWGLOBE_DASHBOARD_USER = "snowglobe-dashboard"; SNOWGLOBE_COMPUTER_AHK = toString computerAhk; + SNOWGLOBE_COMPUTER_WINDOWS_WORKER = "${computerWindows}/bin/worker.exe"; SNOWGLOBE_VM_ACCEL = lib.attrByPath [ "SNOWGLOBE_VM_ACCEL" ] "kvm" config.environment.variables; }; serviceConfig = { diff --git a/tools/computer-test.py b/tools/computer-test.py index 1100bc3ed49edca715f57371cd05666720042813..10e6f4e55574d32c2053d9c2d59eae8c789c3c3f 100644 --- a/tools/computer-test.py +++ b/tools/computer-test.py @@ -75,6 +75,11 @@ class GuestCancellation(unittest.TestCase): self.addCleanup(directory.cleanup) root = Path(directory.name) (root / 'fixture').mkdir() + artifact = root / 'worker.exe' + artifact.write_bytes(b'compiled worker fixture') + environment = patch.dict(computer.os.environ, {'SNOWGLOBE_COMPUTER_WINDOWS_WORKER': str(artifact)}) + environment.start() + self.addCleanup(environment.stop) patcher = patch.object(computer.vms, 'DISKS', root) patcher.start() self.addCleanup(patcher.stop) @@ -129,13 +134,38 @@ class GuestCancellation(unittest.TestCase): return b'' token = computer.DEADLINE.set(time.monotonic() + 30) try: - with patch.object(computer, 'powershell', return_value=b'[true,true]'), patch.object(computer, 'guest_exec', execute), patch.object(computer, 'write_file'), patch.object(computer, 'read_file', return_value=b'M'): + with patch.object(computer, 'guest_exec', execute), patch.object(computer, 'write_file'), patch.object(computer, 'read_file', return_value=b'M'): with self.assertRaises(TimeoutError): computer.worker('fixture', 'windows', {'action': 'input', 'actions': [{'type': 'text', 'text': 'hello'}]}) self.assertEqual(running, []) finally: computer.DEADLINE.reset(token) + def test_cancelled_cold_install_cleans_staging_without_launching_a_request(self): + calls = [] + with computer.vms.computer_lock('fixture', 'status') as active: + operation = computer.OPERATION.set(('fixture', active['id'])) + deadline = computer.DEADLINE.set(time.monotonic() + 30) + def execute(name, executable, arguments, **kwargs): + computer.remaining(1) + calls.append(arguments) + if arguments[0] == '--install': + with computer.vms.computer_control(name) as control: + computer.vms.computer_cancel(control, active['id']) + computer.DEADLINE.set(time.monotonic() - 1) + return b'' + try: + with patch.object(computer, 'read_file', side_effect=ValueError('Missing cache')), \ + patch.object(computer, 'write_file'), patch.object(computer, 'guest_exec', execute), \ + self.assertRaises(InterruptedError): + computer.worker('fixture', 'windows', {'action': 'status'}) + self.assertTrue(any('rmdir' in arguments for arguments in calls)) + self.assertFalse(any(arguments[0] == '--launch' for arguments in calls)) + self.assertFalse((computer.vms.DISKS / 'fixture/computer-operation.json').exists()) + finally: + computer.OPERATION.reset(operation) + computer.DEADLINE.reset(deadline) + def test_console_remains_available_without_guest_agent(self): for platform in ['windows', 'linux']: def virsh(action, *args, **kwargs): @@ -371,7 +401,7 @@ class GuestCancellation(unittest.TestCase): if arguments[0] == '--release': raise TimeoutError() return b'' - with patch.object(computer, 'powershell', return_value=b'[true,true]'), patch.object(computer, 'guest_exec', execute), patch.object(computer, 'write_file'), patch.object(computer, 'read_file', return_value=json.dumps(result).encode()): + with patch.object(computer, 'guest_exec', execute), patch.object(computer, 'write_file'), patch.object(computer, 'read_file', return_value=json.dumps(result).encode()): self.assertEqual(computer.worker('fixture', 'windows', {'action': 'spawn'}), result) pending = computer.vms.DISKS / 'fixture' / 'computer-operation.json' operation = json.loads(pending.read_text()) diff --git a/tools/vm-computer.py b/tools/vm-computer.py index efce7c1f290c99e005c203fa820603bb4c3e9bc1..75cc5ed01fd055d0e98bfe191796e2bd77b9873b 100644 --- a/tools/vm-computer.py +++ b/tools/vm-computer.py @@ -445,49 +445,39 @@ def worker(name, platform, request): identity = OPERATION.get() run = identity[1].removeprefix('operation:') if identity else uuid.uuid4().hex directory = 'C:\\ProgramData\\SnowglobeComputer\\' + run - source = Path(__file__).resolve().parent.parent / 'guest/computer-windows.cs' - content = source.read_bytes() + artifact = os.environ.get('SNOWGLOBE_COMPUTER_WINDOWS_WORKER') + if not artifact: + raise ValueError('The Windows computer worker is unavailable on the host.') + content = Path(artifact).read_bytes() digest = hashlib.sha256(content).hexdigest()[:16] installed_worker = 'C:\\ProgramData\\SnowglobeComputer\\worker-' + digest + '.exe' try: read_file(name, installed_worker, length=1) except ValueError: - setup = "$d='" + directory + "'; New-Item -ItemType Directory -Force -Path $d | Out-Null; " - setup += "@((Test-Path C:\\ProgramData\\SnowglobeComputer\\AutoHotkey.exe),(Test-Path -LiteralPath '" + installed_worker + "')) | ConvertTo-Json -Compress" - installed, exists = json.loads(powershell(name,setup)) - else: - exists = True - guest_exec(name, installed_worker, ['--prepare', directory]) + staging = 'C:\\Windows\\Temp\\snowglobe-worker-' + uuid.uuid4().hex try: - read_file(name, 'C:\\ProgramData\\SnowglobeComputer\\AutoHotkey.exe', length=1) - installed = True - except ValueError: - installed = False + guest_exec(name, 'cmd.exe', ['/d', '/c', 'mkdir', staging]) + write_file(name, staging + '\\worker.exe', content) + guest_exec(name, staging + '\\worker.exe', ['--install', installed_worker]) + finally: + cleanup_deadline = DEADLINE.set(time.monotonic() + 15) + cleanup_operation = OPERATION.set(None) + try: + guest_exec(name, 'cmd.exe', ['/d', '/c', 'rmdir', '/s', '/q', staging, '&', 'exit', '/b', '0']) + finally: + OPERATION.reset(cleanup_operation) + DEADLINE.reset(cleanup_deadline) + guest_exec(name, installed_worker, ['--prepare', directory]) + try: + read_file(name, 'C:\\ProgramData\\SnowglobeComputer\\AutoHotkey.exe', length=1) + installed = True + except ValueError: + installed = False ahk=os.environ.get('SNOWGLOBE_COMPUTER_AHK') if not installed and ahk: write_file(name,'C:\\ProgramData\\SnowglobeComputer\\AutoHotkey.exe',(Path(ahk)/'AutoHotkey32.exe').read_bytes()) write_file(name,'C:\\ProgramData\\SnowglobeComputer\\AutoHotkey-license.txt',(Path(ahk)/'license.txt').read_bytes()) write_file(name,directory+'\\request.json',json.dumps(request).encode()) - if not exists: - source_path = directory + '\\worker.cs' - write_file(name, source_path, content) - compile_script = r""" -$ErrorActionPreference='Stop' -$lock=New-Object Threading.Mutex($false,'Global\SnowglobeComputerCompile-HASH') -if(!$lock.WaitOne(0)){$lock.Dispose();Write-Output 'pending';return} -try { -if(Test-Path -LiteralPath 'WORKER'){return} -$references=@('System.dll','System.Core.dll') -foreach($assembly in @('System.Web.Extensions','System.Management','Microsoft.CSharp','UIAutomationClient','UIAutomationTypes','WindowsBase')) { - $references += [Reflection.Assembly]::LoadWithPartialName($assembly).Location -} -Remove-Item -LiteralPath 'WORKER.new.exe' -Force -ErrorAction SilentlyContinue -Add-Type -TypeDefinition ([IO.File]::ReadAllText('SOURCE')) -ReferencedAssemblies $references -OutputAssembly 'WORKER.new.exe' -OutputType WindowsApplication -Move-Item -LiteralPath 'WORKER.new.exe' -Destination 'WORKER' -} finally {$lock.ReleaseMutex();$lock.Dispose()} -""".replace('SOURCE', source_path).replace('WORKER', installed_worker).replace('HASH',digest) - if powershell(name, compile_script, timeout=60).strip() == b'pending': - raise ValueError('Computer tools are still installing. Retry in a moment.') operation = {'platform': platform, 'id': run, 'worker': installed_worker} record_operation(name, operation) try: -- 2.54.1