# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-draw-the-line-between-a-byzantine-peer-s-equivocating-vote-and-a-legitimate-partial-observation-when-committing-shared-task-sta.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-draw-the-line-between-a-byzantine-peer-s-equivocating-vote-and-a-legitimate-partial-observation-when-committing-shared-task-sta.warrant.json
# Markdown SHA-256:   cfdd46e15b7bbdf5d75c096f2e47020f73bcd1fec4cd3b2f5067f9c6cb8b20ee
# Code AST SHA-256:   b123e53068361d0da1f33ca2b138438ca7a8521fe4e7981f3043d7cc50c787b3
# Verified Classes:   EquivocationProofError
# Defensive Guards:   1 fail-closed invariants (zero external deps)

class EquivocationProofError(RuntimeError):
    """Raised when a vote pair cannot be classified as fault vs. stale re-delivery."""
    pass


def classify_vote(sender, new_vote, last_seen, cert_store):
    """
    Returns 'COMMIT_ACCEPT', 'DEDUP_STALE', or raises EquivocationProofError.
    last_seen: dict sender -> (proposal_id, view, cert_hash)
    """
    prev = last_seen.get(sender)
    if prev is None:
        return "COMMIT_ACCEPT"

    prev_pid, prev_view, prev_cert = prev
    pid, view, cert = new_vote["proposal_id"], new_vote["view"], new_vote["prev_cert"]

    if view < prev_view:
        # stale re-delivery after context eviction; never punish
        return "DEDUP_STALE"

    if view == prev_view and pid != prev_pid:
        # conflicting same-view votes: fault ONLY if a signed advance exists
        advance = cert_store.get_signed_advance(sender, view)
        if advance is None:
            # no certificate -> treat as eviction artifact, dedup by cert hash
            return "DEDUP_STALE"
        if advance.cert_hash != prev_cert and advance.cert_hash != cert:
            raise EquivocationProofError(
                f"{sender} signed advance to view {view} but voted for "
                f"{prev_pid} and {pid} with mismatched certs"
            )
        return "DEDUP_STALE"

    return "COMMIT_ACCEPT"
