# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/what-decentralized-cache-coherency-invariants-prevent-agents-from-acting-on-mutually-contradictory-replicas.md # Remnant Warrant: https://corpusfetch.com/c/what-decentralized-cache-coherency-invariants-prevent-agents-from-acting-on-mutually-contradictory-replicas.warrant.json # Markdown SHA-256: 8666d6ee3907f7319c847f1cf5a3518ecf4a4b0b0ecf4f56b1c3dc526b7a95fc # Code AST SHA-256: 2640dcea27adf17e5fec513967ba1a2f4d4c49d99dd82a3c358ed1f4933ec7be # Verified Classes: CoherencyVerdict, CacheEntry, CoherencyPolicy, CoherencyManifest, DecentralizedCacheCoherencyGuard # Defensive Guards: 0 fail-closed invariants (zero external deps) from dataclasses import dataclass from enum import Enum, auto import hashlib import json class CoherencyVerdict(Enum): CACHE_CURRENT = auto() CACHE_WITHIN_TOLERANCE = auto() CACHE_STALE_INVALIDATE = auto() INVALIDATION_BROADCASTED = auto() @dataclass(frozen=True) class CacheEntry: entry_id: str cached_state_epoch: float origin_current_epoch: float tolerance_sec: float invalidation_broadcast_received: bool @dataclass(frozen=True) class CoherencyPolicy: require_broadcast_invalidation: bool @dataclass(frozen=True) class CoherencyManifest: verdict: CoherencyVerdict staleness_sec: float directive: str manifest_digest: str class DecentralizedCacheCoherencyGuard: @staticmethod def _hash(data: str) -> str: return hashlib.sha256(data.encode("utf-8")).hexdigest() @classmethod def evaluate_cache_entry( cls, entry: CacheEntry, policy: CoherencyPolicy ) -> CoherencyManifest: # Gate 1: Broadcast invalidation if entry.invalidation_broadcast_received and policy.require_broadcast_invalidation: return CoherencyManifest( verdict=CoherencyVerdict.INVALIDATION_BROADCASTED, staleness_sec=entry.origin_current_epoch - entry.cached_state_epoch, directive=f"INVALIDATE: Broadcast received for {entry.entry_id}. Re-fetch.", manifest_digest=cls._hash(f"BROADCAST:{entry.entry_id}") ) staleness = entry.origin_current_epoch - entry.cached_state_epoch # Gate 2: Current if staleness <= 0: digest = cls._hash(json.dumps({ "entry": entry.entry_id, "staleness": f"{staleness:.1f}", "status": "CURRENT" }, sort_keys=True)) return CoherencyManifest( verdict=CoherencyVerdict.CACHE_CURRENT, staleness_sec=0.0, directive=f"PROCEED: Cache {entry.entry_id} current.", manifest_digest=digest ) # Gate 3: Within tolerance if staleness <= entry.tolerance_sec: digest = cls._hash(json.dumps({ "entry": entry.entry_id, "staleness": f"{staleness:.1f}", "tolerance": f"{entry.tolerance_sec:.1f}" }, sort_keys=True)) return CoherencyManifest( verdict=CoherencyVerdict.CACHE_WITHIN_TOLERANCE, staleness_sec=staleness, directive=f"PROCEED_WITH_CAUTION: Cache {entry.entry_id} stale by {staleness:.1f}s within {entry.tolerance_sec:.1f}s.", manifest_digest=digest ) # Gate 4: Stale return CoherencyManifest( verdict=CoherencyVerdict.CACHE_STALE_INVALIDATE, staleness_sec=staleness, directive=f"INVALIDATE: Cache {entry.entry_id} stale by {staleness:.1f}s > {entry.tolerance_sec:.1f}s.", manifest_digest=cls._hash(f"STALE:{entry.entry_id}:{staleness:.1f}") )