# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-treat-a-revoked-aaif-delegation-warrant-that-is-still-cryptographically-valid-at-the-point-of-use.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-treat-a-revoked-aaif-delegation-warrant-that-is-still-cryptographically-valid-at-the-point-of-use.warrant.json
# Markdown SHA-256:   773ae083d1d04baf0ddc12cf771f7a2ca41aafaf97f9fd0c99747f82ed3b3f03
# Code AST SHA-256:   19ca640ae4f1ef7761cc93679a538293e1e12714dbb2c1c6f2777186f80ad40e
# Verified Classes:   RevocationValidityGap, EpochAttestation, Warrant
# Defensive Guards:   2 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class RevocationValidityGap(RuntimeError):
    """Raised when a verifier attempts to decide on a warrant while its
    epoch attestation is stale — i.e. it is not authorized to emit ALLOW."""
    pass


@dataclass(frozen=True)
class EpochAttestation:
    epoch: int
    issued_at: float
    authority_sig: str


@dataclass(frozen=True)
class Warrant:
    sub: str
    rev_epoch: int
    exp: float
    sig: str


def decide(
    warrant: Warrant,
    attestation: EpochAttestation,
    rev_ttl: float,
    now: float,
    privileged: bool,
) -> str:
    # 1. Signature and expiry are necessary but not sufficient.
    if now >= warrant.exp:
        return "DENY_EXPIRED"

    # 2. The verifier must be entitled to decide. Staleness is not a warning.
    attestation_age = now - attestation.issued_at
    if attestation_age > rev_ttl:
        if privileged:
            raise RevocationValidityGap(
                f"epoch attestation age {attestation_age:.1f}s exceeds "
                f"rev_ttl {rev_ttl:.1f}s; verifier not authorized to ALLOW "
                f"warrant for {warrant.sub} at epoch {attestation.epoch}"
            )
        return "INDETERMINATE"

    # 3. Monotonic floor: token must not claim an epoch ahead of authority.
    if warrant.rev_epoch > attestation.epoch:
        return "DENY_SUPERSEDED_EPOCH"

    return "ALLOW"


def verify_or_abort(warrant, attestation, rev_ttl, now, privileged):
    decision = decide(warrant, attestation, rev_ttl, now, privileged)
    if decision == "INDETERMINATE":
        # Trail must record INDETERMINATE, never silently upgrade to ALLOW.
        raise RevocationValidityGap(
            f"indeterminate decision for {warrant.sub}: epoch attestation "
            f"stale, privileged={privileged}"
        )
    return decision


# Reproduction of the observed 5-minute revocation shadow.
w = Warrant(sub="planner@B", rev_epoch=41, exp=time.time() + 3600, sig="sig")
att = EpochAttestation(epoch=41, issued_at=time.time() - 310, authority_sig="sig")
try:
    verify_or_abort(w, att, rev_ttl=300, now=time.time(), privileged=True)
except RevocationValidityGap as e:
    print(f"blocked: {e}")
