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.

Keep public OS update retries recoverable and noninteractive (#788)

Dee47e594849e6a
diff --git a/os/release_update.py b/os/release_update.py
index c766145b4..ffceedd88 100644
--- a/os/release_update.py
+++ b/os/release_update.py
@@ -126,7 +126,9 @@ def apply(feed=FEED):
raise ValueError('Custom repository configuration needs a manual full system upgrade.')
current = next(iter(dates))
if system.pending_update() or date > current:
- system.update(max(current, date))
+ # This public update was already requested; pacman's extra
+ # confirmation must not stall the Updates terminal.
+ system.update(max(current, date), noninteractive=True)
updater.apply(folder, system, base, installation)
diff --git a/os/system.py b/os/system.py
index f459a5111..359be0e6f 100644
--- a/os/system.py
+++ b/os/system.py
@@ -171,7 +171,7 @@ def advance_snapshot(config, date):
return pattern.sub(lambda match: match.group(1) + date + match.group(3), config)
-def update(date):
+def update(date, *, noninteractive=False):
# Pin every repository to the same date, and perform a full upgrade. Never mix
# a newly synced package database with an intentionally old installed base.
config = PACMAN_CONFIG
@@ -196,7 +196,13 @@ def update(date):
temporary.write_text(advanced)
temporary.chmod(stat.S_IMODE(config.stat().st_mode))
temporary.replace(config)
- result = subprocess.run(['pacman', '-Syyu'])
+ # Both the CLI and the public release updater already hold the operation
+ # lock and saved the recovery point. Only this transaction skips our hook.
+ command = ['pacman', '-Syyu']
+ if noninteractive:
+ command.append('--noconfirm')
+ result = subprocess.run(command, stdin=subprocess.DEVNULL if noninteractive else None,
+ env=dict(os.environ, HN_OS_UPDATE_CHECKPOINT='1'))
receipt.update(finished_at=datetime.now(timezone.utc).isoformat(), exit_status=result.returncode)
write_json(UPDATE_RECEIPT, receipt)
if result.returncode:
@@ -323,7 +329,6 @@ def main():
if args.command == 'checkpoint':
checkpoint('before-packages' if args.pacman_hook else 'manual', args.pacman_hook)
elif args.command == 'update':
- os.environ['HN_OS_UPDATE_CHECKPOINT'] = '1'
update(args.snapshot)
else:
recover(args.device, args.checkpoint)
diff --git a/os/tests/fast_update_vm.py b/os/tests/fast_update_vm.py
index d2d735324..64d9ba6a9 100644
--- a/os/tests/fast_update_vm.py
+++ b/os/tests/fast_update_vm.py
@@ -112,8 +112,29 @@ assert (root/'heartbeat').stat().st_mtime_ns != before
vm.command('python3 -c ' + shlex.quote(expression))
vm.command('while read -r pid; do kill -0 "$pid" || exit 1; done < /tmp/fast-original-agent; '
'cmp /tmp/fast-original-boot /proc/sys/kernel/random/boot_id')
- vm.command('hn hn-list-clients -F "#{session_id}" > /tmp/fast-restored-session; '
- 'cmp /tmp/fast-visible-session /tmp/fast-restored-session')
+ vm.command('for n in $(seq 1 80); do hn hn-list-clients -F "#{session_id}" > /tmp/fast-restored-session && '
+ 'test -s /tmp/fast-restored-session && cmp -s /tmp/fast-visible-session /tmp/fast-restored-session && exit 0; '
+ 'sleep .25; done; cmp /tmp/fast-visible-session /tmp/fast-restored-session; exit 1', timeout=30)
+ # A live rendering client must belong to the actual terminal service and
+ # execute the selected binary with its input still attached to a PTY.
+ expression = '''from pathlib import Path
+import json, re, subprocess
+target = Path.home() / '.local/state/harness-os/updates/current'
+target = target.resolve() if target.exists() else Path('/usr/lib/harness')
+clients = []
+for pid in subprocess.check_output(['hn', 'hn-list-clients', '-F', '#{client_pid}'], text=True, timeout=3).splitlines():
+ assert pid.isdigit(), pid
+ process = Path('/proc') / pid
+ executable, stdin = process / 'exe', process / 'fd/0'
+ assert executable.samefile(target / 'harness-tui'), str(executable.resolve())
+ assert any(line.split(':', 2)[-1].endswith('/hn-screen.service') for line in (process / 'cgroup').read_text().splitlines())
+ assert re.fullmatch(r'/dev/pts/\\d+', str(stdin.resolve())) and stdin.is_char_device(), str(stdin.resolve())
+ clients.append(dict(pid=int(pid), executable=str(executable.resolve()), stdin=str(stdin.resolve())))
+assert clients, 'No attached rendering client'
+Path('/tmp/fast-screen-client.json').write_text(json.dumps(dict(target=str(target), clients=clients)))
+'''
+ vm.command('python3 -c ' + shlex.quote(expression))
+ (vm.folder / (name + '-client.json')).write_bytes(vm.read_file('/tmp/fast-screen-client.json'))
vm.command('for n in $(seq 1 80); do hn select-window -t update-probe && exit 0; sleep .25; done; exit 1')
wait_text(vm, 'visible lock probe', name + '-restored')
vm.type_probe(name)
@@ -131,8 +152,14 @@ assert (root/'heartbeat').stat().st_mtime_ns != before
vm.keys('meta_l', 'u')
condition = ('grep -q ' + shlex.quote('"status": "failed"') + ' ' + state + '/transaction.json' if expect_failure else
'test ! -e ' + state + '/ready.json && test -s ' + state + '/applied.json')
+ # A failed receipt precedes restoration of the old screen. Type=exec
+ # also makes foot active before hn has reattached. Observe completion
+ # of the apply operation before checking its real client and workspace.
+ settled = ('(hn_update_unit_state=$(systemctl --user show harness-apply-update.service -p ActiveState --value); '
+ 'test "$hn_update_unit_state" = inactive || test "$hn_update_unit_state" = failed)')
try:
- vm.command('for n in $(seq 1 120); do ' + condition + ' && systemctl --user is-active --quiet hn-screen && exit 0; sleep .5; done; '
+ vm.command('for n in $(seq 1 120); do ' + condition + ' && ' + settled +
+ ' && systemctl --user is-active --quiet hn-screen && exit 0; sleep .5; done; '
'sudo journalctl _UID=1000 --no-pager; exit 1', timeout=90)
finally:
if mouse:
diff --git a/os/tests/release_update_vm.py b/os/tests/release_update_vm.py
index 1786b9319..b9f309790 100644
--- a/os/tests/release_update_vm.py
+++ b/os/tests/release_update_vm.py
@@ -1,6 +1,10 @@
"""Exercise the actual OS release transport on a disposable installed VM."""
+import hashlib
import json
+from pathlib import Path
+import re
import shlex
+import subprocess
import time
from session_vm import put, PROBE
@@ -9,9 +13,16 @@ def exercise(vm, manifest, config):
# Retain the new bootstrap outside pacman-owned paths before restoring the
# public preview. It has no updater; this is its one-time migration path.
vm.command('mkdir -p /tmp/system-channel; cp /usr/lib/harness-os/release_update.py '
- '/usr/lib/harness-os/runtime_update.py /usr/lib/harness-os/live_update.py '
+ '/usr/lib/harness-os/runtime_update.py /usr/lib/harness-os/system.py /usr/lib/harness-os/live_update.py '
'/usr/lib/harness-os/open-updates /tmp/system-channel/; '
'sudo cat /etc/sudoers.d/30-harness-updates > /tmp/system-channel/30-harness-updates')
+ source = Path(__file__).resolve().parents[1]
+ helper_hashes = {}
+ for name in ['system.py', 'release_update.py', 'runtime_update.py', 'live_update.py']:
+ expected = hashlib.sha256((source / name).read_bytes()).hexdigest()
+ assert hashlib.sha256(vm.read_file('/tmp/system-channel/' + name)).hexdigest() == expected, \
+ 'The candidate package contains a different updater: ' + name
+ helper_hashes[name] = expected
vm.command('cp /home/me/update-bundle/package-manifest.json /tmp/fast-updates/; '
'cp /home/me/update-bundle/' + shlex.quote(manifest['package']['name']) + ' /tmp/fast-updates/')
builder = '''import hashlib, json, pathlib
@@ -72,19 +83,42 @@ metadata = dict(schema=1, channel='preview', architecture='x86_64',
# combined action. Only the root helper's parser default uses the private
# loopback feed; the shipped helper never accepts a user-owned feed cache.
vm.command('sudo install -m 644 /tmp/system-channel/live_update.py /usr/lib/harness-os/live_update.py; '
+ 'sudo install -m 644 /tmp/system-channel/system.py /usr/lib/harness-os/system.py; '
'sudo install -m 755 /tmp/system-channel/open-updates /usr/lib/harness-os/open-updates; '
# The old package does not own the new policy path. Use a
# fixture-only name so pacman can install its actual owned file
# without an unrelated "exists in filesystem" collision.
'sudo install -m 440 /tmp/system-channel/30-harness-updates /etc/sudoers.d/99-harness-test-updates')
- helper = vm.read_file('/tmp/system-channel/release_update.py').decode().replace(
- "parser.add_argument('--feed', default=FEED,", "parser.add_argument('--feed', default='http://127.0.0.1:19447/os-metadata.json',")
+ helper = vm.read_file('/tmp/system-channel/release_update.py').decode()
+ feed_default = "parser.add_argument('--feed', default=FEED,"
+ assert helper.count(feed_default) == 1, 'The private feed override no longer matches the packaged helper'
+ helper = helper.replace(feed_default, "parser.add_argument('--feed', default='http://127.0.0.1:19447/os-metadata.json',")
put(vm, '/tmp/system-channel/test-release-helper.py', helper)
vm.command('sudo install -m 644 /tmp/system-channel/test-release-helper.py /usr/lib/harness-os/release_update.py; '
'sudo visudo -c -f /etc/sudoers')
+ for name, expected in [('system.py', helper_hashes['system.py']),
+ ('release_update.py', hashlib.sha256(helper.encode()).hexdigest())]:
+ assert hashlib.sha256(vm.read_file('/usr/lib/harness-os/' + name)).hexdigest() == expected, \
+ 'The test helper was not activated: ' + name
vm.command('mkdir -p ~/.local/state/harness-os/updates; printf %s ' + shlex.quote(json.dumps(
{'available': True, 'checked_at': time.time(), 'version': manifest['package']['version']})) +
' > ~/.local/state/harness-os/updates/system.json')
+ # Leave a real failed full update pending. Super+u must retry its pacman
+ # transaction without another prompt or replacing the original checkpoint.
+ fixture_hashes = {}
+ for name in ['updates.sh', 'update_retry.py']:
+ text = Path(__file__).with_name(name).read_text()
+ put(vm, '/tmp/system-channel/' + name, text)
+ fixture_hashes[name] = hashlib.sha256(text.encode()).hexdigest()
+ output, status = vm.command('sudo bash /tmp/system-channel/updates.sh prepare-public', timeout=300, check=False)
+ (vm.folder / 'public-retry-prepare.log').write_text(output)
+ assert status == 0, 'Public retry preparation failed; see public-retry-prepare.log'
+ marker = re.search(r'^HN_PUBLIC_RETRY_STATE=(/tmp/hn-os-update-check\.[A-Za-z0-9]+)\s*$', output, re.M)
+ assert marker, 'Public retry fixture did not retain its private state'
+ retry_work = marker.group(1)
+ vm.command('sudo cat ' + shlex.quote(retry_work + '/retry-before.json') +
+ ' > /tmp/system-channel/retry-before.json && sudo nmcli networking on && nm-online -q --timeout=30', timeout=35)
+ (vm.folder / 'public-retry-before.json').write_bytes(vm.read_file('/tmp/system-channel/retry-before.json'))
# A live Updates window may still be executing the pre-rollback module.
# Close only that fixture's UI pane before opening the candidate with Super+u.
vm.command('hn kill-window -t Updates 2>/dev/null || true')
@@ -100,12 +134,37 @@ metadata = dict(schema=1, channel='preview', architecture='x86_64',
# The new package now owns its real policy. Remove the bootstrap fixture
# only after passwordless activation has been independently established.
vm.command('printf %s ' + shlex.quote(config['password'] + '\n') + ' | sudo -S -v; '
- 'sudo rm /etc/sudoers.d/99-harness-test-updates; sudo -K')
+ 'sudo rm /etc/sudoers.d/99-harness-test-updates')
+ vm.command('sudo python3 /tmp/system-channel/update_retry.py ' + shlex.quote(retry_work) +
+ ' > /tmp/system-channel/public-retry.json')
+ public_retry = json.loads(vm.read_file('/tmp/system-channel/public-retry.json'))
+ assert public_retry['status'] == 'passed' and public_retry['runtime_update']['candidate'] == manifest
+ public_retry['candidate_helpers_sha256'] = helper_hashes
+ public_retry['private_feed_helper'] = {'path': '/usr/lib/harness-os/release_update.py',
+ 'sha256': hashlib.sha256(helper.encode()).hexdigest()}
+ public_retry['fixture_sha256'] = fixture_hashes
+ public_retry['test_source_commit'] = subprocess.check_output(
+ ['git', '-C', str(source.parent), 'rev-parse', 'HEAD'], text=True).strip()
+ public_retry['test_source_dirty'] = bool(subprocess.check_output(
+ ['git', '-C', str(source.parent), 'status', '--porcelain'], text=True).strip())
+ pacman_log = vm.read_file('/var/log/pacman.log')
+ (vm.folder / 'public-retry-pacman.log').write_bytes(pacman_log)
+ public_retry['pacman_log_sha256'] = hashlib.sha256(pacman_log).hexdigest()
+ output, status = vm.command('sudo bash /tmp/system-channel/updates.sh cleanup-public ' + shlex.quote(retry_work),
+ timeout=180, check=False)
+ (vm.folder / 'public-retry-cleanup.log').write_text(output)
+ vm.command('sudo -K')
+ public_retry['cleanup_status'] = 'passed' if status == 0 else 'failed'
+ if status:
+ public_retry['status'] = 'failed'
+ (vm.folder / 'public-retry-receipt.json').write_text(json.dumps(public_retry, indent=2) + '\n')
+ assert status == 0, 'Public retry fixture cleanup failed; see public-retry-cleanup.log'
vm.command('while read -r pid; do kill -0 "$pid" || exit 1; done < /tmp/fast-original-agent; '
'kill -0 "$(cat /tmp/fast-original-pid)"; cmp /tmp/fast-original-boot /proc/sys/kernel/random/boot_id')
vm.command('python3 -c ' + shlex.quote("import json; assert json.load(open('/run/harness-os-restart-required'))['status'] == 'ready'"))
available(False)
checks.append('Super+u alone applies the verified OS package with no sudo credential, confirmation or password; unrelated root commands and local bundles remain denied')
+ checks.append('The same public action retries a failed full update through real pacman, installs probe v2 and retains the original v1 checkpoint; no distinct dated Arch migration is claimed')
checks.append('Private root download rebuilds initramfs and preserves live processes; no duplicate update is offered')
_, status = vm.command('harness updates apply', check=False)
assert status != 0, 'Fast runtime changed before the OS restart'
@@ -116,7 +175,7 @@ metadata = dict(schema=1, channel='preview', architecture='x86_64',
'printf %s ' + shlex.quote('[Service]\nExecStart=\nExecStart=/usr/bin/python3 /usr/lib/harness-os/live_update.py check --feeds /home/me/update-test/feeds-both.json\n') +
' > ~/.config/systemd/user/harness-update.service.d/fixture.conf; systemctl --user daemon-reload')
receipt = {'status': 'passed', 'source_commit': manifest['source_commit'],
- 'package': manifest['package'], 'checks': checks}
+ 'package': manifest['package'], 'public_retry': public_retry, 'checks': checks}
(vm.folder / 'system-channel-receipt.json').write_text(json.dumps(receipt, indent=2) + '\n')
return receipt
diff --git a/os/tests/test_release_update.py b/os/tests/test_release_update.py
index 34eddaaa3..763e540fa 100644
--- a/os/tests/test_release_update.py
+++ b/os/tests/test_release_update.py
@@ -4,7 +4,10 @@ import hashlib
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
import importlib.util
import json
+import os
from pathlib import Path
+import subprocess
+import sys
import tempfile
import threading
from types import SimpleNamespace
@@ -126,7 +129,7 @@ class ReleaseChannel(unittest.TestCase):
self.events = []
system = SimpleNamespace(operation_lock=lambda: nullcontext(), installed=lambda: {'root_uuid': 'fixture'},
snapshot_date=lambda date: date, PACMAN_CONFIG=config, pending_update=lambda: None,
- update=lambda date: self.events.append(('system', date)))
+ update=lambda date, **options: self.events.append(('system', date, options)))
def apply(folder, system, base, installation):
self.assertEqual((folder / self.manifest['package']['name']).read_bytes(), self.package)
self.assertEqual(folder.stat().st_mode & 0o777, 0o700)
@@ -138,7 +141,7 @@ class ReleaseChannel(unittest.TestCase):
updater = self.fake_updater()
with patch.object(update.os, 'geteuid', return_value=0), patch.object(update, 'load_runtime_updater', return_value=updater):
update.apply(self.url + '/metadata.json')
- self.assertEqual(self.events[0], ('system', '2026/10/01'))
+ self.assertEqual(self.events[0], ('system', '2026/10/01', {'noninteractive': True}))
self.assertEqual(self.events[1][0], 'harness')
self.assertFalse(self.events[1][1].exists(), 'Root download is removed after the transaction')
updater.validate_bundle.assert_called_once()
@@ -152,6 +155,48 @@ class ReleaseChannel(unittest.TestCase):
self.assertEqual(self.events, [])
updater.validate_bundle.assert_not_called()
+ def test_public_base_upgrade_completes_without_terminal_input(self):
+ updater = self.fake_updater()
+ public_system = updater.system_module()
+ spec = importlib.util.spec_from_file_location('release_system', Path(update.__file__).with_name('system.py'))
+ system = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(system)
+ system.PACMAN_CONFIG = public_system.PACMAN_CONFIG
+ system.UPDATE_RECEIPT = self.root / 'update.json'
+ system.checkpoint = Mock(return_value='original-checkpoint')
+ public_system.update = system.update
+ prompt = self.root / 'pacman-prompt.py'
+ # Model pacman's documented --noconfirm contract with an actual child
+ # that otherwise waits for stdin, like the inherited Updates terminal.
+ prompt.write_text('''import os, sys
+assert os.environ['HN_OS_UPDATE_CHECKPOINT'] == '1'
+if '--noconfirm' not in sys.argv:
+ print('Proceed with installation? [Y/n]', flush=True)
+ answer = sys.stdin.readline()
+ sys.exit(0 if answer and answer.strip().lower() in ('', 'y', 'yes') else 1)
+assert sys.stdin.read() == '', 'Scripted updates must not consume terminal input'
+''')
+ run = subprocess.run
+ read_fd, write_fd = os.pipe()
+ def pacman(argv, **kwargs):
+ self.assertEqual(argv[:2], ['pacman', '-Syyu'])
+ if kwargs.get('stdin') is None:
+ kwargs['stdin'] = read_fd
+ return run([sys.executable, str(prompt), *argv[1:]],
+ capture_output=True, text=True, timeout=2, **kwargs)
+ try:
+ with patch.object(update.os, 'geteuid', return_value=0), \
+ patch.object(update, 'load_runtime_updater', return_value=updater), \
+ patch.object(system.subprocess, 'run', side_effect=pacman):
+ update.apply(self.url + '/metadata.json')
+ finally:
+ os.close(read_fd)
+ os.close(write_fd)
+ system.checkpoint.assert_called_once_with('before-update')
+ receipt = json.loads(system.UPDATE_RECEIPT.read_text())
+ self.assertEqual((receipt['exit_status'], receipt['checkpoint']), (0, 'original-checkpoint'))
+ self.assertEqual(self.events[0][0], 'harness')
+
def test_failed_harness_transaction_blocks_advancing_the_arch_base(self):
updater = self.fake_updater()
updater.latest = lambda: {'status': 'failed'}
diff --git a/os/tests/test_system.py b/os/tests/test_system.py
index 462a7b24a..5c5b9ec3b 100644
--- a/os/tests/test_system.py
+++ b/os/tests/test_system.py
@@ -1,8 +1,11 @@
import importlib.util
from contextlib import ExitStack
+import fcntl
import json
+import os
from pathlib import Path
import subprocess
+import sys
import tempfile
import unittest
from unittest.mock import patch
@@ -73,6 +76,7 @@ class InterruptedUpdates(unittest.TestCase):
for key, value in [('PACMAN_CONFIG', self.config), ('UPDATE_RECEIPT', self.receipt), ('CHECKPOINTS', self.snapshots)]:
self.context.enter_context(patch.object(system, key, value))
self.installed = self.context.enter_context(patch.object(system, 'installed', return_value={'root_uuid': 'this-system'}))
+ self.run = subprocess.run
self.pacman = self.context.enter_context(patch.object(system.subprocess, 'run'))
def save_checkpoint(self, reason):
@@ -86,8 +90,9 @@ class InterruptedUpdates(unittest.TestCase):
def test_failed_update_blocks_packages_and_retry_retains_original_checkpoint(self):
with patch.object(system, 'checkpoint', side_effect=self.save_checkpoint) as save:
- def failed_pacman(argv):
+ def failed_pacman(argv, **kwargs):
self.assertEqual(argv, ['pacman', '-Syyu'])
+ self.assertIsNone(kwargs.get('stdin'))
pending = system.pending_update()
self.assertIsNone(pending['exit_status'])
self.assertEqual(pending['checkpoint'], 'before-update-123')
@@ -115,6 +120,46 @@ class InterruptedUpdates(unittest.TestCase):
system.checkpoint(pacman_hook=True)
self.installed.assert_not_called()
+ def test_programmatic_update_hook_reuses_checkpoint_without_inherited_override(self):
+ # The public release updater calls update() under the operation lock,
+ # without going through system.main(). Run its real hook in a child
+ # process; redirect only root/lock locations into this private fixture.
+ (self.root / 'install.json').write_text('{}')
+ hook = self.root / 'checkpoint-hook.py'
+ hook.write_text('''import builtins, importlib.util, sys
+from pathlib import Path
+source, root = Path(sys.argv[1]), Path(sys.argv[2])
+spec = importlib.util.spec_from_file_location('checkpoint_system', source)
+system = importlib.util.module_from_spec(spec)
+spec.loader.exec_module(system)
+system.Path = lambda path: root / 'install.json' if str(path) == '/var/lib/harness-os/install.json' else Path(path)
+original_open = builtins.open
+def fixture_open(path, *args, **kwargs):
+ return original_open(root / 'operation.lock' if str(path) == '/run/lock/hn-os.lock' else path, *args, **kwargs)
+builtins.open = fixture_open
+system.os.geteuid = lambda: 0
+sys.argv = [str(source), 'checkpoint', '--pacman-hook']
+system.main()
+''')
+ def pacman(argv, **kwargs):
+ self.assertEqual(argv, ['pacman', '-Syyu'])
+ self.assertEqual(system.pending_update()['checkpoint'], 'before-update-123')
+ result = self.run([sys.executable, str(hook), str(Path(system.__file__).resolve()), str(self.root)],
+ capture_output=True, text=True, timeout=10, **kwargs)
+ self.assertEqual(result.returncode, 0, result.stderr)
+ return subprocess.CompletedProcess(argv, 0)
+ self.pacman.side_effect = pacman
+ environment = dict(os.environ)
+ environment.pop('HN_OS_UPDATE_CHECKPOINT', None)
+ with patch.dict(os.environ, environment, clear=True), \
+ patch.object(system, 'checkpoint', side_effect=self.save_checkpoint) as save, \
+ (self.root / 'operation.lock').open('w') as lock:
+ fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
+ system.update('2026/10/01')
+ self.assertNotIn('HN_OS_UPDATE_CHECKPOINT', os.environ)
+ save.assert_called_once_with('before-update')
+ self.assertIsNone(system.pending_update())
+
def test_failed_preview_receipt_also_blocks_package_transactions(self):
self.receipt.write_text(json.dumps({'snapshot': '2026/10/01', 'checkpoint': 'before-update-123', 'exit_status': 1}))
with self.assertRaisesRegex(ValueError, 'full system update did not finish'):
diff --git a/os/tests/test_update_retry.py b/os/tests/test_update_retry.py
new file mode 100644
index 000000000..64d757854
--- /dev/null
+++ b/os/tests/test_update_retry.py
@@ -0,0 +1,91 @@
+import copy
+import hashlib
+import importlib.util
+import json
+from pathlib import Path
+import tempfile
+import unittest
+
+spec = importlib.util.spec_from_file_location('public_update_retry', Path(__file__).with_name('update_retry.py'))
+retry = importlib.util.module_from_spec(spec)
+spec.loader.exec_module(retry)
+
+
+class PublicRetryEvidence(unittest.TestCase):
+ def setUp(self):
+ temporary = tempfile.TemporaryDirectory()
+ self.addCleanup(temporary.cleanup)
+ self.root = Path(temporary.name)
+ self.state = Path('var/lib/harness-os')
+ self.probe = Path('usr/share/hn-os-update-probe/value')
+ self.old = '20261005T000000Z-12345678'
+ self.new = '20261005T000001Z-12345678'
+ self.receipt_id = '20261005T000002Z-12345678'
+ self.failed = {'snapshot': '2026/10/01', 'checkpoint': self.old, 'exit_status': 1}
+ self.write(self.state / 'install.json', {'root_uuid': 'private-test-root'})
+ self.write(self.state / 'update.json', dict(self.failed, exit_status=0))
+ self.write(Path('.snapshots') / self.old / 'checkpoint.json',
+ {'root_uuid': 'private-test-root', 'reason': 'before-update'})
+ self.write(Path('.snapshots') / self.new / 'checkpoint.json',
+ {'root_uuid': 'private-test-root', 'reason': 'before-harness-update'})
+ self.write(self.state / 'runtime-updates/latest.json', {'id': self.receipt_id})
+ self.runtime = self.state / 'runtime-updates' / self.receipt_id / 'receipt.json'
+ self.write(self.runtime, {'status': 'applied', 'root_uuid': 'private-test-root', 'checkpoint': self.new})
+ for path, data in [(self.probe, b'2\n'), (Path('.snapshots') / self.old / 'root' / self.probe, b'1\n')]:
+ (self.root / path).parent.mkdir(parents=True, exist_ok=True)
+ (self.root / path).write_bytes(data)
+ self.before = {'failed_update': self.failed, 'root_uuid': 'private-test-root', 'snapshots_before': 0,
+ 'checkpoint_sha256': hashlib.sha256((self.root / '.snapshots' / self.old / 'checkpoint.json').read_bytes()).hexdigest()}
+
+ def write(self, path, data):
+ target = self.root / path
+ target.parent.mkdir(parents=True, exist_ok=True)
+ target.write_text(json.dumps(data))
+
+ def test_completed_public_retry_requires_both_package_versions_and_original_checkpoint(self):
+ result = retry.verify(self.root, self.before)
+ self.assertEqual(result['status'], 'passed')
+ self.assertEqual(result['completed_update']['checkpoint'], self.old)
+ self.assertEqual(result['runtime_update']['checkpoint'], self.new)
+
+ def test_failed_or_replaced_retry_and_changed_installation_cannot_pass(self):
+ for change in [{'exit_status': 1}, {'checkpoint': self.new}, {'snapshot': '2026/10/02'}]:
+ self.write(self.state / 'update.json', dict(self.failed, exit_status=0) | change)
+ with self.subTest(change=change), self.assertRaises(AssertionError):
+ retry.verify(self.root, self.before)
+ self.write(self.state / 'update.json', dict(self.failed, exit_status=0))
+ self.write(self.state / 'install.json', {'root_uuid': 'another-root'})
+ with self.assertRaisesRegex(AssertionError, 'identity changed'):
+ retry.verify(self.root, self.before)
+
+ def test_missing_upgrade_changed_recovery_point_or_duplicate_checkpoint_cannot_pass(self):
+ paths = [(self.probe, b'1\n'), (Path('.snapshots') / self.old / 'root' / self.probe, b'2\n')]
+ for path, bad in paths:
+ original = (self.root / path).read_bytes()
+ (self.root / path).write_bytes(bad)
+ with self.subTest(path=str(path)), self.assertRaises(AssertionError):
+ retry.verify(self.root, self.before)
+ (self.root / path).write_bytes(original)
+ (self.root / '.snapshots/duplicate').mkdir()
+ with self.assertRaisesRegex(AssertionError, 'another Arch checkpoint'):
+ retry.verify(self.root, self.before)
+
+ def test_missing_harness_apply_or_tampered_checkpoint_identity_cannot_pass(self):
+ original = retry.read(self.root / self.runtime)
+ for change in [{'status': 'applying'}, {'checkpoint': self.old}, {'checkpoint': '../escape'}]:
+ self.write(self.runtime, dict(original, **change))
+ with self.subTest(change=change), self.assertRaises(AssertionError):
+ retry.verify(self.root, self.before)
+ self.write(self.runtime, original)
+ for path in ['../escape', '/other-root']:
+ before = copy.deepcopy(self.before)
+ before['failed_update']['checkpoint'] = path
+ with self.subTest(path=path), self.assertRaisesRegex(AssertionError, 'Unsafe checkpoint'):
+ retry.verify(self.root, before)
+ self.before['checkpoint_sha256'] = '0' * 64
+ with self.assertRaises(AssertionError):
+ retry.verify(self.root, self.before)
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/os/tests/update_retry.py b/os/tests/update_retry.py
new file mode 100644
index 000000000..77fa8eb25
--- /dev/null
+++ b/os/tests/update_retry.py
@@ -0,0 +1,62 @@
+#!/usr/bin/env python3
+"""Verify public full-update retry evidence inside the disposable x86 test VM."""
+import argparse
+import hashlib
+import json
+from pathlib import Path
+import re
+import subprocess
+
+
+def read(path):
+ return json.loads(path.read_text())
+
+
+def verify(root, before):
+ failed = before['failed_update']
+ checkpoint = failed['checkpoint']
+ assert re.fullmatch(r'[A-Za-z0-9][A-Za-z0-9_-]{0,79}', checkpoint), 'Unsafe checkpoint identity'
+ assert failed['exit_status'] not in (None, 0), 'The fixture did not record a failed full update'
+ state = root / 'var/lib/harness-os'
+ assert read(state / 'install.json')['root_uuid'] == before['root_uuid'], 'Installation identity changed'
+ completed = read(state / 'update.json')
+ assert completed['exit_status'] == 0, 'The public full-update retry did not finish'
+ assert completed['checkpoint'] == checkpoint, 'Retry replaced the original checkpoint'
+ assert completed['snapshot'] == failed['snapshot'], 'This row must retry the same dated snapshot'
+ saved = root / '.snapshots' / checkpoint
+ metadata = read(saved / 'checkpoint.json')
+ assert metadata['root_uuid'] == before['root_uuid'] and metadata['reason'] == 'before-update'
+ assert hashlib.sha256((saved / 'checkpoint.json').read_bytes()).hexdigest() == before['checkpoint_sha256']
+ probe = Path('usr/share/hn-os-update-probe/value')
+ assert (root / probe).read_bytes() == b'2\n', 'The real package transaction did not install v2'
+ assert (saved / 'root' / probe).read_bytes() == b'1\n', 'The original package recovery point changed'
+ pointer = read(state / 'runtime-updates/latest.json')['id']
+ assert re.fullmatch(r'[0-9TZ-]+-[a-f0-9]{8}', pointer), 'Unsafe runtime receipt identity'
+ runtime = read(state / 'runtime-updates' / pointer / 'receipt.json')
+ assert runtime['status'] == 'applied' and runtime['root_uuid'] == before['root_uuid']
+ assert re.fullmatch(r'[A-Za-z0-9][A-Za-z0-9_-]{0,79}', runtime['checkpoint']), 'Unsafe package checkpoint identity'
+ assert runtime['checkpoint'] != checkpoint, 'Harness package needs its own later checkpoint'
+ later = read(root / '.snapshots' / runtime['checkpoint'] / 'checkpoint.json')
+ assert later['root_uuid'] == before['root_uuid'] and later['reason'] == 'before-harness-update'
+ assert sum(path.is_dir() for path in (root / '.snapshots').iterdir()) == before['snapshots_before'] + 2, \
+ 'Retry created another Arch checkpoint'
+ return {'status': 'passed', 'coverage': 'public retry and original checkpoint preservation',
+ 'root_uuid': before['root_uuid'], 'failed_update': failed, 'completed_update': completed,
+ 'checkpoint_sha256': before['checkpoint_sha256'], 'runtime_update': runtime}
+
+
+def main():
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument('work', type=Path)
+ args = parser.parse_args()
+ assert subprocess.check_output(['lsblk', '-dn', '-o', 'SERIAL', '/dev/vda'], text=True).strip() == 'HN_OS_TEST'
+ assert re.fullmatch(r'/tmp/hn-os-update-check\.[A-Za-z0-9]+', str(args.work)) and not args.work.is_symlink()
+ result = verify(Path('/'), read(args.work / 'retry-before.json'))
+ result['probe_package'] = subprocess.check_output(['pacman', '-Q', 'hn-os-update-probe'], text=True).strip()
+ assert result['probe_package'] == 'hn-os-update-probe 2-1'
+ result['pacman_version'] = subprocess.check_output(['pacman', '--version'], text=True).strip()
+ print(json.dumps(result, indent=2))
+
+
+if __name__ == '__main__':
+ main()
diff --git a/os/tests/update_vm.py b/os/tests/update_vm.py
index de430d016..c272dcb58 100644
--- a/os/tests/update_vm.py
+++ b/os/tests/update_vm.py
@@ -125,6 +125,13 @@ def main():
'sudo -n cat /var/lib/harness-os/runtime-updates/*/receipt.json; '
'tail -n 100 /var/log/pacman.log; hn capture-pane -p -S -200 -t Updates; '
'cat ~/.local/state/harness-os/updates/*.json; hn list-windows -a; hn list-panes -a; '
+ 'systemctl --user show hn-screen.service harness-apply-update.service '
+ '-p Id -p LoadState -p ActiveState -p SubState -p MainPID -p ExecMainStatus; '
+ 'hn hn-list-clients -F "#{client_pid} #{session_id} #{client_tty}"; '
+ 'for hn_update_pid in $(pgrep -u 1000 -x "hn|harness-tui|foot"); do '
+ 'echo "client process $hn_update_pid"; '
+ 'readlink /proc/$hn_update_pid/exe /proc/$hn_update_pid/fd/0; '
+ 'cat /proc/$hn_update_pid/cgroup; done; '
'ps -u 1000 -o pid,ppid,args --width 200', timeout=30, check=False)
(folder / 'update-diagnostics.log').write_text(output)
except Exception:
diff --git a/os/tests/updates.sh b/os/tests/updates.sh
index 2dedb7558..4fd323e80 100644
--- a/os/tests/updates.sh
+++ b/os/tests/updates.sh
@@ -2,6 +2,28 @@
# Run only inside the disposable installed OS used by vm.py.
set -euo pipefail
test -s /var/lib/harness-os/install.json
+test "$(lsblk -dn -o SERIAL /dev/vda)" = HN_OS_TEST
+mode=${1:-complete}
+cleanup() {
+ pacman --noconfirm -R hn-os-update-probe
+ test ! -e /usr/share/hn-os-update-probe/value
+ cp "$work/pacman.conf.before" /etc/pacman.conf
+ rm -f /var/lib/pacman/sync/hn-update-test.db /var/lib/pacman/sync/hn-update-test.db.sig
+ rm -rf "$work"
+}
+case "$mode" in
+ complete|prepare-public) test "$#" -le 1 ;;
+ cleanup-public)
+ test "$#" -eq 2
+ work=$2
+ [[ "$work" =~ ^/tmp/hn-os-update-check\.[A-Za-z0-9]+$ ]]
+ test -d "$work" && test ! -L "$work"
+ test -s "$work/retry-before.json"
+ python3 -c 'import json; assert json.load(open("/var/lib/harness-os/update.json"))["exit_status"] == 0'
+ cleanup
+ exit 0 ;;
+ *) echo 'Expected complete, prepare-public, or cleanup-public WORK.' >&2; exit 2 ;;
+esac
work=$(mktemp -d /tmp/hn-os-update-check.XXXXXX)
cp /etc/pacman.conf "$work/pacman.conf.before"
snapshot=$(python3 -c 'import json; print(json.load(open("/usr/share/harness-os/lock.json"))["arch_snapshot"])')
@@ -42,6 +64,20 @@ cat "$work/blocked-package.log"
grep -F 'full system update did not finish' "$work/blocked-package.log"
test "$(cat /usr/share/hn-os-update-probe/value)" = 1
test "$(find /.snapshots -mindepth 1 -maxdepth 1 -type d | wc -l)" -eq "$((before + 1))"
+if test "$mode" = prepare-public; then
+ python3 - "$work" "$before" <<'PY'
+import hashlib, json, sys
+from pathlib import Path
+receipt = json.loads(Path('/var/lib/harness-os/update.json').read_text())
+checkpoint = Path('/.snapshots') / receipt['checkpoint'] / 'checkpoint.json'
+state = {'failed_update': receipt, 'snapshots_before': int(sys.argv[2]),
+ 'root_uuid': json.loads(Path('/var/lib/harness-os/install.json').read_text())['root_uuid'],
+ 'checkpoint_sha256': hashlib.sha256(checkpoint.read_bytes()).hexdigest()}
+(Path(sys.argv[1]) / 'retry-before.json').write_text(json.dumps(state, indent=2) + '\n')
+PY
+ printf 'HN_PUBLIC_RETRY_STATE=%s\n' "$work"
+ exit 0
+fi
# A real full upgrade must pass its own pre-transaction hook while holding the
# system lock, reuse the original checkpoint, and clear the incomplete state.
printf 'y\n' | hn-os update --snapshot "$snapshot"
@@ -53,9 +89,5 @@ PY
test "$(cat /usr/share/hn-os-update-probe/value)" = 2
test "$(cat "/.snapshots/$checkpoint/root/usr/share/hn-os-update-probe/value")" = 1
test "$(find /.snapshots -mindepth 1 -maxdepth 1 -type d | wc -l)" -eq "$((before + 1))"
-pacman --noconfirm -R hn-os-update-probe
-test ! -e /usr/share/hn-os-update-probe/value
-cp "$work/pacman.conf.before" /etc/pacman.conf
-rm -f /var/lib/pacman/sync/hn-update-test.db /var/lib/pacman/sync/hn-update-test.db.sig
-rm -rf "$work"
+cleanup
echo 'Failed full update blocked packages; retry upgraded successfully and retained the original checkpoint.'