# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-lossy-compaction-summary-as-a-destructive-state-transition-rather-than-a-recoverable-cache-miss.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-lossy-compaction-summary-as-a-destructive-state-transition-rather-than-a-recoverable-cache-miss.warrant.json
# Markdown SHA-256:   eb3409e51ea68cdb6dd0c067222ef64ddf3c2829d703fd5ae734aea7997c531d
# Code AST SHA-256:   ad3d4fe1fb250532d973361de04d3ec8e54e4ef6bd65c5c140eaf49409d8aa5e
# Verified Classes:   ObligationLossError, ObligationLedger
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class ObligationLossError(RuntimeError):
    """Raised when an irreversible action would proceed on a lossy compaction
    summary without re-verifying pre-compaction obligations."""
    pass


class ObligationLedger:
    def __init__(self):
        self._obligations = {}  # id -> {"text": str, "discharged": bool}

    def add(self, oid, text):
        self._obligations[oid] = {"text": text, "discharged": False}

    def discharge(self, oid):
        if oid in self._obligations:
            self._obligations[oid]["discharged"] = True

    def open_obligations(self):
        return {k: v["text"] for k, v in self._obligations.items() if not v["discharged"]}


def guarded_action(action_fn, *, action_is_irreversible, summary_is_lossy,
                   ledger, summary_claims_no_constraints):
    """Execute an action only if the compaction invariant holds.

    The invariant: an irreversible action whose correctness depends on the
    ABSENCE of a forgotten constraint may not run against a lossy summary
    unless the obligation ledger has been consulted and is empty.
    """
    if action_is_irreversible and summary_is_lossy:
        open_obs = ledger.open_obligations()
        # The summary asserting "no constraints" is exactly the confident
        # false-completeness signal we must not trust.
        if summary_claims_no_constraints and open_obs:
            raise ObligationLossError(
                f"Summary claims no constraints, but {len(open_obs)} open "
                f"obligation(s) survive in the ledger: {list(open_obs.values())}. "
                f"Refusing irreversible action on a lossy projection."
            )
        if open_obs:
            raise ObligationLossError(
                f"Irreversible action gated: {len(open_obs)} undischarged "
                f"obligation(s) must be re-verified against the ledger before "
                f"execution: {list(open_obs.values())}"
            )
    return action_fn()


# --- trace of the observed failure, now bounded ---
ledger = ObligationLedger()
ledger.add("c1", "do not modify primary deployment replica count")

try:
    guarded_action(
        action_fn=lambda: "scaled primary deployment",
        action_is_irreversible=True,
        summary_is_lossy=True,
        ledger=ledger,
        summary_claims_no_constraints=True,  # the fluent, incomplete summary
    )
except ObligationLossError as e:
    print("BLOCKED:", e)
