From 84a7a5b23796b15cab830034aae256b9414e9693 Mon Sep 17 00:00:00 2001 From: Mark Gregory Date: Mon, 30 Nov 2026 09:30:49 +0000 Subject: [PATCH] Record reviewed reconciliation for interrupted migrations --- core/migration_recovery.py | 96 +++++++++++++++++++++++++++++--- core/operator_approval.py | 2 +- docs/MIGRATION-RECONCILIATION.md | 21 +++++++ 3 files changed, 110 insertions(+), 9 deletions(-) create mode 100644 docs/MIGRATION-RECONCILIATION.md diff --git a/core/migration_recovery.py b/core/migration_recovery.py index 5c497ac..baf2103 100644 --- a/core/migration_recovery.py +++ b/core/migration_recovery.py @@ -9,7 +9,7 @@ import os from datetime import datetime, timezone from pathlib import Path -from core.approval import validate_context +from core.approval import matches, validate_context from core.compatibility import canonical_hash from core.locking import DeploymentLock from core.migration import validate_history, validate_original_release @@ -46,11 +46,13 @@ class MigrationRecovery: release_containment, writes_enabled_probe, health_probe, + reconciliation_probe=None, ): self.site = site self.store = store self.lock_path = Path(lock_path) + self.reconciliation_probe = reconciliation_probe callbacks = { "identity_probe": identity_probe, @@ -136,7 +138,7 @@ class MigrationRecovery: "Migration recovery verification failed" ) from None - def _inspect(self, identifier): + def _inspect(self, identifier, *, reconciled=None): approval = self.store.load(identifier) self.store.require_operator_policy().verify_seal(approval) @@ -155,12 +157,13 @@ class MigrationRecovery: self.store.consumed_path(identifier) ) - result = self.read_record( - self.store.path( - identifier, - ".migration-result.json", - ) - ) + migration_path = self.store.path(identifier, ".migration-result.json") + if reconciled is None: + result = self.read_record(migration_path) + else: + require(not os.path.lexists(migration_path), + "Existing migration result requires separate review") + result = reconciled require( intent["status"] == "MIGRATION_INTENT" @@ -323,6 +326,83 @@ class MigrationRecovery: return plan + def _reconciliation_plan(self, identifier, operator, now): + require(callable(self.reconciliation_probe), "Trusted reconciliation probe required") + path = self.store.path(identifier, ".migration-result.json") + require(not os.path.lexists(path), "Existing migration result requires separate review") + require(self.containment_probe() is True, "Write containment is not verified") + + approval = self.store.load(identifier) + policy = self.store.require_operator_policy() + policy.verify_seal(approval) + policy.authorise(dict(approval, approver=operator), "reconcile", now) + context = approval["context"] + before_path = self.store.path(identifier, ".migration-intent.json") + intent = self.read_record(before_path) + consumed = self.read_record(self.store.consumed_path(identifier)) + history = validate_history(self.history_probe()) + result = { + "format_version": 1, + "status": "MIGRATED", + "approval_id": identifier, + "intent_checksum": intent["checksum"], + "consumption_checksum": consumed["checksum"], + "site": context["site"], + "release": context["release"], + "revision": context["revision"], + "database_identity": context["database_identity"], + "history_after": history, + "applied_migrations": list(context["pending_migrations"]), + "automatic_database_rollback": False, + "application_activated": False, + "containment_required": True, + } + # Use the established forward-recovery checks before consulting the + # trusted schema/application probe or allowing a durable result. + result["checksum"] = canonical_hash(result) + self._inspect(identifier, reconciled=result) + evidence = self.reconciliation_probe(context, intent, history) + require(matches(evidence, r"[a-f0-9]{64}"), + "Reconciled database and application state could not be verified") + self._inspect(identifier, reconciled=result) + require(self.containment_probe() is True, "Write containment changed") + del result["checksum"] + result["reconciliation"] = {"operator": operator, "evidence_sha256": evidence} + plan = { + "status": "RECONCILIATION_REVIEW", + "confirmation": "RECONCILE-" + identifier, + "result": result, + } + plan["plan_fingerprint"] = canonical_hash(plan) + return plan + + def plan_reconciliation(self, identifier, *, operator): + with DeploymentLock(self.lock_path): + try: + return self._reconciliation_plan(identifier, operator, datetime.now(timezone.utc)) + except Exception: + raise MigrationRecoveryError("Migration reconciliation could not be verified") from None + + def record_reconciliation(self, identifier, *, operator, confirmation, fingerprint): + with DeploymentLock(self.lock_path): + try: + now = datetime.now(timezone.utc) + plan = self._reconciliation_plan(identifier, operator, now) + require(confirmation == plan["confirmation"] and fingerprint == plan["plan_fingerprint"], + "Reconciliation approval does not match current evidence") + # Recheck live authority after the database/application probes. + approval = self.store.load(identifier) + policy = self.store.require_operator_policy() + policy.verify_seal(approval) + policy.authorise(dict(approval, approver=operator), "reconcile", datetime.now(timezone.utc)) + require(self.containment_probe() is True, "Write containment changed") + result = plan["result"] + result["reconciliation"]["plan_fingerprint"] = fingerprint + result["completed_at"] = now.isoformat() + return self.publish(self.store.path(identifier, ".migration-result.json"), result) + except Exception: + raise MigrationRecoveryError("Migration reconciliation could not be recorded") from None + def execute( self, identifier, diff --git a/core/operator_approval.py b/core/operator_approval.py index b8ff953..65dee62 100644 --- a/core/operator_approval.py +++ b/core/operator_approval.py @@ -42,7 +42,7 @@ class OperatorApprovalPolicy: require(type(grant.uid) is int and grant.uid > 0, "Operator grant requires a non-root OS account") validate_approver(grant.principal) - require(grant.operation in ("issue", "execute"), "Invalid operator operation") + require(grant.operation in ("issue", "execute", "reconcile"), "Invalid operator operation") require(principals_by_uid.setdefault(grant.uid, grant.principal) == grant.principal and uids_by_principal.setdefault(grant.principal, grant.uid) == grant.uid, "Operator principals must map one-to-one to OS accounts") diff --git a/docs/MIGRATION-RECONCILIATION.md b/docs/MIGRATION-RECONCILIATION.md new file mode 100644 index 0000000..f9b8d28 --- /dev/null +++ b/docs/MIGRATION-RECONCILIATION.md @@ -0,0 +1,21 @@ +# Reviewed interrupted migration reconciliation + +Use this path if a migration intent and consumed approval survived, the +coordinator died before publishing a migration result, and a reviewed repair +has completed. The runtime does not run the repair or allow an existing result +to be replaced. + +Configure a trusted reconciliation probe. It receives the approved context, +durable intent and observed history. It must independently verify the actual +schema and application state, returning a SHA-256 digest of its evidence. +The existing recovery probes verify the backup, database identity, candidate +and original release identities, and the exact history. The caller needs the +current scoped `reconcile` operator grant. + +Obtain a plan with `plan_reconciliation(identifier, operator=...)` and review +the evidence. Submit its confirmation and fingerprint to +`record_reconciliation`. The runtime reacquires the deployment lock and +rechecks the plan, containment and operator authority before publishing a +single durable result. The ordinary journal stays protected. Forward +activation requires the separate existing recovery confirmation and remains +subject to its health and containment checks. Production execution is disabled.