# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-delegation-chain-has-been-silently-widened-by-a-revoked-but-cached-capability-warrant.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-delegation-chain-has-been-silently-widened-by-a-revoked-but-cached-capability-warrant.warrant.json
# Markdown SHA-256:   7c3a04bf7c6421632f0fe14aaf76598d24e8eb3724c42a089c283b031f0c7d73
# Code AST SHA-256:   3611caf4ff1ad0385aca87bf3ea441b247112af4e96ff1f408f40772da5d4580
# Verified Classes:   DelegationChainViolation, Warrant, RevocationFeed
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass, field
from typing import FrozenSet


class DelegationChainViolation(RuntimeError):
    """Raised when a cached warrant's root authority is no longer live."""
    pass


@dataclass(frozen=True)
class Warrant:
    scope: FrozenSet[str]
    issuer: str
    root_epoch: str          # identity of the root authority this derives from
    derived_at: float
    expires_at: float

    def is_attenuation_of(self, parent: "Warrant") -> bool:
        # Structural narrowing check: scope subset AND same root lineage.
        return self.scope <= parent.scope and self.root_epoch == parent.root_epoch


class RevocationFeed:
    """Append-only set of revoked root epochs, replicated to every leaf."""
    def __init__(self) -> None:
        self._revoked: set[str] = set()
        self._last_poll: float = 0.0

    def revoke(self, root_epoch: str) -> None:
        self._revoked.add(root_epoch)

    def poll(self) -> None:
        self._last_poll = time.monotonic()

    def is_live(self, root_epoch: str) -> bool:
        return root_epoch not in self._revoked


def exercise_capability(
    cached: Warrant,
    parent: Warrant,
    feed: RevocationFeed,
    requested: FrozenSet[str],
    now: float | None = None,
) -> None:
    now = now if now is not None else time.monotonic()

    # 1. Local expiry — necessary, not sufficient.
    if now >= cached.expires_at:
        raise DelegationChainViolation(
            f"warrant for {cached.issuer} expired at {cached.expires_at}"
        )

    # 2. Structural attenuation — the check most systems rely on alone.
    if not cached.is_attenuation_of(parent):
        raise DelegationChainViolation(
            f"cached warrant {cached.scope} is not an attenuation of "
            f"{parent.scope} under root_epoch={cached.root_epoch}"
        )

    # 3. Liveness of the root authority — the check that catches
    #    revoked-but-cached warrants. Without this, a structurally valid
    #    attenuation of a dead root passes every guard above.
    if not feed.is_live(cached.root_epoch):
        raise DelegationChainViolation(
            f"root_epoch={cached.root_epoch} revoked; cached warrant for "
            f"{cached.issuer} is structurally valid but unauthorized"
        )

    # 4. Requested scope must be within the (still-live) cached scope.
    if not requested <= cached.scope:
        raise DelegationChainViolation(
            f"requested {requested} exceeds cached scope {cached.scope}"
        )
    # ... perform the capability exercise ...
