# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-treat-lossy-compaction-summaries-as-non-authoritative-when-restoring-from-checkpoint.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-treat-lossy-compaction-summaries-as-non-authoritative-when-restoring-from-checkpoint.warrant.json
# Markdown SHA-256:   a42f479ce152f84fd9b538e004144f17006bd3e645eff16a9aa80b7f932c2a32
# Code AST SHA-256:   8ba42984cf4f0a8844048a22b90cc5657159698e357fff2d0dbc8fa8288f4054
# Verified Classes:   CompactionFidelityError, SummaryClaim
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class CompactionFidelityError(RuntimeError):
    """Raised when a restored instance attempts to act on a load-bearing
    claim that originates from a lossy compaction summary without
    re-deriving it from the authoritative ledger."""
    pass


class SummaryClaim:
    def __init__(self, text: str, load_bearing: bool, rederivable: bool):
        self.text = text
        self.load_bearing = load_bearing
        self.rederivable = rederivable
        self.status = "unverified"  # unverified -> verified | assumed | rejected


def restore_and_validate(summary_claims, ledger, rederive_fn):
    """
    On cold-start from a checkpoint, every load-bearing summary claim must
    be re-derived from the authoritative ledger before the instance is
    permitted to act on it. Incidental claims may be carried as 'assumed'.
    """
    for claim in summary_claims:
        if not claim.load_bearing:
            claim.status = "assumed"
            continue

        if not claim.rederivable:
            # Load-bearing but no ledger path to confirm it. Refuse to
            # silently inherit it as fact.
            raise CompactionFidelityError(
                f"Load-bearing claim not re-derivable from ledger: {claim.text!r}. "
                f"Cannot treat compaction summary as authoritative."
            )

        derived = rederive_fn(claim, ledger)
        if derived is None:
            raise CompactionFidelityError(
                f"Re-derivation of claim {claim.text!r} returned no ledger evidence; "
                f"summary may have fabricated or distorted this fact."
            )
        if not _consistent(derived, claim.text):
            raise CompactionFidelityError(
                f"Summary claim {claim.text!r} contradicts ledger-derived value "
                f"{derived!r}; refusing to restore with laundered memory."
            )
        claim.status = "verified"

    return summary_claims


def _consistent(derived, claim_text: str) -> bool:
    # Placeholder for the domain-specific consistency check (e.g., numeric
    # match, tier-qualifier match, file-hash match). Must be strict: a
    # plausible-but-wrong summary is the failure mode, so loose matching
    # reintroduces the bug.
    return str(derived).strip() in claim_text
