Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

Read guest agent output without serial control sequences (#701)

Deeab9dd8576e13
diff --git a/os/tests/test_vm_protocol.py b/os/tests/test_vm_protocol.py
index 2f6c865c9..4e24ccf57 100644
--- a/os/tests/test_vm_protocol.py
+++ b/os/tests/test_vm_protocol.py
@@ -1,9 +1,12 @@
"""Exercise command framing with a real shell, without QEMU or guest assumptions."""
import importlib.util
import io
+import json
from pathlib import Path
+import shlex
import socket
import subprocess
+import tempfile
import unittest
spec = importlib.util.spec_from_file_location('hn_vm', Path(__file__).with_name('vm.py'))
@@ -34,6 +37,32 @@ class SerialProtocol(unittest.TestCase):
self.vm.command("printf 'probe failed'; exit 3", timeout=5)
self.assertEqual(self.vm.command('printf diagnostics', timeout=5)[0], 'diagnostics')
+ def test_file_bytes_survive_shell_integration_before_first_json_event(self):
+ # The live guest's OSC 3008 prefix shares the first event's serial line.
+ # Reading the original file must retain that event, not silently skip it.
+ self.vm.send(r'''trap 'printf "\033]3008;start=fixture;type=command\033\\"' DEBUG''' + '\n')
+ with tempfile.TemporaryDirectory() as directory:
+ path = Path(directory) / "agent's first event.jsonl"
+ event = {'type': 'text', 'part': {'text': '42'}}
+ path.write_text(json.dumps(event) + '\n')
+ console, _ = self.vm.command('cat ' + shlex.quote(str(path)))
+ with self.assertRaises(json.JSONDecodeError):
+ json.loads(console)
+ self.assertEqual(json.loads(self.vm.read_file(path)), event)
+
+ def test_file_transfer_preserves_binary_and_empty_files(self):
+ with tempfile.TemporaryDirectory() as directory:
+ path = Path(directory) / 'output'
+ for payload in [bytes(range(256)) * 4, b'']:
+ with self.subTest(size=len(payload)):
+ path.write_bytes(payload)
+ self.assertEqual(self.vm.read_file(path), payload)
+
+ def test_missing_guest_file_is_not_reported_as_empty(self):
+ with tempfile.TemporaryDirectory() as directory:
+ with self.assertRaisesRegex(RuntimeError, 'Guest command failed'):
+ self.vm.read_file(Path(directory) / 'missing')
+
if __name__ == '__main__':
unittest.main()
diff --git a/os/tests/vm.py b/os/tests/vm.py
index 0ac9ff53e..ca54928ef 100644
--- a/os/tests/vm.py
+++ b/os/tests/vm.py
@@ -173,6 +173,18 @@ class VM:
raise RuntimeError(f'Guest command failed ({status}): {command}\n{output[-2000:]}')
return output[:match.start()], status
+ def read_file(self, path, timeout=90):
+ """Read exact guest bytes without serial prompt or shell-integration codes."""
+ marker = 'HN_FILE_' + uuid.uuid4().hex
+ output, _ = self.command(
+ 'hn_file_data=$(base64 < ' + shlex.quote(str(path)) + ') && '
+ f"printf '\\n{marker}:begin\\n%s\\n{marker}:end\\n' \"$hn_file_data\"",
+ timeout=timeout)
+ match = re.search(r'\r?\n' + marker + r':begin\r?\n(.*?)\r?\n' + marker + r':end\r?\n', output, re.S)
+ if not match:
+ raise RuntimeError(f'Guest file transfer is incomplete: {path}')
+ return base64.b64decode(''.join(match.group(1).splitlines()), validate=True)
+
def login_installed(self, config, unlock_delay=0):
if config['encrypt']:
self.unlock_count += 1
@@ -426,10 +438,11 @@ if (rows.some(row => row.engine !== 'terminal')) throw new Error('A plain shell
prompt = 'What is six times seven? Reply with only the decimal number. Do not use tools.'
command = 'cd "$HOME/Projects" && timeout 120 /usr/bin/opencode run --format json ' + shlex.quote(prompt) + ' > /tmp/hn-first-chat.jsonl 2>/tmp/hn-first-chat.err'
_, status = vm.command(user('sh -c ' + shlex.quote(command)), timeout=140, check=False)
- output, _ = vm.command('cat /tmp/hn-first-chat.jsonl')
- errors, _ = vm.command('cat /tmp/hn-first-chat.err')
- (folder / 'first-opencode-chat.jsonl').write_text(output)
- (folder / 'first-opencode-chat.stderr.txt').write_text(errors)
+ raw = vm.read_file('/tmp/hn-first-chat.jsonl')
+ errors = vm.read_file('/tmp/hn-first-chat.err')
+ (folder / 'first-opencode-chat.jsonl').write_bytes(raw)
+ (folder / 'first-opencode-chat.stderr.txt').write_bytes(errors)
+ output = raw.decode('utf-8')
events = []
for line in output.splitlines():
try: