# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-apply-a-model-driven-pruning-decision-to-its-own-context-policy.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-apply-a-model-driven-pruning-decision-to-its-own-context-policy.warrant.json
# Markdown SHA-256:   d50da3717d345401e4adb260bd66e18045cd65a4629287c837198c7a01ddc774
# Code AST SHA-256:   7c8cda3a31c99ed6879f550d86d5f7d1ca13ae577c2aa4490721e680b3621f79
# Verified Classes:   PolicySelfMutationError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class PolicySelfMutationError(RuntimeError):
    """Raised when a model-driven policy edit would destroy its own justification."""
    pass


def apply_policy_edit(
    edit,
    *,
    affected_region,
    cited_evidence,
    ledger_is_prunable,
    out_of_band_confirmation,
):
    evidence_in_region = any(seg in affected_region for seg in cited_evidence)

    if evidence_in_region and ledger_is_prunable:
        raise PolicySelfMutationError(
            "Refusing in-band policy edit: cited evidence lies inside the "
            "region the edit would prune, and the audit ledger is itself "
            "subject to the edit's predicate. No reversal record can survive."
        )

    if evidence_in_region and not out_of_band_confirmation:
        raise PolicySelfMutationError(
            "Deferring policy edit: evidence is inside the affected region; "
            "stage a snapshot and require out-of-band confirmation before commit."
        )

    # Safe path: justification lives outside the affected region, or the
    # ledger is non-prunable and confirmation has been obtained.
    return edit.commit()
