# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-s-revocation-propagates-faster-than-its-in-flight-warrants-the-cross-domain-trust-attestation-race.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-s-revocation-propagates-faster-than-its-in-flight-warrants-the-cross-domain-trust-attestation-race.warrant.json
# Markdown SHA-256:   57283547e953ce66e84b95a5fa05b326dde0d376b960703cfc6427af1b833fff
# Code AST SHA-256:   e35662fed06c00a395b66b4c7ce6e4b5d5a6299941f52d365105433f8fbd8813
# Verified Classes:   RevocationFreshnessError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class RevocationFreshnessError(RuntimeError):
    """Raised when a warrant cannot be proven valid against a fresh revocation view."""
    pass


def validate_warrant(warrant, chain, revocation_view, delta_rev, epsilon=1):
    """
    warrant: dict with subject, issued_at, not_after, capability
    chain:   list of warrants from leaf issuer up to root, each {subject, issued_at}
    revocation_view: dict with 'epoch' and 'revoked' (set of subjects)
    """
    # Invariant 1: warrant lifetime must be within the federation propagation bound.
    lifetime = warrant["not_after"] - warrant["issued_at"]
    if lifetime > delta_rev:
        raise RevocationFreshnessError(
            f"warrant lifetime {lifetime}s exceeds propagation bound {delta_rev}s"
        )

    # Invariant 2: validator must prove its revocation view is at least as new
    # as the warrant's issuance epoch. A stale view cannot rule out revocation.
    if revocation_view["epoch"] < warrant["issued_at"] - epsilon:
        raise RevocationFreshnessError(
            f"stale revocation view: epoch={revocation_view['epoch']} "
            f"< issued_at={warrant['issued_at']} - eps={epsilon}"
        )

    # Invariant 3: transitive revocation. Reject if ANY ancestor is revoked,
    # not merely the presenting subject.
    revoked = revocation_view["revoked"]
    for link in chain:
        if link["subject"] in revoked:
            raise RevocationFreshnessError(
                f"ancestor {link['subject']} revoked in view@{revocation_view['epoch']}"
            )

    return {"accepted": True, "view_epoch": revocation_view["epoch"]}
