# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-auto-compaction-the-fidelity-floor-breach-at-the-summarization-seam.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-auto-compaction-the-fidelity-floor-breach-at-the-summarization-seam.warrant.json
# Markdown SHA-256:   427defebd9133db7873d09d52504c266bf660fc0e6990cae124322119f7b7072
# Code AST SHA-256:   ff0bbeb6413be429f4f78c58d7120c49feb0ccb31a256dc61712abab673be3c0
# Verified Classes:   CompactionFidelityError, Obligation
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class CompactionFidelityError(RuntimeError):
    """Raised when a summary would drop a blocking predicate of an open obligation."""
    pass


class Obligation:
    def __init__(self, oid: str, blocking_predicate: str):
        self.oid = oid
        self.blocking_predicate = blocking_predicate  # the fact that makes it a blocker


def commit_compaction(
    summary_text: str,
    open_obligations: list[Obligation],
    compactor_received_obligations: bool,
    budget_expandable: bool,
    raw_transcript_persisted: bool,
) -> str:
    # Rule 1: blind compaction of open obligations is invalid by construction.
    if open_obligations and not compactor_received_obligations:
        raise CompactionFidelityError(
            f"{len(open_obligations)} open obligations but compactor ran blind; "
            "cannot verify blocking-predicate preservation"
        )

    # Rule 2: every blocking predicate must be entailed by the summary.
    missing = [
        o for o in open_obligations
        if o.blocking_predicate.lower() not in summary_text.lower()
    ]
    if missing:
        ids = ", ".join(o.oid for o in missing)
        if budget_expandable or raw_transcript_persisted:
            # Defer, do not degrade: refuse this commit, keep ground truth.
            raise CompactionFidelityError(
                f"summary drops blocking predicates for obligations [{ids}]; "
                "deferring compaction (expand budget or checkpoint), raw transcript retained"
            )
        raise CompactionFidelityError(
            f"summary drops blocking predicates for [{ids}] and no recovery path exists; "
            "compaction MUST NOT commit"
        )

    # Rule 3: commit is only authoritative once predicates are verified present.
    return summary_text
