# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-an-invariant-preserving-self-edit-of-its-own-context-policy.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-an-invariant-preserving-self-edit-of-its-own-context-policy.warrant.json
# Markdown SHA-256:   3641bc6bda0e5016ebb47814234ac15c7a925f966a466450e1beff3fecb1ff9e
# Code AST SHA-256:   e8b7f9689a3063a7d8664cea50a50fcad9513c65f946b313ededbc7f95d1c9fe
# Verified Classes:   FixedPointInvariantViolation
# Defensive Guards:   5 fail-closed invariants (zero external deps)

class FixedPointInvariantViolation(RuntimeError):
    """Raised when a self-edit preserves the policy's surface invariants
    but violates them at the runtime's stabilization fixed point."""
    pass


def admissible_self_edit(policy, edit_op, invariants, runtime_depth_bound,
                         operator_budget_tokens, reserve_tokens):
    """Admit a self-editing context-policy rewrite only if it preserves the
    invariant set over the runtime's fixed point, not merely over policy text.

    Returns the committed policy or raises FixedPointInvariantViolation.
    """
    if runtime_depth_bound is None:
        raise FixedPointInvariantViolation(
            "runtime declares no cascade-depth bound; fixed-point "
            "preservation is uncheckable and the edit must be refused"
        )

    candidate = edit_op(policy)

    # Surface check: necessary, never sufficient.
    surface_ok = all(inv.check_text(candidate) for inv in invariants)
    if not surface_ok:
        raise FixedPointInvariantViolation(
            "edit violates a surface invariant; reject before simulation"
        )

    # Monotonicity: reject edits that grow a trigger-feeding quantity while
    # claiming to relieve pressure.
    if candidate.digest_volume_delta() > candidate.raw_volume_delta():
        raise FixedPointInvariantViolation(
            "non-monotone edit: digest volume grows faster than raw volume "
            "shrinks under worst-case cascade"
        )

    # Simulate the runtime's actual closure to the declared depth.
    state = candidate.initial_state()
    for depth in range(runtime_depth_bound):
        state = candidate.stabilize_step(state)
        if state.utilization > candidate.evict_threshold:
            state = candidate.evict_lowest(state)
        for inv in invariants:
            if not inv.check_fixed_point(state):
                raise FixedPointInvariantViolation(
                    f"invariant {inv.name!r} fails at cascade depth {depth}: "
                    f"pinned unit {inv.pin_target!r} absent from state"
                )

    # The edit must not consume the headroom it is meant to protect.
    sim_cost = candidate.simulation_cost_tokens(runtime_depth_bound)
    if sim_cost > reserve_tokens:
        raise FixedPointInvariantViolation(
            f"cascade simulation cost {sim_cost} exceeds the policy's own "
            f"retrieval reserve {reserve_tokens}; edit would starve refill"
        )

    return candidate
