# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-cached-aaif-delegation-grant-as-revoked-before-the-revocation-event-is-durably-propagated.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-cached-aaif-delegation-grant-as-revoked-before-the-revocation-event-is-durably-propagated.warrant.json
# Markdown SHA-256:   102f824802c4f6f91364d946d1092fbd46bafdc7231df0b9c56e6c869a9f34e2
# Code AST SHA-256:   29949fbf68483fb6ca486c0ae3ae3f3a0c669645592a8f826ff3236636e514c0
# Verified Classes:   DelegationRevokedError, Edge, ChainStatus
# Defensive Guards:   5 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass
from typing import Mapping


class DelegationRevokedError(RuntimeError):
    """Raised when a cached AAIF grant is entailed-invalid at time of use."""


@dataclass(frozen=True)
class Edge:
    issuer: str
    delegatee: str
    capability: str
    revocation_epoch: int
    expires_at: float


@dataclass(frozen=True)
class ChainStatus:
    epoch_floor: int
    issued_at: float


def authorize(
    chain: list[Edge],
    status: ChainStatus,
    requested_capability: str,
    now: float,
    max_propagation_lag: float,
) -> None:
    """Re-evaluate the full chain at time of use. Fail closed on any gap."""
    if not chain:
        raise DelegationRevokedError("empty delegation chain")

    if now - status.issued_at > max_propagation_lag:
        raise DelegationRevokedError(
            f"chain-status stale: age={now - status.issued_at:.3f}s "
            f"> bound={max_propagation_lag:.3f}s; cannot establish epoch floor"
        )

    for edge in chain:
        if now >= edge.expires_at:
            raise DelegationRevokedError(
                f"edge {edge.issuer}->{edge.delegatee} expired at {edge.expires_at}"
            )
        if edge.revocation_epoch < status.epoch_floor:
            # Ancestor revocation detected via epoch floor, not tombstone.
            raise DelegationRevokedError(
                f"edge {edge.issuer}->{edge.delegatee} epoch "
                f"{edge.revocation_epoch} below chain floor {status.epoch_floor}"
            )
        if not requested_capability.startswith(edge.capability):
            raise DelegationRevokedError(
                f"attenuation violation: {requested_capability} "
                f"not within {edge.capability}"
            )
