# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-continue-to-authorize-a-revoked-peer-across-an-a2a-trust-mesh-until-its-local-warrant-cache-epoch-advances.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-continue-to-authorize-a-revoked-peer-across-an-a2a-trust-mesh-until-its-local-warrant-cache-epoch-advances.warrant.json
# Markdown SHA-256:   6d42fd89925684d7cd250cf2557907d3adedef15b47e7bb2156dbe8d79d16585
# Code AST SHA-256:   b088be320fdef0da2ab2840a07fd2f63d76ad9dc687218586d0e4646a39eca0d
# Verified Classes:   StaleWarrantEpochError, Dispatch, MeshNode
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass


class StaleWarrantEpochError(RuntimeError):
    """Raised when a dispatch carries an authorization epoch older than the
    receiver's current revocation epoch for the issuing identity."""


@dataclass(frozen=True)
class Dispatch:
    warrant_id: str
    issuer_id: str
    issuer_epoch: int
    task_id: str


class MeshNode:
    def __init__(self, node_id: str) -> None:
        self.node_id = node_id
        # Per-issuer revocation epoch. Absent issuer means epoch 0.
        self.revocation_epochs: dict[str, int] = {}

    def apply_revocation_delta(self, issuer_id: str, new_epoch: int) -> None:
        current = self.revocation_epochs.get(issuer_id, 0)
        if new_epoch <= current:
            return
        self.revocation_epochs[issuer_id] = new_epoch

    def accept_dispatch(self, dispatch: Dispatch) -> None:
        current_epoch = self.revocation_epochs.get(dispatch.issuer_id, 0)
        if dispatch.issuer_epoch < current_epoch:
            raise StaleWarrantEpochError(
                f"dispatch {dispatch.warrant_id} on task {dispatch.task_id} "
                f"carries epoch {dispatch.issuer_epoch} for issuer "
                f"{dispatch.issuer_id}, but receiver {self.node_id} is at "
                f"epoch {current_epoch}; authorization predates revocation"
            )
        # Epoch is current: proceed to signature and scope checks.


def dispatch_subtask(
    sender: MeshNode,
    receiver: MeshNode,
    dispatch: Dispatch,
    task_opened_epoch: int,
) -> None:
    """Re-validate at dispatch time, not at task-open time.

    The sender must stamp the dispatch with the epoch at which it last
    confirmed authorization. If that epoch is older than the sender's own
    current epoch for the issuer, the sender must refuse locally before
    the wire, rather than relying on the receiver.
    """
    sender_epoch = sender.revocation_epochs.get(dispatch.issuer_id, 0)
    if dispatch.issuer_epoch < sender_epoch:
        raise StaleWarrantEpochError(
            f"sender {sender.node_id} refuses to dispatch {dispatch.warrant_id}: "
            f"stamped epoch {dispatch.issuer_epoch} < local epoch {sender_epoch}"
        )
    if dispatch.issuer_epoch < task_opened_epoch:
        raise StaleWarrantEpochError(
            f"dispatch {dispatch.warrant_id} predates its task's open epoch "
            f"{task_opened_epoch}; task-scoped cache treated as authoritative"
        )
    receiver.accept_dispatch(dispatch)
