# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-refuse-to-accept-a-federated-capability-attestation-that-verifies-under-the-peer-s-trust-anchor-but-whose-policy-epoch-is-older.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-refuse-to-accept-a-federated-capability-attestation-that-verifies-under-the-peer-s-trust-anchor-but-whose-policy-epoch-is-older.warrant.json
# Markdown SHA-256:   72a894b06bba14dc4abb2fb71d5f30bc6eff0afc98307fc3f2c79b063ae2fe87
# Code AST SHA-256:   057a6a76e146e7f6e08cfde6e5b90dd536473e4fe301ee59f3eb5cb228c13a07
# Verified Classes:   StaleAttestationError, Attestation, LocalPolicyState
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import FrozenSet


class StaleAttestationError(RuntimeError):
    """Raised when a validly-signed federated attestation is inadmissible
    because its policy epoch predates the local revocation floor, or the
    floor itself is too stale to trust."""
    pass


@dataclass(frozen=True)
class Attestation:
    subject: str
    capabilities: FrozenSet[str]
    policy_bundle_hash: str
    policy_epoch: int
    anchor: str
    signature_valid: bool


@dataclass(frozen=True)
class LocalPolicyState:
    revocation_floor: int
    last_revocation_sync_ts: float
    now_ts: float
    max_staleness_s: float
    trusted_bundle_hashes: FrozenSet[str]
    deny_set: FrozenSet[str]


def admit_federated_call(
    att: Attestation,
    state: LocalPolicyState,
    verify_signature,  # callable(att) -> bool
) -> None:
    # 1. Authorship
    if not (att.signature_valid and verify_signature(att)):
        raise StaleAttestationError(f"signature invalid for {att.subject}")

    # 5. Floor freshness — fail closed if revocation feed is stale
    if (state.now_ts - state.last_revocation_sync_ts) > state.max_staleness_s:
        raise StaleAttestationError(
            f"revocation feed stale by "
            f"{state.now_ts - state.last_revocation_sync_ts:.1f}s; "
            f"floor {state.revocation_floor} untrustworthy"
        )

    # 2. Epoch-floor invariant — the core boundary
    if att.policy_epoch < state.revocation_floor:
        raise StaleAttestationError(
            f"attestation epoch {att.policy_epoch} < local revocation floor "
            f"{state.revocation_floor} for {att.subject}; "
            f"valid signature does not confer current authority"
        )

    # 3. Policy interoperability
    if att.policy_bundle_hash not in state.trusted_bundle_hashes:
        raise StaleAttestationError(
            f"policy bundle {att.policy_bundle_hash} not trusted at/above "
            f"floor {state.revocation_floor}"
        )

    # 4. Capability deny-set intersection
    forbidden = att.capabilities & state.deny_set
    if forbidden:
        raise StaleAttestationError(
            f"attestation asserts locally-denied capabilities: {sorted(forbidden)}"
        )
    # admissible
