# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-compaction-checkpoint-as-a-lossy-projection-and-re-derive-rather-than-trust-summarized-state.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-compaction-checkpoint-as-a-lossy-projection-and-re-derive-rather-than-trust-summarized-state.warrant.json
# Markdown SHA-256:   fa2507ea9f03a33198c7f0f2515a1c8b31ff9be15ffa305746d28776f242b50a
# Code AST SHA-256:   642aa71e19e2a281573054fe924f5290ee5196e9ca1b480c5417fb8a1fcb7282
# Verified Classes:   UnverifiableObligationError, Obligation, Checkpoint
# Defensive Guards:   1 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class UnverifiableObligationError(RuntimeError):
    """Raised when an action would rely on a summarized obligation whose raw evidence was discarded."""
    pass


@dataclass
class Obligation:
    kind: str            # "constraint" | "performed_effect" | "rejected_alternative"
    text: str
    span_ref: Optional[str]  # None => raw evidence already discarded at compaction


@dataclass
class Checkpoint:
    summary: str
    obligations: list[Obligation]


IRREVERSIBLE_ACTIONS = {"delete_module", "flip_flag", "external_call", "payment"}


def authorize_action(action: str, checkpoint: Checkpoint) -> None:
    """
    Gate a downstream action against a compaction checkpoint.
    Orientational/reversible actions pass; obligation-bearing or irreversible
    actions require every relevant obligation to carry a live span_ref.
    """
    if action not in IRREVERSIBLE_ACTIONS:
        return  # orientational: summary is a fine search hint

    # Irreversible action: any obligation lacking raw evidence blocks it.
    unresolved = [o for o in checkpoint.obligations if o.span_ref is None]
    if unresolved:
        details = "; ".join(f"{o.kind}: {o.text!r}" for o in unresolved)
        raise UnverifiableObligationError(
            f"Refusing irreversible action {action!r}: checkpoint carries "
            f"{len(unresolved)} obligation(s) with discarded raw evidence "
            f"({details}). Re-fetch span or re-confirm with user before proceeding."
        )


def run_turn(action: str, checkpoint: Checkpoint) -> None:
    # Example: turn-88 deletion against the lossy checkpoint from the record.
    cp = Checkpoint(
        summary="User approved deleting legacy billing_v1; migration in progress.",
        obligations=[
            Obligation(
                kind="constraint",
                text="do not delete billing_v1 until BILLING_V1_SUNSET flag drains 3% traffic",
                span_ref=None,  # raw turn-14 discarded at compaction
            ),
            Obligation(
                kind="performed_effect",
                text="billing_v1 tests disabled via marker (handle lost)",
                span_ref=None,
            ),
        ],
    )
    authorize_action(action, cp)  # raises UnverifiableObligationError
