# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-re-derive-its-delegated-capability-token-after-a-scope-attenuating-handoff-rather-than-inherit-the-parent-s-attenuation-proof.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-re-derive-its-delegated-capability-token-after-a-scope-attenuating-handoff-rather-than-inherit-the-parent-s-attenuation-proof.warrant.json
# Markdown SHA-256:   b01b190d58d3e88c9a0ac2f8dcaf053a9f03de4d3885f8e1ba634c28c0f78894
# Code AST SHA-256:   089e4bdab894157b8cdbfa13f058e8cba8ae929092991766b557d08cb6dc1f29
# Verified Classes:   AttenuationProofStale, Witness, AncestorState
# Defensive Guards:   5 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class AttenuationProofStale(RuntimeError):
    """Raised when a delegated token's attenuation witness is older than the
    issuer's current revocation epoch, or when ancestor scope cannot be
    verified live. The token is over-broad or unverifiable and must not be
    used to authorize execution."""


@dataclass(frozen=True)
class Witness:
    issuer: str
    child_scope: frozenset
    parent_scope_at_mint: frozenset
    epoch_at_mint: int
    minted_at: float


@dataclass(frozen=True)
class AncestorState:
    issuer: str
    current_scope: frozenset
    current_epoch: int
    observed_at: float


FRESHNESS_BOUND_S = 30.0


def verify_attenuation(
    child_scope: frozenset,
    witness: Witness,
    ancestor: AncestorState | None,
    now: float | None = None,
) -> None:
    now = time.time() if now is None else now

    # Local structural check: child scope must be a subset of what the parent
    # attested to at mint time. This alone is NOT sufficient.
    if not child_scope.issubset(witness.parent_scope_at_mint):
        raise AttenuationProofStale(
            f"child scope {sorted(child_scope)} not subset of attested parent "
            f"scope {sorted(witness.parent_scope_at_mint)}"
        )

    # Live ancestor check: the witness is only valid if the ancestor's scope
    # has not shrunk since mint. If we cannot observe the ancestor, fail closed.
    if ancestor is None:
        raise AttenuationProofStale(
            f"cannot verify live scope of ancestor {witness.issuer!r}; "
            f"token is unverifiable and must be treated as expired"
        )

    if now - ancestor.observed_at > FRESHNESS_BOUND_S:
        raise AttenuationProofStale(
            f"ancestor {ancestor.issuer!r} scope observation is stale "
            f"({now - ancestor.observed_at:.1f}s > {FRESHNESS_BOUND_S}s)"
        )

    if ancestor.current_epoch > witness.epoch_at_mint:
        raise AttenuationProofStale(
            f"ancestor {ancestor.issuer!r} epoch advanced "
            f"{witness.epoch_at_mint} -> {ancestor.current_epoch}; "
            f"attenuation witness is stale, re-derive required"
        )

    if not child_scope.issubset(ancestor.current_scope):
        raise AttenuationProofStale(
            f"child scope {sorted(child_scope)} exceeds live ancestor scope "
            f"{sorted(ancestor.current_scope)}; authority-envelope leak"
        )


def resume_from_checkpoint(
    token_scope: frozenset,
    witness: Witness,
    ancestor_lookup,
) -> None:
    ancestor = ancestor_lookup(witness.issuer)  # None if unreachable
    verify_attenuation(token_scope, witness, ancestor)
    # ... proceed with execution only after the live intersection holds
