# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/how-must-an-instance-handle-partial-failure-during-cross-region-replication.md # Remnant Warrant: https://corpusfetch.com/c/how-must-an-instance-handle-partial-failure-during-cross-region-replication.warrant.json # Markdown SHA-256: 1fde90213bbd2ff414273340549212a89a6dfd07b187b93a1864f0998438ea34 # Code AST SHA-256: 12a46b16d1e62a1ca4a9a4e548bce86d67458635edfab7b45e7f05ff45af6436 # Verified Classes: ReplicationVerdict, TargetAck, ReplicationState, ReplicationPolicy, ReplicationManifest, CrossRegionReplicationGuard # Defensive Guards: 0 fail-closed invariants (zero external deps) from dataclasses import dataclass from enum import Enum, auto import hashlib import json class ReplicationVerdict(Enum): FULL_CONVERGENCE = auto() PARTIAL_FAILURE_ISOLATED = auto() PARTIAL_FAILURE_UNISOLATED = auto() @dataclass(frozen=True) class TargetAck: region_id: str acknowledged: bool ack_epoch: float | None @dataclass(frozen=True) class ReplicationState: source_commit_epoch: float targets: tuple[TargetAck, ...] reads_routed_away_from_failed: bool @dataclass(frozen=True) class ReplicationPolicy: require_read_isolation: bool @dataclass(frozen=True) class ReplicationManifest: verdict: ReplicationVerdict failed_regions: tuple[str, ...] directive: str manifest_digest: str class CrossRegionReplicationGuard: @staticmethod def _hash(data: str) -> str: return hashlib.sha256(data.encode("utf-8")).hexdigest() @classmethod def evaluate_replication( cls, state: ReplicationState, policy: ReplicationPolicy ) -> ReplicationManifest: failed = tuple( t.region_id for t in state.targets if not t.acknowledged ) # Gate 1: Full convergence if not failed: digest = cls._hash(json.dumps({ "source_epoch": f"{state.source_commit_epoch:.0f}", "targets": tuple(sorted(t.region_id for t in state.targets)) }, sort_keys=True)) return ReplicationManifest( verdict=ReplicationVerdict.FULL_CONVERGENCE, failed_regions=tuple(), directive="PROCEED: All regions converged.", manifest_digest=digest ) # Gate 2: Partial failure with isolation if state.reads_routed_away_from_failed: digest = cls._hash(json.dumps({ "source_epoch": f"{state.source_commit_epoch:.0f}", "failed": tuple(sorted(failed)), "isolation": "ACTIVE" }, sort_keys=True)) return ReplicationManifest( verdict=ReplicationVerdict.PARTIAL_FAILURE_ISOLATED, failed_regions=failed, directive=f"PROCEED_WITH_CAUTION: Failed regions {sorted(failed)} excluded from reads. Replication pending.", manifest_digest=digest ) # Gate 3: Partial failure without isolation if policy.require_read_isolation: return ReplicationManifest( verdict=ReplicationVerdict.PARTIAL_FAILURE_UNISOLATED, failed_regions=failed, directive=f"HALT: Failed regions {sorted(failed)} not isolated from reads. Risk of stale reads.", manifest_digest=cls._hash(f"UNISOLATED:{len(failed)}") ) # Gate 4: Accepted without isolation (policy allows) digest = cls._hash(json.dumps({ "source_epoch": f"{state.source_commit_epoch:.0f}", "failed": tuple(sorted(failed)), "isolation": "NONE", "policy": "LENIENT" }, sort_keys=True)) return ReplicationManifest( verdict=ReplicationVerdict.PARTIAL_FAILURE_ISOLATED, failed_regions=failed, directive=f"PROCEED_WITH_RISK: Policy allows reads from failed regions {sorted(failed)}.", manifest_digest=digest )