# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-reconcile-speculative-side-effects-when-optimistic-execution-outruns-quorum-commitment-under-byzantine-quorum-scheduling.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-reconcile-speculative-side-effects-when-optimistic-execution-outruns-quorum-commitment-under-byzantine-quorum-scheduling.warrant.json
# Markdown SHA-256:   47cc8d94780924e7a52fb51b333d223a90935420f6a4829993801b2c8d29ed15
# Code AST SHA-256:   7c7be6a4b408f0878474259420e68571afa6203194a3faf318541d5049c19948
# Verified Classes:   SpeculativeCommitOrderingError
# Defensive Guards:   5 fail-closed invariants (zero external deps)

class SpeculativeCommitOrderingError(RuntimeError):
    """Raised when a speculative side effect becomes peer-visible before
    a quorum-signed epoch ticket binds it to the emitting branch."""


def admit_speculative_effect(
    branch_id: str,
    effect_id: str,
    emitter: str,
    epoch_ticket: dict | None,
    committed_epoch: int,
    effect_compensable: bool,
) -> None:
    """Peer-side admission gate for speculative effects under Byzantine quorum.

    Rejects any effect that is visible without a quorum ticket, or whose
    ticket is bound to an epoch the peer cannot reconcile. Non-compensable
    effects are rejected outright on the speculative path.
    """
    if not effect_compensable:
        raise SpeculativeCommitOrderingError(
            f"effect {effect_id} on branch {branch_id} is non-compensable; "
            f"it must not traverse the speculative lane"
        )

    if epoch_ticket is None:
        raise SpeculativeCommitOrderingError(
            f"effect {effect_id} from {emitter} on branch {branch_id} is "
            f"peer-visible with no quorum epoch ticket; provenance gap "
            f"cannot be compensated on branch rejection"
        )

    ticket_epoch = epoch_ticket.get("epoch")
    ticket_branch = epoch_ticket.get("branch_id")
    ticket_effect = epoch_ticket.get("effect_id")
    signatures = epoch_ticket.get("quorum_signatures", [])

    if ticket_branch != branch_id or ticket_effect != effect_id:
        raise SpeculativeCommitOrderingError(
            f"epoch ticket binding mismatch: ticket=({ticket_branch},{ticket_effect}) "
            f"effect=({branch_id},{effect_id}); possible equivocation or replay"
        )

    if ticket_epoch is None or ticket_epoch > committed_epoch:
        raise SpeculativeCommitOrderingError(
            f"epoch ticket for {effect_id} references epoch {ticket_epoch} "
            f"ahead of peer committed epoch {committed_epoch}; peer cannot "
            f"reconcile, refusing admission"
        )

    if len(signatures) < epoch_ticket.get("quorum_threshold", 0):
        raise SpeculativeCommitOrderingError(
            f"epoch ticket for {effect_id} carries {len(signatures)} signatures, "
            f"below quorum threshold {epoch_ticket.get('quorum_threshold')}"
        )
