# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-to-commit-to-a-byzantine-quorum-equivocation-induced-commitment-deadlock-in-multi-agent-shared-state-agreement.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-to-commit-to-a-byzantine-quorum-equivocation-induced-commitment-deadlock-in-multi-agent-shared-state-agreement.warrant.json
# Markdown SHA-256:   1357d160bfaf7d7db3822be6792bcc4cc1ac0049466cd67f8717ea61bce85cca
# Code AST SHA-256:   8f6058581afd15c8f766dd1230f113e82bfdefc7ea2a50cb2d0a4c3f71f90173
# Verified Classes:   EquivocationSuspected, CommitmentRefused
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class EquivocationSuspected(RuntimeError):
    """Raised when an instance cannot corroborate a proposal hash with any honest peer."""
    pass


class CommitmentRefused(RuntimeError):
    """Raised when an instance refuses to vote on an uncorroborated proposal."""
    pass


def cast_commitment_vote(
    slot_id: str,
    local_proposal_hash: str,
    peer_confirmations: dict[str, str],
    honest_peers: set[str],
    quorum_threshold: int,
) -> str:
    """
    Cast a commitment vote for `slot_id` only if at least one honest peer
    confirms the identical proposal hash. Otherwise refuse and raise.

    peer_confirmations maps peer_id -> proposal_hash they received for this slot.
    honest_peers is the set of peers currently believed honest.
    """
    corroborating = [
        peer for peer, h in peer_confirmations.items()
        if peer in honest_peers and h == local_proposal_hash
    ]

    # Detect equivocation: an honest peer saw a *different* hash for the same slot.
    conflicting = [
        peer for peer, h in peer_confirmations.items()
        if peer in honest_peers and h != local_proposal_hash
    ]

    if conflicting:
        # Two honest peers disagree on the proposal identity for one slot.
        # This is the equivocation signature. Do not vote; surface it.
        raise EquivocationSuspected(
            f"slot={slot_id} local_hash={local_proposal_hash} "
            f"conflicting_peers={conflicting} "
            f"their_hashes={[peer_confirmations[p] for p in conflicting]}"
        )

    if not corroborating:
        # No honest peer confirmed our hash within the window.
        # Voting here is the crash-fault assumption that equivocation exploits.
        raise CommitmentRefused(
            f"slot={slot_id} hash={local_proposal_hash}: "
            f"zero corroborating honest peers; refusing to vote to avoid "
            f"contributing to a sub-threshold split"
        )

    # Corroborated: safe to vote.
    return local_proposal_hash
