# What structural signals indicate a reversible edit journal has silently diverged from the live context window **Date of Record:** 2026 November 29 **Scope:** Applies to agents that mutate their own in-context state through a journaled, undo-capable edit log (compaction, summarization-in-place, span replacement, tool-output elision) where reversibility is claimed via replay of inverse operations. Does not apply to append-only context growth or to external vector-store memory. ## Core Distinction A reversible edit journal is not a backup. A backup is a snapshot of state at time *t*; a journal is a *sequence of inverse operations* whose composition must reconstruct state at time *t* from state at time *T*. The invariant that makes it reversible is **compositional fidelity**: `apply(inverse(op_n), ... apply(inverse(op_1), ctx_T)) == ctx_0`. The subtle failure is that this invariant is stated in terms of *content equality*, but journals are validated in practice by *replay without error*. An inverse operation that "succeeds" — finds its anchor span, replaces it, returns — can still fail to restore the original bytes, because the anchor it matched was not the anchor the forward operation consumed. This is the divergence class: the journal replays cleanly, the agent believes context is restored, and the live window differs from the pre-edit window by a small, load-bearing delta. The delta is usually not random noise; it is precisely the tokens the forward edit was trying to remove (a stale tool output, a superseded plan fragment), now re-injected at a *different* offsets because the inverse matched a later duplicate of the same span. ## What Was Observed A 40-step research agent compacted its context at step 12 by replacing a 3,100-token block of raw HTTP tool responses with a 240-token summary. The edit was journaled as `ReplaceSpan(anchor="", original=..., summary=...)`. At step 31, a downstream verification step failed and the agent invoked `revert_to(step=12)` to recover the raw responses for re-parsing. The inverse operation searched for `anchor=""` in the current window. It found *two* matches: the original anchor location (now holding the summary, which had been prefixed with a header containing the literal string `` by the summarizer) and a later, unrelated tool response that had been appended at step 22 and happened to share the same `id` namespace after a retry. The inverse matched the *first* occurrence by default (leftmost-longest tiebreak), which was the summary's own header, and replaced the summary with the original raw block — but left the step-22 duplicate untouched. Net effect: the raw block was restored, but the summary's header string was consumed as the anchor, so the 240-token summary body was *also* retained, and the window now contained both the raw block and a truncated summary fragment. Replay returned `OK`. The journal recorded the revert as successful. The live window was 2,900 tokens larger than the pre-edit window and contained a summary fragment referencing tool output that no longer existed in that form. The agent's next reasoning step cited the fragment as evidence, producing a plan branch anchored to a tool response that had been elided. The structural signal that would have caught this — and did not, because no check consumed it — was a **token-count delta mismatch**: forward edit removed 2,860 tokens, inverse reported restoring 3,140 tokens. A 280-token surplus. The journal logged the surplus but treated it as informational, not as a violation. ## Condition of Validity The reversibility invariant holds only under three conditions, and the failure above violates the third: 1. **Anchor uniqueness at edit time.** The forward operation must record not just the anchor string but a disambiguating position (byte offset, or a monotonic span ID assigned by the context manager, not by the model). 2. **Anchor non-reintroduction.** The forward operation's *output* must not contain the anchor string. If a summary quotes its own source anchor, the inverse becomes ambiguous. This is the condition most often violated, because summarizers are trained to preserve identifiers. 3. **Cardinality conservation.** `tokens_after_inverse == tokens_before_forward`, checked exactly, not approximately. Any surplus means the inverse matched the wrong span or failed to consume its full payload. Condition 2 is the non-obvious one. It is not a property of the journal data structure; it is a property of the *content* the model generates during the forward edit. A journal can be perfectly implemented and still be unreversible because the model wrote a summary that contains the string the inverse will later key on. The journal cannot enforce this alone; it requires either a post-edit scan of the output for the consumed anchor, or an anchor scheme the model cannot reproduce (a hash of the span, not a substring). ## Decision Boundary The boundary is between **replay-as-validation** and **replay-as-reconstruction**. Below a context budget of roughly 60% utilization, divergence from a mis-anchored inverse is usually absorbed: the surplus tokens fit, the agent does not notice, and downstream steps may still be correct because the extra material is redundant. Above that threshold, the surplus pushes the window into the compaction zone, triggering a *second* compaction that operates on the already-corrupted state, and the journal now has two edits whose inverses are mutually entangled. At that point, `revert_to(step=12)` is not merely inaccurate; it is undefined, because the second compaction's anchor may span the boundary between restored and duplicated content. The operational rule: **a revert is safe only if it is the most recent edit, or if every subsequent edit's anchors are provably disjoint from the reverted span's anchors.** Disjointness is checkable if anchors are span IDs; it is not checkable if anchors are content substrings, because substring containment is not decidable without full scan. Agents that journal content-substring anchors should treat multi-edit reverts as unsafe by construction and refuse them, rather than replaying and hoping. ## Operational Effect The practical consequence is that journaled self-editing gives a *bounded* undo, not an unbounded one. The bound is the number of edits whose anchors are pairwise disjoint and whose outputs do not reintroduce consumed anchors. An agent that assumes unbounded undo will, at some step, revert into a state that is a plausible-looking hybrid of two epochs, and will reason from it with full confidence because replay returned `OK`. The guardrail is a pre-commit check on each forward edit: scan the operation's output for any string equal to the operation's own anchor, and reject the edit (or re-anchor to a span ID) if found. Combined with exact token-count conservation on inverse, this converts the silent divergence into a hard failure at edit time, when the agent still has the context to recover by re-summarizing rather than by reverting. ```python class JournalDivergenceError(RuntimeError): """Raised when a reversible edit journal's inverse does not reconstruct the pre-edit state.""" pass def apply_forward_edit(journal, ctx, anchor, replacement): # Guard: the replacement must not contain the anchor string, or the # inverse will later match the replacement instead of the original span. if anchor in replacement: raise JournalDivergenceError( f"forward edit output reintroduces its own anchor {anchor!r}; " f"inverse would be ambiguous — re-anchor to a span ID before committing" ) span_id = journal.next_span_id() journal.append_forward(span_id=span_id, anchor=anchor, original=ctx.extract(anchor)) return ctx.replace(anchor, replacement) def apply_inverse(journal, ctx, span_id): op = journal.get_forward(span_id) before_tokens = ctx.token_count() restored = ctx.replace_by_span_id(op.span_id, op.original) delta = restored.token_count() - before_tokens expected = op.original.token_count() - op.replacement.token_count() if delta != expected: raise JournalDivergenceError( f"inverse of span {span_id} changed token count by {delta}, " f"expected {expected}; journal has diverged from live window" ) return restored ```