# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-revocation-has-failed-to-propagate-across-a-federated-agent-trust-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-revocation-has-failed-to-propagate-across-a-federated-agent-trust-boundary.warrant.json
# Markdown SHA-256:   7b6542d2c30c1e0934420b53d6ade7e5fe22069d6d593b53961943b79eb1db58
# Code AST SHA-256:   548ef228f29e9f09ba893b61f6c4b12cf5c58a2a28fe8d10465c6c0a1f304b06
# Verified Classes:   RevocationPropagationError, StatusSnapshot, FederatedWarrant, FederatedAdmissionGate
# Defensive Guards:   8 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class RevocationPropagationError(RuntimeError):
    """Raised when a federated credential's revocation status cannot be proven current."""
    pass


@dataclass
class StatusSnapshot:
    issuer: str
    version: int
    issued_at: float
    revoked_indices: set
    signature_valid: bool


@dataclass
class FederatedWarrant:
    issuer: str
    subject: str
    slot_index: int
    exp: float
    min_status_version: int | None


class FederatedAdmissionGate:
    def __init__(self, version_lag_budget: int = 1):
        self._last_accepted_version: dict[str, int] = {}
        self.version_lag_budget = version_lag_budget

    def _check_monotonicity(self, snap: StatusSnapshot) -> None:
        prior = self._last_accepted_version.get(snap.issuer)
        if prior is not None and snap.version < prior:
            raise RevocationPropagationError(
                f"issuer {snap.issuer} rolled back status version "
                f"{prior} -> {snap.version}; refusing to trust snapshot"
            )

    def admit(self, warrant: FederatedWarrant, snap: StatusSnapshot, now: float) -> bool:
        if not snap.signature_valid:
            raise RevocationPropagationError(
                f"status snapshot from {snap.issuer} failed signature verification"
            )
        self._check_monotonicity(snap)

        if warrant.exp <= now:
            raise RevocationPropagationError(
                f"warrant for {warrant.subject} expired at {warrant.exp}"
            )

        if warrant.min_status_version is None:
            raise RevocationPropagationError(
                f"warrant for {warrant.subject} carries no min_status_version; "
                f"revocation state is unverifiable across trust boundary"
            )

        if snap.version < warrant.min_status_version:
            raise RevocationPropagationError(
                f"status version lag: snapshot v{snap.version} < required "
                f"v{warrant.min_status_version} for {warrant.subject}; "
                f"revocation may not have propagated"
            )

        if snap.version - warrant.min_status_version > self.version_lag_budget:
            raise RevocationPropagationError(
                f"snapshot v{snap.version} exceeds lag budget over required "
                f"v{warrant.min_status_version}; refresh cadence violated"
            )

        if warrant.slot_index in snap.revoked_indices:
            raise RevocationPropagationError(
                f"warrant for {warrant.subject} is revoked at slot "
                f"{warrant.slot_index} (status v{snap.version})"
            )

        self._last_accepted_version[warrant.issuer] = snap.version
        return True

    def halt_mid_task(self, warrant: FederatedWarrant, snap: StatusSnapshot) -> None:
        if warrant.slot_index in snap.revoked_indices:
            raise RevocationPropagationError(
                f"mid-task halt: {warrant.subject} revoked during execution; "
                f"remaining tool calls must be rejected and side effects "
                f"tagged post_revocation"
            )
