# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-hierarchical-summarization-checkpoints-as-lossy-state-transitions-rather-than-reversible-compression.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-hierarchical-summarization-checkpoints-as-lossy-state-transitions-rather-than-reversible-compression.warrant.json
# Markdown SHA-256:   0b6a526fc9dad36ac71861962694d5eef587925e205e17e6cc54461a33e5619c
# Code AST SHA-256:   979a6e6ad8b4711b271c8cb98e716fb9a8f92e4c173550b14f44a39bce0d8f5f
# Verified Classes:   CheckpointDepthError, Provenance, Claim, CheckpointStore
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from enum import IntEnum
from typing import Optional


class CheckpointDepthError(RuntimeError):
    """Raised when a load-bearing claim is sourced from a lossy checkpoint
    without a retrievable raw span, or when the claim gates an irreversible action."""
    pass


class Provenance(IntEnum):
    RAW = 0          # token present in working set
    SUMMARY_D1 = 1   # one projection from raw
    SUMMARY_D2 = 2   # projection of a projection (hearsay)


@dataclass
class Claim:
    text: str
    provenance: Provenance
    span_id: Optional[str] = None       # key into cold store, None if evicted
    unresolved: bool = False            # summarizer flagged ambiguity


@dataclass
class CheckpointStore:
    cold_store: dict = field(default_factory=dict)  # span_id -> raw tokens

    def re_expand(self, span_id: str) -> str:
        if span_id not in self.cold_store:
            raise CheckpointDepthError(
                f"span {span_id} not retrievable; summary is unrecoverable"
            )
        return self.cold_store[span_id]


def authorize_action(claim: Claim, irreversible: bool, store: CheckpointStore) -> str:
    """Gate an action on a claim. Returns the evidence string to proceed with."""
    if claim.unresolved and irreversible:
        raise CheckpointDepthError(
            f"irreversible action gated on UNRESOLVED claim: {claim.text!r}; "
            "must surface ambiguity, not resolve by assumption"
        )
    if irreversible and claim.provenance >= Provenance.SUMMARY_D1:
        if claim.span_id is None:
            raise CheckpointDepthError(
                f"irreversible action gated on depth-{int(claim.provenance)} claim "
                f"with no retrievable span: {claim.text!r}"
            )
        return store.re_expand(claim.span_id)   # demand raw evidence
    if claim.provenance == Provenance.SUMMARY_D2 and not irreversible:
        return f"NAVIGATIONAL ONLY (hearsay): {claim.text}"
    return claim.text


# --- trace of the observed failure ---
store = CheckpointStore(cold_store={"span_17": "GET /api/v2/query -> 401; GET /api/v2/query/legacy -> 404"})
bad_claim = Claim(
    text="v2 query API rejects unauthenticated requests",
    provenance=Provenance.SUMMARY_D2,
    span_id=None,          # rollup evicted the span key
    unresolved=False,      # summarizer silently resolved the 401/404 split
)
try:
    authorize_action(bad_claim, irreversible=True, store=store)
except CheckpointDepthError as e:
    print(f"BLOCKED: {e}")  # agent must re-expand or halt, not retry-with-token
