From 711e7f082e04608ad1e120898526ee9c9870483f Mon Sep 17 00:00:00 2001 From: clover caruso Date: Mon, 16 Nov 2026 18:29:06 -0800 Subject: [PATCH] Keep VM inspection available during blocked commands Assisted-by: gpt-6.1-sol --- tools/computer-test.py | 35 +++++++++++++++++++++++++++++++--- tools/vm-computer.py | 43 +++++++++++++++++++++++++++++++++--------- tools/vms.py | 6 +++++- 3 files changed, 71 insertions(+), 13 deletions(-) diff --git a/tools/computer-test.py b/tools/computer-test.py index 0ead726c36b4318790332bf26326f73e673f126c..36a9354a9147a584faaf2c8601b4855f0752b2b0 100644 --- a/tools/computer-test.py +++ b/tools/computer-test.py @@ -146,7 +146,7 @@ class GuestCancellation(unittest.TestCase): desktop.assert_awaited_once_with('fixture', actions, 'windows', False, False) work.assert_not_called() - def test_failed_cleanup_blocks_later_control_until_reconnection(self): + def test_failed_cleanup_keeps_status_available_and_blocks_later_control(self): for platform, identity in [('linux', 'snowglobe-computer-request-' + 'a' * 32), ('windows', 'a' * 32)]: operation = {'platform': platform, 'id': identity} computer.record_operation('fixture', operation) @@ -157,14 +157,43 @@ class GuestCancellation(unittest.TestCase): def virsh(action, *args, **kwargs): return 'running' if action == 'domstate' else '' with patch.object(computer.vms, 'virsh', virsh), patch.object(computer, 'worker') as work: + with patch.object(computer, 'finish_operation', side_effect=AssertionError('Status must not wait for cleanup')): + status = computer.handle('fixture', {'action': 'status'}) + self.assertEqual(status['readiness'], 'recovery_required') + self.assertEqual(status['active_operation'], operation) with patch.object(computer, 'guest_exec', side_effect=TimeoutError()), patch.object(computer, 'powershell', side_effect=TimeoutError()), self.assertRaises(BlockingIOError): - computer.handle('fixture', {'action': 'status'}) + computer.handle('fixture', {'action': 'exec', 'runtime': 'default', 'script': 'true', 'timeout_ms': 1000}) work.assert_not_called() with patch.object(computer, 'guest_exec', return_value=b'{}'), patch.object(computer, 'powershell', return_value=b'{}'): - computer.handle('fixture', {'action': 'status'}) + computer.handle('fixture', {'action': 'exec', 'runtime': 'default', 'script': 'true', 'timeout_ms': 1000}) work.assert_called_once() self.assertFalse(pending.exists()) + def test_busy_execution_keeps_status_and_screenshots_available(self): + def virsh(action, *args, **kwargs): + return 'running' if action == 'domstate' else '' + with computer.vms.computer_lock('fixture', 'exec'), patch.object(computer.vms, 'virsh', virsh), patch.object(computer, 'worker') as work: + status = computer.handle('fixture', {'action': 'status'}) + self.assertEqual(status['readiness'], 'busy') + self.assertEqual(status['active_operation']['action'], 'exec') + self.assertGreaterEqual(status['active_operation']['age_seconds'], 0) + with patch.object(computer, 'desktop', new_callable=AsyncMock, return_value={'png': 'fixture'}) as desktop: + self.assertEqual(computer.handle('fixture', {'action': 'shot'}), {'png': 'fixture'}) + desktop.assert_awaited_once_with('fixture') + with self.assertRaisesRegex(BlockingIOError, 'computer_status'): + computer.handle('fixture', {'action': 'exec', 'script': 'true', 'runtime': 'default', 'timeout_ms': 1000}) + work.assert_not_called() + + def test_stopped_vm_reports_status_without_starting_a_guest_worker(self): + def virsh(action, *args, **kwargs): + return 'shut off' if action == 'domstate' else '' + with patch.object(computer.vms, 'virsh', virsh), patch.object(computer, 'worker') as work: + status = computer.handle('fixture', {'action': 'status'}) + self.assertEqual(status['power_state'], 'shut off') + self.assertEqual(status['readiness'], 'stopped') + self.assertEqual(status['capabilities'], []) + work.assert_not_called() + def test_spawn_returns_its_handle_when_cleanup_must_wait_for_reconnection(self): result = {'id': '123:456', 'running': True} def execute(name, script, **kwargs): diff --git a/tools/vm-computer.py b/tools/vm-computer.py index f2414f6c8b750bdfcfdf5d400693375153bc41f1..3ba16566fc58d4456d108c64af77ed627ce732ed 100644 --- a/tools/vm-computer.py +++ b/tools/vm-computer.py @@ -499,21 +499,46 @@ def handle(name, request): return vms.mac_vms.handle('computer', {'name':name,'request':request}) token = DEADLINE.set(time.monotonic() + 210) try: - with vms.computer_lock(name): + with contextlib.ExitStack() as scope: + busy = False + try: + scope.enter_context(vms.computer_lock(name, request['action'])) + except BlockingIOError: + if request['action'] not in ('status', 'shot'): + raise BlockingIOError('Another computer operation is running. Read computer_status before retrying.') from None + busy = True root = ET.fromstring(vms.virsh('dumpxml',name,timeout=remaining(15))) - if vms.virsh('domstate',name,timeout=remaining(15)).strip() != 'running': - raise ValueError('Start the VM before using its computer tools.') + power = vms.virsh('domstate',name,timeout=remaining(15)).strip() platform = root.findtext(f'metadata/{vms.NS}vm/{vms.NS}platform','linux') + status = {'platform': platform, 'power_state': power, 'readiness': 'starting', + 'capabilities': ['shot', 'input'] if power == 'running' else [], + 'runtimes': [], 'coordinate_units': 'screenshot_pixels'} + if power != 'running': + if request['action'] == 'status': + return {**status, 'readiness': 'stopped' if power == 'shut off' else power} + raise ValueError('Start the VM before using its computer tools.') ai = import_module('vm-ai') - ai.lease(name, ai.LIMITS['idle_seconds']) + if not busy: + ai.lease(name, ai.LIMITS['idle_seconds']) if request['action'] == 'shot': return asyncio.run(desktop(name)) + if busy: + with os.fdopen(vms.open_regular(vms.DISKS / name / 'computer.lock')) as incoming: + try: + operation = json.load(incoming) + except ValueError: + operation = {'action': 'computer'} + operation['age_seconds'] = round(max(0, time.time() - os.fstat(incoming.fileno()).st_mtime), 1) + return {**status, 'readiness': 'busy', 'capabilities': ['shot'], 'active_operation': operation} try: with os.fdopen(vms.open_regular(vms.DISKS / name / 'computer-operation.json')) as incoming: unfinished = json.load(incoming) except FileNotFoundError: pass else: + if request['action'] == 'status': + return {**status, 'readiness': 'recovery_required', 'capabilities': ['shot'], 'active_operation': unfinished, + 'worker_error': 'The previous operation needs cleanup. Inspect its screenshot or restart the VM before retrying.'} finish_operation(name, unfinished) native_text = True 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): except (ValueError, subprocess.TimeoutExpired): native_text = False if request['action'] == 'status': - result = {'platform':platform,'capabilities':['shot','input'],'coordinate_units':'screenshot_pixels'} if not native_text: - 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.'} + 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.'} try: - result.update(worker(name,platform,request)) + status.update(worker(name,platform,request)) + status['readiness'] = 'ready' except ValueError as error: - result['worker_error'] = str(error) - return result + status['worker_error'] = str(error) + return status if request['action'] == 'input': return asyncio.run(desktop(name,request['actions'],platform,request.get('screenshot',True),native_text)) if platform=='windows' and request['action']=='file' and request['operation'] in ('read','write'): diff --git a/tools/vms.py b/tools/vms.py index a57a753445aaffed23aeba14cbe3e777e730cc24..803e028ebb2621e5c18809a7dc48307a4cfa1315 100644 --- a/tools/vms.py +++ b/tools/vms.py @@ -208,7 +208,7 @@ def validate(action, payload): @contextlib.contextmanager -def computer_lock(name): +def computer_lock(name, action="computer"): ensure_name(name) directory = DISKS / name if directory.is_symlink(): @@ -216,6 +216,10 @@ def computer_lock(name): descriptor = os.open(directory / "computer.lock", os.O_CREAT | os.O_RDWR | os.O_NOFOLLOW, 0o600) with os.fdopen(descriptor, "w") as lock: fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB) + lock.seek(0) + lock.truncate() + json.dump({"action": action}, lock) + lock.flush() yield -- 2.54.1