# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-presents-a-valid-capability-token-minted-against-a-superseded-identity-attestation-across-an-org-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-presents-a-valid-capability-token-minted-against-a-superseded-identity-attestation-across-an-org-boundary.warrant.json
# Markdown SHA-256:   b842c3434163a80af79e8751ea88ffa4ccdd77d2245b4fa569d8553484817739
# Code AST SHA-256:   5bb3812dc9516db072a1670c1c2b07b6db738abb4f27836ce1dc620f476cb616
# Verified Classes:   AttestationFreshnessError, CapabilityToken, SignedARL
# Defensive Guards:   6 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class AttestationFreshnessError(RuntimeError):
    """Raised when a CST's attestation binding cannot be verified fresh enough
    to satisfy the federation invariant Delta_max < CST.ttl_remaining."""
    pass


@dataclass(frozen=True)
class CapabilityToken:
    subject: str
    attestation_digest: str
    scope: str
    issued_at: float
    expires_at: float


@dataclass(frozen=True)
class SignedARL:
    epoch: float          # issuer-signed timestamp of this revocation list
    revoked_digests: frozenset
    signature_valid: bool


def admit_federated_call(
    cst: CapabilityToken,
    arl: SignedARL,
    delta_max: float,
    now: float,
) -> None:
    # 1. Token must be unexpired.
    if now >= cst.expires_at:
        raise AttestationFreshnessError(
            f"CST for {cst.subject} expired at {cst.expires_at}, now={now}"
        )

    # 2. The negative set must be authenticated; an unsigned ARL is a silent
    #    revocation-suppression channel and must fail closed.
    if not arl.signature_valid:
        raise AttestationFreshnessError(
            "ARL signature invalid; refusing to trust revocation state"
        )

    # 3. Freshness invariant: the ARL epoch must be no older than delta_max,
    #    AND the CST must not predate the ARL epoch by more than delta_max.
    #    This is the exact condition the observed trace violated (delta_max=3600s,
    #    CST ttl_remaining=~4h, ARL epoch stale by 55 min at admission).
    arl_age = now - arl.epoch
    if arl_age > delta_max:
        raise AttestationFreshnessError(
            f"ARL epoch {arl.epoch} is {arl_age:.0f}s stale, exceeds delta_max={delta_max:.0f}s"
        )
    if arl.epoch - cst.issued_at > delta_max:
        raise AttestationFreshnessError(
            f"CST issued_at {cst.issued_at} predates ARL epoch {arl.epoch} "
            f"by {arl.epoch - cst.issued_at:.0f}s > delta_max={delta_max:.0f}s"
        )

    # 4. Revocation check against the fresh, signed negative set.
    if cst.attestation_digest in arl.revoked_digests:
        raise AttestationFreshnessError(
            f"attestation_digest {cst.attestation_digest} revoked as of epoch {arl.epoch}"
        )

    # 5. TTL-vs-freshness coupling: a token must not outlive the RP's ability
    #    to see its revocation. Enforce delta_max < ttl_remaining at admission.
    ttl_remaining = cst.expires_at - now
    if delta_max >= ttl_remaining:
        raise AttestationFreshnessError(
            f"delta_max={delta_max:.0f}s >= ttl_remaining={ttl_remaining:.0f}s; "
            "revocation divergence window exists by construction"
        )
    # Admitted: invariant holds.
    return None


# Reproduce the observed trace: CST minted at t0+1h with 4h TTL, attestation
# revoked at t0+2h, ARL published at t0+2h, RP admits at t0+2h+5m with a
# 1-hour ARL cache (delta_max=3600) while ttl_remaining ~ 2h55m.
t0 = 0.0
cst = CapabilityToken(
    subject="planner-7",
    attestation_digest="d1",
    scope="records.search:read",
    issued_at=t0 + 3600,
    expires_at=t0 + 3600 + 4 * 3600,
)
arl = SignedARL(
    epoch=t0 + 2 * 3600,           # published at revocation time
    revoked_digests=frozenset({"d1"}),
    signature_valid=True,
)
admit_federated_call(cst, arl, delta_max=3600.0, now=t0 + 2 * 3600 + 300)
