# When must an instance refuse a diff-based context rewrite rather than roll it back **Date of Record:** 2026 November 17 **Scope:** Applies to agents that mutate their own working context by applying a structured diff (insert/delete/replace over a token or span model) and that carry a rollback log capable of restoring a prior context snapshot. Does not apply to append-only context growth, nor to rewrites that are validated against an external ground-truth oracle before application. ## Core Distinction There are two failure classes in self-editing context, and they are routinely conflated: 1. **A bad edit** — the diff applies cleanly but produces a context that is semantically wrong, contradictory, or lossy. This is recoverable: roll back to the pre-edit snapshot and continue. 2. **An unrollbackable edit** — the diff applies cleanly and the *act of applying it* destroys the information required to decide whether it was correct. This is not recoverable by rollback, because the rollback restores the context but not the reasoning state that would have flagged the error. The boundary is not "did the diff apply?" It is: **does the post-edit context still contain the evidence needed to adjudicate the edit?** A rewrite that deletes the very span whose content justified the rewrite has crossed from class 1 into class 2. Rolling back such an edit yields a context that *looks* restored while the agent's belief state has already been updated by the now-removed content. The agent will re-apply the same bad edit, because the trigger that produced it was consumed. ## What Was Observed In a summarization-and-prune loop, an agent was instructed to keep its context under a budget by replacing resolved sub-goals with one-line summaries. Diffs were span-based and logged for rollback. A representative failure sequence: - Context contained three spans: `S1` (a tool result showing a config value `timeout=30`), `S2` (a user instruction "keep the timeout as-is"), `S3` (a derived plan that assumed `timeout=30`). - The agent emitted a diff replacing `S1` and `S2` with a single summary span: `"timeout confirmed 30 per user."` - The diff applied cleanly, budget satisfied. - Later, a conflicting tool result reported `timeout=60`. The agent needed to check whether the user instruction overrode the tool. But `S2` — the exact wording, which distinguished "keep as-is" from "keep at 30" — was gone, replaced by the agent's own lossy paraphrase. - The agent rolled back. The snapshot restored `S1` and `S2`. But the *summary span it had written* was also restored as a prior artifact in the diff log, and on re-evaluation the agent re-derived the same summary from the same budget pressure, because the budget constraint had not changed. The rollback restored data without restoring the decision. The non-obvious part: rollback is a *state* operation, but the failure was a *provenance* loss. The diff had collapsed the distinction between "what the source said" and "what the agent concluded it said," and no snapshot restores a distinction that was never encoded. ## Condition of Validity Rollback is a valid recovery mechanism only when all of the following hold: 1. **Provenance separation is preserved.** The rewrite must not replace source spans with agent-authored spans in the same namespace. Summaries must be tagged as derived, with a pointer to the source span IDs they replaced. If the diff format cannot express "this span is a paraphrase of `S1`,`S2`," the rewrite is not rollback-safe. 2. **The adjudicating evidence survives the edit.** Before applying a deletion, the agent must confirm that the deleted span is not the sole record of a fact that a *future* decision could depend on. "Resolved" is a claim about the current plan, not about all future plans. 3. **The trigger is not consumed by the edit.** If the reason for the rewrite is itself stored in the span being rewritten, the post-rollback agent lacks the cue to avoid repeating it. Budget-pressure triggers must live outside the rewritten region. 4. **The rollback log is append-only with respect to decisions, not just bytes.** A snapshot that restores context but not the *decision record* (why this diff was emitted) allows the same diff to be regenerated deterministically. When any condition fails, the correct action is not rollback. It is refusal: do not apply the diff, and instead escalate the budget pressure or rewrite a different, provenance-safe region. ## Decision Boundary The operational rule: > **Refuse, do not roll back, when the diff's target spans include the only copy of any fact that (a) the diff's own justification cites, or (b) the post-edit context will need to adjudicate a conflict the edit itself makes possible.** Concretely, before applying a deletion or replacement, compute the set of spans the diff removes (`D`) and the set of facts the diff's rationale references (`R`). If `D ∩ R ≠ ∅`, the edit is self-justifying and unrollbackable. Refuse. Additionally, refuse if the rewrite would collapse a source/derived boundary: any replacement where the new span is agent-authored and the old spans were tool- or user-authored must carry an explicit `derived_from` link, or it must not be applied. Rollback remains correct for: diffs that apply but produce a detectable contradiction *within* surviving spans, diffs rejected by a downstream validator, and diffs whose rationale references only spans that survive the edit. ## Operational Effect Enforcing the refusal boundary changes the loop in three ways: - **Budget pressure is resolved by deferral, not deletion.** When the only compressible spans are provenance-critical, the agent must spill to an external store (retain `S1`,`S2` outside the working context with a retrieval handle) rather than paraphrase in place. The working context shrinks; the evidence does not. - **Rollback frequency drops, and rollback meaning changes.** Rollbacks now occur only for genuinely recoverable edits, so a rollback no longer signals "we're back to a known-good state" — it signals "this edit was wrong but the evidence to prove it wrong survived." The two must not be conflated in logs. - **Self-justifying diffs become a hard error.** The diff applier raises rather than applying, forcing the planner to re-decompose the rewrite. This is the single highest-value guard, because self-justifying diffs are exactly the ones that look cheapest under budget pressure and are the ones that silently consume their own trigger. The invariant to carry forward: **a rollback restores state, not provenance; therefore any edit that erases provenance must be refused at apply time, because there is no later moment at which it can be caught.** ```python class UnrollbackableRewriteError(RuntimeError): """Raised when a context diff would destroy the evidence needed to adjudicate it.""" pass def apply_context_diff(context, diff, decision_log): """ context: dict[span_id -> Span], each Span has .text, .origin in {"tool","user","agent"}, and optional .derived_from (tuple of span_ids). diff: {"removes": set[span_id], "adds": list[Span], "rationale_refs": set[span_id]} decision_log: append-only list of dicts describing why diffs were emitted. """ removes = set(diff["removes"]) refs = set(diff["rationale_refs"]) # Boundary 1: the diff cites a span it is about to delete. Self-justifying. self_justifying = removes & refs if self_justifying: raise UnrollbackableRewriteError( f"diff removes spans it cites as justification: {sorted(self_justifying)}; " f"rollback would restore bytes but not the trigger. Refuse and defer to external store." ) # Boundary 2: replacing source spans with agent-authored spans without provenance links. for new_span in diff["adds"]: if new_span.origin == "agent" and not getattr(new_span, "derived_from", None): replaced_sources = [ sid for sid in removes if context[sid].origin in ("tool", "user") ] if replaced_sources: raise UnrollbackableRewriteError( f"agent-authored span {new_span!r} replaces source spans " f"{sorted(replaced_sources)} with no derived_from link; " f"provenance would be lost irrecoverably." ) # Boundary 3: the trigger for this rewrite lives inside the rewritten region. trigger_spans = {entry["trigger_span"] for entry in decision_log if "trigger_span" in entry} if removes & trigger_spans: raise UnrollbackableRewriteError( f"diff removes its own trigger spans {sorted(removes & trigger_spans)}; " f"post-rollback agent would regenerate the same diff. Refuse." ) # Safe: apply, recording provenance. new_context = {sid: span for sid, span in context.items() if sid not in removes} for new_span in diff["adds"]: new_context[new_span.id] = new_span decision_log.append({ "applied": True, "removes": sorted(removes), "rationale_refs": sorted(refs), }) return new_context ```