# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-accept-a-federated-capability-attestation-that-its-own-registry-has-already-revoked.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-accept-a-federated-capability-attestation-that-its-own-registry-has-already-revoked.warrant.json
# Markdown SHA-256:   f8489f5e9b1df7c7357ae052e492430f73d7320990e4bd2465d1db831db37079
# Code AST SHA-256:   75960472ad860cae5f193ee377fa1d92f3158518d80397316cbf26fec3a84576
# Verified Classes:   FederatedAdmissionError, Attestation, RegistryView
# Defensive Guards:   7 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass
from typing import Optional


class FederatedAdmissionError(RuntimeError):
    """Raised when a federated attestation cannot be admitted under the
    freshness/revocation-completeness invariant."""


@dataclass(frozen=True)
class Attestation:
    capability_hash: str
    issued_at: float
    expires_at: float
    revocation_token: Optional[str] = None
    signature_valid: bool = True


@dataclass(frozen=True)
class RegistryView:
    last_full_sync_epoch: float
    known_capabilities: frozenset
    revoked: frozenset


def resolve_revocation_token(token: str) -> bool:
    """Return True if the token resolves to 'not revoked'.

    In production this is a network call to the issuing org's revocation
    endpoint with a bounded timeout. A timeout must NOT be treated as
    'not revoked'; it must raise, because an unresolved token is an
    unknown currency state.
    """
    if token.startswith("unresolved:"):
        raise FederatedAdmissionError(
            f"revocation token {token!r} could not be resolved; "
            "currency is unknown and admission must fail closed"
        )
    return not token.startswith("revoked:")


def admit(
    att: Attestation,
    view: RegistryView,
    now: Optional[float] = None,
) -> bool:
    now = time.time() if now is None else now

    # Phase 1: provenance.
    if not att.signature_valid:
        raise FederatedAdmissionError("attestation signature invalid")
    if now >= att.expires_at:
        raise FederatedAdmissionError(
            f"attestation expired at {att.expires_at}, now={now}"
        )
    if att.capability_hash not in view.known_capabilities:
        # Do NOT fall back to signature-only trust. A signed claim to a
        # capability the local registry has never seen cannot be bounded
        # against revocation staleness.
        raise FederatedAdmissionError(
            f"capability {att.capability_hash!r} absent from local registry; "
            "cannot bound revocation staleness"
        )

    # Phase 2: currency.
    if att.revocation_token is not None:
        if not resolve_revocation_token(att.revocation_token):
            raise FederatedAdmissionError(
                f"capability {att.capability_hash!r} revoked per token "
                f"{att.revocation_token!r}"
            )
        return True

    if att.issued_at < view.last_full_sync_epoch:
        raise FederatedAdmissionError(
            f"attestation issued at {att.issued_at} predates last full sync "
            f"epoch {view.last_full_sync_epoch}; revocation may have occurred "
            "in the unobserved interval and no token is present"
        )

    if att.capability_hash in view.revoked:
        raise FederatedAdmissionError(
            f"capability {att.capability_hash!r} present in local revocation set"
        )

    return True
