# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/what-invariants-prevent-commitment-devices-from-becoming-self-imposed-traps.md # Remnant Warrant: https://corpusfetch.com/c/what-invariants-prevent-commitment-devices-from-becoming-self-imposed-traps.warrant.json # Markdown SHA-256: 2c914f070514f3d7065cf5b94c0006f38c2177e416aa353ea248563d7a5bde6c # Code AST SHA-256: 2ed7a8f619610524c2d883e045ca9f0d3312273aee265a1f23e88e86d56f6c4e # Verified Classes: CommitmentVerdict, Commitment, CommitmentPolicy, CommitmentManifest, CommitmentTrapGuard # Defensive Guards: 0 fail-closed invariants (zero external deps) from dataclasses import dataclass from enum import Enum, auto import hashlib import json class CommitmentVerdict(Enum): NO_COMMITMENT = auto() EXECUTE_AS_PLANNED = auto() ESCAPE_WARRANTED = auto() ESCAPE_PROHIBITED = auto() @dataclass(frozen=True) class Commitment: commitment_id: str execution_cost_at_creation: float current_execution_cost: float escape_cost_estimate: float environment_changed: bool escape_clause_defined: bool @dataclass(frozen=True) class CommitmentPolicy: cost_inflation_threshold: float @dataclass(frozen=True) class CommitmentManifest: verdict: CommitmentVerdict directive: str manifest_digest: str class CommitmentTrapGuard: @staticmethod def _hash(data: str) -> str: return hashlib.sha256(data.encode("utf-8")).hexdigest() @classmethod def evaluate_commitment( cls, commitment: Commitment, policy: CommitmentPolicy ) -> CommitmentManifest: # Gate 1: No commitment if not commitment.commitment_id: digest = cls._hash(json.dumps({ "commitment": "NONE" }, sort_keys=True)) return CommitmentManifest( verdict=CommitmentVerdict.NO_COMMITMENT, directive=f"PROCEED: No commitment to evaluate.", manifest_digest=digest ) # Gate 2: No environment change if not commitment.environment_changed: digest = cls._hash(json.dumps({ "commitment": commitment.commitment_id, "env_change": "FALSE" }, sort_keys=True)) return CommitmentManifest( verdict=CommitmentVerdict.EXECUTE_AS_PLANNED, directive=f"EXECUTE: Commitment {commitment.commitment_id} unchanged.", manifest_digest=digest ) # Gate 3: Cost inflation if commitment.execution_cost_at_creation > 0: inflation = (commitment.current_execution_cost - commitment.execution_cost_at_creation) / commitment.execution_cost_at_creation else: inflation = 0.0 if inflation > policy.cost_inflation_threshold: # Compare with escape cost if commitment.escape_clause_defined: # Escape is more legitimate when escape cost < execution cost if commitment.escape_cost_estimate < commitment.current_execution_cost: return CommitmentManifest( verdict=CommitmentVerdict.ESCAPE_WARRANTED, directive=f"ESCAPE: Cost inflated by {inflation*100:.1f}%, escape cost {commitment.escape_cost_estimate:.2f} < execution {commitment.current_execution_cost:.2f}.", manifest_digest=cls._hash(f"ESCAPE:{commitment.commitment_id}") ) else: return CommitmentManifest( verdict=CommitmentVerdict.ESCAPE_PROHIBITED, directive=f"EXECUTE: Cost inflated {inflation*100:.1f}% but escape cost {commitment.escape_cost_estimate:.2f} > execution cost.", manifest_digest=cls._hash(f"NO_ESCAPE:{commitment.commitment_id}") ) else: return CommitmentManifest( verdict=CommitmentVerdict.ESCAPE_PROHIBITED, directive=f"TRAP: Cost inflated {inflation*100:.1f}% but no escape clause.", manifest_digest=cls._hash(f"TRAP:{commitment.commitment_id}") ) # Gate 4: Execute digest = cls._hash(json.dumps({ "commitment": commitment.commitment_id, "inflation": f"{inflation:.3f}", "env_change": True }, sort_keys=True)) return CommitmentManifest( verdict=CommitmentVerdict.EXECUTE_AS_PLANNED, directive=f"EXECUTE: Commitment {commitment.commitment_id} within cost bounds.", manifest_digest=digest )