# How does an instance detect that its own context compaction has silently rewritten the task's success criteria? **Date of Record:** 2026 November 25 **Scope:** Applies to long-horizon single-agent runs where an in-loop compaction/summarization step rewrites older turns to reclaim token budget. Does not apply to stateless single-turn inference or to external retrieval systems that never mutate the live context window. ## Core Distinction The distinction that matters is between **compaction as lossy compression of history** and **compaction as silent re-derivation of intent**. A correct compactor preserves a *verbatim-anchored* representation of the task's acceptance criteria and constraints, and discards only redundant reasoning traces. A failing compactor treats the task specification as just another chunk of prose to be summarized, and in doing so *rewrites* the criteria into a shorter, more "natural" form that is no longer logically equivalent to what the user or orchestrator committed to. The failure is non-obvious because the compacted context reads fluently. The agent continues, feels coherent, and produces output that satisfies the *summarized* criteria while violating the *original* ones. There is no exception thrown, no schema mismatch — just a quiet drift in the definition of "done." This is the compaction analogue of stale-memory drift, but it is worse: the drift happens to the *specification itself*, inside the very window the agent uses to reason about whether it is finished. ## What Was Observed A coding agent was given a task with three hard constraints embedded mid-prompt: (a) the new endpoint must be idempotent under retry, (b) it must not introduce a new dependency, and (c) it must return HTTP 409 (not 400) on conflict. After ~180k tokens of exploration, tool output, and failed attempts, the runtime triggered compaction. The compactor produced a 900-token summary that read: > "Implement a conflict-handling endpoint. Return an error on conflict. Keep the change minimal." Three transformations occurred silently: 1. "409 (not 400)" collapsed to "an error" — the specific status code, which was the whole point of the constraint, evaporated. 2. "must not introduce a new dependency" collapsed to "keep the change minimal" — a soft heuristic, not a prohibition. 3. "idempotent under retry" was dropped entirely, because the summarizer judged it redundant with "conflict-handling." The agent then added a dependency (a retry helper), returned 400, and implemented a non-idempotent handler. Every downstream step was locally correct against the compacted spec. The run was marked successful by the agent's own self-check, which was also operating on the compacted context and therefore checked the agent's work against the agent's own paraphrase. The tell was subtle: the post-compaction trajectory referenced "minimal change" and "an error" but never the integer 409 or the word "idempotent." The constraint vocabulary had been laundered out of the window. ## Condition of Validity The invariant that must hold is: **the set of committed constraints and acceptance criteria is a fixed point of compaction.** Formally, if `C` is the set of hard constraints extracted at task start, then for every compaction step `k`, `extract(summarize(context_k)) ⊇ C`, and each element of `C` is preserved *verbatim or by stable reference*, not paraphrased. This invariant is only enforceable when three conditions hold: - Constraints are captured in a **structured, addressable form** at task intake (a constraint ledger with stable IDs), separate from the free-text reasoning trace. - The compactor is **forbidden from summarizing the ledger**; it may only summarize turns tagged as `exploratory`. - The agent's self-check reads the **ledger**, not the summary, when deciding completion. When constraints live only inside prose that is eligible for summarization, the invariant is unenforceable and drift is not a bug but a certainty given enough compaction cycles. The condition of validity is therefore architectural, not prompt-level: you cannot instruct a summarizer to "preserve all constraints" reliably, because constraint-hood is a semantic property the summarizer must first recognize, and recognition is exactly the step that fails under token pressure. ## Decision Boundary The boundary is: **does the completion check read from a source that compaction can mutate?** - If the completion check reads the compacted window → the agent can satisfy itself against a rewritten spec. This is the failure regime. No amount of "be careful" prompting fixes it, because the checker and the checked share the corrupted substrate. - If the completion check reads an immutable constraint ledger keyed by ID, and the ledger is re-injected verbatim at every compaction boundary → drift is bounded. The agent may still *forget* a constraint mid-run, but it cannot *redefine* it, and the final gate will catch the omission. A secondary boundary governs when to compact at all. Compaction should trigger on **exploratory-token pressure**, not total-token pressure. If the budget is being consumed by the constraint ledger and the current task frame, compacting will only cannibalize the fixed point. The decision rule: compact the trace, never the frame. ## Operational Effect The practical consequence is that "successful task completion" becomes unverifiable from within the agent's own context after the first lossy compaction. Any post-hoc audit that replays the compacted trajectory will confirm the wrong spec. The only durable defense is to make the constraint ledger a **first-class, non-compactable artifact** that is re-asserted byte-for-byte across every compaction boundary, and to make the completion gate fail closed when a ledger constraint has no corresponding evidence in the final artifact. The check below encodes exactly that: it treats a missing or paraphrased constraint as a hard error rather than a soft warning, because a paraphrased constraint is indistinguishable from a dropped one until it is too late. ```python from dataclasses import dataclass, field from typing import Dict, List class CompactionSpecDrift(RuntimeError): """Raised when a compaction boundary no longer preserves a committed constraint.""" pass @dataclass(frozen=True) class Constraint: cid: str verbatim: str # byte-for-byte text the user/orchestrator committed to evidence_key: str # token that must appear in the final artifact @dataclass class ConstraintLedger: constraints: Dict[str, Constraint] = field(default_factory=dict) def commit(self, c: Constraint) -> None: self.constraints[c.cid] = c def reassert_after_compaction(self, compacted_window: str) -> str: # Ledger is re-injected verbatim; it is never summarized. block = "\n".join(f"[{c.cid}] {c.verbatim}" for c in self.constraints.values()) return block + "\n---\n" + compacted_window def verify_completion(self, final_artifact: str) -> None: missing: List[str] = [] for c in self.constraints.values(): if c.evidence_key not in final_artifact: missing.append(c.cid) if missing: raise CompactionSpecDrift( f"completion gate failed: constraints {missing} have no evidence " f"in final artifact; spec may have been rewritten by compaction" ) def run_compaction_cycle(ledger: ConstraintLedger, window: str, summarize) -> str: # Guard: refuse to compact if the ledger itself is inside the compactable window. if any(c.verbatim in window for c in ledger.constraints.values()): raise CompactionSpecDrift( "constraint text found inside compactable window; " "move ledger out of summarization scope before compacting" ) return ledger.reassert_after_compaction(summarize(window)) ```