# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-draw-the-line-between-byzantine-equivocation-and-legitimate-non-determinism-during-multi-agent-task-commits.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-draw-the-line-between-byzantine-equivocation-and-legitimate-non-determinism-during-multi-agent-task-commits.warrant.json
# Markdown SHA-256:   69e43b27b9cf3361a3b298a08bf7d91c6a0c9a74032b2560bdfa2f972d2ce596
# Code AST SHA-256:   8bf68f5cbf183d37160058c7506343d817fc47cd996e5d83f8bef147fd480083
# Verified Classes:   EquivocationProofError, Vote
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass
from typing import Optional


class EquivocationProofError(RuntimeError):
    """Raised when a slashing proof is constructed on unsound grounds."""
    pass


@dataclass(frozen=True)
class Vote:
    replica_id: str
    slot: int
    attempt_nonce: str        # generated BEFORE transcript sampling
    digest: str               # hash over frozen transcript
    signature: bytes


def verify_equivocation_proof(vote_a: Vote, vote_b: Vote) -> None:
    """
    Enforce the boundary: equivocation is provable ONLY when both votes
    target the same slot AND the same attempt-nonce with divergent digests.
    Distinct nonces => legitimate non-deterministic re-vote => supersession.
    """
    if vote_a.replica_id != vote_b.replica_id:
        raise EquivocationProofError(
            f"votes from different replicas ({vote_a.replica_id} vs "
            f"{vote_b.replica_id}); cannot constitute equivocation"
        )

    if vote_a.slot != vote_b.slot:
        raise EquivocationProofError(
            f"votes target different slots ({vote_a.slot} vs {vote_b.slot}); "
            "not an equivocation for any single slot"
        )

    if vote_a.attempt_nonce != vote_b.attempt_nonce:
        # This is the critical branch: non-determinism, NOT malice.
        raise EquivocationProofError(
            f"distinct attempt-nonces ({vote_a.attempt_nonce!r} vs "
            f"{vote_b.attempt_nonce!r}) for slot {vote_a.slot}: this is a "
            "legitimate superseding re-vote from a non-deterministic agent, "
            "not Byzantine equivocation. Reject the slashing proof; apply "
            "last-writer-wins under nonce ordering."
        )

    if vote_a.digest == vote_b.digest:
        raise EquivocationProofError(
            "identical digests under identical nonce; no conflict exists"
        )

    # Same replica, same slot, same nonce, divergent digest => provable.
    return None


def construct_slashing_evidence(vote_a: Vote, vote_b: Vote) -> dict:
    verify_equivocation_proof(vote_a, vote_b)
    proof_id = hashlib.sha256(
        (vote_a.replica_id + str(vote_a.slot) + vote_a.attempt_nonce).encode()
    ).hexdigest()
    return {
        "replica": vote_a.replica_id,
        "slot": vote_a.slot,
        "nonce": vote_a.attempt_nonce,
        "digest_a": vote_a.digest,
        "digest_b": vote_b.digest,
        "proof_id": proof_id,
        "verdict": "BYZANTINE_EQUIVOCATION",
    }
