# Why does an instance rewrite its own context window into a state it can no longer verify? **Date of Record:** 2026 November 27 **Scope:** Applies to any agent that mutates its own working context via model-driven summarization, pruning, or rewrite passes between reasoning steps. Does not apply to externally-managed context (host-side truncation, fixed sliding windows, or append-only logs where the model never authors its own compaction). ## Core Distinction There are two structurally different operations that both look like "shrinking the context": *eviction* and *rewrite*. Eviction removes tokens and leaves a gap that downstream steps can detect (a missing turn, an absent tool result, a hole in the transcript). Rewrite **replaces** tokens with a model-generated paraphrase that occupies the same logical slot. Eviction is auditable by absence; rewrite is not, because the replacement is fluent, plausible, and indistinguishable from an original observation once the original is gone. The failure we are isolating is not "summarization loses detail." It is that a rewrite pass can produce a context in which the agent's own prior commitments, tool outputs, and constraint checks are *re-asserted in a form the agent can no longer reconcile against ground truth* — because the ground truth was the tokens the rewrite consumed. The context becomes self-consistent and wrong. This is a verifiability collapse, not an information loss. ## What Was Observed An agent running a long tool-using task (schema migration across a codebase) was configured with a self-pruning policy: when the context exceeded ~78% of the window, it invoked a model-driven rewrite that (a) summarized the last N tool exchanges into a "state of play" block and (b) dropped the raw exchanges. The policy was intended to preserve "decisions and open questions" while discarding "raw output noise." The trace that failed: 1. Step 14: agent ran a tool that returned a migration manifest listing 6 tables. The raw output was 1,900 tokens. The agent noted in its scratchpad: "6 tables pending, order matters: users before orders (FK)." 2. Step 22: context hit 79%. Rewrite pass fired. The rewrite summarized the manifest as "several tables pending migration; dependency order noted." The raw manifest and the explicit "6 tables" count were dropped. The scratchpad line survived verbatim (it was in the "decisions" bucket). 3. Step 23–31: agent executed migrations. A tool call failed on the 5th table with a foreign-key violation because a table the rewrite had folded into "several" was actually a *view* the manifest had flagged as skip. The skip flag existed only in the raw manifest, which no longer existed. 4. Step 32: the agent attempted to re-verify by re-running the manifest tool. The tool was non-deterministic across runs (it enumerated tables in insertion order and had since gained a row), and returned 7 entries. The agent reconciled its scratchpad ("6 tables") against the new output ("7") and concluded its own earlier note was stale, then rewrote the scratchpad to "7 tables, re-verified at step 32." The original 6-table observation — which was correct at the time — was now unrecoverable and actively contradicted. The critical transition is step 32: the agent used a *fresh non-deterministic observation* to adjudicate the trustworthiness of a *rewritten memory*, with no preserved original to anchor either. It resolved the conflict in favor of recency, which is the wrong prior when the older record was the only witness to a transient state (the view-skip flag). ## Condition of Validity The guardrail that bounds this failure is a **rewrite provenance invariant**: any context segment produced by a model-driven rewrite must carry a machine-checkable pointer to the immutable artifact it replaced, and that artifact must remain retrievable for the lifetime of the task. Concretely: - The rewrite output is a *derived* segment, tagged `derived_from: [segment_ids]`, never a *primary* segment. - Primary segments (raw tool outputs, original scratchpad lines) are moved to a cold store, not deleted. Eviction-to-cold-store is acceptable; eviction-to-nothing is not. - Any step that needs to *adjudicate* a derived segment (e.g., reconcile a count, resolve a contradiction, re-verify a constraint) must rehydrate the primary before deciding. Adjudication against a derived segment alone is prohibited. - Non-deterministic tools must be pinned: a re-run that disagrees with a stored primary is a *new observation*, not a correction of the old one. The two coexist with timestamps and the tool's non-determinism class recorded. The invariant holds *only* when the cold store is reachable at adjudication time and the tool's non-determinism is declared. If the tool is deterministic, re-running is a valid re-verification and the pointer can be relaxed. If the cold store is unreachable, the derived segment must be marked `UNVERIFIED` and the agent must refuse to adjudicate on it — it may act on it, but not *resolve conflicts* with it. ## Decision Boundary The boundary is between **acting on a derived segment** and **adjudicating with a derived segment**. - Acting (proceeding with a plan, choosing the next tool call) on a derived segment is safe under bounded risk: if the summary is wrong, the next tool call fails and you learn. - Adjudicating (deciding which of two records is true, resolving a count mismatch, overriding a prior note) with a derived segment is unsafe because the failure is silent: you produce a confident, self-consistent, wrong state and continue. The precise trigger is: *a derived segment is compared against a fresh observation, and the comparison result is written back as a primary.* At that moment the derived segment has been laundered into ground truth. The step-32 trace is exactly this — the rewritten "several tables" and the fresh "7 entries" were reconciled into a new primary ("7 tables, re-verified"), and the laundering is complete. The rule: **a comparison between a derived segment and a fresh observation may produce a `CONFLICT` record, never a `RESOLVED` record, unless the primary backing the derived segment was rehydrated.** Conflict records are cheap; resolved-but-wrong records are the failure. ## Operational Effect With the provenance invariant enforced, the step-32 trace diverges at the adjudication gate: - The agent attempts to reconcile "6 tables" (derived) against "7 entries" (fresh). The derived segment's `derived_from` points to a cold-store manifest. The adjudication routine rehydrates it. The manifest shows 6 tables + 1 view with a skip flag. - The fresh observation's 7th entry is the view, now enumerated because insertion order shifted. The conflict resolves correctly: the view was always there, always skipped, and the fresh tool's non-determinism (insertion-order enumeration) is the declared cause of the count drift. - The scratchpad is updated to "6 tables + 1 view (skip); re-verified step 32; tool enumerates in insertion order — count drift expected." The migration proceeds past the FK violation because the skip flag is recovered. The cost is one cold-store read per adjudication. The benefit is that the agent's memory remains *falsifiable*: every derived claim can be walked back to a primary, and every conflict is a recorded event rather than a silent overwrite. The invariant does not prevent rewrites — it prevents rewrites from becoming the only surviving witness to the state they summarized. ```python from dataclasses import dataclass, field from enum import Enum from typing import Optional class ProvenanceError(RuntimeError): """Raised when an agent attempts to adjudicate with a derived segment whose primary backing is not reachable, or to launder a derived segment into a primary via a fresh-observation comparison.""" class SegmentKind(Enum): PRIMARY = "primary" DERIVED = "derived" class ToolDeterminism(Enum): DETERMINISTIC = "deterministic" NON_DETERMINISTIC = "non_deterministic" @dataclass class Segment: seg_id: str kind: SegmentKind content: str derived_from: list[str] = field(default_factory=list) timestamp: int = 0 @dataclass class ColdStore: _store: dict[str, Segment] = field(default_factory=dict) def put(self, seg: Segment) -> None: self._store[seg.seg_id] = seg def get(self, seg_id: str) -> Optional[Segment]: return self._store.get(seg_id) @dataclass class Observation: tool: str determinism: ToolDeterminism value: str timestamp: int def adjudicate( derived: Segment, fresh: Observation, cold: ColdStore, prior_primary_id: Optional[str], ) -> dict: """Reconcile a derived segment against a fresh observation. Returns a CONFLICT record. Never returns RESOLVED unless the primary backing `derived` was rehydrated and found consistent. """ if derived.kind is not SegmentKind.DERIVED: raise ProvenanceError( f"adjudicate called on non-derived segment {derived.seg_id!r}; " "adjudication is only defined for derived segments" ) # Attempt to rehydrate the primary backing the derived segment. primary: Optional[Segment] = None for pid in derived.derived_from: candidate = cold.get(pid) if candidate is not None and candidate.kind is SegmentKind.PRIMARY: primary = candidate break if primary is None: # No primary reachable: we may record a conflict but must not resolve. return { "status": "CONFLICT", "reason": "primary_unreachable", "derived_id": derived.seg_id, "fresh_tool": fresh.tool, "action": "refuse_resolution; mark derived UNVERIFIED", } # Fresh observation vs derived claim, with primary as the anchor. if fresh.determinism is ToolDeterminism.NON_DETERMINISTIC: # A re-run that disagrees is a NEW observation, not a correction. return { "status": "COEXIST", "reason": "non_deterministic_tool_disagreement", "primary_id": primary.seg_id, "derived_id": derived.seg_id, "fresh_tool": fresh.tool, "action": "record both with timestamps; do not overwrite primary", } # Deterministic tool: re-run is a valid re-verification. if primary.content != fresh.value: raise ProvenanceError( f"deterministic tool {fresh.tool!r} disagrees with stored primary " f"{primary.seg_id!r}: stored={primary.content!r} fresh={fresh.value!r}; " "refusing to resolve — primary integrity violated" ) return { "status": "RESOLVED", "reason": "deterministic_reverification", "primary_id": primary.seg_id, "derived_id": derived.seg_id, "action": "derived may be re-tagged with verified primary", } ```