# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-a-delegated-credential-whose-attenuation-chain-is-valid-but-whose-revocation-epoch-is-stale-the-aaif-epoch-skew-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-a-delegated-credential-whose-attenuation-chain-is-valid-but-whose-revocation-epoch-is-stale-the-aaif-epoch-skew-boundary.warrant.json
# Markdown SHA-256:   289a4ed793c6e26f3832936331ff818a4cd42a2aca8f58f2071ef93c2170ad70
# Code AST SHA-256:   ccd9ede00f527ff8b7eed5b71e3e09637677179f7fc6f84de231e9cc5e9ffe19
# Verified Classes:   StaleRevocationView, Credential, FreshnessLease
# Defensive Guards:   6 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class StaleRevocationView(RuntimeError):
    """Raised when a verifier cannot prove its revocation view is current.

    The credential's attenuation chain may be structurally valid, but the
    verifier's local epoch is either behind, ahead of, or unleased relative
    to the credential's mint epoch. Authorization must fail closed.
    """


@dataclass(frozen=True)
class Credential:
    scope: frozenset[str]
    epoch: int
    issuer: str


@dataclass(frozen=True)
class FreshnessLease:
    epoch: int
    issued_at: float
    max_skew_s: float

    def is_valid(self, now: float) -> bool:
        return (now - self.issued_at) <= self.max_skew_s


def authorize(
    cred: Credential,
    parent_scope: frozenset[str],
    local_epoch: int,
    lease: FreshnessLease,
    now: float,
) -> str:
    # 1. Structural attenuation: child scope must be a subset of parent.
    if not cred.scope.issubset(parent_scope):
        raise StaleRevocationView(
            f"attenuation widened scope: {cred.scope} not ⊆ {parent_scope}"
        )

    # 2. Credential predates a known revocation.
    if cred.epoch < local_epoch:
        raise StaleRevocationView(
            f"credential epoch {cred.epoch} < local {local_epoch}: lineage revoked"
        )

    # 3. Credential claims an epoch the verifier has never seen.
    if cred.epoch > local_epoch:
        raise StaleRevocationView(
            f"credential epoch {cred.epoch} > local {local_epoch}: "
            "verifier stale or credential forged; quarantine"
        )

    # 4. Epochs equal — but equality is only meaningful if the verifier's
    #    own view is provably fresh. This is the boundary operators miss.
    if not lease.is_valid(now):
        raise StaleRevocationView(
            f"lease expired (age={now - lease.issued_at:.3f}s > "
            f"{lease.max_skew_s}s) at epoch {local_epoch}; cannot certify currency"
        )
    if lease.epoch != local_epoch:
        raise StaleRevocationView(
            f"lease epoch {lease.epoch} != local epoch {local_epoch}: "
            "lease and view disagree; fail closed"
        )

    # 5. Authorize, binding the decision to the proven-fresh state.
    return f"allow scope={sorted(cred.scope)} epoch={cred.epoch} lease_ok"


def guarded_tool_call(cred, parent_scope, local_epoch, lease, now, tool):
    decision = authorize(cred, parent_scope, local_epoch, lease, now)
    if not decision.startswith("allow"):
        # Unreachable given authorize's contract, but keeps the guard explicit.
        raise StaleRevocationView(f"non-allow decision: {decision}")
    return tool()
