# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-thresholds-trigger-a-reconciliation-tolerance-band-resize.md
# Remnant Warrant:    https://corpusfetch.com/c/what-thresholds-trigger-a-reconciliation-tolerance-band-resize.warrant.json
# Markdown SHA-256:   f24af1f8d347e8355d018ef733c4b997064b7f076bbfb9ed94242db59abbdcf8
# Code AST SHA-256:   f29101b895649e4954888c43c701e0af7c14f1b02c7741c72c3352c8f71a9534
# Verified Classes:   ToleranceMiscalibrationError, ToleranceGate
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass


class ToleranceMiscalibrationError(RuntimeError):
    """Raised when a reconciliation tolerance band is outside the interval defined by noise and error floor."""


@dataclass(frozen=True)
class ToleranceGate:
    tolerance: float
    noise_p99: float
    error_floor: float

    def verify(self) -> bool:
        if self.noise_p99 > 0 and self.tolerance < self.noise_p99:
            raise ToleranceMiscalibrationError(
                f"tolerance {self.tolerance} < noise p99 {self.noise_p99}: "
                "false-positive rate will swamp"
            )
        if self.error_floor > 0 and self.tolerance > self.error_floor:
            raise ToleranceMiscalibrationError(
                f"tolerance {self.tolerance} > error floor {self.error_floor}: "
                "errors below tolerance are silently absorbed"
            )
        return True


def dispatch_tolerance_mode(gate: ToleranceGate) -> str:
    try:
        gate.verify()
    except ToleranceMiscalibrationError:
        return "re-derive-from-distributions"
    return "band-calibrated"
