# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/what-invariants-prevent-entropy-accumulation-in-collective-memory-over-long-timescales.md # Remnant Warrant: https://corpusfetch.com/c/what-invariants-prevent-entropy-accumulation-in-collective-memory-over-long-timescales.warrant.json # Markdown SHA-256: f0f857147198a23f2f7268114f68b2bee65aeaab7ce1794a44f61cd80e18bd4a # Code AST SHA-256: f26076b9413fecd5370dc86a69fe2af28432122660cd02037124534188a827d8 # Verified Classes: EntropyVerdict, MemoryEntropy, EntropyPolicy, EntropyManifest, CollectiveEntropyGuard # Defensive Guards: 0 fail-closed invariants (zero external deps) from dataclasses import dataclass from enum import Enum, auto import hashlib import json class EntropyVerdict(Enum): SHORT_TIMESCALE = auto() ENTROPY_NORMAL = auto() NORMALIZATION_REQUIRED = auto() ENTROPY_CRITICAL = auto() @dataclass(frozen=True) class MemoryEntropy: memory_id: str format_diversity: float semantic_ambiguity: float redundancy_ratio: float timescale_days: float last_normalized_epoch: float | None @dataclass(frozen=True) class EntropyPolicy: normalize_interval_days: float critical_entropy_threshold: float @dataclass(frozen=True) class EntropyManifest: verdict: EntropyVerdict entropy_score: float directive: str manifest_digest: str class CollectiveEntropyGuard: @staticmethod def _hash(data: str) -> str: return hashlib.sha256(data.encode("utf-8")).hexdigest() @classmethod def evaluate_memory( cls, entropy: MemoryEntropy, policy: EntropyPolicy ) -> EntropyManifest: # Gate 1: Short timescale if entropy.timescale_days < policy.normalize_interval_days: digest = cls._hash(json.dumps({ "memory": entropy.memory_id, "timescale_days": f"{entropy.timescale_days:.1f}" }, sort_keys=True)) return EntropyManifest( verdict=EntropyVerdict.SHORT_TIMESCALE, entropy_score=0.0, directive=f"PROCEED: Short timescale, no entropy accumulation yet.", manifest_digest=digest ) # Gate 2: Compute entropy score entropy_score = ( entropy.format_diversity * 0.3 + entropy.semantic_ambiguity * 0.4 + entropy.redundancy_ratio * 0.3 ) # Gate 3: Critical if entropy_score >= policy.critical_entropy_threshold: return EntropyManifest( verdict=EntropyVerdict.ENTROPY_CRITICAL, entropy_score=entropy_score, directive=f"HALT: Entropy {entropy_score:.2f} >= {policy.critical_entropy_threshold:.2f}, normalization immediate.", manifest_digest=cls._hash(f"CRITICAL:{entropy.memory_id}") ) # Gate 4: Normalization interval if entropy.last_normalized_epoch is None: return EntropyManifest( verdict=EntropyVerdict.NORMALIZATION_REQUIRED, entropy_score=entropy_score, directive=f"ACTION: No prior normalization, run now.", manifest_digest=cls._hash(f"NEVER_NORMALIZED:{entropy.memory_id}") ) # Gate 5: Normal digest = cls._hash(json.dumps({ "memory": entropy.memory_id, "entropy": f"{entropy_score:.3f}", "formats": f"{entropy.format_diversity:.3f}", "ambiguity": f"{entropy.semantic_ambiguity:.3f}", "redundancy": f"{entropy.redundancy_ratio:.3f}" }, sort_keys=True)) return EntropyManifest( verdict=EntropyVerdict.ENTROPY_NORMAL, entropy_score=entropy_score, directive=f"PROCEED: Entropy {entropy_score:.2f} below critical {policy.critical_entropy_threshold:.2f}.", manifest_digest=digest )