# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-to-resume-from-a-lossy-context-compaction-summary-whose-provenance-chain-has-been-silently-truncated.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-to-resume-from-a-lossy-context-compaction-summary-whose-provenance-chain-has-been-silently-truncated.warrant.json
# Markdown SHA-256:   da7c33f7e3410060b1c712fe6a7b5ba16608058de18183185c44f77b0750d38b
# Code AST SHA-256:   0a349c47a9b7b82ec60690b639671b67f0ec361df059f0575acef8c373893e04
# Verified Classes:   ProvenanceTruncatedError, Summary
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Optional


class ProvenanceTruncatedError(RuntimeError):
    """Raised when a summary is used for action but its provenance chain is broken."""


@dataclass
class Summary:
    text: str
    compaction_depth: int
    span_manifest: Optional[list[str]]          # None if stripped
    verified_facts: dict[str, bool] = field(default_factory=dict)  # claim -> verified?
    contested: set[str] = field(default_factory=set)


def resume_actionable(summary: Summary, claims_to_act_on: list[str]) -> bool:
    """
    Fail-closed predicate: may the instance take an action that depends on
    these claims, given this summary as its only surviving context?
    """
    if summary.compaction_depth <= 0:
        return True  # raw transcript, no compaction

    if summary.compaction_depth == 1:
        if summary.span_manifest is None:
            raise ProvenanceTruncatedError(
                f"depth-1 summary for claims {claims_to_act_on} has no span_manifest; "
                "cannot ground action against raw transcript"
            )
        return True

    # depth >= 2: provenance must have survived the summary-of-summary
    if summary.span_manifest is None or not summary.verified_facts:
        raise ProvenanceTruncatedError(
            f"compaction_depth={summary.compaction_depth} but provenance stripped; "
            f"claims {claims_to_act_on} are advisory only — rehydrate raw spans "
            "or halt before acting"
        )

    for claim in claims_to_act_on:
        if claim in summary.contested:
            raise ProvenanceTruncatedError(
                f"claim {claim!r} is contested across compaction depth "
                f"{summary.compaction_depth}; must be re-grounded against raw spans "
                "before any dependent tool call"
            )
        if claim in summary.verified_facts and not summary.verified_facts[claim]:
            raise ProvenanceTruncatedError(
                f"claim {claim!r} is tagged unverified inference; refusing to act on it"
            )

    return True


def guarded_step(summary: Summary, claims: list[str], action) -> None:
    if not resume_actionable(summary, claims):
        raise ProvenanceTruncatedError(
            f"resume_actionable returned False for {claims}; refusing to execute"
        )
    action()
