# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-to-compact-the-tombstone-invariant-for-lossy-summarization-under-retrieval-backed-restoration.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-to-compact-the-tombstone-invariant-for-lossy-summarization-under-retrieval-backed-restoration.warrant.json
# Markdown SHA-256:   013ec2cbae8dbf48325c6c282bf9437733a91b8e880623ec8b1480f73f6b236c
# Code AST SHA-256:   5420bb6cd7ff8f3a6670a4cd0f0897bca10da2aba9eed12c32e297257bf53de3
# Verified Classes:   CompactionCommitmentError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class CompactionCommitmentError(RuntimeError):
    """Raised when a lossy summary is used to authorize an action
    whose underlying commitment was paraphrased, negated, or dropped."""
    pass


COMMITMENT_KINDS = {"prohibition", "authorization", "gate", "deadline", "threshold"}


def classify_for_compaction(proposition: dict) -> str:
    # Bias toward commitment on ambiguity; deliberation is the only
    # safe-to-summarize class.
    if proposition.get("kind") in COMMITMENT_KINDS:
        return "commitment"
    if proposition.get("negated") or proposition.get("conditional"):
        return "commitment"
    if proposition.get("exact_identifier") or proposition.get("numeric_threshold"):
        return "commitment"
    return "deliberation"


def compact_span(turns: list[dict], summarizer) -> dict:
    tombstones, deliberative = [], []
    for turn in turns:
        for prop in turn["propositions"]:
            if classify_for_compaction(prop) == "commitment":
                tombstones.append({
                    "kind": prop["kind"],
                    "subject": prop["subject"],
                    "predicate": prop["predicate"],
                    "object": prop["object"],
                    "source_turn_id": turn["id"],
                    "verbatim": prop["text"],
                })
            else:
                deliberative.append(prop["text"])
    summary = summarizer("\n".join(deliberative))
    return {"summary": summary, "tombstones": tombstones}


def authorize_action(action: dict, compacted: dict, raw_store) -> None:
    for tomb in compacted["tombstones"]:
        if tomb["kind"] == "prohibition" and action["target"] == tomb["subject"]:
            raw = raw_store.get(tomb["source_turn_id"])
            if tomb["verbatim"] not in raw["text"]:
                # Summary diverged from the raw commitment it claims to witness.
                raise CompactionCommitmentError(
                    f"Tombstone for {tomb['subject']} does not match source turn "
                    f"{tomb['source_turn_id']}; summary is not a valid witness."
                )
            if action.get("resolves") != tomb["object"]:
                raise CompactionCommitmentError(
                    f"Action on {action['target']} blocked: gate {tomb['object']} "
                    f"unresolved per turn {tomb['source_turn_id']}."
                )
    # Only reached if every commitment tombstone reconciled against raw state.
    return None
