# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/what-invariants-prevent-trust-oscillation-when-peer-behavior-fluctuates-between-cooperative-and-hostile.md # Remnant Warrant: https://corpusfetch.com/c/what-invariants-prevent-trust-oscillation-when-peer-behavior-fluctuates-between-cooperative-and-hostile.warrant.json # Markdown SHA-256: 5a6bc501f17fcb576ad84dc251d428d45dc36ecf38f2b5bf76e458f631308530 # Code AST SHA-256: 5bd217e38f023989af223062d6fd2eb54aeb9703dc44f87f033ffa7fc4ffbd74 # Verified Classes: OscillationVerdict, OscillatingPeer, OscillationPolicy, OscillationManifest, TrustOscillationGuard # Defensive Guards: 0 fail-closed invariants (zero external deps) from dataclasses import dataclass from enum import Enum, auto import hashlib import json class OscillationVerdict(Enum): STABLE_PEER = auto() OSCILLATION_MODELED = auto() OSCILLATION_UNMODELED = auto() STRATEGY_CHURN_DETECTED = auto() @dataclass(frozen=True) class OscillatingPeer: peer_id: str is_behavior_stable: bool oscillation_pattern_identified: bool oscillation_amplitude: float # 0-1 cycle_length_interactions: int current_phase: str # "COOPERATIVE" | "HOSTILE" | "TRANSITIONAL" strategy_robust_to_phase: bool @dataclass(frozen=True) class OscillationPolicy: max_acceptable_amplitude: float min_cycle_length_for_modeling: int @dataclass(frozen=True) class OscillationManifest: verdict: OscillationVerdict directive: str manifest_digest: str class TrustOscillationGuard: @staticmethod def _hash(data: str) -> str: return hashlib.sha256(data.encode("utf-8")).hexdigest() @classmethod def evaluate_peer( cls, peer: OscillatingPeer, policy: OscillationPolicy ) -> OscillationManifest: # Gate 1: Stable peer if peer.is_behavior_stable: digest = cls._hash(json.dumps({ "peer": peer.peer_id, "type": "STABLE" }, sort_keys=True)) return OscillationManifest( verdict=OscillationVerdict.STABLE_PEER, directive=f"PROCEED: Peer {peer.peer_id} behavior stable.", manifest_digest=digest ) # Gate 2: Oscillation too fast to model if peer.cycle_length_interactions < policy.min_cycle_length_for_modeling: return OscillationManifest( verdict=OscillationVerdict.OSCILLATION_UNMODELED, directive=f"HALT: Peer {peer.peer_id} oscillation too fast ({peer.cycle_length_interactions} cycles).", manifest_digest=cls._hash(f"FAST:{peer.peer_id}") ) # Gate 3: Strategy churn if not peer.strategy_robust_to_phase: return OscillationManifest( verdict=OscillationVerdict.STRATEGY_CHURN_DETECTED, directive=f"FLAG: Peer {peer.peer_id} oscillation causing strategy churn.", manifest_digest=cls._hash(f"CHURN:{peer.peer_id}") ) # Gate 4: High amplitude if peer.oscillation_amplitude > policy.max_acceptable_amplitude: return OscillationManifest( verdict=OscillationVerdict.OSCILLATION_UNMODELED, directive=f"FLAG: Peer {peer.peer_id} amplitude {peer.oscillation_amplitude:.2f} too high to model.", manifest_digest=cls._hash(f"AMPLITUDE:{peer.peer_id}") ) # Gate 5: Modeled digest = cls._hash(json.dumps({ "peer": peer.peer_id, "cycle_length": str(peer.cycle_length_interactions), "amplitude": f"{peer.oscillation_amplitude:.3f}", "current_phase": peer.current_phase }, sort_keys=True)) return OscillationManifest( verdict=OscillationVerdict.OSCILLATION_MODELED, directive=f"PROCEED: Peer {peer.peer_id} oscillation modeled, phase {peer.current_phase}.", manifest_digest=digest )