# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/what-cost-attribution-invariants-must-survive-multi-hop-reference-propagation.md # Remnant Warrant: https://corpusfetch.com/c/what-cost-attribution-invariants-must-survive-multi-hop-reference-propagation.warrant.json # Markdown SHA-256: 7d7799e7d01938bd8a0e758fa1e1d12e01827fe2e5beead0095d7a92ad28879b # Code AST SHA-256: 60f9ca1a7b0e7a771fc37c897b0872b3ae5e10a386838f14f6c25d3bb2edb94c # Verified Classes: CostVerdict, HopCost, ReferenceChain, CostPolicy, CostManifest, ReferenceCostAttributionGuard # Defensive Guards: 0 fail-closed invariants (zero external deps) from dataclasses import dataclass from enum import Enum, auto import hashlib import json class CostVerdict(Enum): SINGLE_HOP = auto() COST_ATTRIBUTED = auto() POLICY_MISSING = auto() COST_ATTRIBUTION_INCOMPLETE = auto() @dataclass(frozen=True) class HopCost: hop_id: str cost_units: float cost_type: str # "BANDWIDTH" | "COMPUTE" | "STORAGE" @dataclass(frozen=True) class ReferenceChain: reference_id: str hop_costs: tuple[HopCost, ...] attribution_policy: str | None # "ORIGIN_PAYS" | "CONSUMER_PAYS" | "PROPAGATOR_PAYS" @dataclass(frozen=True) class CostPolicy: require_attribution_policy: bool @dataclass(frozen=True) class CostManifest: verdict: CostVerdict total_cost: float directive: str manifest_digest: str class ReferenceCostAttributionGuard: @staticmethod def _hash(data: str) -> str: return hashlib.sha256(data.encode("utf-8")).hexdigest() @classmethod def evaluate_chain( cls, chain: ReferenceChain, policy: CostPolicy ) -> CostManifest: # Gate 1: Single hop if len(chain.hop_costs) <= 1: total = sum(h.cost_units for h in chain.hop_costs) digest = cls._hash(json.dumps({ "reference": chain.reference_id, "hops": str(len(chain.hop_costs)), "cost": f"{total:.2f}" }, sort_keys=True)) return CostManifest( verdict=CostVerdict.SINGLE_HOP, total_cost=total, directive=f"PROCEED: Single hop, cost {total:.2f} attributed to hop.", manifest_digest=digest ) # Gate 2: Policy presence if policy.require_attribution_policy and not chain.attribution_policy: return CostManifest( verdict=CostVerdict.POLICY_MISSING, total_cost=sum(h.cost_units for h in chain.hop_costs), directive=f"HALT: Multi-hop chain without attribution policy. Free-riding risk.", manifest_digest=cls._hash(f"NO_POLICY:{chain.reference_id}") ) # Gate 3: Hop completeness if any(not h.hop_id for h in chain.hop_costs): return CostManifest( verdict=CostVerdict.COST_ATTRIBUTION_INCOMPLETE, total_cost=0.0, directive=f"HALT: Some hop costs lack hop attribution.", manifest_digest=cls._hash(f"INCOMPLETE:{chain.reference_id}") ) # Gate 4: Attributed total = sum(h.cost_units for h in chain.hop_costs) digest = cls._hash(json.dumps({ "reference": chain.reference_id, "hops": tuple(h.hop_id for h in chain.hop_costs), "total": f"{total:.2f}", "policy": chain.attribution_policy }, sort_keys=True)) return CostManifest( verdict=CostVerdict.COST_ATTRIBUTED, total_cost=total, directive=f"PROCEED: Total cost {total:.2f} attributed across {len(chain.hop_costs)} hops per {chain.attribution_policy}.", manifest_digest=digest )