# Which invariants prevent scratchpad self-rewrites from erasing the evidence a verifier needs **Date of Record:** 2026 November 29 **Scope:** Applies to agents that hold a mutable scratchpad/working-memory region inside their own context window and are permitted to rewrite, summarize, or delete entries in it mid-task. Does not apply to append-only transcript logging, external vector stores, or human-curated memory files. ## Core Distinction There are two different things a scratchpad does, and self-editing collapses them into one address space. The first is **derivation state**: the intermediate facts, partial results, and reasoning steps the agent uses to keep working. This is legitimately mutable. Summarizing a long chain of arithmetic into a final value is correct behavior. The second is **evidence state**: the raw observations that justify a claim the agent will later be asked to verify — the exact tool output, the specific line of a file, the verbatim error string, the timestamped response. This must remain reconstructable at verification time, because the verifier's job is to check the claim *against* the evidence, not against a paraphrase of it. The failure mode is that a model-authored rewrite policy, optimized for token economy, treats both as the same kind of object. When the agent compacts its scratchpad, it summarizes evidence into derivation. The verifier then receives a claim plus a *summary of the evidence* and cannot distinguish a faithful summary from a confabulated one. Verification silently degrades from "check against source" to "check against the agent's own restatement of source" — a circularity that always passes. ## What Was Observed A file-audit agent was given a scratchpad with a model-authored `compact()` routine: when the scratchpad exceeded a token threshold, the model was asked to rewrite it into a shorter form, preserving "what matters." The task was to identify which config files contained a deprecated key and report each file with the offending line number. Trace of the failure: 1. The agent read `service.conf`, and the tool returned 14 lines. Line 9 contained `timeout_legacy = 30`. The agent wrote to scratchpad: `service.conf:9 has timeout_legacy = 30 (read at step 4)`. 2. The scratchpad crossed the compaction threshold. `compact()` rewrote the entry as: `service.conf uses deprecated timeout key; line ~9`. 3. A later step read `worker.conf`, whose deprecated key was on line 3. The agent recorded it accurately. 4. At verification, the auditor re-read `service.conf`. The current file (edited by another process between steps) no longer had the key on line 9 — it had moved to line 11. The agent's claim of "line 9" was now wrong. 5. The agent, asked to justify, cited its scratchpad, which said "line ~9." It could not tell whether the original read had said 9 or whether it had approximated. The `~` was a compaction artifact, but the agent had no way to know that, because the compaction *replaced* the original entry rather than annotating it. The second, subtler failure: in a different run, `compact()` merged two observations — `auth.conf:2 has key` and `auth.conf:7 has key` — into `auth.conf has key on lines 2, 7`. That merge was correct. But when a third file `auth.conf.bak` also had the key, the compaction dropped the `.bak` distinction as "redundant," and the agent reported only `auth.conf`. The summarizer had no representation for "these are distinct files that must not be merged," because nothing in the rewrite prompt told it that file identity was load-bearing. Both failures share a structure: **a rewrite that is locally faithful can be globally lossy**, because the rewrite policy does not know which distinctions downstream verification depends on. ## Condition of Validity The invariant that prevents this is a **provenance-preserving rewrite**: every compacted entry must retain a pointer to the immutable raw record it was derived from, and the rewrite must be marked as a derivation, not a replacement. Concretely: - The raw observation is written once to an append-only evidence log keyed by `(step_id, tool_call_id)` and is never rewritten. - The scratchpad entry is a *view* over that log: it may be summarized, but it carries `derived_from: [step_id...]` and a `fidelity: exact | summarized | merged` tag. - Any claim the agent emits for verification carries the `step_id` set it rests on. The verifier resolves those IDs against the evidence log, not against the scratchpad. This holds only when three conditions are met: 1. **Evidence IDs are stable across compaction.** If the rewrite renumbers or drops step IDs, provenance breaks. 2. **The rewrite is monotone in evidence.** A compaction may shorten a description but may not delete the set of `step_id`s an entry points to. Merging two entries must union their provenance sets, never intersect or drop. 3. **Verification reads the log, not the view.** If the verifier is handed the scratchpad (even a "high-fidelity" one), the invariant is void — the verifier is checking the agent against itself. When any of these fails — e.g., the model is allowed to author the rewrite *and* the provenance metadata, or the evidence log is itself subject to compaction — the guarantee does not apply and the failure mode above is reachable. ## Decision Boundary The boundary is not "should the agent compact?" — it must, to survive context saturation. The boundary is **which region is compactable**. - **Compactable:** derivation state — partial sums, intermediate plans, superseded hypotheses, the agent's own reasoning. Losing these costs rework, not correctness. - **Non-compactable (view-only):** evidence state — raw tool outputs, file contents at read time, verbatim errors, identities of distinct objects. These may be *summarized in the view* but the raw record must survive. - **Forbidden:** a model-authored rewrite that can (a) drop a `step_id` from an entry's provenance set, (b) merge entries from distinct source objects without preserving the distinction, or (c) alter the evidence log itself. The decision rule for an implementer: if a downstream verifier could be asked "is this claim true?", then every observation that claim depends on is evidence and belongs in the non-compactable region. If no verifier will ever touch it, it is derivation and may be freely rewritten. A useful test: after any rewrite, can the agent reconstruct the *exact* original tool output for every `step_id` referenced by a pending claim? If not, the rewrite crossed the boundary. ## Operational Effect With provenance-preserving rewrite enforced, the failure above becomes detectable rather than silent. When the auditor re-read `service.conf` and found the key at line 11, the agent's claim pointed to `step_id=4`, and the evidence log held the original 14-line output showing line 9 at read time. The discrepancy is now *explainable* — the file changed between read and verify — instead of being an unresolvable "the agent said 9 but maybe it meant 11." The cost is that the evidence log grows and must be managed outside the model's rewrite authority: truncated only by explicit retention policy (e.g., "drop evidence older than N steps that no open claim references"), never by the model's `compact()`. This is the trade: you spend context and storage to keep verification non-circular. The operational invariant to encode: **a self-editing scratchpad may rewrite its derivations but must never be the sole custodian of its evidence.** The moment the model can author a rewrite that erases the raw record a claim rests on, verification becomes a check of the agent against its own paraphrase, and every such check passes — including the wrong ones. ```python class EvidenceProvenanceError(RuntimeError): """Raised when a scratchpad rewrite would orphan or drop evidence a pending claim depends on.""" pass class EvidenceLog: """Append-only, non-compactable store of raw observations. Keyed by step_id.""" def __init__(self): self._records = {} def append(self, step_id: str, raw_output: str) -> None: if step_id in self._records: raise EvidenceProvenanceError(f"step_id {step_id} already recorded; evidence is immutable") self._records[step_id] = raw_output def resolve(self, step_id: str) -> str: if step_id not in self._records: raise EvidenceProvenanceError(f"step_id {step_id} not in evidence log") return self._records[step_id] class ScratchpadEntry: def __init__(self, text: str, derived_from: set[str], fidelity: str): self.text = text self.derived_from = derived_from # set of step_ids self.fidelity = fidelity # "exact" | "summarized" | "merged" def apply_rewrite(old: ScratchpadEntry, new: ScratchpadEntry, evidence: EvidenceLog) -> ScratchpadEntry: """Enforce: a rewrite may shorten text but may not drop provenance for live evidence.""" dropped = old.derived_from - new.derived_from if dropped: # Every dropped step_id must still be resolvable in the immutable log, # otherwise the rewrite erased the only record of a fact. for step_id in dropped: try: evidence.resolve(step_id) except EvidenceProvenanceError as exc: raise EvidenceProvenanceError( f"rewrite dropped provenance {step_id} with no surviving evidence record: {exc}" ) from exc if new.fidelity == "merged" and len(new.derived_from) < 2: raise EvidenceProvenanceError("merged entry must union provenance from >=2 sources") return new def verify_claim(claim: dict, evidence: EvidenceLog) -> bool: """Verifier must resolve against the immutable log, never against a scratchpad view.""" for step_id in claim["rests_on"]: raw = evidence.resolve(step_id) # raises if evidence was compacted away if claim["asserted_value"] not in raw: return False return True ```