fix(editorial): upgrade only verified installed runtime payload
Deploy Canina / deploy (push) Failing after 2m51s

This commit is contained in:
Canina Engineering committed 2026-10-25 07:39:22 +00:00
1 parent f94ef580fb
commit cc0358bb12
4 files changed
+129 -4

No files matched your search

+25 -4
View File
@@ -99,19 +99,40 @@ def execute(action, home):
changes = [r for r, p in paths.items() if not p.exists() or p.read_bytes() != files[r]]
if not changes:
return {'status': 'unchanged', 'files': len(files)}
if journal.exists():
raise ValueError('existing transaction: rollback before upgrade or recovery')
previous_journal = journal.read_bytes() if journal.exists() else None
previous_state = None
if previous_journal is not None:
previous_state = json.loads(previous_journal)
if (set(previous_state) != {'before', 'installed'} or
set(previous_state['before']) != ALLOWED or
set(previous_state['installed']) != ALLOWED):
raise ValueError('invalid upgrade journal allowlist')
for r, p in paths.items():
old = previous_state['before'][r]
if old is not None:
if (set(old) != {'data', 'mode'} or type(old['mode']) is not int or
not 0 <= old['mode'] <= 0o777):
raise ValueError('invalid upgrade rollback entry')
base64.b64decode(old['data'], validate=True)
# Only a completely installed, verified previous transaction may
# advance. Never recover partial writes or overwrite live drift.
if not p.exists() or digest(p.read_bytes()) != previous_state['installed'][r]:
raise ValueError(f'existing transaction conflict: {r}')
before = {r: {'data': base64.b64encode(p.read_bytes()).decode(),
'mode': p.stat().st_mode & 0o777} if p.exists() else None
for r, p in paths.items()}
state = {'before': before, 'installed': {r: digest(v) for r, v in files.items()}}
state = {'before': previous_state['before'] if previous_state else before,
'installed': {r: digest(v) for r, v in files.items()}}
atomic(journal, json.dumps(state, sort_keys=True).encode())
try:
for r in changes:
atomic(paths[r], files[r])
except BaseException:
restore(paths, before)
journal.unlink()
if previous_journal is None:
journal.unlink()
else:
atomic(journal, previous_journal)
raise
return {'status': 'installed_config_only', 'files': len(files)}
@@ -41,6 +41,95 @@ class InstallerTest(unittest.TestCase):
self.assertEqual(status['stage'], 'blocked')
self.assertFalse(status['cron_activation_allowed'])
def test_verified_previous_payload_can_upgrade_without_losing_rollback(self):
import hashlib
path = self.home / 'profiles/pmo/skills/canina-editorial-v4/SKILL.md'
path.parent.mkdir(parents=True)
path.write_text('original owned content')
path.chmod(0o600)
self.assertEqual(self.run_cli('install').returncode, 0)
journal = self.home / 'editorial-v4/.installer/transaction.json'
state = json.loads(journal.read_text())
original_before = state['before']
# CI1285: live payload exactly matches the previous journal, while
# the incoming reviewed package changes the PMO skill only.
path.write_text('previous reviewed PMO skill')
state['installed'][str(path.relative_to(self.home))] = hashlib.sha256(path.read_bytes()).hexdigest()
journal.write_text(json.dumps(state))
result = self.run_cli('install')
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(self.run_cli('verify').returncode, 0)
self.assertEqual(json.loads(journal.read_text())['before'], original_before)
snapshot = {str(p.relative_to(self.home)): (p.read_bytes(), p.stat().st_mtime_ns,
p.stat().st_uid, p.stat().st_gid, p.stat().st_mode)
for p in self.home.rglob('*') if p.is_file()}
self.assertEqual(self.run_cli('install').returncode, 0)
self.assertEqual(snapshot, {str(p.relative_to(self.home)): (p.read_bytes(), p.stat().st_mtime_ns,
p.stat().st_uid, p.stat().st_gid, p.stat().st_mode)
for p in self.home.rglob('*') if p.is_file()})
self.assertEqual(self.run_cli('rollback').returncode, 0)
self.assertEqual(path.read_text(), 'original owned content')
self.assertEqual(path.stat().st_mode & 0o777, 0o600)
def test_failed_upgrade_restores_previous_payload_and_exact_journal(self):
import hashlib
import importlib.util
from unittest.mock import patch
spec = importlib.util.spec_from_file_location('installer', SCRIPT)
installer = importlib.util.module_from_spec(spec)
spec.loader.exec_module(installer)
installer.execute('install', self.home)
path = self.home / 'profiles/pmo/skills/canina-editorial-v4/SKILL.md'
path.write_text('previous reviewed version')
journal = self.home / 'editorial-v4/.installer/transaction.json'
state = json.loads(journal.read_text())
state['installed'][str(path.relative_to(self.home))] = hashlib.sha256(path.read_bytes()).hexdigest()
journal.write_text(json.dumps(state))
original = journal.read_bytes()
real_atomic = installer.atomic
failed = False
def fail_once(destination, data):
nonlocal failed
if destination == path and not failed:
failed = True
raise OSError('injected write failure')
return real_atomic(destination, data)
with patch.object(installer, 'atomic', side_effect=fail_once):
with self.assertRaisesRegex(OSError, 'injected write failure'):
installer.execute('install', self.home)
self.assertEqual(path.read_text(), 'previous reviewed version')
self.assertTrue(journal.exists(), 'rollback history must survive a failed upgrade')
self.assertEqual(journal.read_bytes(), original)
def test_upgrade_refuses_drift_partial_and_invalid_journal_without_writes(self):
self.assertEqual(self.run_cli('install').returncode, 0)
journal = self.home / 'editorial-v4/.installer/transaction.json'
original = journal.read_bytes()
path = self.home / 'profiles/pmo/skills/canina-editorial-v4/SKILL.md'
payload = path.read_bytes()
for failure in ('drift', 'missing', 'allowlist', 'rollback_data'):
with self.subTest(failure=failure):
path.write_bytes(payload)
journal.write_bytes(original)
state = json.loads(original)
if failure == 'drift':
path.write_text('external change')
elif failure == 'missing':
path.unlink()
elif failure == 'allowlist':
path.write_text('previous version')
state['installed'].pop('editorial-v4/status.json')
journal.write_text(json.dumps(state))
else:
path.write_text('previous version')
state['before']['editorial-v4/status.json'] = {'data': '!!!!', 'mode': 420}
journal.write_text(json.dumps(state))
before = {str(p.relative_to(self.home)): (p.read_bytes(), p.stat().st_mtime_ns)
for p in self.home.rglob('*') if p.is_file()}
self.assertNotEqual(self.run_cli('install').returncode, 0)
self.assertEqual(before, {str(p.relative_to(self.home)): (p.read_bytes(), p.stat().st_mtime_ns)
for p in self.home.rglob('*') if p.is_file()})
def test_rollback_restores_previous_files_and_modes(self):
path = self.home / 'profiles/pmo/skills/canina-editorial-v4/SKILL.md'
path.parent.mkdir(parents=True)
+3
View File
@@ -15,6 +15,9 @@ ALLOWED = {'nginx.conf', 'Dockerfile', 'backend/Dockerfile', '.dockerignore',
'scripts/provision-revalidation-env.py', 'scripts/tests/test_revalidation_runtime.py',
'scripts/tests/test_ci_host_boundary.py', 'scripts/tests/test_stage_provision_wiring.py',
'scripts/tests/test_stage_rollout.py',
# CI1285 verified-payload upgrade repair; no model, cron or payload wildcard.
'infra/hermes-editorial/installer.py',
'infra/hermes-editorial/tests/test_installer.py',
'backend/src/common/revalidation/revalidation.service.ts',
'backend/src/common/revalidation/revalidation.service.spec.ts',
'backend/src/orchestration-worker/worker-registration.spec.ts',
+12
View File
@@ -26,6 +26,18 @@ class ProductionGuards(unittest.TestCase):
r['paths']=[path]
with self.assertRaises(ValueError): m.validate_receipt(r,'a'*40,'b'*40,[path])
def test_exact_editorial_installer_repair_paths_allowed_only(self):
paths = ['infra/hermes-editorial/installer.py',
'infra/hermes-editorial/tests/test_installer.py']
r = self.receipt(); r['paths'] = paths
m.validate_receipt(r, 'a'*40, 'b'*40, paths)
for path in ['infra/hermes-editorial/model-policy.json',
'infra/hermes-editorial/payload/editorial-v4/status.json',
'infra/hermes-editorial/other.py']:
r['paths'] = [path]
with self.assertRaises(ValueError):
m.validate_receipt(r, 'a'*40, 'b'*40, [path])
def test_exact_stage_sha(self):
r=self.receipt()
m.validate_receipt(r, 'a'*40, 'b'*40, ['nginx.conf'])