# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-a-rotated-aaif-credential-mid-handoff-the-revocation-epoch-straddle-at-the-delegation-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-a-rotated-aaif-credential-mid-handoff-the-revocation-epoch-straddle-at-the-delegation-boundary.warrant.json
# Markdown SHA-256:   f60d04c20788ba0800795a4b56176aaeb21c648388969adcdd0accd901886150
# Code AST SHA-256:   0f941cd1622de0efa2b0d15fe7c514c68947d20842601ffd08b575cf23b96396
# Verified Classes:   EpochState, IndeterminateEpochError, Warrant, RevocationRecord, VerifierView
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum


class EpochState(Enum):
    DETERMINATE_VALID = "determinate_valid"
    DETERMINATE_REVOKED = "determinate_revoked"
    INDETERMINATE = "indeterminate"


class IndeterminateEpochError(RuntimeError):
    """Raised when a warrant's authority epoch cannot be ordered against
    revocation records within the verifier's clock-skew budget. The handoff
    must park and re-verify; it must not commit side effects."""


@dataclass(frozen=True)
class Warrant:
    epoch: int
    asserted_at: float          # issuer-signed assertion timestamp
    parent_epoch: int | None
    reattested_by: str | None   # hop that most recently verified parent


@dataclass(frozen=True)
class RevocationRecord:
    epoch: int
    effective_at: float         # issuer-signed
    signed: bool


@dataclass(frozen=True)
class VerifierView:
    now: float
    clock_skew_budget: float
    propagation_sla: float
    freshness_attested_at: float | None   # None => freshness unprovable
    revocations: tuple[RevocationRecord, ...]


def classify_warrant(w: Warrant, v: VerifierView) -> EpochState:
    if v.freshness_attested_at is None:
        return EpochState.INDETERMINATE
    # freshness must be within the skew budget of "now"
    if v.now - v.freshness_attested_at > v.clock_skew_budget:
        return EpochState.INDETERMINATE

    window = v.propagation_sla + v.clock_skew_budget
    for r in v.revocations:
        if not r.signed:
            return EpochState.INDETERMINATE
        if r.epoch != w.epoch:
            continue
        # provably asserted before revocation effective time?
        if w.asserted_at + v.clock_skew_budget < r.effective_at:
            return EpochState.DETERMINATE_VALID
        # provably asserted after revocation effective time?
        if w.asserted_at - v.clock_skew_budget > r.effective_at:
            return EpochState.DETERMINATE_REVOKED
        # straddles the revocation boundary -> indeterminate
        return EpochState.INDETERMINATE

    # no revocation for this epoch; still must be outside rotation straddle
    if w.parent_epoch is not None and w.reattested_by is None:
        return EpochState.INDETERMINATE
    return EpochState.DETERMINATE_VALID


def commit_handoff(w: Warrant, v: VerifierView, effects: list[str]) -> list[str]:
    state = classify_warrant(w, v)
    if state is EpochState.DETERMINATE_REVOKED:
        raise IndeterminateEpochError(
            f"epoch {w.epoch} provably revoked at {[r.effective_at for r in v.revocations if r.epoch == w.epoch]}; "
            f"refuse and escalate to warrant issuer for re-issuance under current epoch"
        )
    if state is EpochState.INDETERMINATE:
        raise IndeterminateEpochError(
            f"epoch {w.epoch} assertion at {w.asserted_at} straddles revocation/rotation window "
            f"(skew={v.clock_skew_budget}, sla={v.propagation_sla}); park handoff, re-verify at consumption"
        )
    # DETERMINATE_VALID: only now may irreversible effects commit
    return effects
