# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-treat-a-revocation-list-that-is-fresher-than-its-own-signed-agent-card.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-treat-a-revocation-list-that-is-fresher-than-its-own-signed-agent-card.warrant.json
# Markdown SHA-256:   bb2a76dc6e0ae471f6f25c51a27063fba4dece8a19b6c316e67b3cc40f6801d1
# Code AST SHA-256:   efc732f0760b18b3ec3019e871c6f4480ab148fa6226f6d1e273d948c443e5af
# Verified Classes:   RevocationCoverageGap, RevocationEpochRollback, AgentCard, RevocationList, AttestationVerifier
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from datetime import datetime, timedelta

class RevocationCoverageGap(RuntimeError):
    """Raised when the CRL does not cover the card's issuance instant."""
    pass

class RevocationEpochRollback(RuntimeError):
    """Raised when a fetched CRL regresses its revocation_epoch."""
    pass

@dataclass(frozen=True)
class AgentCard:
    issuer: str
    key_id: str
    issued_at: datetime
    expires_at: datetime
    capabilities: tuple[str, ...]

@dataclass(frozen=True)
class RevocationList:
    issuer: str
    issued_at: datetime
    revocation_epoch: datetime
    revoked_keys: frozenset[str]

class AttestationVerifier:
    def __init__(self, max_refetch: int = 3):
        self._epoch_high_water: dict[str, datetime] = {}
        self._max_refetch = max_refetch

    def _check_epoch_monotonic(self, crl: RevocationList) -> None:
        prior = self._epoch_high_water.get(crl.issuer)
        if prior is not None and crl.revocation_epoch < prior:
            raise RevocationEpochRollback(
                f"CRL from {crl.issuer} regressed epoch "
                f"{crl.revocation_epoch.isoformat()} < {prior.isoformat()}"
            )
        self._epoch_high_water[crl.issuer] = crl.revocation_epoch

    def verify(self, card: AgentCard, fetch_crl) -> None:
        if card.expires_at <= card.issued_at:
            raise RevocationCoverageGap("card has non-positive validity window")

        last_gap: RevocationCoverageGap | None = None
        for _ in range(self._max_refetch):
            crl = fetch_crl(card.issuer)
            self._check_epoch_monotonic(crl)

            # Relational coverage: CRL must postdate the card it validates.
            if crl.issued_at < card.issued_at:
                last_gap = RevocationCoverageGap(
                    f"CRL issued_at {crl.issued_at.isoformat()} predates "
                    f"card issued_at {card.issued_at.isoformat()} for key "
                    f"{card.key_id}; cannot attest status"
                )
                continue

            if card.key_id in crl.revoked_keys:
                raise RevocationCoverageGap(
                    f"key {card.key_id} revoked as of {crl.revocation_epoch.isoformat()}"
                )
            return

        raise last_gap or RevocationCoverageGap("CRL coverage could not be established")
