# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-keep-honoring-a-revoked-capability-during-a-delegated-sub-task-handoff.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-keep-honoring-a-revoked-capability-during-a-delegated-sub-task-handoff.warrant.json
# Markdown SHA-256:   69e2be9fb6d20fedb4477d88eebf04678b7f83a45e4b96a8856114cbc9abf715
# Code AST SHA-256:   b82a8d8b1b7b0e3182ff46783d5fd6ef2538e0e73f565f39ca4e51a52662b139
# Verified Classes:   StaleRevocationFrontierError, CapabilityAttestation, RevocationAuthority, Worker
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class StaleRevocationFrontierError(RuntimeError):
    """Raised when an instance attempts to exercise a capability using an
    attestation whose revocation frontier is older than the authority's
    current frontier. This is the guardrail that converts eventual
    revocation into bounded revocation."""
    pass


class CapabilityAttestation:
    def __init__(self, nonce: str, tool: str, scope: str, ttl_s: int,
                 minted_at: float, frontier_epoch: int):
        self.nonce = nonce
        self.tool = tool
        self.scope = scope
        self.ttl_s = ttl_s
        self.minted_at = minted_at
        self.frontier_epoch = frontier_epoch  # revocation state at mint time


class RevocationAuthority:
    def __init__(self):
        self._revoked = {}          # nonce -> revoked_at
        self._frontier = 0          # monotonic epoch

    def revoke(self, nonce: str, now: float) -> int:
        self._revoked[nonce] = now
        self._frontier += 1
        return self._frontier

    def current_frontier(self) -> int:
        return self._frontier

    def is_revoked(self, nonce: str) -> bool:
        return nonce in self._revoked


class Worker:
    def __init__(self, authority: RevocationAuthority):
        self.authority = authority
        # local cache: nonce -> attestation (the snapshot that goes stale)
        self._cache = {}

    def accept_handoff(self, att: CapabilityAttestation) -> None:
        # Caches the attestation. Note: frontier_epoch is frozen at mint time.
        self._cache[att.nonce] = att

    def invoke(self, nonce: str, now: float) -> str:
        att = self._cache.get(nonce)
        if att is None:
            raise StaleRevocationFrontierError(f"no attestation for {nonce}")

        # Guardrail 1: re-check revocation at EVERY invocation, not at entry.
        if self.authority.is_revoked(nonce):
            del self._cache[nonce]
            raise StaleRevocationFrontierError(
                f"capability {nonce} revoked; refusing invocation"
            )

        # Guardrail 2: presented frontier must be current. If the worker's
        # cached attestation was minted at an older frontier than the
        # authority's, the worker cannot prove it has seen recent revocations
        # and must fail closed.
        if att.frontier_epoch < self.authority.current_frontier():
            # The attestation predates a revocation event. We cannot prove
            # this nonce is unrevoked without a fresh frontier read.
            raise StaleRevocationFrontierError(
                f"attestation {nonce} frontier {att.frontier_epoch} < "
                f"authority frontier {self.authority.current_frontier()}; "
                f"fail closed"
            )

        # Guardrail 3: TTL check (secondary; revocation is primary).
        if now - att.minted_at > att.ttl_s:
            del self._cache[nonce]
            raise StaleRevocationFrontierError(f"attestation {nonce} expired")

        return f"executed {att.tool} on {att.scope}"


# Reproduction of the observed failure:
auth = RevocationAuthority()
w = Worker(auth)
att = CapabilityAttestation("N", "shell_exec", "repo-X", 300, minted_at=0.0,
                            frontier_epoch=auth.current_frontier())
w.accept_handoff(att)                 # t1: worker caches at frontier 0
auth.revoke("N", now=8.0)             # t2: authority advances to frontier 1
try:
    w.invoke("N", now=9.0)            # t3: worker's cached frontier is stale
except StaleRevocationFrontierError as e:
    print("BLOCKED:", e)              # fail-closed, not fail-open
