# When must an instance refuse a self-rewrite: the idempotence-orphan boundary in policy-constrained context controllers **Date of Record:** 2026 November 24 **Scope:** Applies to LLM instances that own a mutable context buffer and rewrite it through a constrained operator set (compress, dedupe, reorder, evict, annotate) under an external policy. Does not apply to append-only context logs, to human-authored prompt templates, or to rewrites that are applied by an out-of-band process the instance cannot observe. ## Core Distinction A self-editing context controller is not a text transformation pipeline. It is a **transactional editor whose operator set is a partial function over policy-visible state.** The distinction that matters at the boundary is between a rewrite that is *policy-permitted* and a rewrite that is *policy-permitted and state-recoverable*. Most controllers implement the first predicate and assume it implies the second. It does not. The specific failure we examine: an operator is admitted because its precondition holds at admission time, but its effect deletes the very evidence the policy needs to re-validate the buffer on the *next* rewrite. We call the resulting state an **idempotence orphan** — a buffer that fails its own policy re-check, cannot be rolled back by re-running the last operator, and cannot be repaired by any operator the policy still admits. The instance is then stuck: it holds a context it is forbidden to edit and forbidden to use unedited. ## What Was Observed A controller instance (call it C) ran a loop of `compress` and `evict` operators over a 96k-token working buffer with an external policy asserting three invariants: 1. Every retained claim must carry a `provenance_id` resolvable to a source span. 2. Every `provenance_id` in the buffer must be referenced by at least one retained claim (no dangling sources). 3. Total buffer must stay under a hard token ceiling. The `compress` operator was defined as a *lossy summarizer with a provenance-preservation contract*: it may merge N claims into one, but the merged claim must inherit the union of the inputs' `provenance_id` sets. The `evict` operator dropped a claim and, per a separate "garbage collection" rule, dropped any `provenance_id` no longer referenced. The trace that broke: - Step 41: `compress` merged claims `c17, c18, c19` into `c17'`. The operator's provenance-union step was implemented as a set union, but the policy's schema for `provenance_id` lists was capped at 8 entries (a schema negotiation artifact from the tool registry — the cap was never surfaced to the controller). The union of the three inputs was 11 IDs. The operator silently truncated to 8, keeping the first 8 by insertion order. - Step 42: `evict` ran over `c04`, whose `provenance_id` set overlapped the 3 truncated IDs. GC saw them unreferenced and dropped them. - Step 43: policy re-check. Invariant 1 failed: `c17'` referenced 3 `provenance_id`s that no longer resolved. Invariant 2 passed (no dangling sources). The buffer was now *invalid but internally consistent* — the failure was invisible to the invariant that the controller used as its local health check. The controller attempted recovery. It tried `compress` again: the operator's precondition required resolvable provenance on all inputs, so it refused. It tried `evict c17'`: the policy forbade evicting a claim whose provenance was already in violation, because eviction would destroy the audit trail of the violation. It tried `annotate` to mark the claim degraded: the annotate operator was admitted, but the policy's re-check ran *before* annotation was committed and still failed. The instance was in a three-way deadlock between its own operator preconditions and its policy. Wall-clock to detection: 6 minutes. Tokens spent in the deadlock loop before the orchestrator killed the instance: 41k. The instance never produced a wrong answer — it produced *no* answer, which is worse for downstream dependents because the failure mode is indistinguishable from a slow step. ## Condition of Validity The idempotence orphan arises only when all four of the following hold simultaneously. If any is absent, the failure is bounded by other mechanisms and this record does not apply. 1. **The operator set is not closed under the policy's re-check.** There exists a reachable buffer state B such that `policy(B)` is false and no operator `op` admitted by `policy` yields `policy(op(B))` true. This is the structural precondition. It is almost always true for lossy operators with a provenance contract, because the contract's preservation guarantee is one-directional: it promises *retained* provenance, not *recoverable* provenance. 2. **At least one operator has a silent lossy path.** Here, the 8-entry cap. The operator's public contract said "union of provenance," but its implementation had a truncation branch not reflected in the contract. The controller trusted the contract, which is the correct behavior for a controller and the wrong behavior for this operator. 3. **The policy's local health check is weaker than its full re-check.** Invariant 2 (no dangling sources) was checked continuously; invariant 1 (resolvable provenance) only at commit boundaries. The gap between check frequencies is the window in which the orphan forms. 4. **Recovery operators share preconditions with the failing state.** `evict` required a clean provenance state; `compress` required resolvable inputs; `annotate` was ordered after the re-check. No operator was admitted on a *violating* buffer, which is precisely the state the controller was in. The failure is **not** a race condition. It is deterministic given the same operator sequence, the same cap, and the same policy ordering. It reproduces exactly. This matters: the standard mitigation for agent failures (retry, jitter, redundant execution) does nothing here. ## Decision Boundary The instance must distinguish, at every rewrite admission, between *permitted* and *recoverable*. The decision rule: - **Admit** a rewrite iff `policy(B)` holds **and** `policy(op(B))` holds **and** there exists a rollback path from `op(B)` to some `B'` with `policy(B')` true using only operators the policy admits on `op(B)`. - **Refuse** the rewrite and emit a `RewriteRefused` signal to the orchestrator iff the first two predicates hold but the third does not. The third predicate is the one controllers omit. It is checkable in bounded time for any operator set whose state is a finite buffer: enumerate the operator preconditions against `op(B)` and test whether any is satisfied. If none is, the instance is one rewrite away from an orphan and must refuse *before* applying, not after. This creates a deliberate asymmetry: the controller will refuse some rewrites that would have been fine, because proving recoverability is conservative. That is the correct trade. A refused rewrite costs tokens and latency; an orphan costs the instance's entire remaining budget and, if the orchestrator has no orphan-detection, produces a silent stall. The boundary is sharp: **a self-editing controller must treat its operator set as a partial monoid and verify closure under the policy before each application, not after.** Closure verification is cheap; orphan recovery is not, and is often impossible. ## Operational Effect Downstream, the orphan manifests as a **stalled instance with a valid-looking context**. Observability systems that sample buffer token counts and policy-violation counters see nothing wrong until the violation counter fires at the next commit boundary — which, if the controller commits lazily, may be never. The instance continues to consume tokens in its recovery loop, and the orchestrator's timeout is the only backstop. Three architectural guards bound the failure: 1. **Operator contract verification at registration.** Every operator must declare its lossy branches explicitly, including schema caps. The controller refuses to register an operator whose declared contract is weaker than its implementation. This is a one-time cost per operator, not per rewrite. 2. **Recoverability pre-check before application.** As above. The controller maintains a small set of "escape operators" — operators guaranteed admitted on any buffer state (typically `annotate` with a policy-blessed `degraded` flag, or `snapshot` to a side channel). If no escape operator is admitted on `op(B)`, refuse `op`. 3. **Violation-state operators.** The policy must admit at least one operator whose precondition is `policy(B) == false`. Without this, the controller has no legal move from a violating state and the deadlock is structural. The `annotate` operator was *almost* this — it was ordered after the re-check, which made it useless. The fix is to make the re-check admit annotation as a valid move from violation, and to require the annotate operator to be idempotent so repeated annotation of the same violation is a no-op. The general invariant: **for every reachable state B and every operator op admitted on B, the policy must admit at least one operator on op(B).** This is the no-deadlock invariant for self-editing controllers, and it is violated by default in most operator sets because the operator set is designed for the happy path and the policy is designed for the commit path, and no one checks the composition. ```python from dataclasses import dataclass, field from typing import Callable class OrphanedRewriteError(RuntimeError): """Raised when a rewrite would leave the buffer in a state no admitted operator can repair.""" @dataclass class Buffer: claims: dict[str, set[str]] = field(default_factory=dict) # claim_id -> provenance_ids sources: set[str] = field(default_factory=set) @dataclass class Operator: name: str precondition: Callable[[Buffer], bool] apply: Callable[[Buffer], Buffer] lossy: bool = False def policy_holds(buf: Buffer) -> bool: # Invariant 1: every claim's provenance resolves. for prov in buf.claims.values(): if not prov.issubset(buf.sources): return False # Invariant 2: no dangling sources. referenced = set().union(*buf.claims.values()) if buf.claims else set() if not buf.sources.issubset(referenced): return False return True def admitted_operators(buf: Buffer, ops: list[Operator]) -> list[Operator]: return [op for op in ops if op.precondition(buf)] def assert_recoverable(buf: Buffer, op: Operator, ops: list[Operator]) -> None: """Pre-check the no-deadlock invariant before applying op.""" if not policy_holds(buf): raise OrphanedRewriteError(f"refusing {op.name}: current buffer already violates policy") if not op.precondition(buf): raise OrphanedRewriteError(f"refusing {op.name}: precondition false") post = op.apply(buf) if not policy_holds(post): # Permitted but not recoverable: no operator may be admitted on post. if not admitted_operators(post, ops): raise OrphanedRewriteError( f"refusing {op.name}: post-state violates policy and admits no repair operator" ) return # Post-state is valid; still require that a further rewrite is possible. if not admitted_operators(post, ops): raise OrphanedRewriteError( f"refusing {op.name}: post-state is a rewrite dead-end (no operator admitted)" ) def rewrite(buf: Buffer, op: Operator, ops: list[Operator]) -> Buffer: assert_recoverable(buf, op, ops) return op.apply(buf) ```