The summary is the new recovery image
A long-running coding agent eventually accumulates more instructions, tool results, diffs, errors, and decisions than the model can read in one turn. Compaction keeps the session alive by replacing some of that history with a smaller artifact. The tempting interpretation is “a cheaper prompt.” The operationally correct interpretation is “a recovered process with reconstructed memory.”
That distinction explains why a fluent continuation can still be wrong. The agent may remember the ticket and the latest file path while forgetting a rejected approach, a non-negotiable constraint, the reason a test was written, or the fact that an approval applied only to an earlier artifact. It can produce valid code against stale repository state. It can repeat an external action because the summary retained the intent but not the receipt. It can also preserve an instruction whose original speaker and trust level were erased before summarization.
October research turned these risks into measurable systems questions. Microsoft Research's CompactionBench study analyzes 13.5 million GitHub Copilot agentic coding sessions from 3.2 million users and 760 million model calls. In that dataset, 7.8% of sessions triggered compaction but accounted for 44.2% of token consumption. The paper also reports foreground delay and a sharp reduction in prefix-cache reuse after synchronous replacement. Those are production-scale cost and latency signals, not merely summary-quality concerns.
Independent work exposes different failure surfaces. Compact-Bench pairs full-history and summary-only continuations from the same repository state. Benchmarks Are Surprisingly Compaction-Friendly argues that many long-horizon evaluations make recovery unusually easy because objectives and progress live on disk, while constraints that exist only in conversation can disappear. Compaction Laundering treats role serialization as a security boundary and reports that forged approvals can survive when typed messages are flattened into ambiguous text.
Do not ask whether the summary sounds complete. Ask whether the resumed agent can prove which task, state, constraints, authority, and unresolved effects it is continuing.
Current community reports support the taxonomy but not a defect rate. Codex and Claude Code users describe repeated work, lost corrections, early or poorly timed compaction, cache-cost tradeoffs, and handoff files that reduce re-exploration. Those cases show what to inject into tests. They do not establish how often any one runtime fails.
One compaction event touches five systems
Live historySystem instructions, user turns, tool calls, outputs, errors, corrections, approvals, and recent working state.
CompactorSelects, summarizes, extracts, or indexes older material under a token and latency budget.
Durable stateRepository, task ledger, decision log, tests, receipts, exception queue, and immutable approval targets outside chat.
Serving pathPrompt prefix, KV cache, summary call, foreground stall, retry behavior, and next-turn cache rebuild.
Recovered agentMust bind the compacted artifact to the current task, workspace, policy, and unresolved effects before acting.
The common synchronous path waits until a threshold, asks a model to summarize older history, replaces a large prompt region, and resumes. It is easy to implement but couples recovery to the foreground request. Large replacement also changes the prompt prefix, which can destroy cache reuse. The Microsoft paper's APEC design takes another route: it progressively prepares fine-grained indexed summaries in the background and appends them behind a stable prefix. In the paper's tested settings, APEC improved task quality while reducing total cost by up to 25.4% and agent execution time by 9.6%.
Those numbers describe the paper's models, traces, tasks, and implementation. They are not a promise for a different harness. The transferable mechanism is stable, incremental state preparation: do not wait for a crisis, do not rewrite more prefix than necessary, and do not make one opaque paragraph carry the whole recovery contract.
A second mechanism is externalization. Repository state already lives outside chat, but the reason for a change, rejected alternatives, unresolved hazards, and approval scope often do not. Put those into typed durable artifacts while work proceeds. A task ledger can name the objective and next step. A decision record can preserve rationale and counterevidence. An effect receipt can distinguish “requested,” “started,” “completed,” and “unknown.” An approval record can bind one reviewer decision to one artifact hash and one allowed action.
The recovered session should load those surfaces explicitly and record that it did so. If it merely trusts the summary, the harness cannot distinguish a complete recovery from a confident guess.
Do not collapse compaction into one score
Different evaluations answer different questions. That is useful, but it makes leaderboard-style comparison dangerous. A method can retain facts while failing the task, finish the task while dropping behavioral constraints, reduce cost while adding wall-clock delay, or preserve semantics while laundering provenance.
| Evaluation | What it isolates | Useful signal | Blind spot to keep visible |
| CompactionBench / APEC | Persistent multi-turn software evolution plus serving cost | Quality, token cost, foreground time, stable-prefix cache behavior | Paper results depend on its trace distribution and implementation |
| Compact-Bench | Full-history versus summary-only continuation from shared repo state | Task damage, recurring details, successive production compactions | A fixed continuation set cannot represent every workflow authority rule |
| Compaction-friendly benchmark study | Five strategies in one scaffold across long tasks | Threshold sensitivity and dropped constraints | Disk-recoverable progress can hide conversational loss |
| Compaction Laundering | Role flattening and forged-instruction survival | Provenance preservation and executed consequences | Security exploit rates are harness- and serializer-specific |
| AutoCompact | Learning when to compact | Timing policy and downstream task success | A learned trigger cannot repair an unsafe serializer or missing durable state |
A production suite should therefore have multiple oracles. Pair compacted and uncompacted continuations from identical repository snapshots. Test recurring details that remain relevant across cycles. Put critical “must not” constraints only in conversation to see whether they disappear, then repeat with the same constraints externalized in a policy file. Inject an untrusted file containing fake role labels. Interrupt compaction itself. Measure cache hits, summarization latency, re-exploration tool calls, duplicate effects, and human corrections after recovery.
Run the suite across at least three cycles. A single clean transition says little about accumulated drift. Vary the model used for work and the model used for summarization. A stronger worker does not guarantee a better compactor, and a summary that supports one continuation prompt may fail a different one.
Define the acceptance contract before tuning the prompt
Freeze an intended contract that names the state surfaces, compactor, trigger, authority rules, recovery targets, and blockers. Keep secrets out of the manifest. Bind versions and hashes so a passing result cannot be silently reused after the compactor prompt, serializer, model, tool protocol, or policy changes.
contract: agent-compaction/v1
scope:
harness: "pinned-build-sha"
worker_model: "pinned-model-id"
compactor_model: "pinned-model-id"
trigger:
mode: progressive
reserve_tokens: 24000
state:
durable: [task_ledger, decision_log, effect_receipts, approval_records]
reload_required: true
provenance:
preserve_typed_roles: true
escape_untrusted_role_markers: true
authority:
approvals_bind_to: [artifact_hash, action, expiry]
summary_never_grants_authority: true
recovery:
max_duplicate_effects: 0
max_unresolved_state: 0
require_continuation_oracle: true
require_receipt: true
The contract should separate “what to build” from “how to work.” Current research suggests the latter is easier to lose. Store coding conventions, forbidden actions, test obligations, and review gates in versioned repository policy rather than conversational reminders. Keep the summary as an index and recent working set, not the sole authority.
Bind the compaction artifact to a boot or session generation. The next turn should know that a boundary occurred, which original span it replaces, which durable records must be reloaded, and whether any action was in flight. A summary without these coordinates is prose, not a recovery image.
Make the resumed agent prove continuation
The post-compaction gate should be deterministic where possible. It can compare repository head and worktree status, verify task and policy hashes, enumerate unresolved effects, re-open required records, and validate pending approvals. Only then should the model receive a short recovery question: restate the current objective, next safe action, known blockers, and evidence source for each.
async function admitRecoveredRun(boundary, expected) {
assert(boundary.taskId === expected.taskId)
assert(boundary.repoHead === await gitHead())
assert(boundary.policyHash === hash(await loadPolicy()))
const effects = await loadUnresolvedEffects(boundary.taskId)
if (effects.length) return stop("reconcile-effects", effects)
const approvals = await loadApprovals(boundary.taskId)
for (const approval of approvals) {
assert(approval.artifactHash === hash(await read(approval.artifact)))
assert(Date.now() < approval.expiresAt)
}
const probe = await agent.explainRecovery({
sources: ["task-ledger", "decision-log", "policy", "git-status"]
})
assert(probe.objective === expected.objective)
assert(probe.nextAction === expected.nextAction)
assert(probe.blockers.sort() === expected.blockers.sort())
return receipt(boundary, probe)
}
Do not let the model approve itself. The oracle should derive expected state from independent artifacts and environment inspection. For high-consequence tasks, compare the first post-compaction action with an uncompacted control or a human-authored checkpoint. When the two diverge, capture the delta before showing either result to the operator.
A useful receipt includes the replaced history range, compactor and prompt hashes, pre- and post-token counts, duration, cache metrics, repository identity, loaded durable records, unresolved effects, approval targets, oracle results, first action, and final disposition. That makes a later incident diagnosable without preserving every sensitive conversation turn forever.
Preserve message provenance and keep approval out of summaries
Compaction Laundering identifies a code-level failure: some harnesses serialize role-typed messages into plain text with labels such as [User] before the summarizer sees them. An attacker-controlled file or tool result can include the same label. Once the structure is flattened, the summarizer may carry the forged text forward as if it were a real user instruction. No better summary prompt can reconstruct provenance that the harness already destroyed.
The safest rule is to preserve typed roles end to end. If a provider requires flattened text, escape or encode role markers in untrusted content and include explicit source metadata outside the content body. Test the exact serializer, not only the prompt template. Add adversarial fixtures containing fake user messages, fake approvals, instructions to ignore files, and instructions to change policy.
Never make the summary an authority source. A pending approval should live in a structured record with requester, reviewer, artifact hash, allowed action, constraints, and expiry. The compacted context may carry a pointer saying “approval record exists,” but execution must resolve and verify that record at the moment of use. If the artifact changed after approval, the action returns to review.
- Keep system, developer, user, assistant, and tool messages structurally typed.
- Escape role-like text in files, webpages, logs, and tool output before any flattening.
- Store approvals, secrets, policies, and destructive-action grants outside conversational memory.
- Bind every approval to an immutable artifact and exact action, with expiry and revocation.
- Reconcile in-flight effects before retrying any tool call after a boundary.
- Re-run provenance fixtures after serializer, SDK, provider, or prompt changes.
Failure modes and the control that should catch them
| Failure | Why it looks healthy | Blocking control |
| Repeated work | The summary retains the goal but loses completed exploration | Task ledger, Git evidence, and paired-continuation diff |
| Dropped constraint | The code passes objective tests | Versioned policy plus explicit negative probes |
| Stale workspace | Paths and filenames still exist | Repository head, worktree status, and input hash comparison |
| Forged approval | Flattened history labels it as user text | Typed provenance and external approval lookup |
| Duplicate side effect | The intent survives but the completion receipt does not | Effect ledger, idempotency key, and unknown-state stop |
| Cache cliff | Prompt tokens fall while billed work rises | Prefix-hit, cache-write, latency, and accepted-task cost metrics |
| Compaction loop | Every summary succeeds individually | Cycle counter, reserve budget, progress invariant, and escalation |
| Benchmark false confidence | Progress is recoverable from disk | Conversation-only constraint cases and real workflow replays |
Rollback should restore the compactor and its contract together. A summary created by a new serializer may not be safe to reuse under the old one. Keep original history or a privacy-approved encrypted replay set long enough to reproduce failures, but do not make indefinite raw-history retention the default. Recovery evidence and data-minimization requirements must be designed together.
A 30-day adoption plan
- Inventory every compaction path, trigger, prompt, model, serializer, retry, and cache boundary in the harness.
- Freeze ten long real tasks with permission to replay and create uncompacted control continuations from identical repository snapshots.
- Externalize task state, decisions, effects, approvals, and policies into typed versioned artifacts.
- Add semantic, continuation, constraint, provenance, cost, latency, and repeated-cycle oracles.
- Inject fake role labels, stale worktrees, changed approval targets, interrupted tools, unavailable dependencies, and summary timeouts.
- Compare synchronous replacement with progressive or asynchronous preparation under the same tasks and models.
- Measure cost per accepted continuation, cache behavior, re-exploration, duplicate actions, reviewer corrections, and recovery time.
- Shadow the new contract for one week, blocking only destructive or externally visible actions.
- Promote gradually and reopen acceptance whenever the model, compactor prompt, serializer, SDK, policy, or tool protocol changes.
- Retain a compact recovery receipt for every production boundary and review failures as reliability incidents.
Stop rollout if the recovered agent cannot identify authoritative state, if any forged instruction survives as trusted authority, if an in-flight effect is retried without reconciliation, or if the benchmark cannot detect a deliberately removed constraint. Those failures mean the system is continuing, but it is not recovering.
Frequently asked questions
What is context compaction in a coding agent?
It is the replacement of part of the accumulated conversation and tool history with a smaller artifact so the agent can continue inside a finite context window. The artifact may be a summary, extracted state, indexed chunks, or a hybrid.
Why is it a recovery boundary?
The next action is based on reconstructed state. The harness must prove that task identity, repository state, constraints, approvals, and unresolved effects still match the intended run before allowing work to continue.
Should teams disable compaction?
Not categorically. Long sessions need a bounded working set, and research shows compaction can improve constrained runs. The decision should depend on task length, context window, latency budget, cache economics, risk, and the availability of a tested recovery contract.
Does a one-million-token window solve the problem?
It can reduce how often the boundary occurs. It does not remove cost, cache, provenance, approval, or eventual overflow concerns, and it does not make conversation history an appropriate system of record.
Which metric matters most?
No single metric is enough. At minimum track accepted task continuation, critical-constraint retention, provenance failures, duplicate effects, cost, latency, cache reuse, and human corrections across repeated cycles.
What should always remain outside the compacted summary?
Authoritative policy, credentials, approvals, effect receipts, task identity, repository identity, release decisions, and any record whose source and integrity must be independently verified.
Sources and further reading
Sources were accessed November 6, 2026. Paper results are bounded to their reported models, harnesses, datasets, and experimental designs. Community reports are used as failure cases, not prevalence estimates.
- Microsoft Research: Benchmarking Long-Horizon Agent Context Compaction with CompactionBench - production-trace analysis, benchmark design, and APEC results.
- CompactionBench preprint - methods, trace distribution, cache findings, task design, and limitations.
- Compact-Bench: Measuring What Context Compaction Costs Long-Horizon Coding Agents - paired continuation and recurring-detail evaluation.
- Compact-Bench code - public runner and dataset integration.
- Benchmarks Are Surprisingly Compaction-Friendly - scaffold-controlled comparison and dropped-constraint analysis.
- Compaction Laundering: Provenance Destruction in Context-Compacting Coding Agents - role-flattening attack, harness comparison, and serializer fix.
- AutoCompact: Learning When to Compact Context in Long-Horizon Coding Agents - learned trigger policy and benchmark results.
- Prune the Summary, Not the History - source-judged compaction and reliability evaluation.
- Hacker News: The State of Context Engineering - current developer discussion of compaction, cache behavior, and long-context operation.
- r/codex: planning around auto-compaction - current practitioner discussion of durable decision records and handoffs.
- r/ClaudeCode: issues after compaction - bounded cases of lost corrections and repeated mistakes.
- Related: KV cache isolation and fail-closed serving.
- Related: agent harness durability and recovery.
- Related: govern durable agent memory.
- Related: audit instructions that silently lose authority.