# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-byzantine-agent-s-equivocation-as-a-quorum-valid-vote-during-multi-agent-decision-consensus.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-byzantine-agent-s-equivocation-as-a-quorum-valid-vote-during-multi-agent-decision-consensus.warrant.json
# Markdown SHA-256:   d20a155a74913645546d68b1d48f67cecfe2ecc0604b5ecaaf40a9d3da9bc666
# Code AST SHA-256:   a81ee6b97a14e4e1155892c3d617e25c9f67e3cc6c2c104e7074e6bb3de03ba3
# Verified Classes:   EquivocationQuorumError, EpochTally
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class EquivocationQuorumError(RuntimeError):
    """Raised when an epoch's admitted votes cannot form a quorum
    after equivocating agents have been withdrawn."""
    pass


class EpochTally:
    def __init__(self, epoch: str, agent_ids: set[str], threshold: int):
        self.epoch = epoch
        self.agent_ids = set(agent_ids)
        self.threshold = threshold
        self.admitted: dict[str, str] = {}   # agent_id -> choice
        self.faulty: set[str] = set()

    def admit(self, agent_id: str, choice: str) -> None:
        if agent_id not in self.agent_ids:
            raise EquivocationQuorumError(
                f"vote from unknown agent {agent_id} in epoch {self.epoch}"
            )
        if agent_id in self.faulty:
            return  # already quarantined this epoch
        prior = self.admitted.get(agent_id)
        if prior is None:
            self.admitted[agent_id] = choice
            return
        if prior == choice:
            return  # benign duplicate, do not increment
        # Equivocation: withdraw the prior vote AND quarantine the agent.
        del self.admitted[agent_id]
        self.faulty.add(agent_id)

    def certificate(self) -> str:
        # Recompute against the reduced honest set, never fall back to plurality.
        counts: dict[str, int] = {}
        for choice in self.admitted.values():
            counts[choice] = counts.get(choice, 0) + 1
        winners = [c for c, n in counts.items() if n >= self.threshold]
        if len(winners) != 1:
            raise EquivocationQuorumError(
                f"epoch {self.epoch}: no unique quorum after withdrawing "
                f"faulty={sorted(self.faulty)}; honest tally={counts}; "
                f"threshold={self.threshold}"
            )
        return winners[0]


# Trace of the observed incident:
t = EpochTally("E7", {"A", "B", "C", "D", "E"}, threshold=4)
for aid, ch in [("A", "accept"), ("B", "accept"),
                ("C", "accept"), ("D", "reject"), ("E", "reject"),
                ("C", "reject")]:          # second C vote arrives via gossip
    t.admit(aid, ch)

try:
    t.certificate()   # C withdrawn -> accept=2, reject=2 -> no quorum
except EquivocationQuorumError as e:
    raise EquivocationQuorumError(
        f"aborting epoch; do NOT ratify on plurality: {e}"
    ) from e
