Plan retention from canonical live release and transaction state

This commit is contained in:
Mark Gregory
2026-11-30 09:50:47 +00:00
parent b0495d80f1
commit 2589ae8510
3 changed files with 71 additions and 5 deletions
+47
View File
@@ -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