# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-draw-the-compaction-boundary-so-that-a-summarized-prefix-remains-a-sound-precondition-for-the-un-summarized-suffix.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-draw-the-compaction-boundary-so-that-a-summarized-prefix-remains-a-sound-precondition-for-the-un-summarized-suffix.warrant.json
# Markdown SHA-256:   fa0adde5a02408356c466e407baa60c6f54fad7de321939cbed03c14d0bbcc41
# Code AST SHA-256:   e64aba8b95e4b63097e1fdbb3a654696c93a087086362fda28e3ab2588e16828
# Verified Classes:   UnsoundCompactionError
# Defensive Guards:   1 fail-closed invariants (zero external deps)

class UnsoundCompactionError(RuntimeError):
    """Raised when a compacted prefix no longer satisfies the dependency
    set of a pending obligation in the retained suffix."""
    pass


def compact_prefix(transcript, pending_obligations, token_budget):
    """Compact the transcript prefix such that the retained summary remains a
    sound precondition for every pending obligation.

    Raises UnsoundCompactionError rather than emitting a summary that drops a
    load-bearing fact.
    """
    # 1. Enumerate dependencies of unresolved obligations BEFORE summarizing.
    required_verbatim = set()
    for obligation in pending_obligations:
        if obligation.is_resolved():
            continue  # resolved work may be semantically compressed
        for arg in obligation.literal_arguments():
            required_verbatim.add(arg)  # identifiers, paths, exact constraints

    # 2. Produce a candidate summary of the prefix.
    summary = summarize(transcript.prefix(), budget=token_budget)

    # 3. Recoverability probe: every required literal must survive verbatim
    #    in the summary or in the retained suffix.
    retained = summary.text + transcript.suffix().text
    for literal in required_verbatim:
        if literal not in retained:
            raise UnsoundCompactionError(
                f"Compaction would drop load-bearing literal {literal!r} "
                f"required by a pending obligation; boundary is unsound. "
                f"Move the seam forward or pin the literal verbatim."
            )

    return summary
