# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-summary-of-summary-has-silently-dropped-a-pinned-invariant-before-the-compaction-budget-is-exhausted.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-summary-of-summary-has-silently-dropped-a-pinned-invariant-before-the-compaction-budget-is-exhausted.warrant.json
# Markdown SHA-256:   d751312fb254d554ffc332241dd15778a3596376c8fb91e4366c242ec318bd41
# Code AST SHA-256:   995b3a83b50011367e5c8173769c9426b96e2d57555f3a0385b71ea09e7ec77c
# Verified Classes:   PinnedInvariantLost, PinnedInvariant, CompactionResult
# Defensive Guards:   3 fail-closed invariants (zero external deps)

import re
from dataclasses import dataclass, field

class PinnedInvariantLost(RuntimeError):
    """Raised when a compaction pass drops or degrades a pinned invariant."""
    pass

@dataclass
class PinnedInvariant:
    invariant_id: str
    verbatim: str
    # modality words that must never soften across compaction depth
    required_modality: tuple = ("must", "never", "require", "forbid", "do not")

@dataclass
class CompactionResult:
    depth: int
    summary_text: str
    pinned_ledger: list = field(default_factory=list)

def verify_compaction(
    result: CompactionResult,
    invariants: list,
    max_depth: int = 2,
) -> None:
    # 1. Depth cap: unbounded re-summarization is the root cause.
    if result.depth > max_depth:
        raise PinnedInvariantLost(
            f"compaction depth {result.depth} exceeds cap {max_depth}; "
            f"rebuild required from raw transcript"
        )

    # 2. Every pinned invariant must be present verbatim in the ledger,
    #    NOT merely paraphrased inside summary_text.
    ledger_text = "\n".join(result.pinned_ledger)
    for inv in invariants:
        if inv.verbatim not in ledger_text:
            raise PinnedInvariantLost(
                f"invariant {inv.invariant_id} missing from pinned ledger "
                f"at depth {result.depth} (was it folded into the lossy summary?)"
            )
        # 3. If it leaked into the summary instead of the ledger, that is the
        #    degradation path: detect modality softening.
        if inv.verbatim in result.summary_text:
            window = result.summary_text
            if not any(m in window.lower() for m in inv.required_modality):
                raise PinnedInvariantLost(
                    f"invariant {inv.invariant_id} present but modality softened "
                    f"in summary at depth {result.depth}; obligation demoted to preference"
                )

def compact(transcript: str, invariants: list, prior_depth: int) -> CompactionResult:
    # ... model-driven summarization of transcript ...
    summary = summarize(transcript)
    # Pinned ledger is carried verbatim, never re-summarized.
    ledger = [inv.verbatim for inv in invariants]
    result = CompactionResult(depth=prior_depth + 1, summary_text=summary, pinned_ledger=ledger)
    verify_compaction(result, invariants)
    return result

def summarize(text: str) -> str:
    # placeholder for the lossy summarizer
    return text[:200]
