# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-honor-a-delegated-capability-whose-revocation-proof-has-not-yet-propagated-across-a-federation-bounda.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-honor-a-delegated-capability-whose-revocation-proof-has-not-yet-propagated-across-a-federation-bounda.warrant.json
# Markdown SHA-256:   f6fbd96fe5e5034aebb63ede6bcdeb2f1fa8a102a62f1c1629f6a8ba43a753d4
# Code AST SHA-256:   f6baa41f02e61cbd86f18df9f27512fc999eacc3acdddcb08ab9136382232e00
# Verified Classes:   RevocationLagError, RevocationSnapshot, Capability
# Defensive Guards:   6 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class RevocationLagError(RuntimeError):
    """Raised when a delegated capability cannot be proven live against the root
    authority's revocation epoch within the federation's propagation bound."""


@dataclass(frozen=True)
class RevocationSnapshot:
    version: int
    fetched_at: float
    valid_until: float
    source: str  # must be the root authority feed, never a mid-chain peer


@dataclass(frozen=True)
class Capability:
    chain_id: str
    revocation_epoch: int
    expires_at: float
    re_delegable: bool


def verify_capability(
    cap: Capability,
    snap: RevocationSnapshot,
    now: float,
    delta_prop: Optional[float],
    forward_liveness_claim: Optional[bool] = None,
) -> bool:
    # A liveness claim flowing forward along the chain is from an interested
    # party and must never substitute for verifier-side freshness.
    if forward_liveness_claim is not None:
        raise RevocationLagError(
            f"chain {cap.chain_id}: forward liveness assertion from mid-chain "
            f"peer is untrusted; freshness must be verifier-established"
        )

    if delta_prop is None:
        raise RevocationLagError(
            f"chain {cap.chain_id}: federation propagation bound Δ_prop is "
            f"undefined; capability is unexercisable (fail closed)"
        )

    if snap.source != "root-authority-feed":
        raise RevocationLagError(
            f"chain {cap.chain_id}: snapshot sourced from {snap.source!r}, "
            f"not the root authority feed"
        )

    age = now - snap.fetched_at
    if age > delta_prop or now > snap.valid_until:
        # Stale beyond the revocation-propagation bound. Defer if the
        # capability still has TTL to survive a refresh; otherwise refuse.
        if now < cap.expires_at:
            raise RevocationLagError(
                f"chain {cap.chain_id}: snapshot stale (age={age:.2f}s > "
                f"Δ_prop={delta_prop:.2f}s); DEFER and freeze chain, do not "
                f"re-delegate until re-verified"
            )
        raise RevocationLagError(
            f"chain {cap.chain_id}: snapshot stale and capability TTL "
            f"exhausted; REFUSE"
        )

    if snap.version != cap.revocation_epoch:
        raise RevocationLagError(
            f"chain {cap.chain_id}: epoch mismatch "
            f"(cap={cap.revocation_epoch}, snapshot={snap.version}); "
            f"ancestor may have been revoked"
        )

    return True
