diff --git a/core/recovery.py b/core/recovery.py index d41daec..d61699a 100644 --- a/core/recovery.py +++ b/core/recovery.py @@ -11,6 +11,9 @@ from core.compatibility import ( from core.locking import DeploymentLock +ROLLBACK_ELIGIBLE_PHASES = frozenset({"ACTIVATING", "ACTIVATED", "FAILED"}) + + class RecoveryError(RuntimeError): """Recovery safety could not be established.""" @@ -140,11 +143,7 @@ class RecoveryPlanner: ) require( - transaction["phase"] in ( - "ACTIVATING", - "ACTIVATED", - "FAILED", - ), + transaction["phase"] in ROLLBACK_ELIGIBLE_PHASES, "Transaction phase does not permit rollback review", ) diff --git a/core/rollback_archive.py b/core/rollback_archive.py index 5366f15..bad595d 100644 --- a/core/rollback_archive.py +++ b/core/rollback_archive.py @@ -18,6 +18,7 @@ from core.compatibility import ( validate_provenance, ) from core.locking import DeploymentLock +from core.recovery import ROLLBACK_ELIGIBLE_PHASES class RollbackArchiveReader: @@ -65,10 +66,10 @@ class RollbackArchiveReader: ) require( - transaction["phase"] == "FAILED" + transaction["phase"] in ROLLBACK_ELIGIBLE_PHASES and intent["status"] == "ROLLBACK_INTENT" and result["status"] == "ROLLED_BACK", - "Completed failed-transaction rollback required", + "Completed rollback of a failed or interrupted activation required", ) phases = { @@ -195,9 +196,9 @@ class RollbackArchive(RollbackArchiveReader): require( transaction is not None - and transaction["phase"] == "FAILED" + and transaction["phase"] in ROLLBACK_ELIGIBLE_PHASES and transaction["site"] == self.planner.site, - "Failed deployment transaction required", + "Failed or interrupted deployment transaction required", ) identifier = transaction["transaction_id"] @@ -339,6 +340,7 @@ class RollbackArchive(RollbackArchiveReader): ) from None def _execute(self, confirmation, fingerprint): + self.journal.require_writes_allowed() plan = self._plan() require( diff --git a/docs/TRUSTED-RETENTION.md b/docs/TRUSTED-RETENTION.md index f1623bf..d5e7744 100644 --- a/docs/TRUSTED-RETENTION.md +++ b/docs/TRUSTED-RETENTION.md @@ -16,6 +16,8 @@ supply `--site` with the configured site identifier. Each archive must pass the framework's transaction and backup evidence validation. When the live journal is absent, exactly one archived verified transaction must match the active release. Rollback archives also require their original rollback and -backup evidence and the failed release's matching quarantine marker. All +backup evidence and the failed release's matching quarantine marker. A +verified rollback can retire a journal left in an interrupted activation +phase while preserving its original history. All releases referenced by validated archives are protected. Unknown, ambiguous or corrupt archive entries deny planning pending review. diff --git a/tests/unit/test_recovery.py b/tests/unit/test_recovery.py index 7694055..59d1873 100644 --- a/tests/unit/test_recovery.py +++ b/tests/unit/test_recovery.py @@ -120,7 +120,7 @@ class RecoveryTests(unittest.TestCase): runtime_check=self.runtime_check, ) - def prepare(self, failed=True): + def prepare(self, failed=True, *, interrupted=False): transaction = self.journal.create( "example-absdev-net", @@ -150,6 +150,8 @@ class RecoveryTests(unittest.TestCase): ) self.releases.activate("2") + if interrupted: + return transaction transaction = self.journal.transition( transaction["transaction_id"], diff --git a/tests/unit/test_rollback_archive.py b/tests/unit/test_rollback_archive.py index a71655e..c9d104e 100644 --- a/tests/unit/test_rollback_archive.py +++ b/tests/unit/test_rollback_archive.py @@ -4,6 +4,7 @@ import unittest from unittest.mock import Mock, patch import test_rollback +import test_recovery from core.rollback_archive import RollbackArchive, ArchiveError from core.compatibility import canonical_hash @@ -250,3 +251,41 @@ class RollbackArchiveTests(unittest.TestCase): if __name__ == "__main__": unittest.main() + + +class InterruptedRollbackArchiveTests(unittest.TestCase): + def test_confirmed_rollback_can_archive_interrupted_activation(self): + prepare = test_recovery.RecoveryTests.prepare + + def interrupted(recovery): + return prepare(recovery, interrupted=True) + + fixture = RollbackArchiveTests() + self.addCleanup(fixture.doCleanups) + with patch.object(test_recovery.RecoveryTests, "prepare", interrupted): + fixture.setUp() + self.assertEqual(fixture.transaction["phase"], "ACTIVATING") + result = fixture.execute() + self.assertEqual(result["transaction"]["phase"], "ACTIVATING") + self.assertIsNone(fixture.journal.load()) + self.assertTrue(fixture.marker.is_file()) + self.assertEqual(fixture.archive.load(fixture.identifier), result) + + + def test_unfinished_migration_blocks_rollback_journal_retirement(self): + from core.approval_store import ApprovalStore + + fixture = RollbackArchiveTests() + self.addCleanup(fixture.doCleanups) + fixture.setUp() + approvals = fixture.recovery.root / "approvals" + approvals.mkdir(mode=0o700, exist_ok=True) + fixture.journal.bind_migration_guard(ApprovalStore(approvals)) + blocker = approvals / ("b" * 32 + ".migration-intent.json") + blocker.write_text("{}") + before = fixture.journal.path.read_bytes() + with self.assertRaises(ArchiveError): + fixture.execute() + self.assertEqual(fixture.journal.path.read_bytes(), before) + self.assertFalse(fixture.marker.exists()) + self.assertFalse(fixture.archive.path(fixture.identifier).exists())