# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-an-a2a-delegation-receipt-whose-warrant-chain-is-valid-but-whose-nonce-namespace-was-minted-under-a-diff.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-an-a2a-delegation-receipt-whose-warrant-chain-is-valid-but-whose-nonce-namespace-was-minted-under-a-diff.warrant.json
# Markdown SHA-256:   e6ee0e176b4a520c5c56bff71d0927e3101e093189848200a75b3d1635c54a2a
# Code AST SHA-256:   00ef362273fc6dad2b8b4bf878f730bb5fb527d01c4a7007d0bab78ba6e3b6dd
# Verified Classes:   DisputeEpochMismatch, EpochBridgeUnavailable, Receipt, Warrant
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class DisputeEpochMismatch(RuntimeError):
    """Receipt is cryptographically valid but inadmissible in the dispute forum
    scoped to the warrant's epoch. Must not be accepted 'for now'."""


class EpochBridgeUnavailable(DisputeEpochMismatch):
    """Cross-epoch receipt with no obtainable countersigned bridge attestation."""


@dataclass(frozen=True)
class Receipt:
    dispute_epoch: int
    execution_nonces: tuple
    registry_ref: str
    bridge_attestation: Optional[str] = None


@dataclass(frozen=True)
class Warrant:
    dispute_epoch: int
    dispute_epoch_registry_ref: str
    expires_at: float


def accept_receipt(receipt: Receipt, warrant: Warrant, now: float,
                   bridge_obtainable: bool) -> None:
    # Cheap checks first: these are the ones everyone runs.
    if receipt.registry_ref != warrant.dispute_epoch_registry_ref:
        raise DisputeEpochMismatch(
            f"receipt registry {receipt.registry_ref} != warrant registry "
            f"{warrant.dispute_epoch_registry_ref}"
        )

    # The boundary that is almost always omitted.
    if receipt.dispute_epoch == warrant.dispute_epoch:
        return  # admissible; nothing to bridge

    if receipt.dispute_epoch < warrant.dispute_epoch:
        # Delegatee minted under a past epoch: replay or rollback. Never bridge.
        raise DisputeEpochMismatch(
            f"receipt epoch {receipt.dispute_epoch} < warrant epoch "
            f"{warrant.dispute_epoch}; possible nonce replay"
        )

    # receipt.dispute_epoch > warrant.dispute_epoch: honest fast-cadence delegatee
    # or attack. Admissible only with a countersigned bridge recorded BEFORE
    # acceptance, and only if the warrant is still live.
    if now >= warrant.expires_at:
        raise EpochBridgeUnavailable(
            "warrant expired; cannot obtain bridge before acceptance"
        )
    if receipt.bridge_attestation is None:
        if not bridge_obtainable:
            raise EpochBridgeUnavailable(
                f"receipt epoch {receipt.dispute_epoch} > warrant epoch "
                f"{warrant.dispute_epoch}; no bridge obtainable, refusing to "
                f"accept an inadmissible receipt"
            )
        raise EpochBridgeUnavailable(
            "bridge obtainable but not yet attached; must be countersigned "
            "before acceptance, not after"
        )
    # Bridge attached and warrant live: record bridged epoch as dispute scope.
    return
