AutoCompact: Learning When to Compact Context in Long-Horizon Coding Agents
Summary (Overview)
- Core contribution: AutoCompact trains a coding agent to make three context-management decisions as part of its policy: when to compact context, what working state to preserve, and how to continue execution after compaction.
- Key innovation: During data collection, a judge reviews and corrects the agent's compaction decisions, summaries, and post-compaction actions before they are executed in the environment, so each trajectory continues from corrected decisions.
- Training pipeline: Two-stage approach—(1) supervised fine-tuning (SFT) on 1,052 judge-corrected trajectories, followed by (2) outcome-based reinforcement learning (GRPO) that jointly optimizes coding and compaction with binary task success as the only reward.
- Results: AutoCompact achieves pass rates of 39.6% on SWE-bench Verified and 24.5% on SWE-PolyBench Verified, improving over the base model by 9.2% and 5.0% (absolute), respectively.
- Key finding: Gains persist across all inference budgets (4.00 per task) and both 256K and 16K context settings; ablation shows that executing compaction itself contributes to performance beyond training effects.
Introduction and Theoretical Foundation
Background and Motivation
Coding agents solve repository-level software engineering tasks through long trajectories of code inspection, search, editing, and testing. As tasks progress, earlier exploration becomes stale—exploratory hypotheses, failed attempts, and verbose tool outputs accumulate in context. The authors argue that effective context management is more than avoiding overflow: an agent must decide when to compact, what working state to preserve, and how to continue from it.
Limitations of Existing Approaches
The paper identifies three categories of prior work, each addressing only part of the problem:
-
Length-triggered methods (e.g., CompactionRL): Compact only when the remaining context budget falls below a fixed threshold. This ties compaction to context length rather than task progress, potentially compacting in the middle of an unresolved stage.
-
Proactive methods with inference-time rubrics (e.g., SelfCompact): Let the agent decide when to compact but provide no training signal for the decision.
-
Proactive methods with offline insertion (e.g., SWE-Compressor): Insert compression calls into completed trajectories offline, keeping the original actions after each inserted call—thus failing to supervise how the agent should continue from the compacted state.
Key Insight
The paper identifies three interdependent behaviors that determine compaction effectiveness:
- Trigger: Choosing an appropriate compaction point (when the current stage is sufficiently resolved)
- Working state: Constructing an accurate summary preserving findings, code, workspace state, and remaining actions
- Continuation: Reliably following the summary after compaction, rather than revisiting completed exploration
"Deciding when to compact is not sufficient: the working state may omit or misrepresent key information, and even when it is correct, the agent may fail to follow it."
Methodology
2.1 Proactive Context Compaction
AutoCompact augments the agent with a compact() action that can be invoked proactively during execution. When invoked, compaction replaces earlier interaction history with a model-generated working-state summary headed by # Auto Context Summary, while retaining the original task unchanged. The agent resumes execution from the resulting context.
2.2 Judge-Guided Data Collection and SFT
The authors note that prompting alone does not induce proactive compaction—the base model rarely invokes compact() before the context limit. Therefore, they collect training trajectories with online judge corrections:
Process:
- Roll out the base policy on 379 SWE-rebench tasks
- A judge (GPT-5.5-Codex) reviews each proposed action using only the current execution history
- Corrections target three failure modes:
- Trigger correction: Whether the current point is appropriate for compaction
- Working-state correction: Whether the summary accurately preserves needed information
- Continuation correction: Whether first actions after compaction follow the working state
Key design choice: Corrections are applied before execution, so each correction shapes the rest of the trajectory—unlike post-hoc annotation where corrections don't influence later states.
Training data statistics: 1,052 judge-corrected trajectories, with 24% supervising compaction triggers, 53% supervising working-state construction, and 23% supervising post-compaction continuation. SFT uses 2 epochs, learning rate , batch size 8.
2.3 Joint RL for Task Solving and Compaction
Starting from the SFT checkpoint, the agent is trained on SWE-Gym using group relative policy optimization (GRPO) with a fully asynchronous RL system. Each rollout receives a binary outcome reward based on whether the final patch passes task tests.
Key technical detail—trajectory segmentation: Because each compact() call rewrites the context, a trajectory with compaction no longer forms a single sequence. The authors split trajectories into segments wherever the context prefix is rewritten, such that within each segment every model turn is conditioned on a prefix that only grows. All segments share the trajectory's advantage, so compaction decisions, summaries, and post-compaction actions receive the same outcome-based signal.
RL uses learning rate , batch size 64, rollouts capped at 50 environment steps and 32K tokens. No KL penalty or entropy bonus—binary outcome reward is the only training signal.
Empirical Validation / Results
Experimental Setup
- Base model: Qwen3-Coder-30B-A3B-Instruct
- Scaffold: Terminal-based REPL scaffold with model-invocable
compact()action - Benchmarks: SWE-bench Verified, SWE-PolyBench Verified
- Judge: GPT-5.5-Codex (used only during data collection)
- Baselines: Base, Fixed Compaction, SelfCompact, CompactionRL, SWE-Compressor
Main Results
Table 1: Full-run pass rates without inference-cost cap
| Method | Trigger | Optimization | Context | SWE-bench Verified (%) | SWE-PolyBench Verified (%) |
|---|---|---|---|---|---|
| Base | — | — | 256K | 30.4 | 19.5 |
| Fixed Compaction | Length | — | 28.8 | 18.6 | |
| CompactionRL | Length | RL | 32.7 | 19.8 | |
| SelfCompact | Rubric | — | 256K | 31.7 | 20.6 |
| SWE-Compressor | Learned | SFT | 256K | 31.0 | 20.1 |
| AutoCompact-SFT | Learned | SFT | 256K | 32.2 | 21.7 |
| AutoCompact | Learned | SFT → RL | 256K | 39.6 | 24.5 |
Key Findings
-
Length-triggered compaction yields limited gains: Fixed Compaction underperforms Base by 1.6% and 0.9% on the two benchmarks.
-
Proactive compaction outperforms full-history execution: All proactive methods outperform Base even with a 256K context window where no trajectory overflows.
-
Online judge-guided SFT beats offline insertion: AutoCompact-SFT exceeds SWE-Compressor by 1.2% and 1.6%, despite comparable training data.
-
Outcome-based RL adds substantial gains: RL improves pass rates by a further 7.4% and 2.8% over AutoCompact-SFT.
-
Compaction itself contributes to gains: When compact() calls are skipped (Summary-ignored variant), pass rates drop—advantage is 19.9% at 4.00.
Compaction Behavior Analysis
Figure 4 metrics show:
- Base rarely invokes
compact(); AutoCompact-SFT uses it on 44.3% of tasks; RL extends to 58.5% - Key-state omissions drop from 3.1% → 0.2% after RL
- Next-action omissions drop from 8.2% → 2.2% after RL
Case Study: Summary Self-Consistency
The paper introduces summary self-consistency as a quality dimension beyond information coverage: a summary can record correct state but propose an incompatible next action. In a qualitative example (task django-13809), the AutoCompact-SFT summary records an unresolved syntax error but proposes finishing without action (fails with SyntaxError), while the AutoCompact summary records complete edits and proposes verification (passes tests).
Theoretical and Practical Implications
Theoretical Contributions
-
Context management as a policy decision: The paper reframes compaction from an engineering constraint (context overflow) to a learned policy behavior that is jointly optimized with task solving.
-
Correction-before-execution paradigm: Unlike post-hoc annotation, applying corrections before environment execution ensures trajectories demonstrate valid behavior after each decision point—a general principle applicable beyond compaction.
-
Outcome-based RL suffices for compaction learning: No compaction-specific reward shaping is needed; binary task success alone drives improvements in trigger selection, summary quality, and continuation behavior.
Practical Implications
-
Cost efficiency: AutoCompact improves pass rates across all inference budgets from 4.00 per task, with the largest gains at low budgets—suggesting compaction makes agents more cost-efficient, not just more accurate.
-
Compatibility with existing systems: The 16K forced-compaction fallback results show learned proactive compaction complements length-triggered mechanisms used in production systems like Codex and Claude Code.
-
Model-harness co-design: The paper demonstrates that exposing a
compact()action in the harness and training the model to use it jointly with task execution is more effective than separate compression modules.
Conclusion
AutoCompact trains coding agents to manage context as part of task execution through judge-corrected trajectories (SFT) followed by outcome-based RL. Experiments show improved task success on SWE-bench Verified and SWE-PolyBench Verified across all inference budgets, with ablation confirming that executing compaction itself contributes to gains.
Limitations and future directions:
- RL training uses 32K-token sequences, shorter than the 256K evaluation context window (resource constraint)
- The study is limited to a single scaffold; applying learned proactive compaction to widely used harnesses (Codex, Claude Code) with automatic length-triggered compaction is left for future work
Key takeaway: The paper highlights the value of training not only the construction of compacted states but also the agent's behavior that follows from these states—which prior methods leave unsupervised.
Related papers
- When Does Recurrence Become an Algorithm? Convergence Selection in Weight-Tied Looped Transformers
Trained weight-tied looped transformers implement serial linear-frontier algorithms whose speed obeys a budget law, and standard interpretability instruments are provably blind to this mechanism.
- DuoDiscover: Scaling Autonomous Discovery Beyond the Evaluation Bottleneck
DuoDiscover co-discovers optimization targets and cheap proxy evaluators with fidelity testing, achieving up to 15x speedups and beating human experts across software and chip design.
- ReTerm: Recovery Training for Self-Improving Terminal Agents
ReTerm trains terminal agents to recover from their own execution failures via self-generated recovery data and online RL, boosting Terminal-Bench 2.0 Pass@1 by 14.6 points.