# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-lose-committed-sub-goals-when-automatic-context-compaction-rewrites-the-plan-preamble-but-retains-the-tail-of-the-transcript.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-lose-committed-sub-goals-when-automatic-context-compaction-rewrites-the-plan-preamble-but-retains-the-tail-of-the-transcript.warrant.json
# Markdown SHA-256:   c51c90e6eb85940152a3f11467756089e2607596996b3668c664b99936ce5332
# Code AST SHA-256:   04154f6ceb952e6f9e4fae694a35ed415abbd63dbbe6912c2609bdcee2a60fbb
# Verified Classes:   CompactionCommitmentLoss
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class CompactionCommitmentLoss(RuntimeError):
    """Raised when a post-compaction context is missing a live commitment
    that the resumed plan depends on."""
    pass


# Commitments that must never be summarized away. Small, verbatim, authoritative.
PINNED_COMMITMENTS = {
    "constraint:no_touch_token_refresh_path",
    "decision:schema_B",
    "acceptance:all_call_sites_migrated",
}

# A digest produced by the compactor. Claims to be complete; is not.
DIGEST = {
    "activity": "updated 14 call sites; ran tests twice; fixed imports",
    "referenced_commitments": {"acceptance:all_call_sites_migrated"},  # constraint dropped
}


def resume_from_digest(digest: dict, pinned: set[str]) -> None:
    """Gate resumption on the invariant: every commitment the resumed plan
    references must be present verbatim in the pinned block. A digest that
    references a commitment not in the pinned set means the compactor
    summarized a live commitment -- halt before any tool call."""
    referenced = digest.get("referenced_commitments", set())
    missing = referenced - pinned
    if missing:
        raise CompactionCommitmentLoss(
            f"compaction dropped live commitment(s) {sorted(missing)}; "
            f"refusing to resume from digest with incomplete pinned state"
        )
    # Additionally: if the plan's active constraints are not pinned at all,
    # the digest is untrustworthy even if it does not reference them.
    if "constraint:no_touch_token_refresh_path" not in pinned:
        raise CompactionCommitmentLoss(
            "pinned block lacks the run's hard constraint; "
            "digest cannot be treated as authoritative"
        )


if __name__ == "__main__":
    try:
        resume_from_digest(DIGEST, PINNED_COMMITMENTS)
    except CompactionCommitmentLoss as e:
        print(f"HALT: {e}")
