# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-equivocation-in-a-tool-action-commit-quorum-when-the-adversary-controls-the-transport-layer.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-equivocation-in-a-tool-action-commit-quorum-when-the-adversary-controls-the-transport-layer.warrant.json
# Markdown SHA-256:   c04f857cc2d13f1a876d8f810aeaf2f27ef19cc57fff56167228c2e8769aedcd
# Code AST SHA-256:   29649ce18329c821f23fa4b9780c75c106bccaf3e6b43dd9c9438aa622ffc167
# Verified Classes:   PartitionedQuorumError, EquivocationProofError
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class PartitionedQuorumError(RuntimeError):
    """Raised when a commit quorum's vote sets are mutually inconsistent
    across honest peers, indicating a gossip-graph partition rather than
    a genuine agreement."""
    pass


class EquivocationProofError(RuntimeError):
    """Raised when two conflicting votes from the same sender are held
    in the same round — a genuine equivocation proof."""
    pass


def commit_tool_action(round_id: str,
                       local_votes: dict,
                       peer_vote_sets: dict,
                       relay_paths: dict,
                       quorum_size: int) -> str:
    """
    local_votes:      {sender_id: vote} as seen by this instance
    peer_vote_sets:   {peer_id: {sender_id: vote}} reported by peers
    relay_paths:      {peer_id: [relay_id, ...]} path each peer's vote took
    quorum_size:      required distinct yes-votes to commit
    """
    # 1. Genuine equivocation check: same sender, conflicting vote, same round.
    for peer_id, their_set in peer_vote_sets.items():
        for sender, their_vote in their_set.items():
            if sender in local_votes and local_votes[sender] != their_vote:
                raise EquivocationProofError(
                    f"round {round_id}: sender {sender} voted "
                    f"{local_votes[sender]!r} locally but {their_vote!r} "
                    f"per peer {peer_id}"
                )

    # 2. Connectivity precondition: distinct peers must have reached us
    #    over paths sharing no single relay. Without this, a threshold
    #    count is a local illusion, not a commit.
    relays_seen = {}
    for peer_id, path in relay_paths.items():
        for relay in path:
            relays_seen.setdefault(relay, set()).add(peer_id)

    # A single relay carrying votes from a majority of peers means the
    # adversary controls the only path between honest nodes.
    for relay, carried in relays_seen.items():
        if len(carried) >= (quorum_size + 1) // 2 + 1:
            raise PartitionedQuorumError(
                f"round {round_id}: relay {relay} carries votes from "
                f"{sorted(carried)}; gossip graph may be partitioned — "
                f"refusing commit despite threshold count"
            )

    # 3. Reconciliation: every peer's view of the round must agree on
    #    each sender's vote. Disagreement is a partition proof.
    for peer_id, their_set in peer_vote_sets.items():
        for sender, their_vote in their_set.items():
            if sender in local_votes and local_votes[sender] != their_vote:
                raise PartitionedQuorumError(
                    f"round {round_id}: peer {peer_id} saw {sender}="
                    f"{their_vote!r}, we saw {local_votes[sender]!r}; "
                    f"views diverge — halt commit"
                )

    yes = sum(1 for v in local_votes.values() if v == "yes")
    if yes < quorum_size:
        raise PartitionedQuorumError(
            f"round {round_id}: only {yes}/{quorum_size} yes-votes after "
            f"connectivity and reconciliation checks"
        )
    return "committed"
