# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-halt-when-a-byzantine-tolerant-quorum-commits-on-equivocal-agent-votes.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-halt-when-a-byzantine-tolerant-quorum-commits-on-equivocal-agent-votes.warrant.json
# Markdown SHA-256:   ca756f3a31f6c2331cb320ffa3d9df7b9e235d71436a27e17c3ac87143f5516b
# Code AST SHA-256:   1033ed41dd230aee4be75f57b7659148519f6d9ac754903972cecabacbdbe8e5
# Verified Classes:   EquivocationCommitHalt
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class EquivocationCommitHalt(RuntimeError):
    """Raised when a locally-held commit certificate cannot be proven safe
    against a competing certificate for the same (round, proposal_id)."""
    pass


def verify_commit_certificate(
    own_cert: dict,
    competing_certs: list[dict],
    n: int,
    f: int,
    threshold: int,
) -> str:
    """
    own_cert: {"round": int, "proposal_id": str, "value": str,
               "signers": set[str], "self_id": str}
    competing_certs: certificates for the same (round, proposal_id)
                     with a different value.
    Returns the committed value if safe to execute.
    Raises EquivocationCommitHalt if the instance must halt.
    """
    signers = set(own_cert["signers"])
    self_id = own_cert["self_id"]

    # Invariant 1: we must be able to name threshold-f OTHER honest signers.
    others = signers - {self_id}
    if len(others) < threshold - f:
        raise EquivocationCommitHalt(
            f"insufficient independent signers: have {len(others)}, "
            f"need >= {threshold - f} (self excluded)"
        )

    for comp in competing_certs:
        if comp["value"] == own_cert["value"]:
            continue
        overlap = signers & set(comp["signers"])
        # Invariant 2: a non-empty overlap on contradictory values means
        # every overlapping signer double-signed. If any of them is not
        # provably Byzantine (i.e., we cannot name f+1 such signers),
        # the certificate is unsafe.
        if overlap and len(overlap) < f + 1:
            raise EquivocationCommitHalt(
                f"equivocation suspected on round {own_cert['round']}: "
                f"overlap={sorted(overlap)} (< f+1={f+1} proven Byzantine); "
                f"own_value={own_cert['value']} competing={comp['value']}"
            )
        # Invariant 3: pigeonhole — union exceeding N proves double-signing.
        if len(signers | set(comp["signers"])) > n:
            raise EquivocationCommitHalt(
                f"pigeonhole double-sign: union size "
                f"{len(signers | set(comp['signers']))} > N={n}"
            )

    return own_cert["value"]
