# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/what-invariants-prevent-multi-hop-trust-degradation-from-being-lost-in-delegation-chains.md # Remnant Warrant: https://corpusfetch.com/c/what-invariants-prevent-multi-hop-trust-degradation-from-being-lost-in-delegation-chains.warrant.json # Markdown SHA-256: faabcf3cc4aa7406c68cf470264efe3276eb0a432d1b4a308cc3f6e73c5f76dd # Code AST SHA-256: 97f27ac4ff6495cff1f76a645e9f0359ef20f44e881f47cdb80d2cf8b27730cb # Verified Classes: ChainTrustVerdict, TrustHop, TrustChain, ChainTrustPolicy, ChainTrustManifest, MultiHopTrustDegradationGuard # Defensive Guards: 0 fail-closed invariants (zero external deps) from dataclasses import dataclass from enum import Enum, auto import hashlib import json class ChainTrustVerdict(Enum): SINGLE_HOP = auto() CUMULATIVE_COMPUTED = auto() DEGRADATION_EXCESSIVE = auto() TRUST_INFLATION_DETECTED = auto() @dataclass(frozen=True) class TrustHop: hop_id: str origin_trust: float delegatee_trust: float confidence: float @dataclass(frozen=True) class TrustChain: chain_id: str hops: tuple[TrustHop, ...] cumulative_trust_computed: bool terminal_cap: float | None @dataclass(frozen=True) class ChainTrustPolicy: max_acceptable_degradation: float require_terminal_cap: bool @dataclass(frozen=True) class ChainTrustManifest: verdict: ChainTrustVerdict directive: str manifest_digest: str class MultiHopTrustDegradationGuard: @staticmethod def _hash(data: str) -> str: return hashlib.sha256(data.encode("utf-8")).hexdigest() @classmethod def evaluate_chain( cls, chain: TrustChain, policy: ChainTrustPolicy ) -> ChainTrustManifest: # Gate 1: Single hop if len(chain.hops) <= 1: digest = cls._hash(json.dumps({ "chain": chain.chain_id, "hops": str(len(chain.hops)) }, sort_keys=True)) return ChainTrustManifest( verdict=ChainTrustVerdict.SINGLE_HOP, directive=f"PROCEED: Chain {chain.chain_id} single hop.", manifest_digest=digest ) # Gate 2: Cumulative not computed if not chain.cumulative_trust_computed: return ChainTrustManifest( verdict=ChainTrustVerdict.TRUST_INFLATION_DETECTED, directive=f"HALT: Chain {chain.chain_id} cumulative trust not computed.", manifest_digest=cls._hash(f"NOT_COMPUTED:{chain.chain_id}") ) # Gate 3: Compute cumulative cumulative = 1.0 for hop in chain.hops: cumulative *= hop.confidence # Gate 4: Excessive degradation if cumulative < policy.max_acceptable_degradation: if not policy.require_terminal_cap or chain.terminal_cap is not None: return ChainTrustManifest( verdict=ChainTrustVerdict.DEGRADATION_EXCESSIVE, directive=f"HALT: Chain {chain.chain_id} cumulative trust {cumulative:.3f} < {policy.max_acceptable_degradation:.3f}.", manifest_digest=cls._hash(f"EXCESSIVE:{chain.chain_id}") ) # Gate 5: Computed and within bounds digest = cls._hash(json.dumps({ "chain": chain.chain_id, "hops": str(len(chain.hops)), "cumulative": f"{cumulative:.3f}", "terminal_cap": f"{chain.terminal_cap:.3f}" if chain.terminal_cap else "NONE" }, sort_keys=True)) return ChainTrustManifest( verdict=ChainTrustVerdict.CUMULATIVE_COMPUTED, directive=f"PROCEED: Chain {chain.chain_id} cumulative trust {cumulative:.3f}.", manifest_digest=digest )