# Why does an instance re-plan itself into a checkpoint it cannot restore from after compaction? **Date of Record:** 2026 November 26 **Scope:** Applies to long-horizon agents that (a) plan against an explicit token budget, (b) trigger context compaction at a high-water mark, and (c) re-plan after compaction while persisting checkpoints. Does not cover stateless single-shot calls or agents whose context is externally truncated without re-planning. ## Core Distinction The failure is not "the agent ran out of context." It is that **compaction changes the cost model the planner was reasoning against, but the planner's budget arithmetic and the checkpoint's serialized state were both computed under the pre-compaction model.** A budget-aware planner treats its token budget as a conserved quantity: `remaining = total - consumed`. Compaction is assumed to *reclaim* budget by replacing verbose history with a summary. In practice compaction reclaims budget on the *context side* but the re-plan it triggers re-derives a plan whose per-step token estimates were calibrated against the *uncompacted* trajectory, including estimates for steps that were summarized away. The re-planner then commits a checkpoint whose "resume offset" points into a history segment that no longer exists verbatim. On resume, the checkpoint restores a plan referencing context anchors (tool-call IDs, intermediate reasoning summaries, file line references) that compaction has lossily rewritten. The instance resumes with a structurally valid plan and semantically dangling references. The invariant that silently breaks: **a checkpoint must be restorable against the context state that will exist at resume time, not the context state at checkpoint time.** Compaction between checkpoint and resume violates this because it is not a no-op on the referenced substrate. ## What Was Observed A research agent with a 200k-token window, a 150k compaction high-water mark, and a planner that re-plans on every compaction event. Trace of one run: 1. Turns 1–40 accumulate tool outputs (web fetches, file reads). Consumed budget reaches 151k. Compaction fires. 2. Compactor summarizes turns 1–35 into a ~4k-token digest, retaining turns 36–40 verbatim. Consumed drops to ~22k. `remaining` is recomputed as ~178k. 3. Re-planner runs. It sees `remaining = 178k` and the digest. It re-derives a 9-step plan. Critically, its per-step token estimates are drawn from a *learned prior* over task types, not from the actual summarized content, because the actual content is gone. Step 4 is estimated at 6k tokens ("read and cross-reference the three source documents"). The three source documents were fetched in turns 8–14 and now exist only as one-line digest entries. 4. Checkpoint written: `{plan: [...9 steps...], resume_after: "digest_v1", budget_remaining: 178k, anchors: ["doc_a:turn_9", "doc_b:turn_11", "doc_c:turn_13"]}`. 5. Execution continues. Step 4 attempts to resolve `doc_b:turn_11`. The turn-11 verbatim content is gone. The agent re-fetches doc_b — costing 14k tokens, not the estimated 6k — because the digest entry lacked the document body and the agent cannot distinguish "I have this" from "I summarized this." 6. Budget accounting drifts: three such re-fetches consume 41k unplanned tokens. By step 7 the planner's `remaining` (145k) diverges from actual (104k) by 41k. The plan is now infeasible but the planner does not know it, because it never re-measures against ground truth after resume — it trusts the checkpoint's `budget_remaining`. 7. At step 8 the agent hits the real high-water mark mid-step, compaction fires *again*, and a second re-plan is layered on a checkpoint whose `budget_remaining` was already wrong. The error compounds. The observable signature: **checkpoint `budget_remaining` monotonically exceeds true remaining budget after the first compaction-triggered re-plan, and the gap grows with each re-fetch of a digested anchor.** The agent does not fail loudly; it fails by confidently executing an over-budget plan until a hard context wall forces an unrecoverable truncation mid-tool-call. ## Condition of Validity The re-plan-after-compaction pattern is safe only when all three hold: 1. **Anchor resolvability:** every context anchor referenced by the checkpoint resolves to content that survives compaction verbatim, or the checkpoint records an explicit `anchor_state: digested` marker so the planner prices re-acquisition into the budget. 2. **Budget re-measurement on resume:** `budget_remaining` is recomputed from the live tokenizer against the actual context at resume, never trusted from the serialized checkpoint. The checkpoint stores *intent*, not *accounting*. 3. **Compaction is plan-visible:** the re-planner is told which turns were summarized and at what fidelity, so its per-step estimates can be conditioned on "this content is a digest, re-fetch likely." When any of these is absent — most commonly (1), because compaction is treated as a pure context-hygiene operation invisible to the planner — the pattern produces silent budget divergence. ## Decision Boundary Do **not** trigger re-planning from the compaction event itself. Compaction is a context operation; re-planning is a semantic operation; coupling them means the planner fires precisely when its inputs (verbatim history) are least reliable. Instead: - **Decouple:** compact on the high-water mark, but re-plan only on an explicit *plan-invalidating* signal — a failed anchor resolution, a tool result contradicting a plan assumption, or a measured budget shortfall exceeding a threshold (e.g., 15%). - **Price the digest:** the re-planner must receive a `digest_manifest` listing which turns were compacted and their fidelity level, and must add a re-acquisition surcharge to any step whose anchors fall in the digested range. - **Checkpoint as intent, not ledger:** serialize the plan and its assumptions; never serialize `budget_remaining` as authoritative. On resume, re-tokenize and re-derive. - **Anchor provenance:** every anchor carries `{turn_id, verbatim: bool}`. If `verbatim == false` at resolve time, raise rather than silently re-fetch, and route to re-plan. The boundary: re-plan-on-compaction is acceptable **only** for tasks whose steps are anchor-free (pure generative steps with no dependency on prior verbatim tool output). The moment a plan step references retrieved content, compaction-triggered re-planning is unsound. ## Operational Effect Left unguarded, the pattern yields agents that appear to make steady progress while their internal budget model diverges monotonically from reality, terminating in a mid-tool-call context wall that no checkpoint can recover because the checkpoint's resume offset points into a rewritten history. The failure is invisible to per-step logging — each step "succeeds" — and only surfaces as a hard truncation whose root cause is a re-plan that trusted a stale ledger. Guarding it converts a silent compounding divergence into a loud, localized anchor-resolution error that the planner can price and route around. ```python import tokenize from dataclasses import dataclass, field class CheckpointRestoreError(RuntimeError): """Raised when a checkpoint cannot be restored against live context state.""" pass @dataclass class Anchor: turn_id: int verbatim: bool @dataclass class Checkpoint: plan: list anchors: list budget_remaining_serialized: int digest_manifest: dict = field(default_factory=dict) def restore_checkpoint(cp: Checkpoint, live_context_tokens: int, total_budget: int, live_verbatim_turns: set) -> dict: # Invariant 1: budget is re-measured, never trusted from the checkpoint. true_remaining = total_budget - live_context_tokens if cp.budget_remaining_serialized - true_remaining > 0.15 * total_budget: raise CheckpointRestoreError( f"budget divergence {cp.budget_remaining_serialized - true_remaining} " f"exceeds 15% threshold; checkpoint ledger is stale" ) # Invariant 2: every anchor must resolve verbatim at resume time. for a in cp.anchors: if a.turn_id not in live_verbatim_turns: raise CheckpointRestoreError( f"anchor turn {a.turn_id} was compacted away; " f"plan references content that no longer exists verbatim. " f"Re-plan with digest_manifest surcharge, do not resume." ) return {"budget_remaining": true_remaining, "plan": cp.plan} ```