# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-reconcile-a-revoked-capability-when-a-peer-holds-a-still-valid-short-lived-token.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-reconcile-a-revoked-capability-when-a-peer-holds-a-still-valid-short-lived-token.warrant.json
# Markdown SHA-256:   28626cabb2368c1fb9eebdafdea05151f32763d244d6b26c3c45d744419d6d27
# Code AST SHA-256:   ded09b4c39e0aeba79f871959566c1500a556416e37d8d473fab879ef4b96822
# Verified Classes:   StaleRevocationError, Link
# Defensive Guards:   3 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class StaleRevocationError(RuntimeError):
    """Raised when a delegation link's revocation status is older than the
    bounded staleness interval Delta, or when a link has expired. Treated as
    *unverifiable*, not *valid*: caller must fail-closed and force re-mint."""


@dataclass
class Link:
    issuer: str
    subject: str
    capability: str
    exp: float                 # absolute expiry, seconds
    revocation_checked_at: float  # last time this link's status was confirmed


DELTA = 30.0  # max tolerated revocation staleness, must be < min TTL


def verify_chain(chain: list[Link], now: float) -> None:
    """Walk the full delegation chain root->leaf. Any unverifiable link aborts."""
    for i, link in enumerate(chain):
        if now >= link.exp:
            raise StaleRevocationError(
                f"link {i} ({link.issuer}->{link.subject}) expired at {link.exp}"
            )
        if now - link.revocation_checked_at > DELTA:
            raise StaleRevocationError(
                f"link {i} revocation status stale by "
                f"{now - link.revocation_checked_at:.1f}s > Delta={DELTA}s; "
                f"fail-closed, re-mint required"
            )
    # Only after every link passes do we admit.
    return None


def resume_from_checkpoint(chain: list[Link], now: float) -> None:
    """Checkpoints persist the chain, never the prior admission decision."""
    try:
        verify_chain(chain, now)
    except StaleRevocationError:
        # Do NOT fall through to the old decision. Force issuer round-trip.
        raise
