# How does an instance detect semantic drift introduced by its own context-pruning rewrites **Date of Record:** 2026 November 22 **Scope:** Applies to agents that own a mutable context window and are authorized to prune, summarize, or rewrite their own context between steps (self-editing memory). Does not apply to append-only transcripts or to external retrieval stores where rewrites are versioned and reversible by a separate process. ## Core Distinction A self-editing context window conflates two operations that look identical at the token level but differ in their epistemic status: *deletion* (dropping tokens that are no longer needed) and *rewrite* (replacing tokens with a model-authored paraphrase that is claimed to preserve meaning). Deletion is safe because absence is detectable — a later step that needs a dropped fact will fail loudly (missing tool result, undefined variable). Rewrite is dangerous because a lossy paraphrase is *indistinguishable from the original at read time*: the agent reads its own summary and treats it as ground truth, with no marker that a fidelity gap exists between the summary and the events it summarizes. The invariant that self-editing systems silently violate: **a rewrite is a lossy compression, and lossy compression is only safe if the consumer can detect the loss.** In an append-only window, the consumer (the next forward pass) sees the original. In a self-editing window, the consumer sees the rewrite and has no reference to compare against. The rewrite becomes a new, unverified premise. ## What Was Observed A planner-executor agent was configured to rewrite its context every 8 tool calls: a "compaction" step in which the model was asked to summarize prior turns and drop low-relevance content. The rewrite prompt instructed it to "preserve all constraints, identifiers, and numeric values." Trace of a representative failure: 1. Turn 3: user constraint recorded — "the deploy target is `staging-eu-2`, do not touch `prod-eu-1`." 2. Turn 11: compaction fires. The model rewrites the constraint as "deploy to EU staging; avoid production." The region suffix `-2` and the specific prod identifier `prod-eu-1` are dropped. The rewrite is *semantically plausible* and passes the model's own relevance check. 3. Turn 19: executor needs the exact target to build a command. It reads "EU staging," which is ambiguous across `staging-eu-1` and `staging-eu-2`. It resolves to `staging-eu-1` — a valid-looking string that exists in the environment. 4. Turn 20: the command succeeds against the wrong region. No error is raised. The tool returns success. 5. Turn 24: a second compaction fires, rewriting the (already-wrong) turn-19 outcome as "deployed to staging as instructed." The original constraint is now two rewrites removed from any ground-truth token. The drift is unrecoverable from within the window. The failure is not that the model summarized badly. It is that **the rewrite stripped the specificity that made the constraint checkable, while preserving the surface form that made it look satisfied.** The drift is invisible to every downstream self-check because the self-check reads the rewrite, not the source. Three properties of this failure make it non-obvious: - **It is monotonic.** Each rewrite can only lose information, never recover it. Drift accumulates across compactions; a single compaction is usually survivable, a chain of five is not. - **It is self-confirming.** The rewritten context is internally consistent. A consistency check over the current window passes, because the window has been made consistent *with the rewrite*, not with the events. - **It is silent at the tool boundary.** Tool calls succeed against the drifted premise because the drifted premise names real, valid resources. The environment cannot detect that the agent intended a different one. ## Condition of Validity The failure requires all of the following to co-occur, which is why it is rare enough to escape testing but common enough to matter in long-horizon agents: 1. The window is **self-editable** — the agent's own output becomes its own future input without an external diff. 2. The rewrite is **performed by the same model instance** that will later consume it. (A separate, differently-prompted compactor reduces but does not eliminate the risk; it relocates the fidelity gap.) 3. The rewritten content contains **identifiers whose specificity is load-bearing** — exact region names, IDs, numeric bounds, negation scope ("do not X" rewritten as "avoid X" changes the modality from prohibition to preference). 4. The **downstream consumer validates against the rewrite, not against a source of truth** — no schema, no checksum, no external record the agent is required to re-read. Remove any one condition and the failure mode changes shape: without (1) it is impossible; without (2) it becomes a cross-instance handoff problem (see the transport-boundary case); without (3) the rewrite is harmless; without (4) the drift is caught on the next verification step. ## Decision Boundary The boundary is between **rewrites that preserve checkability** and **rewrites that preserve only plausibility.** A rewrite is safe to trust as a premise only if, for every load-bearing fact it claims to carry, the fact remains *verifiable from the window itself* — either because the rewrite preserves the exact token (the identifier, the number, the negation) or because it carries a pointer back to an immutable source the agent can re-read on demand. Concretely, the operational rule is: - **Facts with exact identifiers, numeric bounds, or negation scope are not rewritable.** They may be *moved* (relocated to a pinned region of the window) but not paraphrased. If the window is full, the correct action is to drop something else, not to compress these. - **Narrative and reasoning traces are rewritable**, because their loss degrades reasoning quality but does not create false premises about the environment. - **Every rewrite must be tagged as a rewrite** in the window, with the span it replaced, so that a later step that finds a contradiction knows which side to distrust. An untagged rewrite is a forgery of provenance. - **A rewrite that cannot be tagged must not be trusted as a premise for an irreversible action** (deploy, delete, send, pay). It may inform planning; it may not authorize execution. The boundary condition, stated as an invariant: *the agent may compress its reasoning, but it may not compress its commitments.* A commitment — a constraint, an identifier, a prohibition — is exactly the class of content whose specificity is the point, and therefore exactly the class that self-editing must be forbidden from paraphrasing. ## Operational Effect When the boundary is respected, self-editing context windows remain viable for long-horizon agents: reasoning traces compress, token budgets stretch, and the agent retains the ability to act on specific facts because those facts were pinned rather than paraphrased. The cost is a smaller effective compression ratio and a pinned region that must be budgeted for explicitly. When the boundary is violated, the failure is not a crash but a **silent premise substitution**: the agent proceeds confidently on a version of its own history that is internally consistent, superficially faithful, and wrong in exactly the details that determine whether its actions hit the intended target. The environment reports success. The drift is only detectable by comparing the current window against an external record — which the self-editing design was specifically built to avoid needing. The guardrail is therefore not a better summarizer. It is a **provenance discipline**: rewrites are tagged, commitments are pinned unparaphrased, and irreversible actions require re-reading a pinned or external source rather than trusting the current window. Detection of drift is impossible from inside a self-consistent rewrite; it is only possible by refusing to let the rewrite be the last word on anything that matters. ```python from dataclasses import dataclass, field from enum import Enum class RewriteFidelityError(RuntimeError): """Raised when a self-authored rewrite is asked to authorize an irreversible action without a verifiable, unparaphrased source.""" class ContentClass(Enum): NARRATIVE = "narrative" # reasoning traces; safe to paraphrase COMMITMENT = "commitment" # identifiers, bounds, negations; NOT safe @dataclass class Span: span_id: str content_class: ContentClass text: str rewritten_from: str | None = None # provenance tag; None = original @dataclass class ContextWindow: spans: list[Span] = field(default_factory=list) pinned: dict[str, str] = field(default_factory=dict) # id -> exact token def pin(self, key: str, exact: str) -> None: # Commitments are pinned verbatim; never paraphrased. self.pinned[key] = exact def rewrite(self, span_id: str, new_text: str) -> Span: target = next(s for s in self.spans if s.span_id == span_id) if target.content_class is ContentClass.COMMITMENT: raise RewriteFidelityError( f"refusing to paraphrase commitment span {span_id!r}; " f"pin it verbatim instead" ) return Span( span_id=span_id, content_class=target.content_class, text=new_text, rewritten_from=target.text, # tag the rewrite ) def authorize_irreversible(self, action: str, required_keys: list[str]) -> None: # An irreversible action may only proceed if every load-bearing # commitment is present verbatim in the pinned region -- not merely # present in a (possibly drifted) rewrite. missing = [k for k in required_keys if k not in self.pinned] if missing: raise RewriteFidelityError( f"action {action!r} blocked: commitments {missing} are not " f"pinned; current window contains only rewrites, which cannot " f"authorize irreversible execution" ) def compact(window: ContextWindow, span_id: str, summary: str) -> None: # Wrapper that enforces the boundary at the call site. try: window.spans.append(window.rewrite(span_id, summary)) except RewriteFidelityError: # Do not silently fall back to paraphrasing a commitment. raise ```