feat: preserve failed APT history for one signed UI-approved recovery
Rust development slice / linux (push) Failing after 6s
Rust development slice / linux (push) Failing after 6s
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
"""Signed retry checks on temporary files; no native package execution."""
|
||||
import copy
|
||||
from dataclasses import replace
|
||||
from pathlib import Path
|
||||
import tempfile
|
||||
import uuid
|
||||
from unittest import TestCase
|
||||
from unittest.mock import patch
|
||||
|
||||
from test_guard import apt
|
||||
from test_native_acceptance import context, fixture_transaction, action_manifest, signed_job
|
||||
|
||||
|
||||
class NativeRetryTests(TestCase):
|
||||
def setup_history(self, base):
|
||||
active = base / 'active'
|
||||
active.mkdir()
|
||||
old = replace(context(), grant_id=str(uuid.uuid4()), attempt_id=str(uuid.uuid4()))
|
||||
previous = base / 'history' / old.grant_id
|
||||
previous.mkdir(parents=True)
|
||||
obj = fixture_transaction()
|
||||
obj['baseline_sha256'] = apt.digest(apt.canonical({}))
|
||||
tx = apt.ResolvedAptTransaction.parse(obj)
|
||||
txbytes = apt.canonical(obj)
|
||||
old = replace(old, transaction_sha256=apt.digest(txbytes))
|
||||
old_action = action_manifest(old, obj, {})
|
||||
old_job = apt.canonical(signed_job(old, old_action))
|
||||
old = replace(old, job_payload_sha256=apt.digest(old_job))
|
||||
files = {'context.json': apt.canonical(old.__dict__), 'signed-action.json': old_job,
|
||||
'expectation.json': txbytes, 'baseline.json': apt.canonical({}),
|
||||
'launch.json': apt.canonical({'transaction_sha256': old.transaction_sha256,
|
||||
'effect_armed_ledger_id': old.effect_armed_ledger_id}),
|
||||
'sandbox-result.json': apt.canonical({'transaction_sha256': old.transaction_sha256,
|
||||
'apt_exit_code': 100, 'reboot_required': False})}
|
||||
for name, raw in files.items():
|
||||
(previous / name).write_bytes(raw)
|
||||
original = base / ('bootstrap-attempt-' + tx.recipe_id + '.json')
|
||||
original.write_bytes(apt.canonical(apt.attempt_binding(old)))
|
||||
new = replace(context(), grant_id=str(uuid.uuid4()), attempt_id=str(uuid.uuid4()),
|
||||
job_id='new-job', authorization_id='new-approval', plan_sha256='d' * 64,
|
||||
transaction_sha256=old.transaction_sha256)
|
||||
action = action_manifest(new, obj, {})
|
||||
action.update(schema_version=3, native_retry={
|
||||
'schema_version': 1, 'previous_grant_id': old.grant_id,
|
||||
'previous_attempt_id': old.attempt_id,
|
||||
'bootstrap_marker_sha256': apt.digest(original.read_bytes())})
|
||||
job = apt.canonical(signed_job(new, action))
|
||||
new = replace(new, job_payload_sha256=apt.digest(job))
|
||||
(active / 'baseline.json').write_bytes(apt.canonical({}))
|
||||
(active / 'signed-action.json').write_bytes(job)
|
||||
(active / 'expectation.json').write_bytes(txbytes)
|
||||
return tx, new, action, old, previous, original
|
||||
|
||||
def scenario(self, variation=None):
|
||||
with tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp:
|
||||
base = Path(temp)
|
||||
with patch.object(apt, 'BASE', base), patch.object(apt, 'ACTIVE', base / 'active'), \
|
||||
patch.object(apt, 'secure_path', side_effect=lambda path, **kwargs: path), \
|
||||
patch.object(apt, 'sealed_read', side_effect=lambda path, *args:
|
||||
b'code' if path == apt.CODE else apt.hook_script()
|
||||
if path == Path(apt.HOOK) else path.read_bytes()):
|
||||
tx, new, action, old, previous, original = self.setup_history(base)
|
||||
original_bytes = original.read_bytes()
|
||||
if variation == 'armed':
|
||||
(previous / 'hook-armed.json').write_bytes(b'{}')
|
||||
elif variation == 'dangling-hook':
|
||||
(previous / 'hook-armed.json').symlink_to(previous / 'missing')
|
||||
elif variation == 'marker-hash':
|
||||
action['native_retry']['bootstrap_marker_sha256'] = '0' * 64
|
||||
elif variation == 'same-attempt':
|
||||
new = replace(new, attempt_id=old.attempt_id)
|
||||
elif variation == 'same-approval':
|
||||
new = replace(new, authorization_id=old.authorization_id)
|
||||
elif variation == 'baseline':
|
||||
(apt.ACTIVE / 'baseline.json').write_bytes(b'{"changed":true}')
|
||||
elif variation == 'partial-result':
|
||||
(previous / 'sandbox-result.json').write_bytes(b'{')
|
||||
elif variation == 'other-exit':
|
||||
(previous / 'sandbox-result.json').write_bytes(apt.canonical({
|
||||
'transaction_sha256': old.transaction_sha256,
|
||||
'apt_exit_code': 0, 'reboot_required': False}))
|
||||
elif variation == 'old-payload':
|
||||
(previous / 'signed-action.json').write_bytes(b'{}')
|
||||
elif variation == 'accepted':
|
||||
apt.record_path(tx).write_bytes(b'{}')
|
||||
if variation is not None:
|
||||
with self.assertRaises((apt.Veto, OSError, ValueError)):
|
||||
apt.bootstrap_binding(tx, new, action)
|
||||
self.assertEqual(original.read_bytes(), original_bytes)
|
||||
return
|
||||
# Full signed-loader path accepts schema3 only with bound data.
|
||||
self.assertEqual(apt.load_signed_action(tx, new), action)
|
||||
path, binding = apt.bootstrap_binding(tx, new, action)
|
||||
self.assertEqual(path.name, 'bootstrap-retry-' + tx.recipe_id + '.json')
|
||||
self.assertEqual(binding['native_retry'], action['native_retry'])
|
||||
apt.durable_once(path, binding)
|
||||
apt.check_bootstrap_attempt(tx, new, action)
|
||||
with self.assertRaisesRegex(apt.Veto, 'ALREADY_HANDED_OFF'):
|
||||
apt.durable_once(path, binding)
|
||||
with self.assertRaisesRegex(apt.Veto, 'BOOTSTRAP_ATTEMPT_BINDING'):
|
||||
apt.check_bootstrap_attempt(tx, replace(new, grant_id=str(uuid.uuid4())), action)
|
||||
self.assertEqual(original.read_bytes(), original_bytes)
|
||||
|
||||
def test_single_new_approved_attempt_preserves_original_tombstone(self):
|
||||
self.scenario()
|
||||
|
||||
def test_missing_or_changed_known_no_effect_evidence_vetoes_retry(self):
|
||||
for variation in ('armed', 'dangling-hook', 'marker-hash', 'same-attempt', 'same-approval',
|
||||
'baseline', 'partial-result', 'other-exit', 'old-payload', 'accepted'):
|
||||
with self.subTest(variation=variation):
|
||||
self.scenario(variation)
|
||||
|
||||
def test_retry_reference_has_strict_schema_and_canonical_uuid_paths(self):
|
||||
valid = {'schema_version': 1, 'previous_grant_id': str(uuid.uuid4()),
|
||||
'previous_attempt_id': str(uuid.uuid4()), 'bootstrap_marker_sha256': 'a' * 64}
|
||||
apt.validate_retry_reference(valid)
|
||||
for key, value in (('schema_version', True), ('previous_grant_id', '../active'),
|
||||
('previous_attempt_id', 'not-a-uuid'), ('bootstrap_marker_sha256', 'x'),
|
||||
('unknown', 'field')):
|
||||
altered = copy.deepcopy(valid)
|
||||
altered[key] = value
|
||||
with self.subTest(key=key), self.assertRaises((apt.Veto, ValueError)):
|
||||
apt.validate_retry_reference(altered)
|
||||
Reference in New Issue
Block a user