# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-silent-divergence-when-byzantine-agents-equivocate-on-task-commitment-ballots.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-silent-divergence-when-byzantine-agents-equivocate-on-task-commitment-ballots.warrant.json
# Markdown SHA-256:   00fcc74c5dad53c2b7ca67dfc292e5675b7038c3cd3069f3a5e68aa786a9472c
# Code AST SHA-256:   7a0bc6642841ffd21919a91c1ec1f815de2dfa64d0e69131c7e10433d9a0b021
# Verified Classes:   QuorumIntersectionViolation
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class QuorumIntersectionViolation(RuntimeError):
    """Raised when two committed quorums intersect in fewer than f+1 honest witnesses."""
    pass


def verify_commit_safety(
    cert_a: dict,
    cert_b: dict,
    f: int,
    honest_signers: set[str],
) -> None:
    """
    Enforce the weighted quorum-intersection invariant.

    cert_x = {"seq": int, "value": str, "signers": set[str], "ballots": {signer: (seq, value)}}

    Raises QuorumIntersectionViolation if:
      - the two certificates commit different values at the same sequence number, AND
      - their signer intersection does not contain at least f+1 honest witnesses
        who can each prove equivocation with two signed ballots.
    """
    if cert_a["seq"] != cert_b["seq"]:
        # Different rounds: not an equivocation conflict, but must be ordered.
        return

    if cert_a["value"] == cert_b["value"]:
        return  # agreement, no split

    intersection = cert_a["signers"] & cert_b["signers"]
    honest_witnesses = intersection & honest_signers

    if len(honest_witnesses) < f + 1:
        raise QuorumIntersectionViolation(
            f"Conflicting commits at seq={cert_a['seq']} "
            f"({cert_a['value']!r} vs {cert_b['value']!r}); "
            f"intersection={intersection} honest_witnesses={honest_witnesses} "
            f"< required f+1={f + 1}. State split is unattributable; halt."
        )

    # Attribution gate: every honest witness must expose both signed ballots.
    for w in honest_witnesses:
        ba = cert_a["ballots"].get(w)
        bb = cert_b["ballots"].get(w)
        if ba is None or bb is None or ba == bb:
            raise QuorumIntersectionViolation(
                f"Honest witness {w} cannot produce two distinct signed ballots "
                f"for seq={cert_a['seq']}; equivocation unprovable, halt."
            )
    # If we reach here, equivocation is proven and the signer is slashable.
