From e919bac8ce86abf4941d4e27ca5ec20527d82cbc Mon Sep 17 00:00:00 2001 From: clover caruso Date: Tue, 17 Nov 2026 03:08:16 -0800 Subject: [PATCH] Distinguish guest agent rejection from an unavailable agent Report missing guest files, denied access, full disks, unsupported commands and failed program starts without echoing guest diagnostics. Treat uncertain agent timeouts as timeouts and retain process-result lookup semantics. Assisted-by: gpt-6.1-sol --- tools/computer-test.py | 44 ++++++++++++++++++++++++++++++++++++++++++ tools/vm-computer.py | 29 ++++++++++++++++++++++++---- 2 files changed, 69 insertions(+), 4 deletions(-) diff --git a/tools/computer-test.py b/tools/computer-test.py index 25de1190cdffdf2c9fcb382aeeb6169667275ee7..8e6881c637dca1beba2656991bf59ba429d5ab5d 100644 --- a/tools/computer-test.py +++ b/tools/computer-test.py @@ -100,6 +100,46 @@ class GuestCancellation(unittest.TestCase): computer.write_file('fixture', 'owned-file', data) self.assertEqual(b''.join(written), data) + def test_agent_rejections_report_causes_without_echoing_guest_diagnostics(self): + private = 'private-guest-diagnostic' + cases = [ + ('guest-exec', 'GenericError', 'Failed to execute child process (Invalid argument)', 'program could not start'), + ('guest-file-open', 'GenericError', 'The system cannot find the file specified.', 'file was not found'), + ('guest-file-open', 'GenericError', 'No such file or directory', 'file was not found'), + ('guest-file-open', 'GenericError', 'Permission denied', 'guest denied access'), + ('guest-file-write', 'GenericError', 'No space left on device', 'disk is full'), + ('guest-info', 'CommandNotFound', 'Unknown command', 'does not support'), + ('guest-info', 'GenericError', 'Unknown failure', 'rejected the operation'), + ] + for operation, kind, diagnostic, expected in cases: + for structured in [False, True]: + reply = Mock(stdout=json.dumps({'error': {'class': kind, 'desc': private + ': ' + diagnostic}}) if structured else '', + stderr='' if structured else 'error: guest agent command failed: ' + private + ': ' + kind + ': ' + diagnostic) + with self.subTest(operation=operation, diagnostic=diagnostic, structured=structured), \ + patch.object(computer.subprocess, 'run', return_value=reply), \ + self.assertRaisesRegex(ValueError, expected) as failure: + computer.qga('fixture', operation) + self.assertNotIn(private, str(failure.exception)) + + def test_agent_timeouts_are_uncertain_and_do_not_repeat_the_command(self): + replies = [Mock(stdout='', stderr="error: guest agent command timed out: guest agent didn't respond to command within '10' seconds"), + Mock(stdout='', stderr='error: Timed out during operation: guest agent command'), + computer.subprocess.TimeoutExpired(['virsh'], 15)] + for reply in replies: + with self.subTest(reply=type(reply)), patch.object(computer.subprocess, 'run') as run: + if isinstance(reply, Exception): + run.side_effect = reply + else: + run.return_value = reply + with self.assertRaisesRegex(TimeoutError, 'Inspect the operation status'): + computer.qga('fixture', 'guest-exec', path='owned-program') + run.assert_called_once() + + def test_agent_success_is_not_overridden_by_stderr(self): + reply = Mock(stdout='{"return":{"pid":123}}', stderr='guest agent command failed: stale warning') + with patch.object(computer.subprocess, 'run', return_value=reply): + self.assertEqual(computer.qga('fixture', 'guest-exec'), {'pid': 123}) + def test_windows_powershell_reads_redirected_output_without_console_capture(self): with patch.object(computer, 'write_file'), patch.object(computer, 'guest_exec', return_value=b''), patch.object(computer, 'read_file', return_value=b'file output'): self.assertEqual(computer.powershell('fixture', "Write-Output 'file output'"), b'file output') @@ -211,6 +251,10 @@ class GuestCancellation(unittest.TestCase): status = computer.handle('fixture', {'action':'status'}) self.assertEqual(status['readiness'], 'worker_starting') self.assertIn('Sign in', status['worker_error']) + work.side_effect = TimeoutError('The guest agent timed out.') + status = computer.handle('fixture', {'action':'status'}) + self.assertEqual(status['readiness'], 'worker_starting') + self.assertIn('timed out', status['worker_error']) work.side_effect = None work.return_value = {'runtimes':['bash']} status = computer.handle('fixture', {'action':'status'}) diff --git a/tools/vm-computer.py b/tools/vm-computer.py index 1766ac4d3f338bbaf6739f3ef08e31cf88b13a8d..67c51e5f6cba58f8c298b05081efc3ce03fb4dbc 100644 --- a/tools/vm-computer.py +++ b/tools/vm-computer.py @@ -42,17 +42,38 @@ def qga(name, operation, **arguments): payload = json.dumps({'execute': operation, 'arguments': arguments}) # Virsh reads stdin so guest scripts and file contents never enter host argv. command = 'qemu-agent-command ' + shlex.quote(name) + ' --timeout 10 ' + shlex.quote(payload) + '\n' - result = subprocess.run(['virsh', '--quiet', '--connect', 'qemu:///system'], input=command, - capture_output=True, text=True, timeout=remaining(15)) + try: + result = subprocess.run(['virsh', '--quiet', '--connect', 'qemu:///system'], input=command, + capture_output=True, text=True, timeout=remaining(15)) + except subprocess.TimeoutExpired: + raise TimeoutError('The guest agent timed out. Inspect the operation status before retrying.') from None + details = result.stderr.casefold() + rejected = False for line in result.stdout.splitlines(): if line.startswith('{'): value = json.loads(line) if 'return' in value: return value['return'] + if 'error' in value: + details += json.dumps(value['error']).casefold() + rejected = True if operation == 'guest-exec-status' and any(line.endswith(( f"PID {arguments['pid']} does not exist", 'PID ld does not exist', 'PID lld does not exist', "Invalid parameter 'pid'")) for line in result.stderr.splitlines()): raise ProcessLookupError('The guest process result is unavailable. Check the VM before retrying.') + if any(message in details for message in ('guest agent command timed out', 'timed out waiting', 'timed out during operation')): + raise TimeoutError('The guest agent timed out. Inspect the operation status before retrying.') + if rejected or 'guest agent command failed' in details or 'unable to execute qemu agent command' in details: + if operation == 'guest-exec' and 'failed to execute child process' in details: + raise ValueError('The program could not start. Check its path, arguments, permissions, and architecture.') + for fragments, message in [ + (('no such file or directory', 'cannot find the file specified', 'cannot find the path specified'), 'The guest file was not found. Check its path.'), + (('permission denied', 'access is denied'), 'The guest denied access. Check the file permissions and security policy.'), + (('no space left on device', 'not enough space on the disk', 'disk is full'), 'The guest disk is full. Free space before retrying.'), + (('commandnotfound', 'has not been found', 'not supported'), 'The guest agent does not support this operation. Update the agent.')]: + if any(fragment in details for fragment in fragments): + raise ValueError(message) + raise ValueError('The guest agent rejected the operation. Check its arguments, permissions, and installed tools.') raise ValueError('The guest agent did not answer. Wait for the VM to finish starting, then retry.') @@ -595,7 +616,7 @@ def handle(name, request, *, active_snapshot=None): try: info = qga(name, 'guest-info') native_text = any(command['name'] == 'guest-exec' and command['enabled'] for command in info['supported_commands']) - except (ValueError, subprocess.TimeoutExpired): + except (ValueError, TimeoutError): native_text = False if request['action'] == 'status': return {**status, 'readiness': 'agent_unavailable', 'text_encoding': 'ascii', @@ -608,7 +629,7 @@ def handle(name, request, *, active_snapshot=None): try: status.update(worker(name,platform,request)) status['readiness'] = 'ready' - except ValueError as error: + except (ValueError, TimeoutError) as error: status['worker_error'] = str(error) return status if request['action'] == 'input': -- 2.54.1