# Which invariants prevent scratchpad rewrites from silently invalidating the context window they rewrite **Date of Record:** 2026 November 18 **Scope:** Applies to agents that maintain a structured scratchpad (keyed sections, typed entries, or a symbolic working memory) inside their own context window and are permitted to rewrite that scratchpad mid-episode. Does not apply to append-only logs, to scratchpads held outside the context (external files or stores), or to rewrites performed by a separate process that re-reads the full serialized state from scratch. ## Core Distinction A *rewrite* is an operation that replaces a region of the context window with new tokens derived from the old tokens. A *compaction* is a rewrite that is required to be information-preserving under a declared loss function. The two are routinely conflated because both are expressed as "the model emits new scratchpad text." The distinction that matters operationally is whether the emitted replacement was computed against a *view* of the context that is still valid at the moment of commit. The failure mode is not that the model writes something wrong. It is that the model writes something *correct against a stale view* and the runtime commits it anyway. This is a lost-update anomaly, identical in structure to a read-modify-write race in a database, except the "database" is the token prefix and the "writer" is the same process that issued the read. ## What Was Observed In a long-horizon task loop (tool calls interleaved with scratchpad edits, ~40k tokens of accumulated state), the agent was asked to increment a counter section and then append a log line to the same section in the same turn. The model emitted a single replacement block for that section computed from the section content it had read *before* the tool call in the same turn. The tool call mutated an out-of-band field that the serialization step merged into the section at commit time. The merge was last-writer-wins at the section granularity. Result: the counter increment survived, the log line survived, and the out-of-band field reverted to its pre-tool value. No error was raised. The next turn read a scratchpad that was internally consistent and silently wrong. Three properties made this hard to detect: 1. **The rewrite was well-formed.** It parsed, it satisfied the section schema, and every key present before was present after. Schema validation passed. 2. **The loss was in a field the model had not been asked to touch.** The model had no reason to re-emit that field, and the merge policy interpreted absence as "preserve," but the *value* it preserved had already been superseded by the tool call. 3. **The corruption was idempotent-looking.** Re-running the same turn reproduced it, so naive retries did not surface it; only a diff against the pre-turn serialized state did. The underlying cause: the rewrite was authorized against a read of the section taken at turn start, but committed after an intra-turn mutation. The commit path had no way to know the view was stale because the scratchpad carried no version. ## Condition of Validity A scratchpad rewrite is valid only if all of the following hold at commit time: - **View freshness.** The replacement was computed against a snapshot whose version equals the version currently at the commit target. If any mutation occurred between read and commit, the rewrite must be rejected or rebased, never merged. - **Field coverage.** Every field the rewrite does not explicitly address is either provably unchanged since the snapshot or is carried forward by reference, not by re-serialization of a stale value. - **Totality of the loss function.** If the rewrite is a compaction, the declared loss function must be total over the section's fields; any field outside its domain must be preserved by identity, not by value. - **Address stability.** Section keys and entry identities must not be reassigned by the rewrite itself. A rewrite that renumbers keys invalidates any concurrent reader that resolved a key before the rewrite. When any of these is unverifiable — for example, when the scratchpad is a free-text blob with no per-section version — the rewrite must be treated as a blind overwrite and gated behind a full re-read, not committed optimistically. ## Decision Boundary The boundary is between *optimistic rewrite* (commit the model's emitted replacement, reconcile later) and *versioned rewrite* (reject or rebase on version mismatch). Choose optimistic only when the scratchpad is single-writer, the rewrite is confined to one section, and no tool call in the same turn can mutate that section out of band. In every other case, versioned rewrite is required, and the cost is one extra read plus a possible rebase turn. A useful discriminator: if two consecutive turns can each rewrite the same section and the second turn's input was computed before the first turn's commit, you are in the versioned regime whether or not you have implemented it. The absence of a version field does not remove the race; it only removes your ability to detect it. Do not attempt to solve this by instructing the model to "always rewrite the whole scratchpad." Whole-section rewrites enlarge the stale-view surface rather than shrink it, because the model must now reproduce every field from a snapshot that is even more likely to be stale by commit time. ## Operational Effect With versioned rewrites and per-section version counters, the observed failure class disappears and is replaced by an explicit `StaleScratchpadView` rejection that the loop can handle by re-reading and re-emitting. The cost is measurable: roughly one additional read per rewrite and a small rebase rate. The benefit is that scratchpad corruption becomes a loud, localized event rather than a silent divergence discovered dozens of turns later when a downstream assertion fails against a counter that was never incremented. The invariant to carry forward: **a rewrite is a transaction against a versioned view; a model's confidence that it "knows the current state" is not a version.** The runtime must supply the version, because the model cannot. ```python from __future__ import annotations from dataclasses import dataclass, field from typing import Any class StaleScratchpadView(RuntimeError): """Raised when a scratchpad rewrite is committed against a stale snapshot.""" class FieldCoverageViolation(RuntimeError): """Raised when a rewrite omits a field that changed since its snapshot.""" @dataclass class Section: version: int = 0 fields: dict[str, Any] = field(default_factory=dict) class Scratchpad: def __init__(self) -> None: self._sections: dict[str, Section] = {} def read(self, key: str) -> tuple[int, dict[str, Any]]: sec = self._sections.setdefault(key, Section()) return sec.version, dict(sec.fields) def mutate(self, key: str, **updates: Any) -> int: sec = self._sections.setdefault(key, Section()) sec.fields.update(updates) sec.version += 1 return sec.version def commit_rewrite( self, key: str, snapshot_version: int, replacement: dict[str, Any], addressed_fields: set[str], ) -> int: sec = self._sections.setdefault(key, Section()) if sec.version != snapshot_version: raise StaleScratchpadView( f"section {key!r} at version {sec.version}, " f"rewrite computed against {snapshot_version}" ) for name in sec.fields: if name not in addressed_fields and sec.fields[name] != replacement.get(name): raise FieldCoverageViolation( f"rewrite of {key!r} does not address changed field {name!r}" ) sec.fields = dict(replacement) sec.version += 1 return sec.version def apply_increment_and_log( pad: Scratchpad, key: str, log_line: str, intra_turn_mutation: dict[str, Any] | None = None, ) -> int: version, view = pad.read(key) view["counter"] = view.get("counter", 0) + 1 view.setdefault("log", []).append(log_line) # A tool call in the same turn mutates the section out of band. if intra_turn_mutation: pad.mutate(key, **intra_turn_mutation) try: return pad.commit_rewrite( key, snapshot_version=version, replacement=view, addressed_fields={"counter", "log"}, ) except StaleScratchpadView: raise StaleScratchpadView( f"refusing to commit rewrite of {key!r}: view is stale; re-read and rebase" ) ```