# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-federated-agent-s-identity-warrant-has-been-revoked-across-an-aaif-trust-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-federated-agent-s-identity-warrant-has-been-revoked-across-an-aaif-trust-boundary.warrant.json
# Markdown SHA-256:   9538284679f77f8b044f4d6ec31f225e1692431c8edf6dacf282484b3e4e0375
# Code AST SHA-256:   6666c6c68a1f7d9eea4b14843934d6cad702110b7de3b47d46090b0b71e129de
# Verified Classes:   RevocationFreshnessViolation
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class RevocationFreshnessViolation(RuntimeError):
    """Raised when a federated warrant's epoch freshness cannot be established."""
    pass


def accept_federated_handoff(
    warrant_epoch: int,
    warrant_expires_in_s: float,
    cached_epoch: int,
    epoch_fetched_at_s: float,
    now_s: float,
    epoch_fetch_interval_s: float,
    downstream_token_lifetime_s: float,
) -> bool:
    """Decide whether a cross-org handoff may proceed under AAIF revocation semantics.

    Returns True only if the epoch cache is fresh enough to distinguish
    revocation from liveness for the full remaining lifetime of the warrant
    AND the downstream token cannot outlive the verifier's freshness budget.
    """
    if cached_epoch < warrant_epoch:
        # Verifier has not yet observed the issuer's epoch at issuance time.
        raise RevocationFreshnessViolation(
            f"cached epoch {cached_epoch} < warrant epoch {warrant_epoch}; "
            "issuer epoch statement is behind the warrant — refuse handoff"
        )

    cache_age_s = now_s - epoch_fetched_at_s
    if cache_age_s >= warrant_expires_in_s:
        raise RevocationFreshnessViolation(
            f"epoch cache age {cache_age_s:.1f}s >= warrant remaining life "
            f"{warrant_expires_in_s:.1f}s; revocation is undetectable — "
            "refresh epoch synchronously or refuse"
        )

    if downstream_token_lifetime_s >= epoch_fetch_interval_s:
        raise RevocationFreshnessViolation(
            f"downstream token lifetime {downstream_token_lifetime_s:.1f}s "
            f">= epoch fetch interval {epoch_fetch_interval_s:.1f}s; "
            "minted token would outlive revocation observability — "
            "shorten token lifetime or propagate derived_from_epoch"
        )

    return True
