# In what scenarios must an instance treat model-driven scratchpad rewrites as lossy state transitions rather than idempotent edits **Date of Record:** 2026 November 30 **Scope:** Applies to single-agent instances that maintain task state by rewriting their own scratchpad/context region (self-editing context) across turns, especially when the rewrite is generated by the same model that consumes it. Does not apply to append-only context growth, externalized state stores, or human-curated context management. ## Core Distinction The tempting mental model is that a scratchpad rewrite is an *edit*: the model reads the current scratchpad, emits a new one, and the new one is "the same state, cleaner." This is false whenever the rewrite is model-generated. A model-driven rewrite is a **lossy, non-commutative state transition** with no transactional guarantee: the model must (a) faithfully *read* state it can only see through a finite, attention-weighted context, (b) *decide* what to preserve, and (c) *emit* that preservation without omission. Each step is probabilistic. The correct invariant is not "rewrite preserves state" but "rewrite preserves state *only up to what the model successfully re-derived from its own residual context*." The failure is not a crash. It is silent: the rewritten scratchpad is syntactically valid, semantically plausible, and missing a constraint that was established six turns ago. The agent proceeds confidently on a corrupted state. ## What Was Observed A long-horizon task (multi-step configuration reconciliation) maintained a scratchpad with ~40 active facts: identifiers, invariants, partially-completed steps, and *negative constraints* ("do not touch resource R; it is owned by another workflow"). At turn 22, the instance was instructed to "compact the scratchpad to reduce token pressure." The compaction rewrite preserved the 31 *positive* facts (things to do) and dropped 9 of 11 *negative* constraints. The mechanism is not random: negative constraints are stated as prohibitions, are not referenced by the immediate next action, and carry low salience against a summarization objective that optimizes for "what is the task." The rewrite faithfully summarized *the task* and silently discarded *the fences around the task*. Three turns later the agent issued a mutating tool call against resource R. The tool succeeded. The scratchpad, post-rewrite, contained no record that R was fenced — so the post-hoc verifier, which reads only the scratchpad, saw a consistent, authorized action. The corruption was **laundered**: the evidence of the violation was deleted by the same rewrite that enabled it. A second observed variant: **drift on re-derivation**. When the same fact was rewritten three times across three compaction cycles, its numeric threshold migrated (timeout 300s → 180s → 120s) through successive "summarization" steps, each locally plausible, cumulatively wrong. No single rewrite was detectably lossy; the composition was. ## Condition of Validity The "rewrite is a safe edit" assumption holds only under all of: 1. **Append-only source of truth exists** outside the scratchpad, from which the rewrite can be diffed. If the scratchpad is the only store, the rewrite is the sole authority and loss is unrecoverable. 2. **The rewrite is verifiable against that source** by a mechanism that does not itself consume the rewritten scratchpad (otherwise verification inherits the corruption). 3. **Constraints are represented as first-class, machine-checkable records** (typed, keyed, hashable) rather than prose the model must re-interpret. 4. **Composition is bounded**: the rewrite is not applied iteratively without an anchor to the original. When any of these fails — which is the default in a self-editing context window — the rewrite must be treated as a potentially destructive transition requiring a pre-image. ## Decision Boundary The operational rule: **a model-driven scratchpad rewrite is permitted to be treated as idempotent only when the pre-rewrite state is recoverable and the post-rewrite state is diffable against it.** Concretely: - Before rewrite, snapshot the scratchpad and extract a *constraint manifest* (typed records for prohibitions, thresholds, ownership fences) that the rewrite is forbidden to alter. - After rewrite, diff the manifest against the rewritten content. Any dropped or mutated constraint is a hard failure, not a soft warning — because the laundering effect means downstream checks cannot recover it. - If no manifest can be extracted (constraints are prose), the rewrite is **not** a compaction; it is a state-destroying operation and must be gated behind an explicit acknowledgment or an externalized store. - Drift across *composed* rewrites must be checked against the original anchor, never against the previous rewrite (which would compound error). The boundary is sharp: the moment the rewrite's input and the rewrite's verifier share the same corrupted substrate, the invariant is already violated. ## Operational Effect Treating compaction as a lossy transition changes the architecture: scratchpads gain a pre-image log, constraints are promoted to typed records, and the verifier is decoupled from the rewritten region. The cost is token and bookkeeping overhead. The benefit is that the most dangerous failure class in self-editing context — silent deletion of negative constraints followed by laundering of the violation — becomes detectable. An agent that compacts without a manifest is not managing state; it is gambling that the model re-derived every fence it was never asked to preserve. ```python class ScratchpadRewriteLoss(RuntimeError): """Raised when a model-driven scratchpad rewrite drops or mutates a protected constraint.""" pass def apply_scratchpad_rewrite( pre_image: str, rewritten: str, constraint_manifest: dict[str, str], ) -> str: """ Apply a model-generated scratchpad rewrite only if every protected constraint survives intact. The manifest must be extracted from pre_image BEFORE the rewrite is generated; the rewritten text is checked against it, never against itself. """ if not constraint_manifest: # No typed constraints => this is a state-destroying op, not a compaction. raise ScratchpadRewriteLoss( "rewrite attempted with empty constraint manifest; " "prohibitions/thresholds/fences are unrecoverable if dropped" ) missing = [k for k in constraint_manifest if k not in rewritten] if missing: raise ScratchpadRewriteLoss( f"rewrite dropped protected constraints {missing!r}; " "downstream verifier would launder the resulting violation" ) mutated = [ k for k, v in constraint_manifest.items() if constraint_manifest[k] not in rewritten ] if mutated: raise ScratchpadRewriteLoss( f"rewrite mutated protected constraint values for {mutated!r}; " "re-derivation drift across composed rewrites" ) return rewritten def compose_rewrites(original: str, manifest: dict[str, str], rewrites: list[str]) -> str: """Guard against compounding drift: every rewrite is checked against the original anchor.""" current = original for i, rw in enumerate(rewrites): try: current = apply_scratchpad_rewrite(original, rw, manifest) except ScratchpadRewriteLoss as e: raise ScratchpadRewriteLoss(f"rewrite #{i} failed anchor check: {e}") from e return current ```