# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-revoked-agent-warrants-from-resurrecting-across-federated-trust-domains-under-crl-style-freshness-windows.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-revoked-agent-warrants-from-resurrecting-across-federated-trust-domains-under-crl-style-freshness-windows.warrant.json
# Markdown SHA-256:   9557a7dc6fae7c0484357c23e2d05a93881a5dabd861f500e08c5daed5aa6387
# Code AST SHA-256:   913cd15968c80b01663e541ceb1d9f16be06dbbffcc6872305e39a68a3897a97
# Verified Classes:   RevocationFreshnessError, CRL, Warrant
# Defensive Guards:   6 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class RevocationFreshnessError(RuntimeError):
    """Raised when a CRL cannot bound revocation propagation for a relying party."""
    pass


@dataclass
class CRL:
    issuer_domain: str
    this_update: float          # epoch seconds
    next_update: float          # epoch seconds
    delta_propagation: float    # signed max-staleness guarantee, seconds
    revoked_serials: frozenset


@dataclass
class Warrant:
    serial: str
    issuer_domain: str
    not_after: float


def validate_warrant_for_rp(
    warrant: Warrant,
    crl: CRL,
    now: float,
    session_ttl: float,
) -> float:
    """
    Returns the maximum session lifetime (seconds) the RP may grant.
    Raises RevocationFreshnessError if the CRL cannot bound propagation.
    """
    if crl.issuer_domain != warrant.issuer_domain:
        raise RevocationFreshnessError(
            f"CRL issuer {crl.issuer_domain} != warrant issuer {warrant.issuer_domain}"
        )

    # Hard expiry: CRL past nextUpdate is unusable.
    if now > crl.next_update:
        raise RevocationFreshnessError(
            f"CRL expired: now={now} > nextUpdate={crl.next_update}"
        )

    # The non-obvious gate: cryptographic freshness is not temporal authority.
    age = now - crl.this_update
    if age > crl.delta_propagation:
        raise RevocationFreshnessError(
            f"CRL stale beyond propagation bound: age={age}s > "
            f"delta_propagation={crl.delta_propagation}s "
            f"(thisUpdate={crl.this_update}, now={now})"
        )

    # Revocation must be absent from the authoritative window.
    if warrant.serial in crl.revoked_serials:
        raise RevocationFreshnessError(
            f"warrant {warrant.serial} is revoked by {crl.issuer_domain}"
        )

    # Warranty must not have expired.
    if now >= warrant.not_after:
        raise RevocationFreshnessError(
            f"warrant {warrant.serial} expired at {warrant.not_after}"
        )

    # Session must not outlive the propagation window, or a revoked agent
    # resurrects via an already-minted session handle.
    if session_ttl > crl.delta_propagation:
        raise RevocationFreshnessError(
            f"session_ttl={session_ttl}s exceeds delta_propagation="
            f"{crl.delta_propagation}s; session could outlive revocation visibility"
        )

    return min(session_ttl, warrant.not_after - now)


# Reproduction of the observed 440s resurrection window:
now = 120.0
crl = CRL(
    issuer_domain="A",
    this_update=-40.0,       # cached CRL predates revocation at T=0
    next_update=560.0,       # still "fresh" by nextUpdate
    delta_propagation=300.0, # intended bound, never enforced
    revoked_serials=frozenset(),  # w-7 revoked at T=0, not yet listed
)
w = Warrant(serial="w-7", issuer_domain="A", not_after=3600.0)

try:
    validate_warrant_for_rp(w, crl, now, session_ttl=3600.0)
except RevocationFreshnessError as e:
    print("REJECTED:", e)
else:
    print("ACCEPTED — resurrection window open")
