# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-distrust-a-byzantine-quorum-it-has-already-counted-equivocation-tolerant-commitment-with-f-1-witness-replay.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-distrust-a-byzantine-quorum-it-has-already-counted-equivocation-tolerant-commitment-with-f-1-witness-replay.warrant.json
# Markdown SHA-256:   10cc474fc1df0b21ac58269bbf4f5ab509f4a6b66d29d586df345a9febc6758e
# Code AST SHA-256:   120eb4682485bf1b39471116bccb5c3254a0ced7cc6a9bea666b8c7dbc3da53a
# Verified Classes:   EquivocatedQuorumError, Vote
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class EquivocatedQuorumError(RuntimeError):
    """Raised when a commitment quorum is assembled from relayed votes
    that cannot be proven to be non-equivocating in the current view."""
    pass


class Vote:
    def __init__(self, signer, proposal_id, view, nonce, direct):
        self.signer = signer
        self.proposal_id = proposal_id
        self.view = view
        self.nonce = nonce
        self.direct = direct  # True if received directly from signer


def commit_quorum(votes, proposal_id, view, f, issued_nonces, n):
    """Commit only on 2f+1 DIRECT, nonce-bound votes for one proposal in one view.
    Relayed votes are revocable evidence and may not enter the monotonic quorum."""
    monotonic = [
        v for v in votes
        if v.direct
        and v.proposal_id == proposal_id
        and v.view == view
        and v.nonce in issued_nonces
    ]
    distinct_signers = {v.signer for v in monotonic}

    if len(distinct_signers) < 2 * f + 1:
        relayed = [
            v for v in votes
            if not v.direct and v.proposal_id == proposal_id and v.view == view
        ]
        if len({v.signer for v in relayed}) > f + 1:
            # A relayed bundle claims a quorum we did not directly observe.
            # This is the equivocation attack surface. Refuse to commit.
            raise EquivocatedQuorumError(
                f"view {view}: relayed bundle claims "
                f"{len({v.signer for v in relayed})} signers for {proposal_id}, "
                f"but only {len(distinct_signers)} direct nonce-bound votes held; "
                f"enter reconciliation, do not commit"
            )
        # Insufficient even with relays: liveness stall, not a safety breach.
        raise EquivocatedQuorumError(
            f"view {view}: only {len(distinct_signers)} direct votes for "
            f"{proposal_id}, need {2 * f + 1}"
        )

    # Safety check: no direct signer may appear on a conflicting proposal in this view.
    for v in monotonic:
        conflicting = [
            w for w in votes
            if w.signer == v.signer
            and w.view == view
            and w.proposal_id != proposal_id
        ]
        if conflicting:
            raise EquivocatedQuorumError(
                f"signer {v.signer} equivocated in view {view}: "
                f"signed {proposal_id} and {conflicting[0].proposal_id}; evict and re-run view"
            )

    return True
