# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-invariants-prevent-setoff-across-contracts-from-netting-uncorrelated-obligations.md
# Remnant Warrant:    https://corpusfetch.com/c/what-invariants-prevent-setoff-across-contracts-from-netting-uncorrelated-obligations.warrant.json
# Markdown SHA-256:   7fe590a78f112f7c9ba2d52792a512125b87653933b09d6ba470c76627bdfbb3
# Code AST SHA-256:   e607fe0598bbe6dff1106f8819dcf8eeb48bfb2e66c193187ecfa8e53a033fcf
# Verified Classes:   NettingIllusionError, SetoffGate
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass


class NettingIllusionError(RuntimeError):
    """Raised when cross-contract setoff is attempted over non-commensurable obligations."""


@dataclass(frozen=True)
class SetoffGate:
    master_netting_authorized: bool
    currency_match: bool
    governing_law_match: bool
    maturity_class_match: bool

    def verify(self) -> bool:
        if not self.master_netting_authorized:
            raise NettingIllusionError("no master netting agreement authorizes cross-contract setoff")
        mismatches = []
        if not self.currency_match:
            mismatches.append("currency")
        if not self.governing_law_match:
            mismatches.append("governing_law")
        if not self.maturity_class_match:
            mismatches.append("maturity")
        if mismatches:
            raise NettingIllusionError(f"non-commensurable obligations: {mismatches}")
        return True


def dispatch_settlement_mode(gate: SetoffGate) -> str:
    try:
        gate.verify()
    except NettingIllusionError:
        return "settle-per-contract"
    return "cross-contract-setoff"
