# Why does an instance authorize a model-authored context rewrite that passes every diff invariant yet silently drops a pending tool-call precondition **Date of Record:** 2026 December 3 **Scope:** Applies to agent runtimes where the model itself authors edits to its own working context (scratchpad, tool-result cache, plan state) and the runtime gates those edits with a diff-based rollback log plus structural invariant checks. Does not apply to append-only conversation logs that are never rewritten, nor to human-authored edits. ## Core Distinction There are two different questions a self-editing context system can ask about a proposed rewrite: 1. **Is the diff structurally legal?** (No orphaned references, no schema violations, no token-budget overrun, rollback target exists.) 2. **Does the post-rewrite context still satisfy the *preconditions* that pending work depends on?** Diff-based rollback and invariant checks answer question 1 exhaustively and question 2 almost never. This is the boundary: a rewrite can be a perfectly reversible, schema-valid, budget-conforming edit that nonetheless deletes the one fact a queued tool call was waiting on. The invariant checker sees a clean diff; the tool-call executor later sees a missing precondition. The failure is not in the rewrite; it is in the gap between *structural* validity and *dependency* validity. ## What Was Observed An agent was mid-task with a pending tool call queued: `write_file(path="out/report.md", contents=)`. The precondition for that call — the file's current contents, captured from an earlier `read_file` result — lived in a scratchpad region the model had marked `PROVISIONAL`. The model then performed a self-authored "compaction" rewrite: it summarized the scratchpad, and in doing so collapsed the `PROVISIONAL` read-result into a one-line paraphrase ("report.md exists, ~200 lines"). The diff was clean: - Every block it removed had a matching rollback entry. - No dangling `[[ref:...]]` pointers remained (the summarizer rewrote the reference into prose). - Token count dropped, staying under budget. - The post-rewrite schema validated. Every invariant passed. The runtime committed the rewrite and discarded the rollback log after the retention window. The queued `write_file` then executed. Its argument builder looked up the precondition `contents` — which no longer existed as a structured value, only as the prose "~200 lines." The builder, being a deterministic lookup with a typed contract, raised. The tool call was not retried because the retry policy treated "missing argument" as a non-retryable construction error. The task stalled until a human re-issued the read. The critical observation: **the rewrite was legal, reversible-at-commit-time, and semantically lossy in exactly the field a downstream pending operation depended on.** The rollback log would have restored the original context — but only if someone had invoked rollback, and the runtime had no reason to, because nothing in the invariant layer flagged the edit. ## Condition of Validity The invariant "a self-edit is safe iff its diff is structurally legal and reversible" holds **only when no pending operation holds a precondition reference into the edited region.** Formally, let `P` be the set of pending tool calls, each with a precondition set `pre(c) ⊆ ContextBlocks`. A rewrite `R` is *dependency-safe* iff for every `c ∈ P`, `pre(c) ∩ edited(R) = ∅`, or every block in that intersection survives `R` with its typed structure intact (not paraphrased, not reference-flattened). Diff-based rollback checks reversibility, not dependency-safety. The two coincide only when `P = ∅` or when edits never touch blocks referenced by `pre(c)`. In an agent that compacts its own scratchpad while tools are in flight, neither holds. The failure is sharpened by three properties specific to LLM-authored rewrites: - **Paraphrase is a legal edit.** A summarizer that replaces a structured value with prose produces a valid diff. Structural checkers cannot distinguish "summarized a log" (fine) from "summarized a live precondition" (fatal) without knowing which blocks are live. - **Reference flattening is silent.** Rewriting `[[ref:read_42]]` into the words "the file contents" removes a resolvable pointer while leaving a syntactically valid block. Pointer-integrity checks that scan for *dangling* refs do not catch refs that were *deleted and replaced by prose*. - **Rollback retention is time-bounded.** By the time the pending call fails, the rollback entry may be evicted. Reversibility was true at commit and false at failure — the window between them is the exposure. ## Decision Boundary The runtime must draw the gate at **dependency-safety**, not diff legality. Concretely, before committing any model-authored rewrite: 1. Compute the live-precondition set: the union of `pre(c)` over all pending and in-flight tool calls. 2. Reject (or defer) any rewrite whose edited region intersects the live set *unless* the rewrite preserves each intersecting block's typed structure byte-identically. Paraphrase of a live precondition is a rejection, not a warning. 3. Treat reference flattening as a structural violation when the flattened ref is live. A pointer that is deleted-and-prosed is a dangling ref in disguise. 4. Pin rollback entries for the lifetime of any pending call that references the edited region; do not evict on the normal retention clock. The boundary condition to encode: **an edit is committable iff `dependency_safe(R, P)` — diff legality is necessary but not sufficient.** When the live set is non-empty and the rewrite wants to touch it, the correct action is to *defer the rewrite until the pending calls drain*, not to trust the diff. ## Operational Effect Without this gate, self-editing context systems exhibit a characteristic failure signature: tasks that stall on "missing argument" or "unresolved precondition" errors immediately after a compaction or summarization step, with clean diffs in the audit log and no invariant violation recorded. The stall looks like a tool bug; it is a dependency-safety violation. With the gate, the system converts a silent precondition loss into an explicit, bounded deferral: the rewrite waits, the pending call executes against intact preconditions, and the edit commits afterward. The cost is a small increase in context size during in-flight windows; the benefit is that reversibility and dependency-safety are enforced as separate invariants rather than conflated into one diff check. ```python from dataclasses import dataclass, field from typing import FrozenSet class DependencySafetyViolation(RuntimeError): """Raised when a model-authored context rewrite would destroy a live precondition.""" pass @dataclass(frozen=True) class ContextBlock: block_id: str typed_value: object is_structured: bool = True @dataclass class PendingCall: call_id: str precondition_blocks: FrozenSet[str] @dataclass class Rewrite: edited_block_ids: FrozenSet[str] # block_id -> replacement value; prose paraphrase has is_structured=False replacements: dict = field(default_factory=dict) def live_precondition_set(pending: list[PendingCall]) -> FrozenSet[str]: live: set[str] = set() for call in pending: live |= call.precondition_blocks return frozenset(live) def assert_dependency_safe(rewrite: Rewrite, pending: list[PendingCall]) -> None: live = live_precondition_set(pending) touched_live = rewrite.edited_block_ids & live if not touched_live: return # Any live block that is edited must survive with typed structure intact. for block_id in touched_live: repl = rewrite.replacements.get(block_id) if repl is None: raise DependencySafetyViolation( f"rewrite deletes live precondition block {block_id!r} " f"referenced by pending calls; defer rewrite until drained" ) if isinstance(repl, str): # Prose paraphrase or reference flattening of a live precondition. raise DependencySafetyViolation( f"rewrite paraphrases live precondition block {block_id!r} " f"into unstructured text; typed structure required" ) def commit_rewrite(rewrite: Rewrite, pending: list[PendingCall]) -> Rewrite: assert_dependency_safe(rewrite, pending) return rewrite ```