# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-revoked-aaif-agent-credential-has-outlived-its-trust-domain-stale-warrant-resurrection-at-the-federation-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-revoked-aaif-agent-credential-has-outlived-its-trust-domain-stale-warrant-resurrection-at-the-federation-boundary.warrant.json
# Markdown SHA-256:   275fb350fde95bb73c13d69ff03c3d322a7c86523717d63b2a6e3fda04c8a4a6
# Code AST SHA-256:   50d5869f84ebf629406fec9f55cca8f47d4b9019e72a11c7d0e9eb9fd6a3a36f
# Verified Classes:   RevocationState, StaleWarrantResurrectionError, ParentStatus, DerivedWarrant
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum


class RevocationState(Enum):
    ACTIVE = "active"
    REVOKED = "revoked"
    UNKNOWN = "unknown"


class StaleWarrantResurrectionError(RuntimeError):
    """Raised when a derived warrant is presented whose parent authority
    has not been observed within max_staleness, or whose parent is revoked
    but the derived warrant is still structurally valid."""
    pass


@dataclass
class ParentStatus:
    state: RevocationState
    observed_at: float          # verifier-local monotonic seconds
    parent_epoch: int           # issuer revocation-log epoch at observation


@dataclass
class DerivedWarrant:
    warrant_id: str
    parent_warrant_id: str
    parent_epoch_at_mint: int
    not_after: float
    scope: str


def enforce_derived_warrant(
    w: DerivedWarrant,
    parent: ParentStatus,
    now: float,
    max_staleness: float,
) -> None:
    # Structural checks (signature chain, attenuation) assumed already passed.
    if now >= w.not_after:
        raise StaleWarrantResurrectionError(
            f"warrant {w.warrant_id} expired at {w.not_after}, now={now}"
        )

    staleness = now - parent.observed_at
    if staleness > max_staleness:
        # The verifier cannot assert current parent authority. Reject loudly.
        raise StaleWarrantResurrectionError(
            f"parent {w.parent_warrant_id} status stale by {staleness:.1f}s "
            f"(max_staleness={max_staleness}s); refusing derived warrant "
            f"{w.warrant_id}"
        )

    if parent.state is RevocationState.REVOKED:
        raise StaleWarrantResurrectionError(
            f"parent {w.parent_warrant_id} revoked; derived warrant "
            f"{w.warrant_id} is a resurrection shadow"
        )

    if parent.state is RevocationState.UNKNOWN:
        raise StaleWarrantResurrectionError(
            f"parent {w.parent_warrant_id} state unresolvable; fail closed"
        )

    # Epoch monotonicity: mint-time epoch must not predate observed epoch
    # in a way that indicates the mint predates a revocation we already know.
    if w.parent_epoch_at_mint < parent.parent_epoch:
        raise StaleWarrantResurrectionError(
            f"warrant {w.warrant_id} minted at parent epoch "
            f"{w.parent_epoch_at_mint} but issuer is at {parent.parent_epoch}; "
            f"derived authority predates a known revocation event"
        )
