authoravatarclover caruso<git@paperclover.net>2026-11-17 01:30:06-08:00
committeravatarclover caruso<git@paperclover.net>2026-11-22 00:44:02-08:00
logbd135e7d420ebca93413f25f49ed0b0256791a79
tree2f796cc0df4d8c7f286a6690c9676da571818ad3
parent38232d1d522b2c555b7ccf5fd3d84499829fb64e
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

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

5 files changed, 85 insertions(+), 36 deletions(-)

guest/computer-windows.cs+9-1
......@@ -123,9 +123,17 @@ public static class ComputerWorker {
123123 [DllImport("user32.dll")] static extern bool SetProcessDPIAware();
124124 [DllImport("advapi32.dll",SetLastError=true)] static extern bool GetTokenInformation(IntPtr token,int informationClass,out int elevation,int length,out int returned);
125125 [STAThread] public static int Main(string[] args) {
126 if(args.Length==2 && (args[0]=="--prepare" || args[0]=="--cleanup" || args[0]=="--release")) {
126 if(args.Length==2 && (args[0]=="--prepare" || args[0]=="--cleanup" || args[0]=="--release" || args[0]=="--install")) {
127127 try {
128128 if(args[0]=="--prepare")System.IO.Directory.CreateDirectory(args[1]);
129 else if(args[0]=="--install") {
130 System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(args[1]));
131 string temporary=args[1]+"."+Guid.NewGuid().ToString("N")+".new";
132 try {
133 System.IO.File.Copy(System.Reflection.Assembly.GetExecutingAssembly().Location,temporary,true);
134 if(!System.IO.File.Exists(args[1]))System.IO.File.Move(temporary,args[1]);
135 } finally {System.IO.File.Delete(temporary);}
136 }
129137 else Cleanup(args[1],args[0]=="--release");
130138 return 0;
131139 }
nixos/computer-windows.nix created+19
......@@ -0,0 +1,19 @@
1{ runCommand, fetchurl, mono, unzip }:
2let
3 references = fetchurl {
4 url = "https://api.nuget.org/v3-flatcontainer/microsoft.netframework.referenceassemblies.net48/1.0.3/microsoft.netframework.referenceassemblies.net48.1.0.3.nupkg";
5 hash = "sha256-in40hTjn65E1FpaRFon0nj1PY/i6tRdDK74Vm4sRBKI=";
6 };
7in runCommand "computer-windows" {
8 nativeBuildInputs = [ mono unzip ];
9} ''
10 unzip -q ${references} -d references
11 ref="$PWD/references/build/.NETFramework/v4.8"
12 mkdir -p "$out/bin"
13 mcs -noconfig -nostdlib -deterministic -optimize+ -target:winexe -platform:anycpu \
14 -r:"$ref/mscorlib.dll" -r:"$ref/System.dll" -r:"$ref/System.Core.dll" \
15 -r:"$ref/System.Management.dll" -r:"$ref/Microsoft.CSharp.dll" \
16 -r:"$ref/System.Web.Extensions.dll" -r:"$ref/WindowsBase.dll" \
17 -r:"$ref/UIAutomationClient.dll" -r:"$ref/UIAutomationTypes.dll" \
18 -out:"$out/bin/worker.exe" ${../guest/computer-windows.cs}
19''
nixos/configuration.nix+3-1
......@@ -6,9 +6,10 @@ let
66 proxyToken = "/var/lib/snowglobe/dashboard-proxy.token";
77 hostTools = lib.fileset.toSource {
88 root = ../.;
9 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 ];
9 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 ];
1010 };
1111 computerAhk = pkgs.callPackage ./computer-ahk.nix { };
12 computerWindows = pkgs.callPackage ./computer-windows.nix { };
1213 nativePkl = pkgs.callPackage ./pkl.nix { };
1314 hostPython = pkgs.callPackage ./host-python.nix { };
1415 screen = pkgs.callPackage ./vm-screen.nix { };
......@@ -259,6 +260,7 @@ in
259260 SNOWGLOBE_POOL = lib.attrByPath [ "SNOWGLOBE_POOL" ] "snowglobe-demo" config.environment.variables;
260261 SNOWGLOBE_DASHBOARD_USER = "snowglobe-dashboard";
261262 SNOWGLOBE_COMPUTER_AHK = toString computerAhk;
263 SNOWGLOBE_COMPUTER_WINDOWS_WORKER = "${computerWindows}/bin/worker.exe";
262264 SNOWGLOBE_VM_ACCEL = lib.attrByPath [ "SNOWGLOBE_VM_ACCEL" ] "kvm" config.environment.variables;
263265 };
264266 serviceConfig = {
tools/computer-test.py+32-2
......@@ -75,6 +75,11 @@ class GuestCancellation(unittest.TestCase):
7575 self.addCleanup(directory.cleanup)
7676 root = Path(directory.name)
7777 (root / 'fixture').mkdir()
78 artifact = root / 'worker.exe'
79 artifact.write_bytes(b'compiled worker fixture')
80 environment = patch.dict(computer.os.environ, {'SNOWGLOBE_COMPUTER_WINDOWS_WORKER': str(artifact)})
81 environment.start()
82 self.addCleanup(environment.stop)
7883 patcher = patch.object(computer.vms, 'DISKS', root)
7984 patcher.start()
8085 self.addCleanup(patcher.stop)
......@@ -129,13 +134,38 @@ class GuestCancellation(unittest.TestCase):
129134 return b''
130135 token = computer.DEADLINE.set(time.monotonic() + 30)
131136 try:
132 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'):
137 with patch.object(computer, 'guest_exec', execute), patch.object(computer, 'write_file'), patch.object(computer, 'read_file', return_value=b'M'):
133138 with self.assertRaises(TimeoutError):
134139 computer.worker('fixture', 'windows', {'action': 'input', 'actions': [{'type': 'text', 'text': 'hello'}]})
135140 self.assertEqual(running, [])
136141 finally:
137142 computer.DEADLINE.reset(token)
138143
144 def test_cancelled_cold_install_cleans_staging_without_launching_a_request(self):
145 calls = []
146 with computer.vms.computer_lock('fixture', 'status') as active:
147 operation = computer.OPERATION.set(('fixture', active['id']))
148 deadline = computer.DEADLINE.set(time.monotonic() + 30)
149 def execute(name, executable, arguments, **kwargs):
150 computer.remaining(1)
151 calls.append(arguments)
152 if arguments[0] == '--install':
153 with computer.vms.computer_control(name) as control:
154 computer.vms.computer_cancel(control, active['id'])
155 computer.DEADLINE.set(time.monotonic() - 1)
156 return b''
157 try:
158 with patch.object(computer, 'read_file', side_effect=ValueError('Missing cache')), \
159 patch.object(computer, 'write_file'), patch.object(computer, 'guest_exec', execute), \
160 self.assertRaises(InterruptedError):
161 computer.worker('fixture', 'windows', {'action': 'status'})
162 self.assertTrue(any('rmdir' in arguments for arguments in calls))
163 self.assertFalse(any(arguments[0] == '--launch' for arguments in calls))
164 self.assertFalse((computer.vms.DISKS / 'fixture/computer-operation.json').exists())
165 finally:
166 computer.OPERATION.reset(operation)
167 computer.DEADLINE.reset(deadline)
168
139169 def test_console_remains_available_without_guest_agent(self):
140170 for platform in ['windows', 'linux']:
141171 def virsh(action, *args, **kwargs):
......@@ -371,7 +401,7 @@ class GuestCancellation(unittest.TestCase):
371401 if arguments[0] == '--release':
372402 raise TimeoutError()
373403 return b''
374 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()):
404 with patch.object(computer, 'guest_exec', execute), patch.object(computer, 'write_file'), patch.object(computer, 'read_file', return_value=json.dumps(result).encode()):
375405 self.assertEqual(computer.worker('fixture', 'windows', {'action': 'spawn'}), result)
376406 pending = computer.vms.DISKS / 'fixture' / 'computer-operation.json'
377407 operation = json.loads(pending.read_text())
tools/vm-computer.py+22-32
......@@ -445,49 +445,39 @@ def worker(name, platform, request):
445445 identity = OPERATION.get()
446446 run = identity[1].removeprefix('operation:') if identity else uuid.uuid4().hex
447447 directory = 'C:\\ProgramData\\SnowglobeComputer\\' + run
448 source = Path(__file__).resolve().parent.parent / 'guest/computer-windows.cs'
449 content = source.read_bytes()
448 artifact = os.environ.get('SNOWGLOBE_COMPUTER_WINDOWS_WORKER')
449 if not artifact:
450 raise ValueError('The Windows computer worker is unavailable on the host.')
451 content = Path(artifact).read_bytes()
450452 digest = hashlib.sha256(content).hexdigest()[:16]
451453 installed_worker = 'C:\\ProgramData\\SnowglobeComputer\\worker-' + digest + '.exe'
452454 try:
453455 read_file(name, installed_worker, length=1)
454456 except ValueError:
455 setup = "$d='" + directory + "'; New-Item -ItemType Directory -Force -Path $d | Out-Null; "
456 setup += "@((Test-Path C:\\ProgramData\\SnowglobeComputer\\AutoHotkey.exe),(Test-Path -LiteralPath '" + installed_worker + "')) | ConvertTo-Json -Compress"
457 installed, exists = json.loads(powershell(name,setup))
458 else:
459 exists = True
460 guest_exec(name, installed_worker, ['--prepare', directory])
457 staging = 'C:\\Windows\\Temp\\snowglobe-worker-' + uuid.uuid4().hex
461458 try:
462 read_file(name, 'C:\\ProgramData\\SnowglobeComputer\\AutoHotkey.exe', length=1)
463 installed = True
464 except ValueError:
465 installed = False
459 guest_exec(name, 'cmd.exe', ['/d', '/c', 'mkdir', staging])
460 write_file(name, staging + '\\worker.exe', content)
461 guest_exec(name, staging + '\\worker.exe', ['--install', installed_worker])
462 finally:
463 cleanup_deadline = DEADLINE.set(time.monotonic() + 15)
464 cleanup_operation = OPERATION.set(None)
465 try:
466 guest_exec(name, 'cmd.exe', ['/d', '/c', 'rmdir', '/s', '/q', staging, '&', 'exit', '/b', '0'])
467 finally:
468 OPERATION.reset(cleanup_operation)
469 DEADLINE.reset(cleanup_deadline)
470 guest_exec(name, installed_worker, ['--prepare', directory])
471 try:
472 read_file(name, 'C:\\ProgramData\\SnowglobeComputer\\AutoHotkey.exe', length=1)
473 installed = True
474 except ValueError:
475 installed = False
466476 ahk=os.environ.get('SNOWGLOBE_COMPUTER_AHK')
467477 if not installed and ahk:
468478 write_file(name,'C:\\ProgramData\\SnowglobeComputer\\AutoHotkey.exe',(Path(ahk)/'AutoHotkey32.exe').read_bytes())
469479 write_file(name,'C:\\ProgramData\\SnowglobeComputer\\AutoHotkey-license.txt',(Path(ahk)/'license.txt').read_bytes())
470480 write_file(name,directory+'\\request.json',json.dumps(request).encode())
471 if not exists:
472 source_path = directory + '\\worker.cs'
473 write_file(name, source_path, content)
474 compile_script = r"""
475$ErrorActionPreference='Stop'
476$lock=New-Object Threading.Mutex($false,'Global\SnowglobeComputerCompile-HASH')
477if(!$lock.WaitOne(0)){$lock.Dispose();Write-Output 'pending';return}
478try {
479if(Test-Path -LiteralPath 'WORKER'){return}
480$references=@('System.dll','System.Core.dll')
481foreach($assembly in @('System.Web.Extensions','System.Management','Microsoft.CSharp','UIAutomationClient','UIAutomationTypes','WindowsBase')) {
482 $references += [Reflection.Assembly]::LoadWithPartialName($assembly).Location
483}
484Remove-Item -LiteralPath 'WORKER.new.exe' -Force -ErrorAction SilentlyContinue
485Add-Type -TypeDefinition ([IO.File]::ReadAllText('SOURCE')) -ReferencedAssemblies $references -OutputAssembly 'WORKER.new.exe' -OutputType WindowsApplication
486Move-Item -LiteralPath 'WORKER.new.exe' -Destination 'WORKER'
487} finally {$lock.ReleaseMutex();$lock.Dispose()}
488""".replace('SOURCE', source_path).replace('WORKER', installed_worker).replace('HASH',digest)
489 if powershell(name, compile_script, timeout=60).strip() == b'pending':
490 raise ValueError('Computer tools are still installing. Retry in a moment.')
491481 operation = {'platform': platform, 'id': run, 'worker': installed_worker}
492482 record_operation(name, operation)
493483 try: