# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-keep-a-revoked-delegated-capability-token-alive-across-an-a2a-handoff-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-keep-a-revoked-delegated-capability-token-alive-across-an-a2a-handoff-boundary.warrant.json
# Markdown SHA-256:   94bd30995cabb74c19a76d758d9534a69affdfed59a917de45531d025e184a7e
# Code AST SHA-256:   4c26cd5ffcb30ebdf992ae1e5db43e84e01a09c0a30be450feacdb5f0b3c3b2e
# Verified Classes:   StaleDelegatedCapabilityError, DecisionRecord
# Defensive Guards:   6 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class StaleDelegatedCapabilityError(RuntimeError):
    """Raised when a cached authorization decision outlives its derivation basis."""


@dataclass(frozen=True)
class DecisionRecord:
    jti: str
    epoch: int
    scopes: frozenset
    decided_at: float
    decision_ttl: float  # must be <= revocation propagation SLA


def authorize_side_effect(
    record: DecisionRecord,
    required_scope: str,
    current_epoch: int,
    now: float,
    token_exp: float,
) -> None:
    # 1. Decision must not have outlived its own TTL.
    if now - record.decided_at > record.decision_ttl:
        raise StaleDelegatedCapabilityError(
            f"decision for jti={record.jti} aged out "
            f"({now - record.decided_at:.1f}s > {record.decision_ttl}s ttl)"
        )
    # 2. Decision basis must match the live revocation epoch.
    if record.epoch != current_epoch:
        raise StaleDelegatedCapabilityError(
            f"jti={record.jti} derived at epoch {record.epoch}, "
            f"control plane at {current_epoch}; revocation may have landed"
        )
    # 3. Credential itself must still be live (catches resume-after-expiry).
    if now >= token_exp:
        raise StaleDelegatedCapabilityError(
            f"jti={record.jti} token expired at {token_exp:.0f}, now {now:.0f}"
        )
    # 4. Scope must still be present in the cached decision.
    if required_scope not in record.scopes:
        raise StaleDelegatedCapabilityError(
            f"jti={record.jti} lacks scope {required_scope!r}"
        )


def resume_from_checkpoint(checkpoint: dict, current_epoch: int) -> DecisionRecord:
    # A checkpoint without jti/epoch is an unrevokable grant; refuse it.
    if "jti" not in checkpoint or "epoch" not in checkpoint:
        raise StaleDelegatedCapabilityError(
            "checkpoint carries authorization verdict without jti/epoch basis"
        )
    rec = DecisionRecord(
        jti=checkpoint["jti"],
        epoch=checkpoint["epoch"],
        scopes=frozenset(checkpoint["scopes"]),
        decided_at=checkpoint["decided_at"],
        decision_ttl=checkpoint["decision_ttl"],
    )
    if rec.epoch != current_epoch:
        raise StaleDelegatedCapabilityError(
            f"resumed jti={rec.jti} at stale epoch {rec.epoch} "
            f"(live {current_epoch}); re-derive before acting"
        )
    return rec
