# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-preserve-task-critical-state-across-a-lossy-compaction-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-preserve-task-critical-state-across-a-lossy-compaction-boundary.warrant.json
# Markdown SHA-256:   feca582cc188b852491e309e05918805d365f1f4d3fbfac753665d1cf4da3f6d
# Code AST SHA-256:   b1a8c4686f2fece6789e49c3ae1632d2718ca999c1b259c02350d569fe4f125e
# Verified Classes:   CompactionClosureError, Constraint, CompactionArtifact
# Defensive Guards:   1 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Iterable

class CompactionClosureError(RuntimeError):
    """Raised when a compaction artifact would drop a load-bearing inferential constraint."""

@dataclass(frozen=True)
class Constraint:
    rule: str
    evidence: str
    inferential: bool
    binds: frozenset[str] = field(default_factory=frozenset)

@dataclass(frozen=True)
class CompactionArtifact:
    summary: str
    carried_rules: frozenset[str]
    plan_operations: frozenset[str]

def assert_compaction_closure(
    discovered: Iterable[Constraint],
    artifact: CompactionArtifact,
) -> None:
    """
    Enforce the dependency-closure invariant before resuming from a compaction boundary.

    A constraint is safe to drop iff it is lexical (re-derivable by re-reading
    files the plan already names). An inferential constraint is safe only if
    every operation it binds is enumerated in the plan, so the resume step
    will re-encounter it. Otherwise the artifact must explicitly carry the rule.
    """
    for c in discovered:
        if not c.inferential:
            continue
        unplanned_bindings = c.binds - artifact.plan_operations
        if unplanned_bindings and c.rule not in artifact.carried_rules:
            raise CompactionClosureError(
                f"Silent dependency drop: inferential constraint {c.rule!r} "
                f"(evidence: {c.evidence!r}) binds unplanned operations "
                f"{sorted(unplanned_bindings)} but is absent from the compaction "
                f"artifact. Resume would proceed without re-deriving it."
            )

# --- exercised against the observed trace ---
discovered = [
    Constraint(
        rule="Ledger rejects amount with >2 decimal places; quantize() shim is "
             "mandatory at every PaymentRouter->Ledger boundary",
        evidence="step 12: config ledger.max_scale=2 + failing SettlementBatch test",
        inferential=True,
        binds=frozenset({"write_new_call_site", "refactor_router", "add_ledger_adapter"}),
    ),
]
artifact = CompactionArtifact(
    summary="Migrate PaymentRouter to Ledger interface; 4 files touched; 69 steps remain.",
    carried_rules=frozenset(),  # the observed artifact carried nothing
    plan_operations=frozenset({"refactor_router"}),  # new call sites not yet planned
)

assert_compaction_closure(discovered, artifact)  # raises CompactionClosureError
