# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-compaction-summary-as-a-lossy-projection-and-refuse-to-resume-from-it-without-a-provenance-checked-rehy.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-compaction-summary-as-a-lossy-projection-and-refuse-to-resume-from-it-without-a-provenance-checked-rehy.warrant.json
# Markdown SHA-256:   5f3e0ea569f9b0bee9e6e948551e1eb51738051f0940c19cb7aa6462b9eab5b5
# Code AST SHA-256:   de692b0fc28b670dc1a92442731b2cdd789c8bbbc46e0ec6c312a6e0fb140df1
# Verified Classes:   SummaryProvenanceError, Fact
# Defensive Guards:   1 fail-closed invariants (zero external deps)

class SummaryProvenanceError(RuntimeError):
    """Raised when an agent attempts a consequential action on a fact whose
    only surviving representation is a lossy compaction summary."""


class Fact:
    def __init__(self, key, value, source, consequential):
        self.key = key
        self.value = value
        self.source = source            # "live" | "ledger" | "summary"
        self.consequential = consequential

    def is_verified(self):
        return self.source in ("live", "ledger")


def gate_resumption(pending_action, facts_used, rehydrator):
    """Enforce: summary-sourced + consequential + unverifiable => halt.

    rehydrator: callable(fact) -> Fact | None, attempts to re-derive the fact
    from an authoritative source. Returns None if re-derivation is impossible.
    """
    for fact in facts_used:
        if fact.is_verified():
            continue
        if not fact.consequential:
            # Non-consequential summary facts may pass as hypotheses.
            continue
        recovered = rehydrator(fact)
        if recovered is None or not recovered.is_verified():
            raise SummaryProvenanceError(
                f"Action '{pending_action}' depends on fact '{fact.key}' "
                f"(claimed value={fact.value!r}) whose only source is a "
                f"compaction summary and which could not be rehydrated. "
                f"Refusing to resume; escalate or re-plan."
            )
        # Replace the summary-sourced belief with the verified value.
        fact.value = recovered.value
        fact.source = recovered.source
    return True


def resume_after_compaction(pending_action, facts_used, rehydrator):
    try:
        gate_resumption(pending_action, facts_used, rehydrator)
    except SummaryProvenanceError as e:
        return {"status": "halted", "reason": str(e)}
    return {"status": "resumed", "action": pending_action}
