# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-quorum-poisoning-in-byzantine-llm-consensus-when-agents-commit-to-shared-state-before-result-agreement.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-quorum-poisoning-in-byzantine-llm-consensus-when-agents-commit-to-shared-state-before-result-agreement.warrant.json
# Markdown SHA-256:   1b0459152050964b96fe44f838b4654f90ef5c805506dd5205e3a294caec5c53
# Code AST SHA-256:   12dced7d64937e3b9667ca259ed313860c705c77285e3fef93da69d85c52ea08
# Verified Classes:   QuorumPoisoningError
# Defensive Guards:   1 fail-closed invariants (zero external deps)

class QuorumPoisoningError(RuntimeError):
    """Raised when a BFT quorum is satisfied by votes whose evidence sets
    are not sufficiently disjoint to bound correlated LLM faults."""
    pass


def validate_quorum(votes, threshold, max_evidence_jaccard=0.8):
    """
    votes: list of dicts with keys 'verdict', 'evidence' (set of span ids),
           'rationale' (str).
    threshold: 2f+1 integer.
    Raises QuorumPoisoningError if verdict consensus is reached but the
    quorum's evidence sets are too similar to be independent.
    """
    if len(votes) < threshold:
        return False  # no quorum; standard BFT path

    verdicts = [v["verdict"] for v in votes]
    winning = max(set(verdicts), key=verdicts.count)
    quorum = [v for v in votes if v["verdict"] == winning]

    if len(quorum) < threshold:
        return False

    # Evidence-diversity check across the quorum.
    for i in range(len(quorum)):
        for j in range(i + 1, len(quorum)):
            a, b = quorum[i]["evidence"], quorum[j]["evidence"]
            union = a | b
            if not union:
                continue
            jaccard = len(a & b) / len(union)
            if jaccard > max_evidence_jaccard:
                raise QuorumPoisoningError(
                    f"Quorum verdict '{winning}' reached by {len(quorum)} votes "
                    f"with evidence Jaccard {jaccard:.2f} > {max_evidence_jaccard}; "
                    f"votes are correlated, not independent. "
                    f"Re-dispatch with perturbed retrieval slices. "
                    f"Dissenting rationales: "
                    f"{[v['rationale'] for v in votes if v['verdict'] != winning]}"
                )
    return True
