# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-model-driven-context-pruning-as-an-irreversible-state-mutation-requiring-a-pre-prune-checkpoint.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-model-driven-context-pruning-as-an-irreversible-state-mutation-requiring-a-pre-prune-checkpoint.warrant.json
# Markdown SHA-256:   83fdaf499ecacad7c723b9057c6d0cb38ef4337edc80efcc4eb9a123f5b221b6
# Code AST SHA-256:   8fc78a8b10ca9860363fd9513b74f96ea417147f385124059c2d77c57436a19c
# Verified Classes:   IrreversiblePruneError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class IrreversiblePruneError(RuntimeError):
    """Raised when a model-driven context prune would destroy evidence
    without a durable pre-prune checkpoint and literal retrieval anchors."""
    pass


def apply_prune(window: list[str], eviction_ids: set[str],
                summary: str, anchors: dict[str, str],
                store: dict[str, list[str]], step: int) -> list[str]:
    """
    window       : current token blocks
    eviction_ids : block ids the model chose to drop
    summary      : model-authored paraphrase (NOT a pointer)
    anchors      : eviction_id -> literal retrieval key into `store`
    store        : append-only external transcript, keyed by block id
    """
    evicted = [b for b in window if b in eviction_ids]

    # 1. Every evicted block must be recoverable from the immutable store.
    for block_id in eviction_ids:
        if block_id not in store:
            raise IrreversiblePruneError(
                f"step {step}: evicting {block_id!r} not present in append-only "
                f"store; prune would be unrecoverable"
            )

    # 2. Every evicted block must have a literal anchor retained in the window.
    #    A prose summary is not an anchor: it cannot re-fetch the evidence.
    missing = [b for b in eviction_ids if b not in anchors]
    if missing:
        raise IrreversiblePruneError(
            f"step {step}: eviction set {missing!r} has no literal anchor; "
            f"summary alone is irreversible and cannot be re-expanded on "
            f"contradiction"
        )

    # 3. A pre-prune checkpoint must exist before the window is rewritten.
    if f"checkpoint:{step}" not in store:
        raise IrreversiblePruneError(
            f"step {step}: no pre-prune checkpoint; refusing irreversible "
            f"mutation of sole evidence store"
        )

    # Only now is the prune a reversible view transition.
    return [b for b in window if b not in eviction_ids] + [summary]
