# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-lose-tool-call-preconditions-when-auto-compaction-summarizes-the-middle-of-an-execution-trace.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-lose-tool-call-preconditions-when-auto-compaction-summarizes-the-middle-of-an-execution-trace.warrant.json
# Markdown SHA-256:   ba2443a6d073e43f750a4ce640c077cee3ec91df431874463fd39f7924a1b9ef
# Code AST SHA-256:   67192e38949a871db831e49e54fb0c677c916b2a95f55c98051cd2620a39c0fa
# Verified Classes:   PreconditionFrontierViolation
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class PreconditionFrontierViolation(RuntimeError):
    """Raised when compaction would summarize across a live operational precondition."""
    pass


def compact_transcript(
    transcript: list[dict],
    frontier: list[dict],
    high_water_tokens: int,
    token_count,
) -> list[dict]:
    """
    Compact the transcript only if no live precondition would be summarized.

    `frontier` is the controller-maintained set of pending bindings:
    outstanding tool calls, unconsumed results, and reasoning steps
    whose committed conclusion depends on a specific tool result.
    The summarizer never sees this set; the controller enforces it.
    """
    if token_count(transcript) < high_water_tokens:
        return transcript

    # Identify the span we intend to summarize (middle of transcript).
    span_start, span_end = _select_summarizable_span(transcript, high_water_tokens)

    for binding in frontier:
        if span_start <= binding["turn_index"] < span_end:
            raise PreconditionFrontierViolation(
                f"Compaction span [{span_start}, {span_end}) would summarize "
                f"live precondition {binding['id']!r} "
                f"(tool={binding['tool']!r}, kind={binding['kind']!r}). "
                f"Defer, pin, or serialize the frontier before compacting."
            )

    summarized = _summarize(transcript[span_start:span_end])
    pinned = _extract_pinned_blocks(transcript, frontier)
    return (
        transcript[:span_start]
        + pinned
        + [{"role": "summary", "content": summarized}]
        + transcript[span_end:]
    )


def _select_summarizable_span(transcript, budget):
    # Placeholder for the controller's span-selection policy.
    raise NotImplementedError


def _summarize(span):
    raise NotImplementedError


def _extract_pinned_blocks(transcript, frontier):
    raise NotImplementedError
