From 404ab75905d234f1a740ad06c39cb9df8a307345 Mon Sep 17 00:00:00 2001 From: Mark Gregory Date: Mon, 30 Nov 2026 09:54:05 +0000 Subject: [PATCH] Accept validated retired deployment archives for retention planning --- core/inspection_cli.py | 7 ++-- core/retention.py | 31 ++++++++++++++++-- docs/TRUSTED-RETENTION.md | 11 +++++-- tests/unit/test_retention_archive.py | 48 ++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+), 7 deletions(-) create mode 100644 tests/unit/test_retention_archive.py diff --git a/core/inspection_cli.py b/core/inspection_cli.py index a715d10..4d2446c 100644 --- a/core/inspection_cli.py +++ b/core/inspection_cli.py @@ -229,6 +229,7 @@ def parser(): retention_source.add_argument("--policy") retention_source.add_argument("--runtime-root") retention.add_argument("--keep", type=int) + retention.add_argument("--site") provision = commands.add_parser( "provision-plan", @@ -270,10 +271,10 @@ def main(argv=None): elif args.command == "retention-plan": if args.runtime_root: - report = plan_site_retention(args.runtime_root, keep=args.keep) + report = plan_site_retention(args.runtime_root, keep=args.keep, site=args.site) else: - if args.keep is not None: - raise InspectionError("--keep requires --runtime-root") + if args.keep is not None or args.site is not None: + raise InspectionError("--keep and --site require --runtime-root") report = plan_retention(read_json(args.policy)) elif args.command == "provision-plan": diff --git a/core/retention.py b/core/retention.py index efddd01..7307cab 100644 --- a/core/retention.py +++ b/core/retention.py @@ -191,11 +191,13 @@ def plan_retention(policy): return plan -def plan_site_retention(runtime_root, *, keep): +def plan_site_retention(runtime_root, *, keep, site=None): """Read canonical runtime state under its deployment and journal locks.""" from pathlib import Path from core.approval_store import ApprovalStore + from core.archive import TransactionArchive + from core.backup_evidence import BackupEvidence from core.locking import DeploymentLock from core.operation_guard import OperationGuard from core.releases import ReleaseManager @@ -213,7 +215,32 @@ def plan_site_retention(runtime_root, *, keep): releases = ReleaseManager(root / "app") inventory = releases.inventory() current = releases.current() + protected = set() + archive_directory = root / "archive" + if archive_directory.exists() or archive_directory.is_symlink(): + require(isinstance(site, str) and re.fullmatch(r"[a-z0-9][a-z0-9-]{0,62}", site), + "Configured site identity required for retention archives") + archives = TransactionArchive( + site=site, directory=archive_directory, + lock_path=journal.directory / ".deployment.lock", journal=journal, + releases=releases, provenance=None, backup_evidence=BackupEvidence(journal.directory), + backup_verify=None, health_probe=None, + ) + matching = [] + for path in archive_directory.iterdir(): + require(re.fullmatch(r"[a-f0-9]{32}\.archive\.json", path.name), + "Unexpected retention archive entry") + archived = archives.load(path.name[:32])["transaction"] + protected.update((archived["previous"], archived["candidate"])) + if archived["candidate"] == current: + matching.append(archived) + if transaction is None: + require(len(matching) == 1, "Active release has no unambiguous verified archive") + transaction = matching[0] + require(transaction is not None, "No verified retention transaction") + require(site is None or transaction["site"] == site, "Retention transaction belongs to another site") references = [transaction["previous"], transaction["candidate"]] + protected.update(references) require(current in references, "Live active release differs from the transaction") unresolved = ( transaction["phase"] != "VERIFIED" @@ -224,7 +251,7 @@ def plan_site_retention(runtime_root, *, keep): "inventory": inventory, "current": current, "keep": keep, - "protected": references, + "protected": sorted(protected), "failed": [], "unresolved": inventory if unresolved else [], }) diff --git a/docs/TRUSTED-RETENTION.md b/docs/TRUSTED-RETENTION.md index 6b7188c..ac37234 100644 --- a/docs/TRUSTED-RETENTION.md +++ b/docs/TRUSTED-RETENTION.md @@ -7,7 +7,14 @@ migration guard under the deployment and journal locks. It conservatively retains all releases while a deployment or migration is unresolved. Invalid, missing, or inconsistent source state denies planning. -This mode covers the live journal and its migration guard. A site whose -live journal has been retired requires separate archived-state qualification. The existing `--policy` command evaluates caller-supplied data as a simulation. Both outputs are read-only. They grant no archival or deletion authority. + +For an existing runtime with retired deployment journals, place verified +ordinary transaction archives in the canonical `archive/` directory and +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. All releases referenced by validated archives are protected. +Unknown archive entries, rollback archive formats, and ambiguous or corrupt +records deny planning pending review. diff --git a/tests/unit/test_retention_archive.py b/tests/unit/test_retention_archive.py new file mode 100644 index 0000000..6a0a715 --- /dev/null +++ b/tests/unit/test_retention_archive.py @@ -0,0 +1,48 @@ +import unittest + +import test_archive + +from core.retention import RetentionError, plan_site_retention + + +class ArchivedRetentionTests(unittest.TestCase): + def setUp(self): + self.fixture = test_archive.ArchiveTests() + self.addCleanup(self.fixture.doCleanups) + self.fixture.setUp() + self.root = self.fixture.root.resolve() + self.fixture.journal.bind_migration_guard(self.fixture.fixture.approval_store) + directory = self.root / "archive" + self.fixture.directory.rename(directory) + self.fixture.archive.directory = directory + self.fixture.archive.lock_path = self.fixture.journal.directory / ".deployment.lock" + self.record = self.fixture.execute() + self.path = self.fixture.archive.path(self.record["transaction"]["transaction_id"]) + + def plan(self, site="example-absdev-net"): + return plan_site_retention(self.root, keep=2, site=site) + + def test_verified_archive_supplies_retired_journal(self): + before = self.path.read_bytes() + plan = self.plan() + self.assertEqual(plan["status"], "READY") + self.assertEqual(plan["retained"], ["1", "2"]) + self.assertEqual(plan["journal_checksum"], self.record["transaction"]["checksum"]) + self.assertFalse(plan["execution_permitted"]) + self.assertIsNone(self.fixture.journal.load()) + self.assertEqual(before, self.path.read_bytes()) + + def test_missing_or_corrupt_archive_denies_planning(self): + self.path.write_text("{}") + with self.assertRaises(RetentionError): + self.plan() + self.path.unlink() + with self.assertRaises(RetentionError): + self.plan() + + def test_wrong_site_or_unrecognised_archive_denies_planning(self): + with self.assertRaises(RetentionError): + self.plan("other-site") + (self.path.parent / "unknown.json").write_text("{}") + with self.assertRaises(RetentionError): + self.plan()