# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-quorum-attestation-is-unsound-in-byzantine-multi-agent-commitment-the-equivocation-blind-supermajority.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-quorum-attestation-is-unsound-in-byzantine-multi-agent-commitment-the-equivocation-blind-supermajority.warrant.json
# Markdown SHA-256:   f55010f326558da6740be119015c253d1b1e91a1e45a00bde0b077415d3650c1
# Code AST SHA-256:   2abcf4e14fc5d01286fea15e277e033b4132a5196a5d69975b8f8f363273ca10
# Verified Classes:   EquivocationDetected
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class EquivocationDetected(RuntimeError):
    """Raised when a quorum certificate contains a signer that voted twice in a view."""
    pass


def verify_quorum_certificate(cert, f, task_id, view):
    # cert: list of dicts with keys agent_id, epoch, result_hash, vote_seq, signature
    threshold = 2 * f + 1
    seen = {}          # (agent_id, epoch) -> (result_hash, vote_seq)
    signers = set()

    for att in cert:
        if att["task_id"] != task_id or att["view"] != view:
            raise EquivocationDetected(
                f"attestation out of scope: task={att['task_id']} view={att['view']}"
            )
        key = (att["agent_id"], att["epoch"])
        if key in seen:
            prev_hash, prev_seq = seen[key]
            if prev_hash != att["result_hash"] or att["vote_seq"] <= prev_seq:
                raise EquivocationDetected(
                    f"signer {key} equivocated: {prev_hash}@{prev_seq} vs "
                    f"{att['result_hash']}@{att['vote_seq']}"
                )
        seen[key] = (att["result_hash"], att["vote_seq"])
        signers.add(key)

    if len(signers) < threshold:
        raise EquivocationDetected(
            f"insufficient distinct signers: {len(signers)} < {threshold}"
        )

    # All signers must agree on a single result hash for this view.
    hashes = {h for (h, _) in seen.values()}
    if len(hashes) != 1:
        raise EquivocationDetected(
            f"conflicting result hashes in single view: {hashes}"
        )
    return next(iter(hashes))
