# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-a-delegated-warrant-whose-revocation-proof-is-still-in-flight.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-a-delegated-warrant-whose-revocation-proof-is-still-in-flight.warrant.json
# Markdown SHA-256:   6bfb565f842256eb62b1ca4b8be60fd26e88ac445d414a0ffb74f9eff9d8b94b
# Code AST SHA-256:   aa52e6ae742a2127deb9db342c0982f816f186b5482016d97b25b22fbea43008
# Verified Classes:   RevocationIndeterminateError, AttenuationViolationError, Warrant, RevocationView
# Defensive Guards:   7 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass, field


class RevocationIndeterminateError(RuntimeError):
    """Raised when a warrant cannot be proven un-revoked within its horizon."""
    pass


class AttenuationViolationError(RuntimeError):
    """Raised when a child warrant exceeds its parent's horizon or epoch bounds."""
    pass


@dataclass
class Warrant:
    warrant_id: str
    issuer: str
    parent_id: str | None
    embedded_epoch: int          # issuer's epoch at issuance
    revocation_horizon_s: float  # max acceptable verifier staleness
    expiry: float                # wall-clock


@dataclass
class RevocationView:
    # issuer -> (current_epoch, last_sync_wallclock)
    epochs: dict[str, tuple[int, float]] = field(default_factory=dict)

    def sync_age(self, issuer: str) -> float | None:
        entry = self.epochs.get(issuer)
        if entry is None:
            return None  # never synced: infinite staleness
        return time.time() - entry[1]


def authorize(
    warrant: Warrant,
    chain: dict[str, Warrant],
    view: RevocationView,
    now: float | None = None,
) -> None:
    now = time.time() if now is None else now

    if now >= warrant.expiry:
        raise RevocationIndeterminateError(
            f"warrant {warrant.warrant_id} expired at {warrant.expiry}"
        )

    # Walk ancestors; each must be provably un-revoked within its horizon.
    current = warrant
    while current is not None:
        issuer = current.issuer
        entry = view.epochs.get(issuer)
        if entry is None:
            # Cold start / never synced: zero revocation knowledge.
            raise RevocationIndeterminateError(
                f"no revocation sync for issuer {issuer}; "
                f"warrant {current.warrant_id} indeterminate"
            )
        current_epoch, last_sync = entry
        staleness = now - last_sync
        if staleness > current.revocation_horizon_s:
            raise RevocationIndeterminateError(
                f"issuer {issuer} sync stale {staleness:.1f}s > "
                f"horizon {current.revocation_horizon_s:.1f}s; "
                f"warrant {current.warrant_id} indeterminate"
            )
        if current.embedded_epoch < current_epoch:
            raise RevocationIndeterminateError(
                f"warrant {current.warrant_id} epoch {current.embedded_epoch} "
                f"< issuer {issuer} epoch {current_epoch}: revoked"
            )

        # Attenuation invariant: child cannot exceed parent's horizon.
        parent_id = current.parent_id
        if parent_id is None:
            break
        parent = chain.get(parent_id)
        if parent is None:
            raise AttenuationViolationError(
                f"warrant {current.warrant_id} references missing parent {parent_id}"
            )
        if current.revocation_horizon_s > parent.revocation_horizon_s:
            raise AttenuationViolationError(
                f"warrant {current.warrant_id} horizon "
                f"{current.revocation_horizon_s:.1f}s exceeds parent "
                f"{parent.revocation_horizon_s:.1f}s"
            )
        if current.embedded_epoch < parent.embedded_epoch:
            raise AttenuationViolationError(
                f"warrant {current.warrant_id} epoch {current.embedded_epoch} "
                f"below parent {parent_id} epoch {parent.embedded_epoch}"
            )
        current = parent
