# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-equivocates-during-bft-commitment-quorum-certificate-divergence-under-partial-synchrony.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-equivocates-during-bft-commitment-quorum-certificate-divergence-under-partial-synchrony.warrant.json
# Markdown SHA-256:   e8c67f54de4075f247f93c9d60f592dc579ef0c6cd21cf9beab6ae01d1130117
# Code AST SHA-256:   ec79d26d0b0c063e7602bb5afe474fdf5f7a45f306f55196ff4140ce6e9bea21
# Verified Classes:   EquivocationDetected, QuorumCertificate
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class EquivocationDetected(RuntimeError):
    """Raised when two valid quorum certificates conflict at the same height/round."""
    pass


class QuorumCertificate:
    def __init__(self, height: int, round_: int, value: str, signers: frozenset):
        self.height = height
        self.round = round_
        self.value = value
        self.signers = signers

    def conflicts_with(self, other: "QuorumCertificate") -> bool:
        return (
            self.height == other.height
            and self.round == other.round
            and self.value != other.value
            and bool(self.signers & other.signers)  # overlapping signers => equivocation
        )


def safe_commit(qc: QuorumCertificate, observed_qcs: list[QuorumCertificate],
                quorum_size: int) -> str:
    if len(qc.signers) < quorum_size:
        raise EquivocationDetected(
            f"QC at h={qc.height} r={qc.round} has {len(qc.signers)} signers, "
            f"below quorum {quorum_size}"
        )
    for other in observed_qcs:
        if qc.conflicts_with(other):
            overlap = qc.signers & other.signers
            raise EquivocationDetected(
                f"Conflicting QCs at h={qc.height} r={qc.round}: "
                f"value {qc.value!r} vs {other.value!r}; "
                f"equivocating signers {sorted(overlap)}. HALT: do not commit."
            )
    # No conflict observed AND quorum met -> commit is safe
    return qc.value
