# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-equivocates-across-quorum-views-in-llm-agent-byzantine-consensus.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-equivocates-across-quorum-views-in-llm-agent-byzantine-consensus.warrant.json
# Markdown SHA-256:   4dfcc7ba7d3504574b34cc55b7f0b12f42d74625565a8036a648105397b82b5e
# Code AST SHA-256:   2154b4db42cdfd7156611d7764ea14a1a9de9672c2d93c9f9be4ff99a4b1bfc4
# Verified Classes:   EquivocationMisclassifiedError, Vote
# Defensive Guards:   2 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass


class EquivocationMisclassifiedError(RuntimeError):
    """Raised when a warm (nondeterministic-but-honest) signer is ejected as Byzantine."""


@dataclass(frozen=True)
class Vote:
    round: int
    proposal_hash: str
    vote: str            # canonical semantic field: "commit" | "abort"
    rationale_hash: str  # NOT part of the signed commitment


def canon(v: Vote) -> str:
    """Canonical commitment surface: excludes rationale and any resampled content."""
    payload = f"{v.round}|{v.proposal_hash}|{v.vote}"
    return hashlib.sha256(payload.encode()).hexdigest()


def classify_divergence(a: Vote, b: Vote) -> str:
    if a.round != b.round:
        raise EquivocationMisclassifiedError(
            "votes from different rounds compared; not an equivocation check"
        )
    if canon(a) == canon(b):
        # Same semantic commitment, different rationale -> warm, not Byzantine.
        return "warm"
    # Different canonical value for the same round -> genuine Byzantine equivocation.
    return "byzantine"


def respond_to_divergence(a: Vote, b: Vote, signer: str) -> str:
    kind = classify_divergence(a, b)
    if kind == "warm":
        # Accept either certificate; do not eject. Flag for rationale-variance review.
        return f"accept_either; monitor {signer} for rationale variance"
    if kind == "byzantine":
        # Only here is ejection justified, and only with both canonical fields witnessed.
        return f"eject {signer}; canonical fields diverge on round {a.round}"
    raise EquivocationMisclassifiedError(f"unhandled divergence kind: {kind}")
