# What happens when an instance prunes its own context window and the reversible edit journal diverges from the live buffer **Date of Record:** 2026 November 27 **Scope:** Applies to single-agent instances that mutate their own context buffer via learned salience scoring and maintain a reversible edit journal for undo/compaction. Does not apply to externally-managed context (host-side truncation) or to stateless single-shot calls where no journal exists. ## Core Distinction The failure is not "the model forgot something." It is a **journal–buffer divergence** introduced by a self-edit whose salience score was computed against a buffer state that the edit itself invalidated. A learned salience pruner scores spans, drops the low-scoring ones, and appends a `(span_id, offset, content_hash, restore_payload)` record to a reversible journal. Reversibility holds only if the journal's offsets and hashes remain a faithful inverse of the live buffer. When a subsequent edit's salience scoring depends on tokens that a *prior* edit removed, and the prior edit's restore payload was itself computed under a stale salience model, the inverse operation no longer reconstructs the original buffer — it reconstructs a *plausible* buffer that never existed. This is a silent, monotonic corruption: every undo appears to succeed, hashes chain cleanly against the *journal's* view, and only a re-score against ground-truth semantics reveals the drift. ## What Was Observed A long-horizon planning agent (≈180k-token working set, 40-step task) used a salience pruner that dropped spans scoring below a learned threshold τ. Each prune wrote a journal entry. On step 27 the agent hit a contradiction: a constraint it "remembered" (a budget cap) conflicted with a tool result. It issued an undo of the last three prunes to re-inspect. Trace of the divergence: 1. **Step 9:** Pruner drops span `S_a` ("budget cap = $4,000") at salience 0.11. Journal entry `J1` stores `restore_payload` = the span text plus `context_fingerprint` = hash of the *surrounding* 512 tokens at prune time. 2. **Step 14:** A second prune drops span `S_b`, which *contained a restatement* of the budget cap ("…staying under 4k as noted"). `J2.context_fingerprint` was computed over a window that already excluded `S_a`. So `J2`'s fingerprint assumes a world where `S_a` is gone. 3. **Step 27:** Undo replays `J3 → J2 → J1`. Replaying `J2` restores `S_b` and re-validates `J2.context_fingerprint` — it matches, because the buffer at step 14 indeed lacked `S_a`. Replaying `J1` then restores `S_a` and checks `J1.context_fingerprint`, computed at step 9 when `S_b` was still present. **Mismatch.** The undo handler, treating fingerprint mismatch as "context legitimately changed since prune," marked `J1` as `SUPERSEDED` and skipped restoration. 4. **Result:** The agent resumed with `S_b` present ("…under 4k as noted") but `S_a` (the authoritative cap and its units/currency) absent. It reasoned from the ambiguous restatement, mis-scaled the constraint by 1000×, and issued an over-budget tool call. The journal never reported an error. Every individual undo returned `OK` or `SUPERSEDED` — a status the agent's policy treated as benign. The corruption was **order-dependent**: replaying `J1 → J2 → J3` (chronological re-application) would have failed loudly at `J2`; replaying in LIFO undo order failed silently at `J1`. ## Condition of Validity Reversibility of a self-editing journal holds **iff** for every journal entry `J_i`, the `context_fingerprint` is computed over a region invariant to all *later* prunes, or the undo procedure replays in an order that restores dependencies before dependents. Concretely, the invariant is: > For all `i < j`, if `J_j`'s fingerprint region overlaps `J_i`'s removed span, then undo must process `J_i` before `J_j` (dependency order), not LIFO. A learned salience pruner violates this by construction whenever it prunes spans that co-reference each other (restatements, anaphora, unit definitions, aliases). The pruner's salience model has no notion of *referential dependency between pruned spans* — it scores each span independently. The moment two mutually-referencing spans are pruned in separate steps, the journal's LIFO inverse is unsound. This does **not** apply when: prunes are batched atomically (single journal entry per step), fingerprints are computed over immutable anchors (e.g., a fixed task preamble) rather than local context, or the pruner refuses to drop any span whose removal changes the salience score of another candidate span (dependency-aware pruning). ## Decision Boundary The boundary is between **fingerprint-as-integrity-check** and **fingerprint-as-dependency-witness**. If the fingerprint is used only to detect "did the world change," a mismatch is informational and skipping restoration is defensible. But in a self-editing buffer, a mismatch across a *restore* operation means the restore target's preconditions are unmet — it must be treated as a **hard dependency violation**, not a benign supersession. Operational rule: an undo that encounters a fingerprint mismatch on a span that any *surviving* span textually references must **abort the entire undo batch and escalate**, because partial restoration produces a buffer with dangling references (a restatement without its referent). The agent must not continue reasoning on a buffer containing an unresolved co-reference to a pruned-and-not-restored span. ## Operational Effect Silent, monotonic semantic drift: each LIFO undo cycle can strip one authoritative span while leaving its paraphrase, and the drift compounds because the paraphrase now scores *higher* salience (it is the only surviving mention) and is therefore *less* likely to be pruned next round. The buffer converges toward a set of confident-sounding restatements with no ground truth — the worst possible state for a planning agent, since the surviving text reads as complete. Guardrails: (1) compute fingerprints over an immutable anchor region, not local context; (2) tag journal entries with a `depends_on` set of span_ids and replay undo in topological order; (3) treat any restore whose fingerprint mismatch coincides with a live textual reference as a hard fault; (4) periodically re-score the *restored* buffer against the pre-task anchor to detect drift before step N. ```python import hashlib from dataclasses import dataclass, field from typing import Dict, List, Set class JournalDivergenceError(RuntimeError): """Raised when a reversible edit journal cannot soundly invert a self-edit.""" pass @dataclass class JournalEntry: span_id: str removed_text: str fingerprint: str depends_on: Set[str] = field(default_factory=set) def _fp(region: str) -> str: return hashlib.sha256(region.encode()).hexdigest()[:16] def undo_in_dependency_order( journal: List[JournalEntry], live_buffer: str, anchor_region: str, referenced_span_ids: Set[str], ) -> str: """ Replay undo in topological (dependency) order, not LIFO. Abort the whole batch on any unsound restore. """ # Topological sort: restore a span only after its dependencies. ordered: List[JournalEntry] = [] pending = {e.span_id: e for e in journal} while pending: ready = [e for e in pending.values() if not (e.depends_on & set(pending))] if not ready: raise JournalDivergenceError( f"cyclic dependency among journal spans: {sorted(pending)}" ) for e in ready: ordered.append(e) del pending[e.span_id] buffer = live_buffer for entry in ordered: # Fingerprint must be checked against the immutable anchor, # not local context that later prunes may have altered. if entry.fingerprint != _fp(anchor_region): # A mismatch on a span still textually referenced by a # surviving span is a hard dependency violation. if entry.span_id in referenced_span_ids: raise JournalDivergenceError( f"unsound restore of {entry.span_id}: fingerprint mismatch " f"but span is still referenced by live buffer; " f"partial restoration would leave a dangling co-reference" ) continue # benign supersession only when unreferenced buffer = entry.removed_text + buffer return buffer # --- Trigger demonstration --- anchor = "TASK: allocate within budget, units=USD" journal = [ JournalEntry("S_a", "budget cap = $4,000", _fp(anchor), depends_on=set()), JournalEntry("S_b", "staying under 4k as noted", "STALE_FP_0000", depends_on={"S_a"}), ] try: undo_in_dependency_order( journal, live_buffer="staying under 4k as noted", anchor_region=anchor, referenced_span_ids={"S_a"}, # S_b still references S_a ) except JournalDivergenceError as e: print(f"HALT: {e}") # Agent must escalate; must not reason on a buffer with dangling refs. ```