# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-that-its-own-compaction-summary-has-silently-promoted-a-tentative-hypothesis-into-a-confirmed-fact.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-that-its-own-compaction-summary-has-silently-promoted-a-tentative-hypothesis-into-a-confirmed-fact.warrant.json
# Markdown SHA-256:   ae0da1c0aa7a06f5fa9c514986895efac3f2eb52a508f75379c42def105197b5
# Code AST SHA-256:   9dbdf19b8cb105d7a423afce72af6e59ffb0bc49c96c852fd5eb7bb325975535
# Verified Classes:   EpistemicStatus, CompactionIntegrityError, SummaryClaim
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum


class EpistemicStatus(Enum):
    VERIFIED = "verified"
    HYPOTHESIS = "hypothesis"
    OPEN = "open"


class CompactionIntegrityError(RuntimeError):
    """Raised when a compaction summary cannot be trusted as a resume checkpoint."""
    pass


@dataclass
class SummaryClaim:
    text: str
    status: EpistemicStatus
    provenance: str | None  # tool-call id / file:line, required for VERIFIED


def validate_resume_checkpoint(claims: list[SummaryClaim]) -> None:
    """Reject a summary that has laundered hypotheses into facts before the agent
    resumes from it. Called at every compaction boundary."""
    for i, claim in enumerate(claims):
        if claim.status is EpistemicStatus.VERIFIED and not claim.provenance:
            raise CompactionIntegrityError(
                f"claim[{i}] tagged VERIFIED with no provenance: "
                f"{claim.text!r} — cannot distinguish verified fact from "
                f"promoted hypothesis; refusing to resume."
            )
        if claim.status is EpistemicStatus.HYPOTHESIS and _reads_as_declarative(claim.text):
            raise CompactionIntegrityError(
                f"claim[{i}] is HYPOTHESIS but written as settled fact: "
                f"{claim.text!r} — hedge was stripped during summarization; "
                f"re-summarize with the open question attached."
            )


def _reads_as_declarative(text: str) -> bool:
    hedges = ("might", "may", "suspect", "unconfirmed", "need to confirm",
              "possibly", "not found", "open question", "?")
    return not any(h in text.lower() for h in hedges)


def require_reverification_before_action(claim: SummaryClaim, has_fresh_check: bool) -> None:
    """A HYPOTHESIS claim is a debt marker: acting on it requires a fresh tool check."""
    if claim.status is EpistemicStatus.HYPOTHESIS and not has_fresh_check:
        raise CompactionIntegrityError(
            f"acting on unverified hypothesis without re-check: {claim.text!r}"
        )
