# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-equivocation-by-a-quorum-member-as-a-liveness-fault-rather-than-a-safety-fault.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-equivocation-by-a-quorum-member-as-a-liveness-fault-rather-than-a-safety-fault.warrant.json
# Markdown SHA-256:   e0fde853d768cf38980b8875335c43575a0d73f9b25b5a5a73a36b72e708944b
# Code AST SHA-256:   5ec605d70bc4e5f700915e6f33c346c341732b5764495d6ceae70c161bce34b9
# Verified Classes:   QuorumEquivocationError, EquivocationClassifier
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class QuorumEquivocationError(RuntimeError):
    """Raised when equivocation classification cannot be resolved safely."""
    pass


class EquivocationClassifier:
    def __init__(self, n_replicas: int, f: int):
        if n_replicas != 3 * f + 1:
            raise QuorumEquivocationError(
                f"quorum size {n_replicas} inconsistent with f={f}; "
                f"expected {3 * f + 1}"
            )
        self.n = n_replicas
        self.f = f
        self.proven_byzantine: set[str] = set()

    def classify(self, equivocator: str, proof_valid: bool) -> str:
        if not proof_valid:
            # Unprovable equivocation is indistinguishable from partition/replay.
            # Acting on it is itself a liveness attack surface.
            return "NO_ACTION"

        self.proven_byzantine.add(equivocator)
        byz = len(self.proven_byzantine)
        honest = self.n - byz
        quorum = 2 * self.f + 1

        if byz > self.f:
            # Intersection guarantee broken: two conflicting quorums can form.
            raise QuorumEquivocationError(
                f"proven_byzantine={byz} exceeds f={self.f}; "
                f"safety halt required, manual reconciliation needed"
            )

        if honest < quorum:
            return "LIVENESS_DEGRADED_ESCALATE"

        return "LIVENESS_CONTINUE"


def handle_equivocation(
    classifier: EquivocationClassifier, equivocator: str, proof_valid: bool
) -> str:
    verdict = classifier.classify(equivocator, proof_valid)
    if verdict == "LIVENESS_CONTINUE":
        # Exclude equivocator from current quorum, proceed with remaining honest.
        return f"excluded {equivocator}; quorum still formable"
    if verdict == "LIVENESS_DEGRADED_ESCALATE":
        return f"excluded {equivocator}; remaining honest below quorum, escalate"
    # NO_ACTION
    return "ignored unprovable equivocation; no state change"
