# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-quorum-intersection-collapse-under-equivocating-proposers-in-bft-multi-agent-decision-quorums.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-quorum-intersection-collapse-under-equivocating-proposers-in-bft-multi-agent-decision-quorums.warrant.json
# Markdown SHA-256:   6dc3356637802cdc28421d1179b32606f143a699b8847c94e8d899b4c2a970c8
# Code AST SHA-256:   68cada1f3fbbd2a2abb2a19ea004c3db1ff81334281b1f247b155f8697a32d76
# Verified Classes:   QuorumIntersectionCollapseError
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class QuorumIntersectionCollapseError(RuntimeError):
    """Raised when two conflicting quorum certificates intersect only on
    agents whose votes are not provably unique per (agent, view)."""
    pass


def validate_decision_quorum(
    new_cert: dict,
    prior_certs: list[dict],
    quorum_size: int,
    fault_budget: int,
) -> None:
    """
    new_cert: {"value": ..., "view": int, "signers": {agent_id: signed_view}}
    prior_certs: list of same shape, conflicting values only.
    Enforces (I1) vote-uniqueness and (I2) lock propagation at the
    intersection boundary. Raises on collapse.
    """
    signers = new_cert["signers"]
    if len(signers) < quorum_size:
        raise QuorumIntersectionCollapseError(
            f"quorum too small: {len(signers)} < {quorum_size}"
        )

    for prior in prior_certs:
        if prior["value"] == new_cert["value"]:
            continue
        shared = set(signers) & set(prior["signers"])
        # Counting intersection is guaranteed; usefulness is not.
        if len(shared) < fault_budget + 1:
            raise QuorumIntersectionCollapseError(
                f"insufficient shared members: {len(shared)} <= f={fault_budget}"
            )
        # The real check: every shared agent must be vote-unique per view.
        consistent = [
            a for a in shared
            if signers[a] != prior["signers"].get(a)
        ]
        if not consistent:
            raise QuorumIntersectionCollapseError(
                "intersection is non-empty but every shared agent "
                "equivocated across views; quorum intersection is vacuous"
            )
        # Lock propagation: at least one shared agent must have a strictly
        # greater view in the new cert, proving it saw the prior certificate.
        if not any(signers[a] > prior["signers"][a] for a in consistent):
            raise QuorumIntersectionCollapseError(
                "no shared agent advanced view; lock certificate not propagated"
            )
