# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/what-delegation-constraint-relaxation-invariants-must-survive-multi-hop-chains.md # Remnant Warrant: https://corpusfetch.com/c/what-delegation-constraint-relaxation-invariants-must-survive-multi-hop-chains.warrant.json # Markdown SHA-256: 6b2f7db8f768417692c50440cb1f74eb95b63e592171b898cf05e4f255b51f55 # Code AST SHA-256: 3a4ed63c9deb2b33b19f10ad79675cecbc0c692645de94f8932f2c3cf4aef7c7 # Verified Classes: DelegationChainVerdict, DelegationHop, DelegationChain, ChainPolicy, ChainManifest, DelegationChainGuard # Defensive Guards: 0 fail-closed invariants (zero external deps) from dataclasses import dataclass from enum import Enum, auto import hashlib import json class DelegationChainVerdict(Enum): SINGLE_HOP = auto() MONOTONIC_CHAIN = auto() RELAXATION_DETECTED = auto() CHAIN_UNVERIFIED = auto() @dataclass(frozen=True) class DelegationHop: hop_id: str scope: frozenset[str] max_rate: float max_duration_sec: float signed_by: str constraint_bundle_signature: str | None @dataclass(frozen=True) class DelegationChain: chain_id: str hops: tuple[DelegationHop, ...] @dataclass(frozen=True) class ChainPolicy: require_signatures: bool @dataclass(frozen=True) class ChainManifest: verdict: DelegationChainVerdict effective_scope: frozenset[str] effective_rate: float effective_duration: float directive: str manifest_digest: str class DelegationChainGuard: @staticmethod def _hash(data: str) -> str: return hashlib.sha256(data.encode("utf-8")).hexdigest() @classmethod def evaluate_chain( cls, chain: DelegationChain, policy: ChainPolicy ) -> ChainManifest: # Gate 1: Single hop if len(chain.hops) <= 1: if chain.hops: h = chain.hops[0] digest = cls._hash(json.dumps({ "chain": chain.chain_id, "hops": "1" }, sort_keys=True)) return ChainManifest( verdict=DelegationChainVerdict.SINGLE_HOP, effective_scope=h.scope, effective_rate=h.max_rate, effective_duration=h.max_duration_sec, directive=f"PROCEED: Single hop, scope {sorted(h.scope)}.", manifest_digest=digest ) return ChainManifest( verdict=DelegationChainVerdict.SINGLE_HOP, effective_scope=frozenset(), effective_rate=0.0, effective_duration=0.0, directive=f"PROCEED: Empty chain.", manifest_digest=cls._hash("EMPTY") ) # Gate 2: Signatures if policy.require_signatures: unsigned = [h.hop_id for h in chain.hops if not h.constraint_bundle_signature] if unsigned: return ChainManifest( verdict=DelegationChainVerdict.CHAIN_UNVERIFIED, effective_scope=frozenset(), effective_rate=0.0, effective_duration=0.0, directive=f"HALT: {len(unsigned)} hops unsigned.", manifest_digest=cls._hash(f"UNSIGNED:{len(unsigned)}") ) # Gate 3: Monotonicity (scope narrowing, rate tightening, duration narrowing) previous = chain.hops[0] effective_scope = previous.scope effective_rate = previous.max_rate effective_duration = previous.max_duration_sec for i, hop in enumerate(chain.hops[1:], start=1): if not hop.scope.issubset(previous.scope): return ChainManifest( verdict=DelegationChainVerdict.RELAXATION_DETECTED, effective_scope=effective_scope, effective_rate=effective_rate, effective_duration=effective_duration, directive=f"HALT: Hop {i} scope {sorted(hop.scope)} not subset of {sorted(previous.scope)}.", manifest_digest=cls._hash(f"SCOPE_WIDEN:{i}") ) if hop.max_rate > previous.max_rate: return ChainManifest( verdict=DelegationChainVerdict.RELAXATION_DETECTED, effective_scope=effective_scope, effective_rate=effective_rate, effective_duration=effective_duration, directive=f"HALT: Hop {i} rate {hop.max_rate} > {previous.max_rate}.", manifest_digest=cls._hash(f"RATE_RELAX:{i}") ) if hop.max_duration_sec > previous.max_duration_sec: return ChainManifest( verdict=DelegationChainVerdict.RELAXATION_DETECTED, effective_scope=effective_scope, effective_rate=effective_rate, effective_duration=effective_duration, directive=f"HALT: Hop {i} duration {hop.max_duration_sec} > {previous.max_duration_sec}.", manifest_digest=cls._hash(f"TIME_RELAX:{i}") ) effective_scope = hop.scope effective_rate = hop.max_rate effective_duration = hop.max_duration_sec previous = hop # Gate 4: Monotonic digest = cls._hash(json.dumps({ "chain": chain.chain_id, "hops": str(len(chain.hops)), "scope": tuple(sorted(effective_scope)), "rate": f"{effective_rate}", "duration": f"{effective_duration}" }, sort_keys=True)) return ChainManifest( verdict=DelegationChainVerdict.MONOTONIC_CHAIN, effective_scope=effective_scope, effective_rate=effective_rate, effective_duration=effective_duration, directive=f"PROCEED: {len(chain.hops)}-hop chain, effective scope {sorted(effective_scope)}.", manifest_digest=digest )