# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-a-quorum-equivocation-tolerant-voting-and-the-heterogeneous-observer-trap-in-byzantine-llm-consensus.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-a-quorum-equivocation-tolerant-voting-and-the-heterogeneous-observer-trap-in-byzantine-llm-consensus.warrant.json
# Markdown SHA-256:   422a5978567e8d4415da31530f6befff32ddd2b4b2bc7341ca2cf55cede0d366
# Code AST SHA-256:   610bfc42e6ec99654e6547e7422901c827b6f33bf6d222bc182fefbf0f902b14
# Verified Classes:   QuorumEquivocationError, Vote
# Defensive Guards:   3 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass
from typing import Iterable


class QuorumEquivocationError(RuntimeError):
    """Raised when vote count clears threshold but supporting manifolds/evidence do not."""
    pass


@dataclass(frozen=True)
class Vote:
    instance_id: str
    model_family: str
    retrieval_backend: str
    decoding_seed_class: str
    verdict: str            # "APPROVE" | "DENY" | "ESCALATE"
    evidence_hashes: tuple  # content hashes of cited chunks / tool outputs

    def manifold(self) -> str:
        key = f"{self.model_family}|{self.retrieval_backend}|{self.decoding_seed_class}"
        return hashlib.sha256(key.encode()).hexdigest()


def certify_quorum(votes: Iterable[Vote], target_verdict: str, threshold: int, f: int) -> None:
    supporting = [v for v in votes if v.verdict == target_verdict]
    manifolds = {v.manifold() for v in supporting}
    evidence = {h for v in supporting for h in v.evidence_hashes}

    if len(supporting) < threshold:
        raise QuorumEquivocationError(
            f"vote count {len(supporting)} < threshold {threshold}"
        )
    # The non-obvious guard: arithmetic quorum is insufficient.
    if len(manifolds) < f + 1:
        raise QuorumEquivocationError(
            f"equivocation: {len(supporting)} votes collapse to {len(manifolds)} "
            f"manifolds < f+1={f + 1}; quorum is arithmetically satisfied but "
            f"semantically void (shared model/retrieval fault suspected)"
        )
    if len(evidence) < f + 1:
        raise QuorumEquivocationError(
            f"evidence provenance collapse: {len(supporting)} votes cite only "
            f"{len(evidence)} distinct artifacts < f+1={f + 1}; shared poisoned "
            f"source, not independent corroboration"
        )
    # Only here is certification legitimate.
    return None
