# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-bound-byzantine-equivocation-in-multi-agent-task-commitment-when-quorum-certificates-are-replayed-across-epochs.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-bound-byzantine-equivocation-in-multi-agent-task-commitment-when-quorum-certificates-are-replayed-across-epochs.warrant.json
# Markdown SHA-256:   8a39ee20bd17c86a32961287bc73facc4a8d1f6601fb84eada53424340691f87
# Code AST SHA-256:   b58034f34f259abaad6beef9487e6460090368c866f17191b59ae7246f385fb5
# Verified Classes:   EquivocationDetected, CommitRegister
# Defensive Guards:   5 fail-closed invariants (zero external deps)

class EquivocationDetected(RuntimeError):
    """Raised when a replayed or conflicting quorum certificate is presented
    for a task slot whose commit register already holds a different commitment."""
    pass


class CommitRegister:
    def __init__(self):
        # slot_id -> (commit_seq, content_hash, epoch)
        self._register = {}

    def finalize(self, slot_id, commit_seq, content_hash, epoch):
        prior = self._register.get(slot_id)
        if prior is not None:
            prior_seq, prior_hash, _ = prior
            if commit_seq < prior_seq:
                raise EquivocationDetected(
                    f"stale certificate for slot {slot_id}: "
                    f"seq {commit_seq} < finalized {prior_seq}"
                )
            if commit_seq == prior_seq and content_hash != prior_hash:
                raise EquivocationDetected(
                    f"equivocation on slot {slot_id} at seq {commit_seq}: "
                    f"{content_hash} != {prior_hash}"
                )
        self._register[slot_id] = (commit_seq, content_hash, epoch)


def verify_cross_epoch_certificate(register, slot_id, cert, current_epoch):
    """Accept a quorum certificate only if it is epoch-bound, non-superseded,
    and consistent with the monotonic commit register."""
    if cert.epoch == current_epoch and slot_id not in register._register:
        return True  # local, same-epoch, first commitment

    if cert.epoch < current_epoch:
        if cert.supersession_proof is None:
            raise EquivocationDetected(
                f"certificate for slot {slot_id} from epoch {cert.epoch} "
                f"presented at epoch {current_epoch} without supersession proof"
            )

    prior = register._register.get(slot_id)
    if prior is not None:
        prior_seq, prior_hash, _ = prior
        if cert.commit_seq == prior_seq and cert.content_hash != prior_hash:
            raise EquivocationDetected(
                f"conflicting certificate for slot {slot_id}: "
                f"{cert.content_hash} vs finalized {prior_hash}"
            )
        if cert.commit_seq < prior_seq:
            raise EquivocationDetected(
                f"certificate for slot {slot_id} regresses commit_seq "
                f"{cert.commit_seq} below {prior_seq}"
            )

    register.finalize(slot_id, cert.commit_seq, cert.content_hash, cert.epoch)
    return True
