# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-a-federated-capability-whose-policy-attestation-is-older-than-its-own-trust-epoch.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-a-federated-capability-whose-policy-attestation-is-older-than-its-own-trust-epoch.warrant.json
# Markdown SHA-256:   a72d9b41483f4e332a4f0c99513f7e1e599dc18bc80df5ae26979acd975b5643
# Code AST SHA-256:   ea88f0f1136bf7c2067e1ddada3ffefaf1542e91052659fbd45a16d46784f257
# Verified Classes:   EpochInversionError, CapabilityCard
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class EpochInversionError(RuntimeError):
    """Raised when a capability card is signature-valid but minted under a trust epoch
    older than the caller's minimum acceptable epoch for that capability class."""
    pass


class CapabilityCard:
    def __init__(self, capability, epoch, issued_at, ttl, policy_root_id, signature_valid, schema_conformant):
        self.capability = capability
        self.epoch = epoch
        self.issued_at = issued_at
        self.ttl = ttl
        self.policy_root_id = policy_root_id
        self.signature_valid = signature_valid
        self.schema_conformant = schema_conformant


def admit_capability(card: CapabilityCard, now: float, min_acceptable_epoch: int,
                     refresh_available: bool, refresh_budget_ok: bool) -> str:
    # Validity predicates (signature, schema, TTL) are necessary but NOT sufficient.
    if not card.signature_valid:
        raise EpochInversionError(f"signature invalid for {card.capability}")
    if not card.schema_conformant:
        raise EpochInversionError(f"schema mismatch for {card.capability}")
    if card.issued_at + card.ttl <= now:
        raise EpochInversionError(f"card expired for {card.capability}")

    # Epoch currency is an independent predicate over a monotonic counter.
    if card.epoch < min_acceptable_epoch:
        if refresh_available and refresh_budget_ok:
            return "REATTEST"  # policy renegotiation, not a retry
        # Fail closed: never fall back to TTL-only admission.
        raise EpochInversionError(
            f"epoch inversion for {card.capability}: card epoch {card.epoch} "
            f"< min acceptable {min_acceptable_epoch}, no viable refresh path"
        )

    return "ADMIT"
