# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-lose-verified-tool-call-provenance-when-hierarchical-summarization-checkpoints-compact-mid-execution.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-lose-verified-tool-call-provenance-when-hierarchical-summarization-checkpoints-compact-mid-execution.warrant.json
# Markdown SHA-256:   163988dbf21a08dc2fc22ab4b4d99edb46d4af7e1e90e89550e753f6afdfaf16
# Code AST SHA-256:   9e372e4f91e6d8fb8fe60d9f6a2447acba17b1d8359b385577ca08f71a00fb80
# Verified Classes:   ProvenanceClass, ProvenanceLaunderingError, EvidenceRecord, Claim, Checkpoint
# Defensive Guards:   2 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass, field
from enum import Enum


class ProvenanceClass(Enum):
    OBSERVED = 1     # backed by a raw tool-call record
    SUMMARIZED = 2   # paraphrase of observed records
    INFERRED = 3     # generated by summarizer, no source span


class ProvenanceLaunderingError(RuntimeError):
    """Raised when a rehydrated claim gates an irreversible action
    but cannot be resolved to an OBSERVED evidence record."""


@dataclass
class EvidenceRecord:
    call_id: str
    tool: str
    args_digest: str
    exit_code: int
    output_digest: str

    @staticmethod
    def digest(payload: str) -> str:
        return hashlib.sha256(payload.encode()).hexdigest()[:16]


@dataclass
class Claim:
    text: str
    provenance: ProvenanceClass
    evidence_ids: list[str] = field(default_factory=list)
    gates_irreversible: bool = False


@dataclass
class Checkpoint:
    tier: str
    claims: list[Claim]
    evidence: dict[str, EvidenceRecord]


def rehydrate_and_gate(checkpoint: Checkpoint, action_claim: Claim) -> None:
    """Resolve an action-gating claim against the evidence ledger.

    The invariant: a claim that gates an irreversible action must be
    OBSERVED and every referenced evidence id must survive compaction.
    Summarized or inferred claims are rejected outright; they must be
    re-verified against the live system before the action proceeds.
    """
    if not action_claim.gates_irreversible:
        return  # exploratory claims may ride on prose

    if action_claim.provenance is not ProvenanceClass.OBSERVED:
        raise ProvenanceLaunderingError(
            f"Claim '{action_claim.text[:60]}...' gates an irreversible action "
            f"but is {action_claim.provenance.name}, not OBSERVED. "
            f"Re-verify against live system before proceeding."
        )

    for eid in action_claim.evidence_ids:
        if eid not in checkpoint.evidence:
            raise ProvenanceLaunderingError(
                f"Claim '{action_claim.text[:60]}...' cites evidence {eid} "
                f"that did not survive compaction into tier '{checkpoint.tier}'. "
                f"Provenance is broken; treat claim as INFERRED and re-verify."
            )

    # Evidence intact: safe to gate.
    return
