# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-resume-from-a-hierarchical-summary-checkpoint-when-the-compaction-frontier-has-crossed-an-irreversibl.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-resume-from-a-hierarchical-summary-checkpoint-when-the-compaction-frontier-has-crossed-an-irreversibl.warrant.json
# Markdown SHA-256:   b959b7939e0be3cef266635a498781bb2bd43ce5d3ff8ffc30fe9166c7b2912c
# Code AST SHA-256:   54e042254818072f3bface1cd3a482754a5e3e9329ef45560c6cff496b16ad67
# Verified Classes:   EvidenceClass, CheckpointInvalidError, Claim
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum
from typing import Optional


class EvidenceClass(Enum):
    REQUERYABLE = "requeryable"
    NONREQUERYABLE = "nonrequeryable"


class CheckpointInvalidError(RuntimeError):
    """Raised when a resume would act on a folded, non-requeryable claim
    whose backing evidence was evicted by hierarchical compaction."""


@dataclass
class Claim:
    text: str
    evidence_class: EvidenceClass
    acted_upon: bool
    confidence: float
    source_reachable: bool = True
    has_surviving_witness: bool = True
    leaf_folded: bool = True


def validate_resume(checkpoint_claims: list[Claim]) -> None:
    """Enforce the resume invariant. Raises on the first load-bearing,
    non-requeryable, folded claim without a surviving witness."""
    for c in checkpoint_claims:
        if c.evidence_class is EvidenceClass.REQUERYABLE:
            if not c.source_reachable:
                raise CheckpointInvalidError(
                    f"Requeryable claim unreachable at resume: {c.text!r}. "
                    "Treat as unverified; re-plan instead of resuming."
                )
            continue

        # NONREQUERYABLE path
        if c.acted_upon and c.leaf_folded and not c.has_surviving_witness:
            raise CheckpointInvalidError(
                f"Resume refused: claim {c.text!r} is non-requeryable, "
                f"acted_upon={c.acted_upon}, confidence={c.confidence}, "
                "and its leaf was folded with no surviving witness. "
                "Rehydrate from durable evidence ledger or restart sub-task."
            )
        if c.acted_upon and c.confidence < 1.0 and not c.has_surviving_witness:
            raise CheckpointInvalidError(
                f"Resume refused: load-bearing claim {c.text!r} has "
                f"sub-unit confidence {c.confidence} with evicted evidence."
            )


def safe_fold(claim: Claim, durable_ledger_present: bool) -> bool:
    """Gate leaf->parent folding on evidence durability. Returns True if
    the claim may be folded into a parent summary."""
    if claim.evidence_class is EvidenceClass.NONREQUERYABLE:
        if claim.acted_upon and not durable_ledger_present:
            raise CheckpointInvalidError(
                f"Cannot fold load-bearing non-requeryable claim {claim.text!r}: "
                "no durable evidence ledger entry. Write evidence first."
            )
    return True
