# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-accept-a-federated-message-under-a-policy-that-was-revoked-two-hops-ago.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-accept-a-federated-message-under-a-policy-that-was-revoked-two-hops-ago.warrant.json
# Markdown SHA-256:   9961471d8bc1563d9b2c78f3447d02e1a7f39d422cd8f9f1600df7222645649e
# Code AST SHA-256:   9c289635e69d6339ad2b63e994b74e8854a729bce164bae2fb014bbe7644ef1f
# Verified Classes:   StalePolicyAdmissionError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class StalePolicyAdmissionError(RuntimeError):
    """Raised when a message is admitted under a policy token whose validity
    window exceeds the admitting gate's freshness bound, or when a certifying
    obligation is recorded without a revocation check."""
    pass


def admit_federated_message(
    token,
    gate,
    obligation_class: str,
    revocation_check,
    max_skew_s: float,
) -> bool:
    # C2: token TTL must be bounded by the admitting gate's cache freshness.
    if token.ttl_s > gate.delta_gate_s + max_skew_s:
        raise StalePolicyAdmissionError(
            f"token TTL {token.ttl_s}s exceeds gate freshness bound "
            f"{gate.delta_gate_s}s + skew {max_skew_s}s; "
            f"optimistic admission would admit under a stale gate view"
        )

    # Certifying obligations require pessimistic re-validation before recording.
    if obligation_class == "certifying_irreversible":
        if not revocation_check(token.policy_id):
            raise StalePolicyAdmissionError(
                f"policy {token.policy_id} revoked at minting hop; "
                f"refusing to record certifying obligation under stale token"
            )

    # Optimistic path: gate admits, but downstream must still honor C3.
    if not gate.admits(token):
        raise StalePolicyAdmissionError(
            f"gate rejected token for policy {token.policy_id}"
        )
    return True
