# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-revoked-aaif-capability-token-as-still-live-because-the-revocation-event-and-the-in-flight-tool-call-crosse.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-revoked-aaif-capability-token-as-still-live-because-the-revocation-event-and-the-in-flight-tool-call-crosse.warrant.json
# Markdown SHA-256:   f3907a3b6b167a315eb5ae690ed4a66b59ce8bf1fa239e42f30838895a6bc744
# Code AST SHA-256:   35e1daf7816a2f1d0c7bf974d05c5929df2242db53f2afb1e1f05dd4f402b3b8
# Verified Classes:   RevocationWindowViolation, EffectResult, TokenState
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass


class RevocationWindowViolation(RuntimeError):
    """Raised when an effect commits inside the revocation-uncertainty window
    and cannot be proven authorized at commit time."""
    pass


@dataclass(frozen=True)
class EffectResult:
    effect_id: str
    committed: bool
    reversible: bool
    # Monotonic revocation epoch observed by the *executing* party at commit,
    # signed by the revocation issuer. -1 means "no witness provided".
    commit_revocation_epoch: int


@dataclass(frozen=True)
class TokenState:
    token_id: str
    last_observed_revocation_epoch: int  # agent's own view, monotonic


def classify_effect(result: EffectResult, token: TokenState) -> str:
    """Classify a committed effect as CLEAN, QUARANTINE, or REJECT.

    Invariant: an effect is CLEAN only if the executing party's revocation view
    at commit was at least as fresh as the agent's own. Dispatch-time validity
    is irrelevant to this decision.
    """
    if not result.committed:
        return "NO_EFFECT"

    if result.commit_revocation_epoch < 0:
        # No signed witness: cannot prove authorization at commit.
        if result.reversible:
            return "QUARANTINE"
        raise RevocationWindowViolation(
            f"effect {result.effect_id} committed without a revocation witness "
            f"and is irreversible; cannot establish authorization at commit"
        )

    if result.commit_revocation_epoch < token.last_observed_revocation_epoch:
        # Executing party's view was staler than ours: commit landed in the
        # uncertainty window. Effect may be unauthorized.
        if result.reversible:
            return "QUARANTINE"
        raise RevocationWindowViolation(
            f"effect {result.effect_id} committed at revocation epoch "
            f"{result.commit_revocation_epoch}, behind agent epoch "
            f"{token.last_observed_revocation_epoch}; irreversible, must not "
            f"be merged into authoritative episodic state"
        )

    return "CLEAN"


def merge_into_episodic_state(result: EffectResult, token: TokenState) -> None:
    verdict = classify_effect(result, token)
    if verdict == "CLEAN":
        # safe to record as established state
        _record_established(result)
    elif verdict == "QUARANTINE":
        _record_quarantine(result)
    elif verdict == "NO_EFFECT":
        return
    else:
        raise RevocationWindowViolation(f"unclassified verdict {verdict!r}")
