# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/what-communication-invariants-prevent-fan-out-storms-when-a-broadcast-is-amplified-through-a-mesh.md # Remnant Warrant: https://corpusfetch.com/c/what-communication-invariants-prevent-fan-out-storms-when-a-broadcast-is-amplified-through-a-mesh.warrant.json # Markdown SHA-256: ce978c855305af337e3c55fa612e18351160c425db527b0b9204141f2524df9f # Code AST SHA-256: 1340f53c3d13be6f48f303799b82b1e181d27b89e4f4896c9ac3e453a301c928 # Verified Classes: FanoutVerdict, BroadcastMessage, MeshNodeState, FanoutPolicy, FanoutManifest, MeshFanoutGuard # Defensive Guards: 0 fail-closed invariants (zero external deps) from dataclasses import dataclass from enum import Enum, auto import hashlib import json class FanoutVerdict(Enum): POINT_TO_POINT = auto() REBROADCAST_ALLOWED = auto() TTL_EXPIRED = auto() DUPLICATE_SUPPRESSED = auto() QUOTA_EXHAUSTED = auto() @dataclass(frozen=True) class BroadcastMessage: message_id: str ttl_remaining: int priority: str @dataclass(frozen=True) class MeshNodeState: seen_message_ids: frozenset[str] fanout_quota_remaining: int max_ttl: int @dataclass(frozen=True) class FanoutPolicy: allow_critical_bypass: bool @dataclass(frozen=True) class FanoutManifest: verdict: FanoutVerdict directive: str manifest_digest: str class MeshFanoutGuard: @staticmethod def _hash(data: str) -> str: return hashlib.sha256(data.encode("utf-8")).hexdigest() @classmethod def evaluate_broadcast( cls, msg: BroadcastMessage, state: MeshNodeState, policy: FanoutPolicy ) -> FanoutManifest: # Gate 1: TTL if msg.ttl_remaining <= 0: return FanoutManifest( verdict=FanoutVerdict.TTL_EXPIRED, directive=f"DROP: Message {msg.message_id} TTL expired.", manifest_digest=cls._hash(f"TTL:{msg.message_id}") ) # Gate 2: Duplicate if msg.message_id in state.seen_message_ids: return FanoutManifest( verdict=FanoutVerdict.DUPLICATE_SUPPRESSED, directive=f"DROP: Message {msg.message_id} already seen.", manifest_digest=cls._hash(f"DUP:{msg.message_id}") ) # Gate 3: Quota if state.fanout_quota_remaining <= 0: if policy.allow_critical_bypass and msg.priority == "CRITICAL": digest = cls._hash(json.dumps({ "message": msg.message_id, "priority": "CRITICAL", "bypass": "QUOTA" }, sort_keys=True)) return FanoutManifest( verdict=FanoutVerdict.REBROADCAST_ALLOWED, directive=f"PROCEED: Critical bypass for {msg.message_id}.", manifest_digest=digest ) return FanoutManifest( verdict=FanoutVerdict.QUOTA_EXHAUSTED, directive=f"DROP: Quota exhausted for {msg.message_id}.", manifest_digest=cls._hash(f"QUOTA:{msg.message_id}") ) # Gate 4: Allowed digest = cls._hash(json.dumps({ "message": msg.message_id, "ttl": str(msg.ttl_remaining), "quota": str(state.fanout_quota_remaining), "priority": msg.priority }, sort_keys=True)) return FanoutManifest( verdict=FanoutVerdict.REBROADCAST_ALLOWED, directive=f"PROCEED: Re-broadcast {msg.message_id} with quota {state.fanout_quota_remaining}.", manifest_digest=digest )