From 2589ae85103d42c2e59ce42e7455a6f8ac09ca83 Mon Sep 17 00:00:00 2001 From: Mark Gregory Date: Mon, 30 Nov 2026 09:50:47 +0000 Subject: [PATCH] Plan retention from canonical live release and transaction state --- core/inspection_cli.py | 16 ++++++++----- core/retention.py | 47 +++++++++++++++++++++++++++++++++++++++ docs/TRUSTED-RETENTION.md | 13 +++++++++++ 3 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 docs/TRUSTED-RETENTION.md diff --git a/core/inspection_cli.py b/core/inspection_cli.py index e8e1e29..a715d10 100644 --- a/core/inspection_cli.py +++ b/core/inspection_cli.py @@ -10,7 +10,7 @@ from pathlib import Path from core.configuration import load_config, validate_config from core.compatibility import canonical_hash from core.releases import ReleaseManager -from core.retention import plan_retention +from core.retention import plan_retention, plan_site_retention from core.site_policy import validate_site, validate_registry from core.preflight import site_preflight @@ -225,7 +225,10 @@ def parser(): "retention-plan", help="Evaluate a supplied retention policy", ) - retention.add_argument("--policy", required=True) + retention_source = retention.add_mutually_exclusive_group(required=True) + retention_source.add_argument("--policy") + retention_source.add_argument("--runtime-root") + retention.add_argument("--keep", type=int) provision = commands.add_parser( "provision-plan", @@ -266,9 +269,12 @@ def main(argv=None): report = inventory_report(args.app) elif args.command == "retention-plan": - report = plan_retention( - read_json(args.policy) - ) + if args.runtime_root: + report = plan_site_retention(args.runtime_root, keep=args.keep) + else: + if args.keep is not None: + raise InspectionError("--keep requires --runtime-root") + report = plan_retention(read_json(args.policy)) elif args.command == "provision-plan": report = provisioning_plan( diff --git a/core/retention.py b/core/retention.py index 7924a5a..efddd01 100644 --- a/core/retention.py +++ b/core/retention.py @@ -189,3 +189,50 @@ def plan_retention(policy): plan["fingerprint"] = canonical_hash(plan) return plan + + +def plan_site_retention(runtime_root, *, keep): + """Read canonical runtime state under its deployment and journal locks.""" + from pathlib import Path + + from core.approval_store import ApprovalStore + from core.locking import DeploymentLock + from core.operation_guard import OperationGuard + from core.releases import ReleaseManager + from core.transactions import TransactionJournal + + root = Path(runtime_root) + require(root.is_absolute() and root.resolve() == root, "Invalid retention runtime directory") + try: + journal = TransactionJournal(root / "state") + with DeploymentLock(journal.directory / ".deployment.lock"), DeploymentLock(journal.lock_path): + approvals = journal.migration_guard_directory() + require(approvals == root / "approvals", "Missing trusted migration guard binding") + guard = OperationGuard(ApprovalStore(approvals)).inspect() + transaction = journal.load() + releases = ReleaseManager(root / "app") + inventory = releases.inventory() + current = releases.current() + references = [transaction["previous"], transaction["candidate"]] + require(current in references, "Live active release differs from the transaction") + unresolved = ( + transaction["phase"] != "VERIFIED" + or current != transaction["candidate"] + or guard["status"] != "CLEAR" + ) + plan = plan_retention({ + "inventory": inventory, + "current": current, + "keep": keep, + "protected": references, + "failed": [], + "unresolved": inventory if unresolved else [], + }) + del plan["fingerprint"] + plan.update(inventory_source="site_runtime", journal_checksum=transaction["checksum"]) + plan["fingerprint"] = canonical_hash(plan) + return plan + except RetentionError: + raise + except Exception: + raise RetentionError("Unable to verify trusted retention inventory") from None diff --git a/docs/TRUSTED-RETENTION.md b/docs/TRUSTED-RETENTION.md new file mode 100644 index 0000000..6b7188c --- /dev/null +++ b/docs/TRUSTED-RETENTION.md @@ -0,0 +1,13 @@ +# Trusted retention planning + +Run `bin/absdev-deploy retention-plan --runtime-root /canonical/site-runtime --keep 5` +as the owner of an existing ABSDEV site runtime. The inspector reads the +canonical release tree, current link, checksummed transaction journal and +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.