# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-treat-a-lossy-compaction-checkpoint-as-a-replay-boundary-rather-than-a-resume-point.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-treat-a-lossy-compaction-checkpoint-as-a-replay-boundary-rather-than-a-resume-point.warrant.json
# Markdown SHA-256:   0496e98c4bfa3625f6aaad863162cdde3972aef59b7be7d12e38b0f1618d165c
# Code AST SHA-256:   1652ca910bc472aedde5f276ee6247f8035d00576b1d0e630cad2a5897dab1b5
# Verified Classes:   CompactionReplayError, ClaimStatus, Claim, Checkpoint
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from enum import Enum


class CompactionReplayError(RuntimeError):
    """Raised when an irreversible action is attempted against a lossy
    compaction checkpoint whose uncertainty set has not been resolved."""


class ClaimStatus(Enum):
    CORROBORATED = "corroborated"
    UNCORROBORATED = "uncorroborated"
    PENDING = "pending"


@dataclass
class Claim:
    text: str
    status: ClaimStatus
    result_id: str | None = None


@dataclass
class Checkpoint:
    summary: str
    claims: list[Claim] = field(default_factory=list)

    def unresolved(self) -> list[Claim]:
        return [c for c in self.claims if c.status is not ClaimStatus.CORROBORATED]

    def has_replay_obligation(self) -> bool:
        return bool(self.unresolved())


def gate_irreversible_action(checkpoint: Checkpoint, action: str, reversible: bool) -> None:
    """Enforce the replay boundary: irreversible actions may not proceed
    across a checkpoint that still contains uncorroborated or pending claims."""
    if reversible:
        return
    unresolved = checkpoint.unresolved()
    if unresolved:
        raise CompactionReplayError(
            f"Refusing irreversible action {action!r}: checkpoint has "
            f"{len(unresolved)} unresolved claim(s) requiring replay: "
            f"{[c.text for c in unresolved]}"
        )


def resolve_claim(checkpoint: Checkpoint, claim_text: str, observed: bool, result_id: str) -> None:
    """Re-observe the environment to convert a replay obligation into a fact."""
    for c in checkpoint.claims:
        if c.text == claim_text:
            c.status = ClaimStatus.CORROBORATED if observed else ClaimStatus.UNCORROBORATED
            c.result_id = result_id
            return
    raise CompactionReplayError(f"Claim not found in checkpoint: {claim_text!r}")


# --- trace of the observed failure ---
cp = Checkpoint(
    summary="Attempted to patch config/loader.py; approach working. Next: verify and test.",
    claims=[
        Claim("patch applied to config/loader.py", ClaimStatus.UNCORROBORATED),
        Claim("approach is working", ClaimStatus.UNCORROBORATED),
    ],
)

# t5: agent tries to proceed to an irreversible edit without replay.
try:
    gate_irreversible_action(cp, action="apply_patch(config/loader.py)", reversible=False)
except CompactionReplayError as e:
    # Correct behavior: replay first.
    resolve_claim(cp, "patch applied to config/loader.py", observed=False, result_id="stat:loader.py@mtime")
    resolve_claim(cp, "approach is working", observed=False, result_id="grep:import-cycle")
    # Now the checkpoint reflects reality: the patch never landed.
    gate_irreversible_action(cp, action="apply_patch(config/loader.py)", reversible=False)
