# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-halt-when-a-federated-capability-grant-crosses-an-attestation-epoch-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-halt-when-a-federated-capability-grant-crosses-an-attestation-epoch-boundary.warrant.json
# Markdown SHA-256:   639f7ec8078ef1a81fe02033e05c629de3f0ebd7945944b23fd47a1caf258d8d
# Code AST SHA-256:   8ce576436f3cf954063c3ddd4d0a3ca81fa7c53a24d2d32c67eac134f762eb02
# Verified Classes:   EpochResolutionError, CapabilityToken
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass


class EpochResolutionError(RuntimeError):
    """Raised when a federated capability cannot be evaluated under the
    policy epoch its attestation was issued against."""


@dataclass(frozen=True)
class CapabilityToken:
    subject: str
    resource_class: str
    epoch_id: str
    not_before: float
    not_after: float
    signature_valid: bool


def enforce_federated_capability(
    token: CapabilityToken,
    now: float,
    receiver_epoch_id: str,
    resolvable_epochs: set[str],
    attested_equivalences: dict[str, set[str]],
) -> None:
    if not token.signature_valid:
        raise EpochResolutionError(f"signature invalid for subject={token.subject}")
    if not (token.not_before <= now <= token.not_after):
        raise EpochResolutionError(
            f"token outside validity window: now={now} "
            f"window=[{token.not_before}, {token.not_after}]"
        )

    # The non-obvious boundary: the token's epoch must be evaluable at the
    # receiver, either directly (pinned snapshot) or via attested equivalence.
    if token.epoch_id in resolvable_epochs:
        return
    if receiver_epoch_id in attested_equivalences.get(token.epoch_id, set()):
        return

    raise EpochResolutionError(
        f"cannot evaluate capability for {token.resource_class!r} issued under "
        f"epoch {token.epoch_id!r} at receiver epoch {receiver_epoch_id!r}: "
        f"no pinned snapshot and no attested equivalence. Refusing to "
        f"re-resolve scope under local policy."
    )
