Accept validated retired deployment archives for retention planning

This commit is contained in:
Mark Gregory
2026-11-30 09:54:40 +00:00
parent 2589ae8510
commit 404ab75905
4 changed files with 90 additions and 7 deletions
+4 -3
View File
@@ -229,6 +229,7 @@ def parser():
retention_source.add_argument("--policy") retention_source.add_argument("--policy")
retention_source.add_argument("--runtime-root") retention_source.add_argument("--runtime-root")
retention.add_argument("--keep", type=int) retention.add_argument("--keep", type=int)
retention.add_argument("--site")
provision = commands.add_parser( provision = commands.add_parser(
"provision-plan", "provision-plan",
@@ -270,10 +271,10 @@ def main(argv=None):
elif args.command == "retention-plan": elif args.command == "retention-plan":
if args.runtime_root: 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: else:
if args.keep is not None: if args.keep is not None or args.site is not None:
raise InspectionError("--keep requires --runtime-root") raise InspectionError("--keep and --site require --runtime-root")
report = plan_retention(read_json(args.policy)) report = plan_retention(read_json(args.policy))
elif args.command == "provision-plan": elif args.command == "provision-plan":
+29 -2
View File
@@ -191,11 +191,13 @@ def plan_retention(policy):
return plan 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.""" """Read canonical runtime state under its deployment and journal locks."""
from pathlib import Path from pathlib import Path
from core.approval_store import ApprovalStore from core.approval_store import ApprovalStore
from core.archive import TransactionArchive
from core.backup_evidence import BackupEvidence
from core.locking import DeploymentLock from core.locking import DeploymentLock
from core.operation_guard import OperationGuard from core.operation_guard import OperationGuard
from core.releases import ReleaseManager from core.releases import ReleaseManager
@@ -213,7 +215,32 @@ def plan_site_retention(runtime_root, *, keep):
releases = ReleaseManager(root / "app") releases = ReleaseManager(root / "app")
inventory = releases.inventory() inventory = releases.inventory()
current = releases.current() 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"]] references = [transaction["previous"], transaction["candidate"]]
protected.update(references)
require(current in references, "Live active release differs from the transaction") require(current in references, "Live active release differs from the transaction")
unresolved = ( unresolved = (
transaction["phase"] != "VERIFIED" transaction["phase"] != "VERIFIED"
@@ -224,7 +251,7 @@ def plan_site_retention(runtime_root, *, keep):
"inventory": inventory, "inventory": inventory,
"current": current, "current": current,
"keep": keep, "keep": keep,
"protected": references, "protected": sorted(protected),
"failed": [], "failed": [],
"unresolved": inventory if unresolved else [], "unresolved": inventory if unresolved else [],
}) })
+9 -2
View File
@@ -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, retains all releases while a deployment or migration is unresolved. Invalid,
missing, or inconsistent source state denies planning. 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. The existing `--policy` command evaluates caller-supplied data as a simulation.
Both outputs are read-only. They grant no archival or deletion authority. 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.
+48
View File
@@ -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()