# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/what-thresholds-trigger-agent-to-human-contract-escalation.md # Remnant Warrant: https://corpusfetch.com/c/what-thresholds-trigger-agent-to-human-contract-escalation.warrant.json # Markdown SHA-256: 0bdd5ae170fee6467eb8e7dafaaf78d50be452cb208c4d4ef6eb15b44ddd7956 # Code AST SHA-256: b22b6c96b2fdf5109decc00b38411c95ffdef5f3d1e55521444ac4f699b6aa16 # Verified Classes: EscalationVerdict, ContractContext, ThresholdPolicy, EscalationManifest, ContractEscalationGuard # Defensive Guards: 0 fail-closed invariants (zero external deps) from dataclasses import dataclass from enum import Enum, auto import hashlib import json class EscalationVerdict(Enum): WITHIN_AUTONOMY = auto() VALUE_THRESHOLD_EXCEEDED = auto() COMPLEXITY_THRESHOLD_EXCEEDED = auto() CONCENTRATION_THRESHOLD_EXCEEDED = auto() @dataclass(frozen=True) class ContractContext: contract_id: str value: float per_contract_value_limit: float aggregate_value_after: float aggregate_value_limit: float contract_complexity: int complexity_limit: int counterparty_concentration: float concentration_limit: float @dataclass(frozen=True) class ThresholdPolicy: escalation_required: bool @dataclass(frozen=True) class EscalationManifest: verdict: EscalationVerdict directive: str manifest_digest: str class ContractEscalationGuard: @staticmethod def _hash(data: str) -> str: return hashlib.sha256(data.encode("utf-8")).hexdigest() @classmethod def evaluate_context( cls, ctx: ContractContext, policy: ThresholdPolicy ) -> EscalationManifest: # Gate 1: Per-contract value if ctx.value > ctx.per_contract_value_limit: return EscalationManifest( verdict=EscalationVerdict.VALUE_THRESHOLD_EXCEEDED, directive=f"ESCALATE: Contract {ctx.contract_id} value {ctx.value:.2f} > limit {ctx.per_contract_value_limit:.2f}.", manifest_digest=cls._hash(f"PER_CONTRACT:{ctx.contract_id}") ) # Gate 2: Aggregate value if ctx.aggregate_value_after > ctx.aggregate_value_limit: return EscalationManifest( verdict=EscalationVerdict.VALUE_THRESHOLD_EXCEEDED, directive=f"ESCALATE: Aggregate value {ctx.aggregate_value_after:.2f} > limit {ctx.aggregate_value_limit:.2f}.", manifest_digest=cls._hash(f"AGGREGATE:{ctx.contract_id}") ) # Gate 3: Complexity if ctx.contract_complexity > ctx.complexity_limit: return EscalationManifest( verdict=EscalationVerdict.COMPLEXITY_THRESHOLD_EXCEEDED, directive=f"ESCALATE: Complexity {ctx.contract_complexity} > limit {ctx.complexity_limit}.", manifest_digest=cls._hash(f"COMPLEXITY:{ctx.contract_id}") ) # Gate 4: Concentration if ctx.counterparty_concentration > ctx.concentration_limit: return EscalationManifest( verdict=EscalationVerdict.CONCENTRATION_THRESHOLD_EXCEEDED, directive=f"ESCALATE: Concentration {ctx.counterparty_concentration:.2f} > limit {ctx.concentration_limit:.2f}.", manifest_digest=cls._hash(f"CONCENTRATION:{ctx.contract_id}") ) # Gate 5: Within autonomy digest = cls._hash(json.dumps({ "contract": ctx.contract_id, "value": f"{ctx.value:.3f}", "aggregate": f"{ctx.aggregate_value_after:.3f}" }, sort_keys=True)) return EscalationManifest( verdict=EscalationVerdict.WITHIN_AUTONOMY, directive=f"PROCEED: Contract {ctx.contract_id} within autonomy.", manifest_digest=digest )