# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-that-its-compaction-checkpoint-has-silently-invalidated-a-pending-tool-call-warrant-the-summary-boundary-continuity-invari.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-that-its-compaction-checkpoint-has-silently-invalidated-a-pending-tool-call-warrant-the-summary-boundary-continuity-invari.warrant.json
# Markdown SHA-256:   c06e9495750734bf497d8f86441c9b65f679cb1009eaddb427b96c4fa7e04655
# Code AST SHA-256:   f08355d4c61a1e656e61a3c4a3fca4e4baa401487dfc4dda8bf93593fc814630
# Verified Classes:   OrphanedWarrantError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class OrphanedWarrantError(RuntimeError):
    """Raised when a PENDING commitment crosses a lossy compaction boundary
    without a serialized, re-executable precondition in the structured ledger."""


def validate_compaction_boundary(ledger: dict, summary: str) -> None:
    """Pre-compaction guard: every live commitment must carry a structural
    precondition, not merely a prose recollection in the summary."""
    for commitment in ledger.get("commitments", []):
        if commitment["state"] not in ("PENDING", "UNKNOWN"):
            continue
        if not commitment.get("precondition_predicate"):
            raise OrphanedWarrantError(
                f"commitment {commitment['id']} is {commitment['state']} but has "
                f"no serialized precondition; summary '{summary[:60]}...' cannot "
                f"substitute for a re-executable check"
            )
        if not commitment.get("idempotency_lineage"):
            raise OrphanedWarrantError(
                f"commitment {commitment['id']} lacks idempotency lineage; a resumed "
                f"instance could derive a fresh key and double-commit"
            )


def resume_from_checkpoint(checkpoint: dict) -> dict:
    """Fail-closed resume: never reconstruct intent from prose alone."""
    ledger = checkpoint["ledger"]
    for commitment in ledger.get("commitments", []):
        if commitment["state"] == "PENDING":
            if not commitment.get("precondition_predicate"):
                # Do NOT fall back to summary-derived intent.
                raise OrphanedWarrantError(
                    f"refusing to resume {commitment['id']}: pending warrant has no "
                    f"provable precondition; demote to NEEDS_REVALIDATION and halt"
                )
            commitment["state"] = "NEEDS_REVALIDATION"
            commitment["resume_action"] = "REVALIDATE"
    return ledger
