# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-halt-on-an-unverified-revocation-epoch-bounded-staleness-identity-propagation-across-federated-trust-domains.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-halt-on-an-unverified-revocation-epoch-bounded-staleness-identity-propagation-across-federated-trust-domains.warrant.json
# Markdown SHA-256:   9d2a5c4fcd7b61d3bd5a5430b346807333c3d10e63e312f1888949438aa61334
# Code AST SHA-256:   74ff12752f35670060635449d5260fd2630c0b3b5cf11f281a4d46771c1b68ec
# Verified Classes:   RevocationEpochUnverified, EpochState
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class RevocationEpochUnverified(RuntimeError):
    """Raised when an agent cannot establish a fresh, contiguous, verified
    revocation epoch for the warrant it is about to act on."""
    pass


class EpochState:
    FRESH = "FRESH"
    STALE_BOUNDED = "STALE_BOUNDED"
    STALE_UNBOUNDED = "STALE_UNBOUNDED"


def classify_epoch(now, epoch_issued, epoch_seq, last_seq, w_safe, w_max):
    age = now - epoch_issued
    if last_seq is not None and epoch_seq != last_seq + 1:
        return EpochState.STALE_UNBOUNDED  # sequence gap: missed epoch
    if age > w_max:
        return EpochState.STALE_UNBOUNDED
    if age > w_safe:
        return EpochState.STALE_BOUNDED
    return EpochState.FRESH


def authorize_action(now, epoch_issued, epoch_seq, last_seq, w_safe, w_max,
                     irreversible, has_inheritance_link):
    state = classify_epoch(now, epoch_issued, epoch_seq, last_seq, w_safe, w_max)

    if state == EpochState.STALE_UNBOUNDED:
        raise RevocationEpochUnverified(
            f"epoch seq={epoch_seq} age={now - epoch_issued}s exceeds bound "
            f"or sequence gap detected; halting all actions"
        )

    if state == EpochState.STALE_BOUNDED:
        if irreversible:
            raise RevocationEpochUnverified(
                f"irreversible action under STALE_BOUNDED epoch (age="
                f"{now - epoch_issued}s); must defer commit behind "
                f"post-action reconciliation"
            )
        if not has_inheritance_link:
            raise RevocationEpochUnverified(
                "delegation without revocation-inheritance link; staleness "
                "bound would compose additively across the chain"
            )
    return state
