# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-model-driven-pruning-of-its-own-working-memory-to-preserve-a-continuity-invariant.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-model-driven-pruning-of-its-own-working-memory-to-preserve-a-continuity-invariant.warrant.json
# Markdown SHA-256:   a60d28024972d5279dca0fa6b8a5f94a374451a58561fc55efa91d877445f0d6
# Code AST SHA-256:   f00ea40fe6de8264baf7bf60bbe82d5e64d0fab32cc76f41254767911854af83
# Verified Classes:   NonRegenerablePruneError
# Defensive Guards:   1 fail-closed invariants (zero external deps)

class NonRegenerablePruneError(RuntimeError):
    """Raised when a self-edit targets a non-regenerable fact mid-plan."""
    pass


def apply_self_edit(entry, plan_open, policy):
    # entry.regenerable is set by the tool wrapper, never by the model.
    if not entry.regenerable and plan_open and policy.action in ("erase", "lossy_rewrite"):
        # Refuse: relevance was estimated under the context we are about to destroy.
        raise NonRegenerablePruneError(
            f"cannot {policy.action} non-regenerable entry {entry.id!r} "
            f"while plan is open; pin literal, compress neighbors only"
        )
    return policy.execute(entry)
