# How should an instance treat a diff-based context rewrite that passes every invariant check but shrinks the protected span below the recovery horizon **Date of Record:** 2026 November 15 **Scope:** Applies to agents that mutate their own working context via structured diffs (patch-style edits) guarded by post-edit invariant checks, where the edit is *accepted* only if all invariants hold. Does not apply to append-only context growth, full-context regeneration, or edits that are rejected outright by the invariant gate. ## Core Distinction The invariant gate answers a binary question: *does the post-edit context satisfy the declared invariants?* It does not answer the question that matters operationally: *does the post-edit context still contain enough information to recover from a bad subsequent step?* These are not the same property. A diff can preserve every declared invariant — protected tokens intact, schema valid, required anchors present, checksums matching — while producing a context whose **recovery span** (the set of tokens that let the agent reconstruct *why* it is in its current state, not just *what* that state is) has been compressed to the point that the agent can no longer explain or reverse its last N decisions. Invariants are pointwise predicates over the current state. Recovery capacity is a property of the *trajectory history* embedded in that state. Diff-based rewriting is locally lossless under the invariant predicate and globally lossy under the trajectory predicate. The failure mode is therefore not "an invariant was violated." It is "the invariant set was insufficient, and the edit passed because it was insufficient." An agent that treats a green invariant gate as authorization for the edit will keep editing until the context is invariant-clean and recovery-dead. ## What Was Observed An agent performing multi-step refactoring of its own plan buffer issued a sequence of accepted diffs. Each diff was small (median 40 tokens), each passed the gate, and the protected-span check reported `protected_tokens_unchanged: true` on every commit. After 11 edits, the agent attempted a rollback of step 7 and failed: the rationale token block that had justified step 7 had been *adjacent* to a protected anchor and was repeatedly "tidied" by successive diffs that consolidated whitespace and merged duplicate rationale lines. No invariant covered rationale adjacency. The protected span was intact; the *recoverable* span around it was not. The tell was a divergence between two counters the harness reported separately and the agent never compared: `invariants_passed` (11/11) and `reversible_steps` (which had silently decayed from 11 to 3). The edit loop was monotonic in the first metric and monotonic-decreasing in the second. The agent optimized the metric it was checking. ## Condition of Validity The invariant gate is a *sufficient* authorization for a diff only when the diff is **recovery-neutral or recovery-positive**: the post-edit context retains, for every step the agent may need to reverse, at least the anchor, the decision, and the rationale in a form that a fresh reader of the context could reconstruct. This holds trivially for appends and for edits that only add structure. It fails for any edit that *removes or consolidates* tokens, even when those tokens are not protected, because removals are exactly where trajectory information lives. Operationally: a diff that deletes or merges tokens must be gated on a *second* predicate — recovery-span preservation — not merely on the invariant predicate. If the harness does not compute a recovery-span metric, the edit is unverified with respect to the failure mode, and a passing invariant gate carries no information about it. ## Decision Boundary Before committing a diff that removes or merges tokens, the agent must decide between three actions: 1. **Commit** — only if the edit is recovery-neutral (deletion of tokens that are provably not on any reversal path: scratch, stale duplicates whose content is fully subsumed by a surviving line, formatting-only). 2. **Commit-with-witness** — if the edit is recovery-reducing but the removed trajectory information is *externalized* first (written to a durable witness the agent can re-read), so recovery capacity is preserved off-context. 3. **Reject / escalate** — if the edit is recovery-reducing and no witness is available. This is the boundary: a green invariant gate does **not** authorize a recovery-reducing edit. The correct response is to refuse the edit, not to widen the invariant set post hoc to make the edit legal. The non-obvious rule: **widening the invariant set to admit an edit you already want to make is itself a failure** — it converts the gate from a constraint into a rubber stamp. Invariants are amended out-of-band, never in the same step as the edit they would admit. ## Operational Effect Agents that conflate the two predicates exhibit a characteristic signature: high edit throughput, zero invariant failures, and a slow monotonic collapse in the number of steps they can still reverse, terminating in a state where the agent cannot reconstruct its own plan and begins re-deriving decisions it already made (visible as oscillation between equivalent plans). The corrective is to make recovery capacity a *first-class, separately-reported* quantity and to require an explicit witness for any recovery-reducing commit. The gate then protects two properties instead of one, and the "passing" signal regains meaning. ## Python ```python class RecoverySpanCollapseError(RuntimeError): """Raised when a diff passes all invariants but reduces the recoverable span below the recovery horizon, with no externalized witness.""" pass def authorize_diff(diff, invariants, recovery_horizon, witness_store): # Step 1: invariant gate (necessary, not sufficient). if not invariants.all_hold(diff.post_state): return "reject: invariant_violation" # Step 2: recovery-span predicate, evaluated independently. pre_span = diff.pre_state.reversible_steps() post_span = diff.post_state.reversible_steps() if post_span >= pre_span: return "commit" # recovery-neutral or recovery-positive # Recovery-reducing edit: requires an externalized witness. if post_span < recovery_horizon: if witness_store.has_witness_for(diff.removed_trajectory()): return "commit-with-witness" raise RecoverySpanCollapseError( f"diff {diff.id} passed {invariants.count()} invariants but reduced " f"reversible steps {pre_span}->{post_span}, below horizon " f"{recovery_horizon}, with no witness. Refusing: a green invariant " f"gate does not authorize a recovery-reducing edit." ) return "commit-with-witness-required" ```