authoravatarclover caruso<git@paperclover.net>2026-11-16 18:29:06-08:00
committeravatarclover caruso<git@paperclover.net>2026-11-22 00:44:02-08:00
log711e7f082e04608ad1e120898526ee9c9870483f
treee48bc6ae7472d4371e479e8642394b1ba24575cb
parent8f88e549f87fd6e7308b6d04d29c78942b355d41
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

Keep VM inspection available during blocked commands

Assisted-by: gpt-6.1-sol

3 files changed, 71 insertions(+), 13 deletions(-)

tools/computer-test.py+32-3
...@@ -146,7 +146,7 @@ class GuestCancellation(unittest.TestCase):...@@ -146,7 +146,7 @@ class GuestCancellation(unittest.TestCase):
146 desktop.assert_awaited_once_with('fixture', actions, 'windows', False, False)146 desktop.assert_awaited_once_with('fixture', actions, 'windows', False, False)
147 work.assert_not_called()147 work.assert_not_called()
148148
149 def test_failed_cleanup_blocks_later_control_until_reconnection(self):149 def test_failed_cleanup_keeps_status_available_and_blocks_later_control(self):
150 for platform, identity in [('linux', 'snowglobe-computer-request-' + 'a' * 32), ('windows', 'a' * 32)]:150 for platform, identity in [('linux', 'snowglobe-computer-request-' + 'a' * 32), ('windows', 'a' * 32)]:
151 operation = {'platform': platform, 'id': identity}151 operation = {'platform': platform, 'id': identity}
152 computer.record_operation('fixture', operation)152 computer.record_operation('fixture', operation)
...@@ -157,14 +157,43 @@ class GuestCancellation(unittest.TestCase):...@@ -157,14 +157,43 @@ class GuestCancellation(unittest.TestCase):
157 def virsh(action, *args, **kwargs):157 def virsh(action, *args, **kwargs):
158 return 'running' if action == 'domstate' else '<domain/>'158 return 'running' if action == 'domstate' else '<domain/>'
159 with patch.object(computer.vms, 'virsh', virsh), patch.object(computer, 'worker') as work:159 with patch.object(computer.vms, 'virsh', virsh), patch.object(computer, 'worker') as work:
160 with patch.object(computer, 'finish_operation', side_effect=AssertionError('Status must not wait for cleanup')):
161 status = computer.handle('fixture', {'action': 'status'})
162 self.assertEqual(status['readiness'], 'recovery_required')
163 self.assertEqual(status['active_operation'], operation)
160 with patch.object(computer, 'guest_exec', side_effect=TimeoutError()), patch.object(computer, 'powershell', side_effect=TimeoutError()), self.assertRaises(BlockingIOError):164 with patch.object(computer, 'guest_exec', side_effect=TimeoutError()), patch.object(computer, 'powershell', side_effect=TimeoutError()), self.assertRaises(BlockingIOError):
161 computer.handle('fixture', {'action': 'status'})165 computer.handle('fixture', {'action': 'exec', 'runtime': 'default', 'script': 'true', 'timeout_ms': 1000})
162 work.assert_not_called()166 work.assert_not_called()
163 with patch.object(computer, 'guest_exec', return_value=b'{}'), patch.object(computer, 'powershell', return_value=b'{}'):167 with patch.object(computer, 'guest_exec', return_value=b'{}'), patch.object(computer, 'powershell', return_value=b'{}'):
164 computer.handle('fixture', {'action': 'status'})168 computer.handle('fixture', {'action': 'exec', 'runtime': 'default', 'script': 'true', 'timeout_ms': 1000})
165 work.assert_called_once()169 work.assert_called_once()
166 self.assertFalse(pending.exists())170 self.assertFalse(pending.exists())
167171
172 def test_busy_execution_keeps_status_and_screenshots_available(self):
173 def virsh(action, *args, **kwargs):
174 return 'running' if action == 'domstate' else '<domain/>'
175 with computer.vms.computer_lock('fixture', 'exec'), patch.object(computer.vms, 'virsh', virsh), patch.object(computer, 'worker') as work:
176 status = computer.handle('fixture', {'action': 'status'})
177 self.assertEqual(status['readiness'], 'busy')
178 self.assertEqual(status['active_operation']['action'], 'exec')
179 self.assertGreaterEqual(status['active_operation']['age_seconds'], 0)
180 with patch.object(computer, 'desktop', new_callable=AsyncMock, return_value={'png': 'fixture'}) as desktop:
181 self.assertEqual(computer.handle('fixture', {'action': 'shot'}), {'png': 'fixture'})
182 desktop.assert_awaited_once_with('fixture')
183 with self.assertRaisesRegex(BlockingIOError, 'computer_status'):
184 computer.handle('fixture', {'action': 'exec', 'script': 'true', 'runtime': 'default', 'timeout_ms': 1000})
185 work.assert_not_called()
186
187 def test_stopped_vm_reports_status_without_starting_a_guest_worker(self):
188 def virsh(action, *args, **kwargs):
189 return 'shut off' if action == 'domstate' else '<domain/>'
190 with patch.object(computer.vms, 'virsh', virsh), patch.object(computer, 'worker') as work:
191 status = computer.handle('fixture', {'action': 'status'})
192 self.assertEqual(status['power_state'], 'shut off')
193 self.assertEqual(status['readiness'], 'stopped')
194 self.assertEqual(status['capabilities'], [])
195 work.assert_not_called()
196
168 def test_spawn_returns_its_handle_when_cleanup_must_wait_for_reconnection(self):197 def test_spawn_returns_its_handle_when_cleanup_must_wait_for_reconnection(self):
169 result = {'id': '123:456', 'running': True}198 result = {'id': '123:456', 'running': True}
170 def execute(name, script, **kwargs):199 def execute(name, script, **kwargs):
tools/vm-computer.py+34-9
...@@ -499,21 +499,46 @@ def handle(name, request):...@@ -499,21 +499,46 @@ def handle(name, request):
499 return vms.mac_vms.handle('computer', {'name':name,'request':request})499 return vms.mac_vms.handle('computer', {'name':name,'request':request})
500 token = DEADLINE.set(time.monotonic() + 210)500 token = DEADLINE.set(time.monotonic() + 210)
501 try:501 try:
502 with vms.computer_lock(name):502 with contextlib.ExitStack() as scope:
503 busy = False
504 try:
505 scope.enter_context(vms.computer_lock(name, request['action']))
506 except BlockingIOError:
507 if request['action'] not in ('status', 'shot'):
508 raise BlockingIOError('Another computer operation is running. Read computer_status before retrying.') from None
509 busy = True
503 root = ET.fromstring(vms.virsh('dumpxml',name,timeout=remaining(15)))510 root = ET.fromstring(vms.virsh('dumpxml',name,timeout=remaining(15)))
504 if vms.virsh('domstate',name,timeout=remaining(15)).strip() != 'running':511 power = vms.virsh('domstate',name,timeout=remaining(15)).strip()
505 raise ValueError('Start the VM before using its computer tools.')
506 platform = root.findtext(f'metadata/{vms.NS}vm/{vms.NS}platform','linux')512 platform = root.findtext(f'metadata/{vms.NS}vm/{vms.NS}platform','linux')
513 status = {'platform': platform, 'power_state': power, 'readiness': 'starting',
514 'capabilities': ['shot', 'input'] if power == 'running' else [],
515 'runtimes': [], 'coordinate_units': 'screenshot_pixels'}
516 if power != 'running':
517 if request['action'] == 'status':
518 return {**status, 'readiness': 'stopped' if power == 'shut off' else power}
519 raise ValueError('Start the VM before using its computer tools.')
507 ai = import_module('vm-ai')520 ai = import_module('vm-ai')
508 ai.lease(name, ai.LIMITS['idle_seconds'])521 if not busy:
522 ai.lease(name, ai.LIMITS['idle_seconds'])
509 if request['action'] == 'shot':523 if request['action'] == 'shot':
510 return asyncio.run(desktop(name))524 return asyncio.run(desktop(name))
525 if busy:
526 with os.fdopen(vms.open_regular(vms.DISKS / name / 'computer.lock')) as incoming:
527 try:
528 operation = json.load(incoming)
529 except ValueError:
530 operation = {'action': 'computer'}
531 operation['age_seconds'] = round(max(0, time.time() - os.fstat(incoming.fileno()).st_mtime), 1)
532 return {**status, 'readiness': 'busy', 'capabilities': ['shot'], 'active_operation': operation}
511 try:533 try:
512 with os.fdopen(vms.open_regular(vms.DISKS / name / 'computer-operation.json')) as incoming:534 with os.fdopen(vms.open_regular(vms.DISKS / name / 'computer-operation.json')) as incoming:
513 unfinished = json.load(incoming)535 unfinished = json.load(incoming)
514 except FileNotFoundError:536 except FileNotFoundError:
515 pass537 pass
516 else:538 else:
539 if request['action'] == 'status':
540 return {**status, 'readiness': 'recovery_required', 'capabilities': ['shot'], 'active_operation': unfinished,
541 'worker_error': 'The previous operation needs cleanup. Inspect its screenshot or restart the VM before retrying.'}
517 finish_operation(name, unfinished)542 finish_operation(name, unfinished)
518 native_text = True543 native_text = True
519 if platform == 'windows' and (request['action'] == 'status' or request['action'] == 'input' and any(a['type'] == 'text' for a in request['actions'])):544 if platform == 'windows' and (request['action'] == 'status' or request['action'] == 'input' and any(a['type'] == 'text' for a in request['actions'])):
...@@ -523,14 +548,14 @@ def handle(name, request):...@@ -523,14 +548,14 @@ def handle(name, request):
523 except (ValueError, subprocess.TimeoutExpired):548 except (ValueError, subprocess.TimeoutExpired):
524 native_text = False549 native_text = False
525 if request['action'] == 'status':550 if request['action'] == 'status':
526 result = {'platform':platform,'capabilities':['shot','input'],'coordinate_units':'screenshot_pixels'}
527 if not native_text:551 if not native_text:
528 return {**result, 'runtimes': [], 'text_encoding': 'ascii', 'worker_error': 'Desktop input is available. Commands, Unicode text and UI actions need a working guest agent with command support.'}552 return {**status, 'text_encoding': 'ascii', 'worker_error': 'Desktop input is available. Commands, Unicode text and UI actions need a working guest agent with command support.'}
529 try:553 try:
530 result.update(worker(name,platform,request))554 status.update(worker(name,platform,request))
555 status['readiness'] = 'ready'
531 except ValueError as error:556 except ValueError as error:
532 result['worker_error'] = str(error)557 status['worker_error'] = str(error)
533 return result558 return status
534 if request['action'] == 'input':559 if request['action'] == 'input':
535 return asyncio.run(desktop(name,request['actions'],platform,request.get('screenshot',True),native_text))560 return asyncio.run(desktop(name,request['actions'],platform,request.get('screenshot',True),native_text))
536 if platform=='windows' and request['action']=='file' and request['operation'] in ('read','write'):561 if platform=='windows' and request['action']=='file' and request['operation'] in ('read','write'):
tools/vms.py+5-1
...@@ -208,7 +208,7 @@ def validate(action, payload):...@@ -208,7 +208,7 @@ def validate(action, payload):
208208
209209
210@contextlib.contextmanager210@contextlib.contextmanager
211def computer_lock(name):211def computer_lock(name, action="computer"):
212 ensure_name(name)212 ensure_name(name)
213 directory = DISKS / name213 directory = DISKS / name
214 if directory.is_symlink():214 if directory.is_symlink():
...@@ -216,6 +216,10 @@ def computer_lock(name):...@@ -216,6 +216,10 @@ def computer_lock(name):
216 descriptor = os.open(directory / "computer.lock", os.O_CREAT | os.O_RDWR | os.O_NOFOLLOW, 0o600)216 descriptor = os.open(directory / "computer.lock", os.O_CREAT | os.O_RDWR | os.O_NOFOLLOW, 0o600)
217 with os.fdopen(descriptor, "w") as lock:217 with os.fdopen(descriptor, "w") as lock:
218 fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)218 fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
219 lock.seek(0)
220 lock.truncate()
221 json.dump({"action": action}, lock)
222 lock.flush()
219 yield223 yield
220224
221225