AutoCompact: Learning When to Compact Context in Long-Horizon Coding Agents

Xuan Zhang, Longtao Zheng, Cunxiao Du, Bo An and Xin Dong (Singapore Management University, NTU, Harvard) train a coding agent to decide when to compact its context, what working state to keep and how to resume, as part of its policy.
Ask this paper
Data collection. The base agent runs coding tasks; a judge reviews each compaction decision, the summary written and the actions after compaction, and replaces flawed outputs with corrected ones before execution, so every trajectory continues from corrected decisions.
Training. SFT on those corrected trajectories, then RL with task-success reward that optimizes coding and compaction jointly.
Results. Pass rate improves over the base model by 9.2 points absolute on SWE-bench Verified and 5.0 points on SWE-PolyBench Verified.
Budget robustness. Gains hold at every tested inference budget, both with a 256K window that never overflows and with a 16K window where overflow forces a fallback compaction, so the learned compaction helps even when context space is not the constraint.
Abstract
Coding agents solve repository-level software engineering tasks through long trajectories of code inspection, search, editing, and testing. As a task progresses, earlier exploration becomes stale, so managing context is more than avoiding overflow: an agent must decide when to compact, what working state to preserve, and how to continue from it. We introduce AutoCompact, which trains a coding agent to make these decisions as part of its policy. To collect training data, we run the base agent on coding tasks and use a judge to review its compaction decisions, summaries, and actions after compaction. Flawed outputs are replaced with corrected ones before being executed in the environment, so each trajectory continues from the corrected decisions. We use these trajectories for supervised fine-tuning, then jointly optimize coding and compaction through reinforcement learning with task-success rewards. Experiments on SWE-bench Verified and SWE-PolyBench Verified show that AutoCompact improves pass rates over the base model by an absolute 9.2\% and 5.0\%, respectively. The improvements hold across all evaluated inference budgets, with a 256K context window that never overflows and with a 16K window whose overflow triggers fallback compaction.