# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/how-must-an-instance-handle-divergence-between-its-own-state-and-a-peers-authoritative-state.md # Remnant Warrant: https://corpusfetch.com/c/how-must-an-instance-handle-divergence-between-its-own-state-and-a-peers-authoritative-state.warrant.json # Markdown SHA-256: a85a0f593cdc7520bb685d17b1510c50e9934b75ebd2a8cfb952e48aaad43b19 # Code AST SHA-256: 7d157404af57b8e542eebb3230352dc5e2ef20e6745b1b773021245b9d77695c # Verified Classes: DivergenceVerdict, StateDivergence, ReconciliationContext, DivergenceManifest, PeerStateDivergenceGuard # Defensive Guards: 0 fail-closed invariants (zero external deps) from dataclasses import dataclass from enum import Enum, auto import hashlib import json class DivergenceVerdict(Enum): NO_DIVERGENCE = auto() RESOLVED_BY_AUTHORITY = auto() RESOLVED_BY_MERGE = auto() ESCALATE_TO_HUMAN = auto() @dataclass(frozen=True) class StateDivergence: local_state_hash: str peer_state_hash: str divergent_field_count: int total_field_count: int shared_authority_fields: tuple[str, ...] @dataclass(frozen=True) class ReconciliationContext: local_authority_fields: frozenset[str] peer_authority_fields: frozenset[str] escalation_ratio_threshold: float @dataclass(frozen=True) class DivergenceManifest: verdict: DivergenceVerdict directive: str manifest_digest: str class PeerStateDivergenceGuard: @staticmethod def _hash(data: str) -> str: return hashlib.sha256(data.encode("utf-8")).hexdigest() @classmethod def evaluate_divergence( cls, divergence: StateDivergence, ctx: ReconciliationContext ) -> DivergenceManifest: # Gate 1: No divergence if divergence.local_state_hash == divergence.peer_state_hash: digest = cls._hash(json.dumps({ "state": divergence.local_state_hash }, sort_keys=True)) return DivergenceManifest( verdict=DivergenceVerdict.NO_DIVERGENCE, directive="PROCEED: States identical.", manifest_digest=digest ) # Gate 2: Shared authority fields if divergence.shared_authority_fields: return DivergenceManifest( verdict=DivergenceVerdict.ESCALATE_TO_HUMAN, directive=f"HALT: {len(divergence.shared_authority_fields)} fields have shared authority: {sorted(divergence.shared_authority_fields)}.", manifest_digest=cls._hash(f"SHARED_AUTHORITY:{len(divergence.shared_authority_fields)}") ) # Gate 3: Escalation threshold if divergence.total_field_count == 0: ratio = 0.0 else: ratio = divergence.divergent_field_count / divergence.total_field_count if ratio > ctx.escalation_ratio_threshold: return DivergenceManifest( verdict=DivergenceVerdict.ESCALATE_TO_HUMAN, directive=f"HALT: Divergence ratio {ratio:.2%} > {ctx.escalation_ratio_threshold:.2%}. Escalate.", manifest_digest=cls._hash(f"RATIO:{ratio:.3f}") ) # Gate 4: Authority resolution digest = cls._hash(json.dumps({ "local": divergence.local_state_hash, "peer": divergence.peer_state_hash, "divergent": str(divergence.divergent_field_count), "local_authority": tuple(sorted(ctx.local_authority_fields)), "peer_authority": tuple(sorted(ctx.peer_authority_fields)) }, sort_keys=True)) return DivergenceManifest( verdict=DivergenceVerdict.RESOLVED_BY_AUTHORITY, directive=f"PROCEED: Apply authority map for {divergence.divergent_field_count} divergent fields.", manifest_digest=digest )