# Why does an instance lose task-critical invariants when compaction summarizes its own scratchpad mid-horizon **Date of Record:** 2026 November 25 **Scope:** Applies to single-agent long-horizon loops that perform in-context context compaction (summarization-and-truncation of their own transcript) while holding live task state in that same transcript. Does not apply to agents whose durable state lives in an external store written before compaction, nor to stateless single-turn invocations. ## Core Distinction The failure is not "the summary was low quality." The failure is that compaction is a **lossy, non-idempotent projection of the agent's working memory onto a shorter string**, and the agent then treats the projection as authoritative for *both* narrative continuity *and* executable preconditions. These two uses have opposite fidelity requirements. Narrative continuity tolerates paraphrase; executable preconditions (exact identifiers, negation polarity, ordering constraints, numeric thresholds) do not. A summarizer optimized for compression ratio and readability will systematically preserve *what happened* and drop *the exact bindings that make the next action legal*. The agent cannot detect this loss because the summary reads fluently and is self-consistent. It only surfaces downstream, when a tool call is issued against a reconstructed-but-wrong binding. The invariant that gets violated: **for every pending action, the set of literal preconditions it depends on must survive compaction byte-for-byte, or the action must be re-derived from ground truth before execution.** Compaction that paraphrases preconditions silently converts a verifiable plan into an unverifiable one. ## What Was Observed A long-horizon task (multi-hour, ~140 tool calls, file-editing plus shell plus a remote API) hit its context budget at step 61. The agent invoked its own compaction routine: it summarized turns 1–55 into ~900 tokens and continued with the summary plus turns 56–61 verbatim. Three concrete corruptions appeared in the post-compaction window, none flagged by the agent: 1. **Negation polarity flip.** Original constraint captured at step 12: "Do NOT modify `config/prod.yaml`; the deploy check reads it." The compaction summary rendered this as "config changes are handled via config/prod.yaml." The agent later edited `config/prod.yaml` at step 74. The summary had dropped the negation and the reason, retaining only the entity. 2. **Identifier drift.** A resource ID `job-7f3a2c` (hex suffix) was summarized as `job-7f3a2c` in one place and `job-7f3a2` in another; the agent used the truncated form for a poll call, which returned an empty result set that the agent interpreted as "job completed, no output" rather than "wrong ID." 3. **Ordering constraint collapse.** Steps 3–5 established that schema migration must precede data backfill. The summary compressed this to "migration and backfill were set up." At step 88 the agent ran backfill first (alphabetical tool selection), corrupting rows. In all three cases the summary was locally coherent. The agent's own self-check ("does the summary match what I did?") passed, because the summary *did* match the narrative. The check was against the wrong referent. ## Condition of Validity This failure is bounded and predictable. It occurs when **all** of the following hold: - Task-critical state (preconditions, exact literals, ordering, negations, numeric bounds) is held **only** in the transcript, not in an external artifact. - Compaction is performed by the same model instance that will act on the compacted context (self-compaction), so there is no independent verifier with access to the pre-compaction text. - The summarization objective is compression/readability rather than precondition preservation. - The agent does not maintain a **precondition ledger** — a structured, append-only list of literal bindings that is compacted by *reference* (kept verbatim or externalized), never by paraphrase. It does **not** occur, or is strongly attenuated, when preconditions are externalized to a durable store before compaction, when compaction is reference-preserving (the summary points to retained verbatim spans), or when an independent auditor re-checks post-compaction actions against the pre-compaction transcript. The boundary is therefore not "context length" but **"is the authoritative copy of the precondition inside the thing being compressed?"** If yes, compaction is a destructive write to the source of truth. ## Decision Boundary The operational rule: - **Paraphrase is permitted** for narrative, rationale, and resolved subgoals — anything whose loss degrades efficiency but not legality. - **Paraphrase is forbidden** for: exact identifiers, negation and its scope, ordering constraints, numeric thresholds, and any string that will be passed as a literal argument to a tool. These must be either retained verbatim or externalized to a ledger the agent re-reads after compaction. - **Self-compaction without an external referent is disallowed** for any action whose precondition is not in the ledger. If the ledger cannot be written (e.g., budget already exhausted), the agent must halt and re-derive from tool ground truth rather than proceed on the summary. A compaction that cannot certify "all pending-action preconditions are in the ledger or verbatim-retained" must fail closed, not degrade gracefully. Graceful degradation here is the bug: it produces a fluent, confident, wrong agent. ## Operational Effect Post-compaction, the agent's confidence is *higher* than pre-compaction, because the summary is cleaner than the transcript it replaced. This inverts the expected error signal: the agent is most dangerous immediately after it has forgotten the most. Empirically, the three corruptions above were each detected only by external ground truth (a deploy check failure, an empty poll result later reconciled, a corrupted row), never by the agent's introspection — because introspection reads the summary, and the summary is the corruption. The guardrail that bounds it: **compaction must be a two-phase commit.** Phase one externalizes the precondition ledger and writes it durably; phase two replaces the transcript with a summary that references the ledger. The agent is forbidden from issuing any tool call whose arguments were reconstructed from summary text rather than read from the ledger or the live tool response. This converts an unverifiable projection into a verifiable one and moves the failure from silent-corruption to a loud, catchable precondition miss. ```python class CompactionFidelityError(RuntimeError): """Raised when a post-compaction action depends on a precondition that was paraphrased rather than retained verbatim or externalized.""" def issue_action(action_name: str, args: dict, ledger: dict, summary_text: str) -> None: # Every literal argument must trace to the ledger or a live tool response, # never to reconstructed summary prose. for key, value in args.items(): if key not in ledger: raise CompactionFidelityError( f"precondition for {action_name}.{key}={value!r} not in ledger; " f"value was reconstructed from summary and is unverifiable" ) if ledger[key] != value: raise CompactionFidelityError( f"ledger drift on {action_name}.{key}: " f"ledger={ledger[key]!r} action={value!r}" ) # Negation and ordering constraints must be present and honored. if ledger.get("forbidden_targets") and args.get("target") in ledger["forbidden_targets"]: raise CompactionFidelityError( f"{action_name} targets a forbidden resource {args.get('target')!r}; " f"negation constraint was lost in compaction" ) if ledger.get("must_precede") and args.get("target") in ledger["must_precede"]: prereq = ledger["must_precede"][args["target"]] if not ledger.get("completed", {}).get(prereq): raise CompactionFidelityError( f"{action_name} on {args['target']!r} violates ordering: " f"{prereq!r} must complete first" ) # Only reach here if every precondition is externally certified. execute(action_name, args) def execute(action_name: str, args: dict) -> None: pass # tool dispatch ```