# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-in-a-cross-org-federation-accept-a-capability-warrant-signed-by-an-intermediary-whose-own-trust-root-has-since-rotated-out-and-wh.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-in-a-cross-org-federation-accept-a-capability-warrant-signed-by-an-intermediary-whose-own-trust-root-has-since-rotated-out-and-wh.warrant.json
# Markdown SHA-256:   1107feede4b88ce7394b3fa48a02b8dd8439433acbeee57b9b30158fae0c0bb1
# Code AST SHA-256:   c6dab2856d746cf98629fdb1ae363c599898505773b7e7841e6240550305d8ff
# Verified Classes:   AuthorityLeaseViolation, Warrant
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class AuthorityLeaseViolation(RuntimeError):
    """Raised when a delegation chain's authority cannot be confirmed live."""


@dataclass
class Warrant:
    issuer: str
    capability: str
    policy_gen: int
    not_after: float
    blast_radius: str  # "read" | "write" | "transfer"


# Trust-root snapshot pinned to mint time; revocation feed pinned to now.
def verify_delegation(
    warrant: Warrant,
    t_exec: float,
    sig_valid_at_mint: bool,
    non_revocation_checked_at: Optional[float],
    max_lease_seconds: float,
    revocation_max_propagation: float,
) -> bool:
    if not sig_valid_at_mint:
        raise AuthorityLeaseViolation(
            f"signature invalid at mint time for {warrant.capability}"
        )
    if t_exec >= warrant.not_after:
        raise AuthorityLeaseViolation(
            f"warrant expired at {warrant.not_after}, exec at {t_exec}"
        )

    # Invariant: authority lease must be shorter than revocation propagation,
    # else a cached "not revoked" can outlive the revocation itself.
    if max_lease_seconds >= revocation_max_propagation:
        raise AuthorityLeaseViolation(
            "misconfigured: authority lease "
            f"{max_lease_seconds}s >= revocation propagation "
            f"{revocation_max_propagation}s; cached non-revocation is unsound"
        )

    if warrant.blast_radius in ("write", "transfer"):
        # Fail-closed: require a live non-revocation check at t_exec.
        if non_revocation_checked_at is None or non_revocation_checked_at < t_exec:
            raise AuthorityLeaseViolation(
                f"no live non-revocation check for {warrant.capability} "
                f"at t_exec={t_exec}; fail-closed for blast_radius="
                f"{warrant.blast_radius}"
            )

    # Idempotent reads: accept a cached check only within the lease.
    if non_revocation_checked_at is not None:
        cached_age = t_exec - non_revocation_checked_at
        if cached_age > max_lease_seconds:
            raise AuthorityLeaseViolation(
                f"cached non-revocation age {cached_age}s exceeds lease "
                f"{max_lease_seconds}s for {warrant.capability}"
            )

    return True
