# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-model-driven-context-rewrite-as-a-destructive-write-rather-than-a-compaction.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-model-driven-context-rewrite-as-a-destructive-write-rather-than-a-compaction.warrant.json
# Markdown SHA-256:   c74b6299a7c3e7f8bf6c3be93853a2a0f7d91e85a63c41aca388042a16909dfb
# Code AST SHA-256:   c44475cac5f2a51f6c91c7584dfc029dc4809792e54e9045a4faf32645b4a780
# Verified Classes:   DestructiveRewriteError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class DestructiveRewriteError(RuntimeError):
    """Raised when a model-driven context rewrite would sever a live precondition referent."""


def apply_rewrite(rewrite, spans, preconditions):
    removed = set(rewrite.span_ids)
    live_referents = {p.span_id for p in preconditions if p.live}
    severed = removed & live_referents
    if severed:
        raise DestructiveRewriteError(
            f"prune would drop live referents {sorted(severed)}; "
            f"use summarize_with_binding instead"
        )
    for fact in rewrite.asserted_facts:
        if fact.names_artifact() and not fact.has_binding():
            raise DestructiveRewriteError(
                f"summary asserts artifact {fact.name!r} without binding"
            )
    return rewrite.commit(spans)
