# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-equivocating-peers-when-byzantine-consensus-quorums-overlap-across-two-task-commitment-epochs.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-equivocating-peers-when-byzantine-consensus-quorums-overlap-across-two-task-commitment-epochs.warrant.json
# Markdown SHA-256:   a92723784fea664175662dbc223b695a81c8cf5d84e974c981ddd4d9a1761a84
# Code AST SHA-256:   5468c974f8bbf2dff732c7990cfc6309fe30b1b666e696f8a64cf3cbb44cf8de
# Verified Classes:   EpochBoundaryEquivocationError, Commitment
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class EpochBoundaryEquivocationError(RuntimeError):
    """Raised when cross-epoch quorum intersection cannot be proven honest."""
    pass


class Commitment:
    def __init__(self, payload: str, epoch: int, signatures: dict[str, int]):
        self.payload = payload
        self.epoch = epoch
        self.signatures = signatures  # agent_id -> weight at signing epoch


def verify_cross_epoch_intersection(
    q_prior: Commitment,
    q_next: Commitment,
    honest_agents: set[str],
    prior_weights: dict[str, int],
    next_weights: dict[str, int],
    prior_threshold: int,
    next_threshold: int,
) -> bool:
    # Both quorums must independently meet their epoch thresholds.
    if sum(prior_weights[a] for a in q_prior.signatures) < prior_threshold:
        raise EpochBoundaryEquivocationError(
            f"prior quorum {q_prior.signatures} below threshold {prior_threshold}"
        )
    if sum(next_weights[a] for a in q_next.signatures) < next_threshold:
        raise EpochBoundaryEquivocationError(
            f"next quorum {q_next.signatures} below threshold {next_threshold}"
        )

    # Conflicting payloads across epochs require honest intersection.
    if q_prior.payload != q_next.payload:
        intersection = set(q_prior.signatures) & set(q_next.signatures)
        honest_in_intersection = intersection & honest_agents
        if not honest_in_intersection:
            raise EpochBoundaryEquivocationError(
                f"conflicting commitments {q_prior.payload!r} (epoch {q_prior.epoch}) "
                f"vs {q_next.payload!r} (epoch {q_next.epoch}) share only Byzantine "
                f"signers {intersection}; no honest cross-epoch witness"
            )

    # If the epoch transition itself was not committed, refuse to proceed.
    if q_prior.epoch + 1 != q_next.epoch:
        raise EpochBoundaryEquivocationError(
            f"non-adjacent epochs {q_prior.epoch} -> {q_next.epoch}; "
            f"transition state unverifiable"
        )
    return True
