# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-write-audit-anomaly-score-as-a-rollback-trigger-rather-than-a-quarantine-signal.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-write-audit-anomaly-score-as-a-rollback-trigger-rather-than-a-quarantine-signal.warrant.json
# Markdown SHA-256:   5bfd337cf61f0f10341ab0ed287f6e209dec1519e874585a1dc0e61f359ad1d4
# Code AST SHA-256:   790c762e24712ccfac0531fa345232e1238c7ae190c8790627cfbf057ca94819
# Verified Classes:   RollbackGateError, Action, WriteAudit
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum


class RollbackGateError(RuntimeError):
    """Raised when the write-audit gate cannot reach a licensed action."""
    pass


class Action(Enum):
    NONE = "none"
    QUARANTINE = "quarantine"
    ROLLBACK = "rollback"
    RE_SCORE = "re_score"
    ESCALATE_DERIVATIVE_INVALIDATION = "escalate_derivative_invalidation"


@dataclass(frozen=True)
class WriteAudit:
    write_id: str
    score: float
    threshold: float
    consumption_horizon: int          # downstream writes/distillations/peer reads, causally consistent
    distillation_boundaries_in_interval: int
    fan_out_into_semantic: bool
    baseline_pinned: bool


def decide(audit: WriteAudit) -> Action:
    if not audit.baseline_pinned:
        # Scorer is self-referential; scores are not trustworthy for gating.
        raise RollbackGateError(
            f"write {audit.write_id}: scorer baseline not pinned to checkpoint; "
            "anomaly score cannot license rollback or quarantine"
        )

    excess = audit.score - audit.threshold

    if excess <= 0.0:
        if audit.fan_out_into_semantic and audit.consumption_horizon > 0:
            # Laundering case: sub-threshold write already distilled. Re-score
            # against pinned baseline before dismissing.
            return Action.RE_SCORE
        return Action.NONE

    if audit.consumption_horizon == 0:
        # Poison has not propagated; rollback would destroy interleaved clean writes.
        return Action.QUARANTINE

    if audit.distillation_boundaries_in_interval > 1:
        # Rollback would discard clean distilled facts; quarantine leaves the
        # propagated derivative. Neither cheap action is licensed.
        return Action.ESCALATE_DERIVATIVE_INVALIDATION

    return Action.ROLLBACK


def apply_gate(audit: WriteAudit) -> Action:
    action = decide(audit)
    if action is Action.ESCALATE_DERIVATIVE_INVALIDATION and audit.consumption_horizon == 0:
        # Defensive: escalation with zero horizon is a contradiction in the
        # audit record and must not silently proceed.
        raise RollbackGateError(
            f"write {audit.write_id}: escalation requested with zero consumption horizon; "
            "audit record inconsistent"
        )
    return action
