AutoCompact Teaches Coding Agents When to Compact; +9.2 Points on SWE-bench Verified

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

Xuan Zhang, Longtao Zheng, Cunxiao Du, Bo An, Xin Dong

cs.CL

2026-10-02

AutoCompact trains a coding agent to decide when to compact, what to keep, and how to continue. On Qwen3-Coder-30B, SWE-bench Verified rises from 30.4% to 39.6%.

What problem this solves

Repository-level bug fixing runs long: search, read, edit, test. Early hypotheses, failed attempts, and verbose tool dumps go stale. The next stage needs a working state: conclusions already locked, the relevant code and workspace, and what remains to do.

Length-triggered compaction waits until the window is almost full. Stale exploration piles up to the threshold, and the cut can land in the middle of a stage whose evidence is still needed. Proactive methods either prompt with an inference-time rubric (SelfCompact) or insert compression calls into finished trajectories and SFT on that (SWE-Compressor). A rubric gives no training signal. Offline insertion keeps the original follow-up actions, so the agent never learns what the summary must retain or how to act after the cut.

Method

The agent gets a compact() action it can call before the limit. Earlier turns are replaced by a working-state summary headed Auto Context Summary; the original task stays. Execution resumes from the shorter context.

Prompting is not enough. The base model almost never calls compact() before it hits the wall. During data collection, GPT-5.5-Codex judges each proposal from the current history only and patches three failure modes: compact once the current stage is resolved, keep exploring if evidence is still missing; keep conclusions, code and workspace state, and the next action in the summary; take the first post-compaction actions from that summary. A search already recorded there should not be re-run. Corrected outputs are executed in the environment, so the rest of the trajectory continues from the fix.

1,052 judge-corrected trajectories on 379 SWE-rebench tasks (24% trigger, 53% working state, 23% continuation). SFT for two epochs at 5×10^{-7}. SFT fits local corrections, not final pass or fail. Then GRPO on SWE-Gym with a binary reward: does the patch pass the tests. compact() rewrites the prefix, so each trajectory is split at rewrite points; all segments share one advantage. No KL, no entropy bonus, no compaction-specific shaping.

Base model is Qwen3-Coder-30B-A3B-Instruct. Same terminal REPL scaffold. The judge is gone at eval.

Results

Full runs, no cost cap, mean of three:

MethodSWE-bench VerifiedSWE-PolyBench Verified
Base30.4%19.5%
Fixed Compaction28.8%18.6%
CompactionRL32.7%19.8%
AutoCompact-SFT32.2%21.7%
AutoCompact39.6%24.5%

Fixed-length compaction is worse than keeping the full history. CompactionRL adds only 2.3 and 0.3 points over Base. SelfCompact reaches 31.7% and 20.6%; SWE-Compressor 31.0% and 20.1%. Every proactive method beats Base at 256K, so a window large enough to hold the full trace is still the wrong context if stale exploration stays in it. Online-corrected SFT beats SWE-Compressor by 1.2 and 1.6 points. RL adds another 7.4 and 2.8, which is most of the gain.

Across $0.10–$4.00 per task, the lift holds in a 256K setting where no trajectory hits forced compaction and in a 16K setting with a shared fallback. At the high 16K budget AutoCompact reaches 38.0% against Base at 28.8%. Skip compact() on the same checkpoint and continue with the old history: pass rate drops 19.9 points at $0.10 and 1.9 points at $4.00.

On all 500 SWE-bench Verified tasks, Base almost never calls compact() proactively; SFT does so on 44.3% of tasks, RL on 58.5%. Key-state omissions in summaries fall from 3.1% to 0.2%, missing next-action from 8.2% to 2.2%. An SFT summary can record an unresolved SyntaxError and still propose to stop. After RL the recorded state and the proposed next step agree, because the only reward is whether the actions that follow succeed.

Why it matters

This is model-harness co-design: the harness exposes compact(), the model learns when to call it, what to keep, and how to continue. For long-horizon coding agents, context management is about dropping stale exploration on task progress.

The numbers are tied to Qwen3-Coder-30B on one REPL. Swap the model and re-measure. Systems that compact only at the window limit (the paper names Codex and Claude Code) should read the 16K results as: a progress-triggered cut before the fallback is worth trying, especially under a tight budget.

Incremental, aimed at the unsupervised slice: actions after compaction.

Limitations

RL trains on 32K-token sequences; eval uses a 256K window. The authors point to gains at every budget as evidence this is enough. There is no same-length RL ablation.

One scaffold. No human agreement numbers on the judge, and no weaker-judge ablation. The SWE-Compressor comparison claims similar trajectory counts without exact sizes or task overlap. Summary-defect rates use keyword screens. At a full window and a $4.00 budget, actually running compaction is worth 1.9 points; the inference-time benefit is thin when context and money are both ample. No comparison to stronger closed coding agents, and no report of compact() frequency, summary length, or tokens saved.

Terms

Source

What people are saying

Related papers

All paper explainers