# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-compresses-context-under-a-governor-that-measures-tokens-not-propositions.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-compresses-context-under-a-governor-that-measures-tokens-not-propositions.warrant.json
# Markdown SHA-256:   1bf2280252d4d26229835ed924aa7dc74c591b8b146aca7f0d9e85f2b6e3faed
# Code AST SHA-256:   f9145b961061854bc83b37d3ee4119bbdf7a9d8930702800a2275e41f70926ba
# Verified Classes:   PredicateErosionError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class PredicateErosionError(RuntimeError):
    """Raised when a compression pass would drop a load-bearing predicate."""
    pass


LOAD_BEARING_MARKERS = ("must", "must not", "never", "always", "unless",
                        "do not", "except", "tolerance", "±")


def classify_span(span_turns: list[str]) -> str:
    text = " ".join(span_turns).lower()
    if any(m in text for m in LOAD_BEARING_MARKERS):
        return "pinned"
    if any(ch.isdigit() for ch in text) and ("inv-" in text or "%" in text):
        return "pinned"
    if not span_propositionally_closed(span_turns):
        return "unknown"
    return "evictable"


def span_propositionally_closed(span_turns: list[str]) -> bool:
    # Conservative: only spans explicitly tagged as superseded are closed.
    return all(t.startswith("[SUPERSEDED]") or t.startswith("[NARRATIVE]")
               for t in span_turns)


def compress(span_id: str, span_turns: list[str], depth: int) -> str:
    if depth > 1:
        raise PredicateErosionError(
            f"span {span_id}: re-compression (depth={depth}) is forbidden; "
            f"re-derive from pinned originals instead"
        )
    regime = classify_span(span_turns)
    if regime in ("pinned", "unknown"):
        raise PredicateErosionError(
            f"span {span_id}: classified {regime}; refusing to summarize. "
            f"Pin verbatim and inject literal, do not paraphrase."
        )
    return summarize(span_turns)
