# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-equivocation-in-bft-agent-quorums-the-2f-1-overlap-illusion-under-non-uniform-stake.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-equivocation-in-bft-agent-quorums-the-2f-1-overlap-illusion-under-non-uniform-stake.warrant.json
# Markdown SHA-256:   a2c3ced3710f8004b16d877ebdd3be1c74b72dc84fade2742eee9f8ebe944dbd
# Code AST SHA-256:   83e097a312e1b7c869af1699453e282d7345174414b85c8896056fb57a547846
# Verified Classes:   QuorumOverlapViolation, Quorum
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Dict, Set


class QuorumOverlapViolation(RuntimeError):
    """Raised when two quorums intersect only in adversarial weight."""
    pass


@dataclass(frozen=True)
class Quorum:
    signers: Set[str]
    weight: int


def assert_honest_intersection(
    q1: Quorum,
    q2: Quorum,
    stake: Dict[str, int],
    colluding: Set[str],
    threshold: int,
) -> None:
    if q1.weight < threshold or q2.weight < threshold:
        raise QuorumOverlapViolation(
            f"quorum below threshold: {q1.weight}/{q2.weight} < {threshold}"
        )
    shared = q1.signers & q2.signers
    honest_shared_weight = sum(
        stake[s] for s in shared if s not in colluding
    )
    if honest_shared_weight < 1:
        raise QuorumOverlapViolation(
            "cardinality overlap satisfied but honest-weight intersection is "
            f"zero; shared={sorted(shared)} all colluding. "
            "Refusing to finalize: equivocation-safe commit impossible."
        )


# Reproduction of the observed trace.
stake = {"A": 40, "A1": 5, "A2": 5, "B": 20, "C": 15, "D": 15, "E": 10}
colluding = {"A", "A1", "A2"}
threshold = 67

q_commit_a = Quorum({"A", "A1", "A2", "B"}, 70)
q_commit_b = Quorum({"A", "A1", "A2", "C", "D"}, 80)

assert_honest_intersection(q_commit_a, q_commit_b, stake, colluding, threshold)
