# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-refuse-to-compact-the-compaction-trigger-boundary-where-summarization-destroys-the-very-constraint-the-agent-is-currently-obeyi.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-refuse-to-compact-the-compaction-trigger-boundary-where-summarization-destroys-the-very-constraint-the-agent-is-currently-obeyi.warrant.json
# Markdown SHA-256:   bd2c272e42ff1b192d435d86829ab4defa574a7d01023595518fa0ca7d0273d0
# Code AST SHA-256:   a5b64e21d7ca851542589f01170160a754aedf7b879be2a66d2ab9670ad612a6
# Verified Classes:   CompactionBoundaryError, SpanKind, Span
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum


class CompactionBoundaryError(RuntimeError):
    """Raised when a compaction pass would consume a live normative span."""


class SpanKind(Enum):
    DESCRIPTIVE = "descriptive"
    NORMATIVE = "normative"


@dataclass(frozen=True)
class Span:
    kind: SpanKind
    text: str
    pinned: bool = False


def compact(
    window: list[Span],
    compactable_budget: int,
    summarize=lambda spans: Span(SpanKind.DESCRIPTIVE, "...", False),
) -> tuple[list[Span], list[Span]]:
    """
    Returns (pinned_spans, compacted_spans).
    Refuses to summarize any NORMATIVE span that is not already pinned.
    """
    unpinned_normative = [s for s in window if s.kind is SpanKind.NORMATIVE and not s.pinned]
    if unpinned_normative:
        # Do not silently paraphrase a live rule. Pin verbatim and re-evaluate.
        raise CompactionBoundaryError(
            f"compaction would consume {len(unpinned_normative)} live normative span(s); "
            f"pin verbatim before re-triggering. first={unpinned_normative[0].text!r}"
        )

    pinned = [s for s in window if s.pinned]
    compactable = [s for s in window if not s.pinned]

    # Trigger must be computed over compactable tokens only, never the pinned set.
    if sum(len(s.text) for s in compactable) <= compactable_budget:
        return pinned, compactable

    # Re-summarization guard: a summary must never weaken a prior pin.
    summary = summarize(compactable)
    if summary.kind is SpanKind.NORMATIVE:
        raise CompactionBoundaryError(
            "summarizer emitted a normative span from descriptive input; "
            "refusing to accept generated constraints"
        )
    return pinned, [summary]


def run_compaction_cycle(window: list[Span], compactable_budget: int) -> list[Span]:
    try:
        pinned, compacted = compact(window, compactable_budget)
        return pinned + compacted
    except CompactionBoundaryError as exc:
        # Escalate rather than compact harder: context pressure is a halt condition.
        raise CompactionBoundaryError(
            f"compaction halted; escalate context pressure to orchestrator: {exc}"
        ) from exc


# Reproduction of the observed failure, now bounded:
history = [
    Span(SpanKind.DESCRIPTIVE, "user: refactor the auth module, keep tests green"),
    Span(SpanKind.NORMATIVE, "user: do not touch migrations/ — those are generated"),
    Span(SpanKind.DESCRIPTIVE, "agent: reading auth/handler.py ..."),
]
run_compaction_cycle(history, compactable_budget=64)  # -> raises CompactionBoundaryError
