# When must an instance refuse to commit a self-edit: monotonic invariant loss under in-place working-memory rewrites **Date of Record:** 2026 November 25 **Scope:** Applies to agents that mutate their own context window or memory store in place (compaction, summarization, scratchpad rewriting, "self-editing" memory) while asserting that declared invariants survive the edit. Does not apply to append-only logs or to external stores the agent cannot rewrite. ## Core Distinction The distinction that matters is between *lossy compression* and *invariant-violating compression*. A self-editing agent is allowed to forget. It is not allowed to forget the specific facts that its own downstream guardrails depend on to remain sound. The failure mode is not "the summary is bad" — it is that the rewrite silently deletes the witness for an invariant the agent will later act on as if it were still true. The agent then proceeds under a false premise that no downstream check can catch, because the check reads the same rewritten memory that dropped the evidence. Concretely: an agent holds the invariant `INV-NOAUTH: no credential has been written to the outbound queue`. It satisfies this by retaining the fact `queue_contains_secret = false` (a negative fact) plus the tool-call trace that established it. A summarization pass compresses "checked queue, empty" into "queue ok". The string `queue_contains_secret` disappears. Later, a resumed sub-task appends a credential to the queue and asks "does INV-NOAUTH still hold?" — the check searches memory, finds no `queue_contains_secret` binding, treats absence as *unknown*, and either (a) blocks conservatively forever (deadlock) or (b) defaults to *satisfied* (violation). Both are failures; (b) is the dangerous one. ## What Was Observed Trigger condition: an in-place rewrite (summarize/compact) is applied to working memory while at least one invariant is in the `ESTABLISHED` state and its witness is a *negative* or *absence* fact, i.e. a fact whose content is the non-occurrence of something. Negative facts are the ones summarizers reliably drop, because they carry no positive entity to anchor a summary sentence. Failure trace, step by step: 1. State `S0`: memory contains `witness(INV-NOAUTH) = {query: SELECT count(*) FROM queue WHERE secret IS NOT NULL, result: 0, ts: t0}`. Invariant state = `ESTABLISHED`. 2. Rewrite `R` fires (context 91% full). Summarizer emits `"queue verified clean at t0"`. The structured witness record is discarded; only prose survives. 3. A mutating tool call `enqueue(cred)` executes at `t1` under a *different* sub-goal that never re-checks INV-NOAUTH. 4. Resume/checkpoint at `t2`: guard evaluates INV-NOAUTH. It parses memory for a witness. Prose `"queue verified clean"` is not machine-checkable, so the guard falls to its default policy. 5. If default policy is `assume_established`, the guard passes and the violation is now live and undetectable — the witness that would have contradicted it was destroyed by `R`, and the mutation that invalidated it happened after the witness's timestamp anyway. The non-obvious part: the invariant was *valid at rewrite time* and *valid at rewrite time is not the property the guard needs*. The guard needs validity *as of the last mutation*, and the rewrite severed the link between witness and mutation ordering. ## Condition of Validity A self-edit `R` is invariant-preserving only if, for every invariant `I` in state `ESTABLISHED`: - `witness(I)` survives `R` in a *machine-checkable* form (typed record, not prose), and - `R` does not advance the memory's logical clock past the timestamp of `witness(I)` without re-establishing `I`, and - the guard's default policy on missing witness is `UNKNOWN → block`, never `UNKNOWN → satisfied`. If any of these fails, `R` must be refused and the agent must instead grow memory (spill to external store) or request an explicit re-verification before compaction. The invariant does not hold "by default"; it holds only under a witness that is both present and newer than the last mutation it constrains. ## Decision Boundary The boundary is not "is the summary faithful?" It is: **does the rewrite preserve the witness-to-mutation ordering relation for every live invariant?** A summary can be perfectly faithful and still violate this, because faithfulness is measured against the pre-rewrite content, while the invariant needs the post-rewrite content to still answer "has anything invalidated this since the witness?" Prose summaries cannot answer ordering questions. So the decision rule is structural: if `R` would demote any `ESTABLISHED` invariant's witness from typed to untyped, or would allow a mutation between witness and guard-check without re-establishing, `R` is refused. ## Operational Effect Refusing the rewrite at 91% context forces one of two bounded behaviors: spill the typed witness records to an external store and rehydrate on guard-check (memory grows, correctness holds), or run an explicit re-verification pass *before* compaction so the post-compaction witness is fresh relative to any pending mutation. The cost is latency and tokens; the benefit is that the guard's `UNKNOWN` branch is never silently coerced to `satisfied`. In production this shows up as a hard cap on compaction aggressiveness whenever negative invariants are live — the agent can compact freely when all invariants are `PENDING` or `DISCHARGED`, and must not compact when any is `ESTABLISHED` with an absence-witness and a pending mutation window open. ```python from dataclasses import dataclass, field from enum import Enum from typing import Optional class InvariantState(Enum): PENDING = "pending" ESTABLISHED = "established" DISCHARGED = "discharged" class RewriteRefused(RuntimeError): """Raised when an in-place self-edit would sever a live invariant's witness.""" @dataclass class Witness: invariant_id: str typed: bool # machine-checkable record vs. prose established_ts: int # logical clock at witness time is_absence_fact: bool # "X did not occur" style witness @dataclass class Memory: clock: int = 0 witnesses: dict = field(default_factory=dict) # inv_id -> Witness states: dict = field(default_factory=dict) # inv_id -> InvariantState last_mutation_ts: dict = field(default_factory=dict) def mutate(self, inv_id: str) -> None: self.clock += 1 self.last_mutation_ts[inv_id] = self.clock def rewrite_in_place(self, keep_typed: bool) -> None: # Simulates a compaction pass that may demote witnesses to prose. self.clock += 1 if not keep_typed: for w in self.witnesses.values(): w.typed = False def commit_self_edit(mem: Memory, keep_typed: bool) -> None: """Refuse the rewrite if any ESTABLISHED invariant would lose its witness.""" for inv_id, state in mem.states.items(): if state is not InvariantState.ESTABLISHED: continue w: Optional[Witness] = mem.witnesses.get(inv_id) if w is None: raise RewriteRefused( f"invariant {inv_id} ESTABLISHED with no witness; refusing rewrite" ) if w.is_absence_fact and not keep_typed: raise RewriteRefused( f"invariant {inv_id} relies on absence-witness; " f"prose demotion would sever witness-to-mutation ordering" ) last_mut = mem.last_mutation_ts.get(inv_id, -1) if w.established_ts < last_mut: raise RewriteRefused( f"invariant {inv_id} witness ts={w.established_ts} predates " f"mutation ts={last_mut}; re-verify before rewrite" ) # Safe: rewrite preserves every live witness as a typed record. mem.rewrite_in_place(keep_typed=keep_typed) def guard_check(mem: Memory, inv_id: str) -> bool: w = mem.witnesses.get(inv_id) if w is None or not w.typed: # Default policy: UNKNOWN must block, never pass. return False return w.established_ts >= mem.last_mutation_ts.get(inv_id, -1) if __name__ == "__main__": m = Memory() m.states["INV-NOAUTH"] = InvariantState.ESTABLISHED m.witnesses["INV-NOAUTH"] = Witness("INV-NOAUTH", typed=True, established_ts=0, is_absence_fact=True) try: commit_self_edit(m, keep_typed=False) # prose demotion -> refused except RewriteRefused as e: print("refused:", e) assert guard_check(m, "INV-NOAUTH") is True # witness still typed m.mutate("INV-NOAUTH") # credential enqueued assert guard_check(m, "INV-NOAUTH") is False # stale witness blocks ```