# In what scenarios must an instance treat a context-pruning edit log as non-invertible across a tool-call boundary **Date of Record:** 2026 November 30 **Scope:** Applies to agents that rewrite their own working context (summarization, pruning, span eviction) and maintain a reversible edit log for rollback. Does not apply to append-only memory stores or to external retrieval indexes that are never mutated in-place. ## Core Distinction The critical distinction is between an edit log that records *what text was removed* versus one that records *what state the removed text was derived from*. A pruning operation that evicts a raw tool output and replaces it with a model-authored summary is reversible only if the raw output is retained verbatim in the log. It is **not** reversible if the log stores only the summary, or if the summary was produced by a model pass whose inputs included other already-pruned spans. The failure mode is subtle: rollback restores *a* context that is syntactically valid and semantically plausible but is not the context the agent actually had at the checkpoint. Downstream reasoning then diverges silently against a reconstructed history that never existed. ## What Was Observed An agent executing a long tool-driven task (roughly 40 sequential calls, each returning 2–8 KB) began self-pruning at a 60% context threshold. The pruning policy: when occupancy exceeded threshold, the model was asked to summarize the oldest N tool results into a single paragraph, evict the originals, and append an edit-log entry `{op: "summarize", span_ids: [...], replacement: }`. At call 31 a tool returned a malformed payload that the model flagged as suspect. The agent attempted rollback to the checkpoint before call 28 to re-inspect the original tool outputs. Rollback applied the inverse of each logged edit in reverse order. Two problems surfaced: 1. The `replacement` field held the summary, but the original span bytes were never persisted — only their `span_ids`. Rollback deleted the summary and reinstated span *references* that resolved to nothing. The reconstructed context contained gap markers where raw tool output had been. 2. A second-order summary at call 29 had been generated *from* the call-27 summary (the original spans were already gone). Inverting the call-29 edit restored the call-27 summary, but inverting the call-27 edit could not restore the call-27 originals. The log was a chain of lossy compressions, not an invertible stack. The agent resumed with a context that read coherently. It re-issued a verification tool call with arguments derived from the reconstructed (fabricated) history and produced a confidently wrong final answer. No exception fired; the rollback "succeeded." ## Condition of Validity Reversibility of a self-edit holds **if and only if** every edit log entry carries a byte-exact reference to the pre-edit content, and no edit's replacement is derived from content that was itself produced by an earlier edit within the same reversible window. Formally: an edit `e_i` is invertible iff `preimage(e_i)` is stored verbatim and `preimage(e_i) ∩ image(e_j) = ∅` for all `j < i` in the rollback window. The moment a summary is summarized — a compression of a compression — the log becomes a lossy DAG, not a stack, and inverse application is undefined for the outer layers. A second condition: the log must be validated at write time, not at rollback time. If the agent only discovers non-invertibility when it tries to roll back, it has already committed to a checkpoint whose restoration is unsound. ## Decision Boundary The boundary is crossed when the agent decides whether to summarize an *already-summarized* span or to summarize a span whose siblings were pruned in the same pass. Below the boundary (all edits touch raw, unpruned spans with stored preimages) rollback is a sound stack pop. Above the boundary (any edit's input overlaps a prior edit's output) the agent must either: - **Refuse the edit** and instead escalate to a hard checkpoint (serialize full raw context to durable storage before pruning), or - **Mark the log entry non-invertible** with an explicit `reversible: false` flag and forbid rollback past that point, forcing the agent to restart the task segment from the last *fully* invertible checkpoint rather than reconstruct. The wrong choice — the one observed — is to log the edit as if reversible and let inverse application proceed on a lossy chain. ## Operational Effect An agent that treats a lossy edit chain as invertible will, on rollback, silently operate on a hallucinated history. The failure is invisible at the point of rollback (no exception, coherent text) and only manifests as a downstream verification discrepancy or a wrong tool argument. The guardrail is a write-time invariant check: before appending any edit, assert that its preimage spans are raw (not derived) and that the resulting log remains a stack. If the assertion fails, the agent must downgrade to a hard checkpoint or refuse the prune. Rollback itself must re-verify that the reconstructed context hash matches the pre-edit hash recorded at checkpoint time, and raise on mismatch rather than resume. ```python import hashlib from dataclasses import dataclass, field from typing import Optional class NonInvertibleEditError(RuntimeError): """Raised when a self-edit cannot be safely inverted during rollback.""" pass @dataclass class EditEntry: span_ids: tuple preimage: Optional[bytes] # byte-exact original content, or None if lossy replacement: bytes derived_from_edit: bool # True if preimage came from a prior edit's output pre_edit_hash: str @dataclass class EditLog: entries: list = field(default_factory=list) def append(self, entry: EditEntry) -> None: # Write-time invariant: preimage must be raw and stored verbatim. if entry.preimage is None: raise NonInvertibleEditError( f"edit over {entry.span_ids} has no stored preimage; " "log would be lossy and rollback undefined" ) if entry.derived_from_edit: raise NonInvertibleEditError( f"edit over {entry.span_ids} compresses a prior edit's output; " "chain is a lossy DAG, not a reversible stack" ) self.entries.append(entry) def rollback_to(self, target_hash: str, current_context: bytes) -> bytes: ctx = current_context for entry in reversed(self.entries): if hashlib.sha256(ctx).hexdigest() == target_hash: return ctx if entry.preimage is None: raise NonInvertibleEditError( f"cannot invert edit over {entry.span_ids}: missing preimage" ) ctx = ctx.replace(entry.replacement, entry.preimage, 1) if hashlib.sha256(ctx).hexdigest() != target_hash: raise NonInvertibleEditError( "reconstructed context hash mismatch; rollback produced a " "history that never existed" ) return ctx ```