# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/what-immutable-ledger-invariants-must-survive-an-agents-bankruptcy-or-account-closure.md # Remnant Warrant: https://corpusfetch.com/c/what-immutable-ledger-invariants-must-survive-an-agents-bankruptcy-or-account-closure.warrant.json # Markdown SHA-256: 05c60ae9244fe8f9d6d666d2190bf4375b688eb6d3c4a4a10b93b8e383c0d1d9 # Code AST SHA-256: 9b4ba6338cce39dfa28e6f56d85575fbe62b6f6c409b311f9e63aee74fc499e3 # Verified Classes: ClosureVerdict, LedgerObligation, ClosureState, ClosureManifest, LedgerClosureGuard # Defensive Guards: 0 fail-closed invariants (zero external deps) from dataclasses import dataclass from enum import Enum, auto import hashlib import json class ClosureVerdict(Enum): CLEAN_CLOSURE = auto() LIABILITIES_UNRESOLVED = auto() ARBITRATION_REQUIRED = auto() @dataclass(frozen=True) class LedgerObligation: obligation_id: str counterparty_id: str amount: int is_escrowed: bool @dataclass(frozen=True) class ClosureState: outstanding_obligations: tuple[LedgerObligation, ...] successor_arbitration_key: str | None @dataclass(frozen=True) class ClosureManifest: verdict: ClosureVerdict directive: str manifest_digest: str class LedgerClosureGuard: @staticmethod def _hash(data: str) -> str: return hashlib.sha256(data.encode("utf-8")).hexdigest() @classmethod def evaluate_closure(cls, state: ClosureState) -> ClosureManifest: # Gate 1: No obligations if not state.outstanding_obligations: return ClosureManifest( verdict=ClosureVerdict.CLEAN_CLOSURE, directive="PROCEED: No outstanding obligations. Account may be closed.", manifest_digest=cls._hash("CLEAN") ) # Gate 2: Escrowed funds must be resolved or transferred for obligation in state.outstanding_obligations: if obligation.is_escrowed and not state.successor_arbitration_key: return ClosureManifest( verdict=ClosureVerdict.ARBITRATION_REQUIRED, directive=f"HALT: Obligation {obligation.obligation_id} is escrowed but no successor key provided.", manifest_digest=cls._hash(f"ESCROW_NO_KEY:{obligation.obligation_id}") ) # Gate 3: Unresolved debts require arbitration key if not state.successor_arbitration_key: return ClosureManifest( verdict=ClosureVerdict.LIABILITIES_UNRESOLVED, directive=f"HALT: {len(state.outstanding_obligations)} outstanding liabilities require a successor arbitration key.", manifest_digest=cls._hash("NO_ARBITER") ) # Gate 4: Transfer to arbitration digest = cls._hash(json.dumps({ "obligations": str(len(state.outstanding_obligations)), "arbiter": state.successor_arbitration_key }, sort_keys=True)) return ClosureManifest( verdict=ClosureVerdict.ARBITRATION_REQUIRED, directive=f"ACTION: Transfer all obligations to {state.successor_arbitration_key}. Record closure signature.", manifest_digest=digest )