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.

OS tests: discard passing VM disk artifacts (#1044)

Dee9afa898d78a3
diff --git a/os/DEVELOPMENT.md b/os/DEVELOPMENT.md
index 8f3117220..544a55c4e 100644
--- a/os/DEVELOPMENT.md
+++ b/os/DEVELOPMENT.md
@@ -11,6 +11,16 @@ Preview 12 adds one-action updates, including mouse activation, while preserving
running agents and terminals. Remote-launcher argument tests passed but remote
display/SSH interaction is still unverified.
+## Test artifact retention
+
+After a test passes and its VMs stop, discard its writable disk copies. Retain
+logs and receipts; do not archive `guest.raw`, `target.raw`, `encrypted.raw` or
+maintenance disks in task worktrees. The Asahi acceptance drivers enforce this
+after writing a passing receipt. Failed disks are temporary diagnosis artifacts:
+remove them when the diagnosis/task ends. Remove downloaded fixture images when
+the task ends too; Actions retains the reproducible producer artifacts. Never
+remove an active VM disk or an original input during a test.
+
## Next release priorities
### NVIDIA readiness
diff --git a/os/tests/asahi_encryption_vm.py b/os/tests/asahi_encryption_vm.py
index dbb867801..77d7ef060 100644
--- a/os/tests/asahi_encryption_vm.py
+++ b/os/tests/asahi_encryption_vm.py
@@ -5,6 +5,7 @@ The maintenance VM sees only regular cloned image files. This does not install
on a Mac, publish a password-bearing image, or claim physical keyboard support.
"""
import argparse
+from vm_artifacts import discard_passed_disks
import hashlib
import json
from pathlib import Path
@@ -231,6 +232,7 @@ def main():
if not receipt['source_unchanged']:
receipt.update(status='failed', error='Original source image changed.')
(output / 'receipt.json').write_text(json.dumps(receipt, indent=2) + '\n')
+ discard_passed_disks(output, receipt, maintenance, disk)
if receipt['status'] != 'passed':
raise SystemExit(1)
diff --git a/os/tests/asahi_firstboot_vm.py b/os/tests/asahi_firstboot_vm.py
index f9114afdc..ddc6eb538 100644
--- a/os/tests/asahi_firstboot_vm.py
+++ b/os/tests/asahi_firstboot_vm.py
@@ -5,6 +5,7 @@ This native Apple Silicon-host VM checks the image, not physical Apple hardware.
It never opens a host disk and never modifies the declared source image.
"""
import argparse
+from vm_artifacts import discard_passed_disks
import json
from pathlib import Path
import platform
@@ -238,7 +239,12 @@ def main():
vm.close()
receipt['finished_at'] = time.time()
receipt['source_unchanged'] = digest(source) == args.sha256
+ if not receipt['source_unchanged']:
+ receipt.update(status='failed', error='Original source image changed.')
(output / 'receipt.json').write_text(json.dumps(receipt, indent=2) + '\n')
+ discard_passed_disks(output, receipt, disk)
+ if receipt['status'] != 'passed':
+ raise SystemExit(1)
if __name__ == '__main__':
diff --git a/os/tests/asahi_install_vm.py b/os/tests/asahi_install_vm.py
index a15a965a3..732c366be 100644
--- a/os/tests/asahi_install_vm.py
+++ b/os/tests/asahi_install_vm.py
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
"""Drive the real install form, then unlock and boot its fresh encrypted target."""
import argparse
+from vm_artifacts import discard_passed_disks
import json
from pathlib import Path
import platform
@@ -175,11 +176,15 @@ def main():
try:
if vm:
vm.close()
+ except BaseException as error:
+ receipt.update(status='failed', cleanup_error=str(error))
+ raise
finally:
receipt.update(finished_at=time.time(), original_source_unchanged=digest(source) == args.sha256)
if not receipt['original_source_unchanged']:
receipt.update(status='failed', error='The source image changed.')
(output / 'receipt.json').write_text(json.dumps(receipt, indent=2) + '\n')
+ discard_passed_disks(output, receipt, maintenance, disk)
if __name__ == '__main__':
diff --git a/os/tests/asahi_live_vm.py b/os/tests/asahi_live_vm.py
index df33ac99f..957c0ab00 100644
--- a/os/tests/asahi_live_vm.py
+++ b/os/tests/asahi_live_vm.py
@@ -5,6 +5,7 @@ Only fresh regular test disks are writable. QEMU supplies a firmware device-tree
ESP identity; it does not emulate Apple's boot policy or physical hardware.
"""
import argparse
+from vm_artifacts import discard_passed_disks
import json
from pathlib import Path
import platform
@@ -212,12 +213,16 @@ def main():
try:
if vm:
vm.close()
+ except BaseException as error:
+ receipt.update(status='failed', cleanup_error=str(error))
+ raise
finally:
receipt.update(finished_at=time.time(), original_media_unchanged=digest(args.iso) == media['artifact']['sha256'],
original_payload_unchanged=digest(args.image) == media['media']['payload']['sha256'])
if not receipt['original_media_unchanged'] or not receipt['original_payload_unchanged']:
receipt.update(status='failed', error='An original input image changed.')
(output / 'receipt.json').write_text(json.dumps(receipt, indent=2) + '\n')
+ discard_passed_disks(output, receipt, maintenance, disk)
if __name__ == '__main__':
diff --git a/os/tests/asahi_startup_vm.py b/os/tests/asahi_startup_vm.py
index 21bf78667..1a3b52994 100644
--- a/os/tests/asahi_startup_vm.py
+++ b/os/tests/asahi_startup_vm.py
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
"""Fresh encrypted install -> account -> UEFI unlock -> Harness -> second boot."""
import argparse
+from vm_artifacts import discard_passed_disks
import json
from pathlib import Path
import platform
@@ -163,11 +164,15 @@ def main():
try:
if vm:
vm.close()
+ except BaseException as error:
+ receipt.update(status='failed', cleanup_error=str(error))
+ raise
finally:
receipt.update(finished_at=time.time(), original_source_unchanged=digest(source) == args.sha256)
if not receipt['original_source_unchanged']:
receipt.update(status='failed', error='The source image changed.')
(output / 'receipt.json').write_text(json.dumps(receipt, indent=2) + '\n')
+ discard_passed_disks(output, receipt, maintenance, disk)
if receipt['status'] != 'passed':
raise SystemExit(1)
diff --git a/os/tests/asahi_storage_vm.py b/os/tests/asahi_storage_vm.py
index 7e8384856..4c98c70f7 100644
--- a/os/tests/asahi_storage_vm.py
+++ b/os/tests/asahi_storage_vm.py
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
"""Fresh encrypted Asahi payload install with real interruption and offline retries."""
import argparse
+from vm_artifacts import discard_passed_disks
import json
from pathlib import Path
import platform
@@ -104,11 +105,15 @@ def main():
try:
if vm:
vm.close()
+ except BaseException as error:
+ receipt.update(status='failed', cleanup_error=str(error))
+ raise
finally:
receipt.update(finished_at=time.time(), original_source_unchanged=digest(source) == args.sha256)
if not receipt['original_source_unchanged']:
receipt.update(status='failed', error='The source image changed.')
(output / 'receipt.json').write_text(json.dumps(receipt, indent=2) + '\n')
+ discard_passed_disks(output, receipt, maintenance, disk)
if receipt['status'] != 'passed':
raise SystemExit(1)
diff --git a/os/tests/asahi_target_vm.py b/os/tests/asahi_target_vm.py
index b4eb2d835..b9dc3c53f 100644
--- a/os/tests/asahi_target_vm.py
+++ b/os/tests/asahi_target_vm.py
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
"""Native Asahi partition-preparation test on a fresh, owned sparse VM disk."""
import argparse
+from vm_artifacts import discard_passed_disks
import json
from pathlib import Path
import platform
@@ -83,9 +84,13 @@ def main():
try:
if vm:
vm.close()
+ except BaseException as error:
+ receipt.update(status='failed', cleanup_error=str(error))
+ raise
finally:
receipt['finished_at'] = time.time()
(output / 'receipt.json').write_text(json.dumps(receipt, indent=2) + '\n')
+ discard_passed_disks(output, receipt, maintenance, disk)
if receipt['status'] != 'passed':
raise SystemExit(1)
diff --git a/os/tests/test_vm_artifacts.py b/os/tests/test_vm_artifacts.py
new file mode 100644
index 000000000..38b3ae72a
--- /dev/null
+++ b/os/tests/test_vm_artifacts.py
@@ -0,0 +1,66 @@
+import json
+from pathlib import Path
+import tempfile
+import unittest
+
+from vm_artifacts import discard_passed_disks
+
+
+class DiskCleanup(unittest.TestCase):
+ def test_passing_receipt_keeps_evidence_and_removes_only_owned_disks(self):
+ with tempfile.TemporaryDirectory() as temp:
+ root = Path(temp)
+ disk = root / 'target.raw'
+ disk.write_bytes(b'disk')
+ log = root / 'serial.log'
+ log.write_text('passed')
+ receipt = {'status': 'passed', 'checks': ['encrypted reboot']}
+ (root / 'receipt.json').write_text(json.dumps(receipt))
+ discard_passed_disks(root, receipt, disk, disk)
+ self.assertFalse(disk.exists())
+ self.assertEqual(log.read_text(), 'passed')
+ retained = json.loads((root / 'receipt.json').read_text())
+ self.assertEqual(retained['status'], 'passed')
+ self.assertEqual(retained['disk_cleanup']['removed'],
+ [{'path': 'target.raw', 'logical_bytes': 4}])
+
+ def test_failed_test_retains_disk_for_diagnosis(self):
+ with tempfile.TemporaryDirectory() as temp:
+ disk = Path(temp) / 'target.raw'
+ disk.write_bytes(b'disk')
+ discard_passed_disks(temp, {'status': 'failed'}, disk)
+ self.assertTrue(disk.exists())
+
+ def test_external_fixture_or_symlink_refused_before_any_deletion(self):
+ with tempfile.TemporaryDirectory() as temp:
+ root = Path(temp)
+ output = root / 'test'
+ output.mkdir()
+ source = root / 'input.raw'
+ source.write_bytes(b'input')
+ disk = output / 'target.raw'
+ disk.write_bytes(b'test')
+ link = output / 'link.raw'
+ link.symlink_to(source)
+ for forbidden in (source, link, output / 'serial.log'):
+ with self.subTest(forbidden=forbidden):
+ receipt = {'status': 'passed'}
+ (output / 'receipt.json').write_text(json.dumps(receipt))
+ with self.assertRaises(ValueError):
+ discard_passed_disks(output, receipt, disk, forbidden)
+ self.assertTrue(disk.exists())
+ self.assertEqual(source.read_bytes(), b'input')
+ self.assertEqual(json.loads((output / 'receipt.json').read_text())['status'], 'failed')
+
+ def test_unrecorded_success_does_not_remove_disk(self):
+ with tempfile.TemporaryDirectory() as temp:
+ root = Path(temp)
+ disk = root / 'target.raw'
+ disk.write_bytes(b'disk')
+ with self.assertRaises(FileNotFoundError):
+ discard_passed_disks(root, {'status': 'passed'}, disk)
+ self.assertTrue(disk.exists())
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/os/tests/vm_artifacts.py b/os/tests/vm_artifacts.py
new file mode 100644
index 000000000..acd680cb8
--- /dev/null
+++ b/os/tests/vm_artifacts.py
@@ -0,0 +1,40 @@
+"""Discard owned test disks after a passing receipt and VM shutdown."""
+import json
+from pathlib import Path
+import stat
+
+
+def discard_passed_disks(output, receipt, *disks):
+ if receipt.get('status') != 'passed':
+ return
+ output = Path(output).resolve()
+ record = output / 'receipt.json'
+ try:
+ # Require the durable result first. Validate the entire deletion set
+ # before removing anything; input fixtures are outside this directory.
+ if json.loads(record.read_text()) != json.loads(json.dumps(receipt)):
+ raise ValueError('Write the matching passing receipt before cleanup')
+ owned = []
+ for path in dict.fromkeys(map(Path, disks)):
+ if path.is_symlink():
+ raise ValueError('Refusing a symlink disk')
+ path = path.resolve()
+ path.relative_to(output)
+ if path.suffix not in {'.raw', '.qcow2', '.img'}:
+ raise ValueError('Not a disposable disk')
+ if path.exists():
+ info = path.stat()
+ if not stat.S_ISREG(info.st_mode):
+ raise ValueError('Not a regular disk file')
+ owned.append((path, info))
+ removed = []
+ receipt['disk_cleanup'] = {'removed': removed}
+ for path, info in owned:
+ path.unlink()
+ removed.append({'path': str(path.relative_to(output)),
+ 'logical_bytes': info.st_size})
+ record.write_text(json.dumps(receipt, indent=2) + '\n')
+ except Exception as error:
+ receipt.update(status='failed', disk_cleanup_error=str(error))
+ record.write_text(json.dumps(receipt, indent=2) + '\n')
+ raise