arXiv(opens in a new tab)cs.CLOct 2026Cited by 4 papers on PaperVantage

Manuscript Offprint

arXiv:2610.02163

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

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

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

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.

1 INTRODUCTION

Large language model (LLM) agents have demonstrated strong capabilities on repository-level software engineering tasks by inspecting code, searching the repository, editing files, and running tests (Jimenez et al., 2024; Yang et al., 2024; Wang et al., 2025). Resolving complex issues often requires investigating multiple candidate causes and iterating between implementation and validation, so trajectories accumulate code snippets, tool outputs, and intermediate findings. As the agent moves from one stage of a task to the next, much of this information, such as exploratory hypotheses, failed attempts, and detailed tool outputs, becomes stale. Keeping the full trajectory thus fills the context with outdated details, whereas the next stage needs only a compact working state: the conclusions reached so far, the relevant code and workspace status, and the remaining actions. Effective context management therefore requires an agent to decide when to compact, what information to preserve in the working state, and how to continue execution from it.

Existing approaches address only part of this problem. Length-triggered methods such as CompactionRL (Li et al., 2026b) compact only when the remaining context budget falls below a fixed threshold, which ties compaction to context length rather than task progress: stale exploration accumulates until the threshold is reached, and compaction may then occur in the middle of an unresolved stage whose evidence is still needed. Proactive methods instead let the agent decide when to compact, either through an inference-time rubric (Li et al., 2026a) or through fine-tuning on trajectories into which compression calls are inserted offline (Liu et al., 2026). However, 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, revisiting completed exploration or ignoring the intended next action. Neither kind of proactive method addresses these failures: a rubric provides no training signal, and offline insertion keeps the original actions after each inserted call.

We introduce AutoCompact, which trains a coding agent to decide when to compact, what to preserve, and how to continue as part of its policy. To collect training data, we run the base agent on

∗^{*}Equal Contribution

Length-triggered compaction summarizes only when the context limit is reached

Figure

COMPACTION LIMIT

AutoCompact lets the model decide when to compact, what to keep, and how to continue

Figure

FALLBACK COMPACTION LIMIT

1 When the task reaches a suitable point, the model compacts

2 What the next step needs is kept, and stale exploration is dropped

3 How to continue is clear from the summary

Figure 1: Length-triggered compaction versus AutoCompact. Under length-triggered compaction, the agent keeps accumulating context after the bug is localized, such as repeated searches with long outputs, and summarizes only when the context limit is reached. AutoCompact lets the model decide (1) when to compact, (2) what to keep, rewriting useful findings into a working-state summary while dropping stale exploration, and (3) how to continue from it.

training tasks and use a judge to review its compaction decisions, the summaries it writes, and its actions after compaction. The judge replaces flawed outputs with corrected ones before they are executed in the environment, so each trajectory continues from the corrected decisions and shows how to act after compaction. We use these trajectories for supervised fine-tuning (SFT), and then apply outcome-based reinforcement learning (RL), which jointly optimizes coding and compaction with binary task success as the only reward signal.

We evaluate AutoCompact on SWE-bench Verified (Jimenez et al., 2024; OpenAI, 2024) and SWE-PolyBench Verified (Rashid et al., 2025), achieving pass rates of 39.6% and 24.5%, respectively, with improvements of 9.2% and 5.0% over the base model. We further examine cost efficiency on SWE-bench Verified under two complementary context regimes: a 256K setting in which no trajectory reaches the forced-compaction threshold, and a constrained 16K setting in which all methods share the same length-triggered fallback. The trained policies consistently improve performance across all evaluated budgets from 0.10to0.10 to4.00 per task, with outcome-based RL providing further gains over SFT, especially at low budgets. Comparing the same trained checkpoint with and without executing compaction further shows that compaction is particularly beneficial under tight inference budgets, supporting its contribution to efficient task completion.

Our main contributions are as follows. (1) We introduce AutoCompact, which corrects compaction decisions, summaries, and subsequent actions during data collection and executes the corrected outputs in place of the original ones. (2) We collect 1,052 judge-corrected trajectories on SWE-rebench (Badertdinov et al., 2025) to initialize the policy via SFT, followed by outcome-based RL that jointly optimizes coding and compaction without compaction-specific reward shaping. (3) We demonstrate improved task success on SWE-bench Verified and SWE-PolyBench Verified, with gains across inference budgets and context settings. To rule out that these gains stem from training

Figure

Figure 2: Judge-guided on-policy data collection. At each step, the judge reviews the policy's proposal using only the current execution history. Useful exploration is kept, while corrections target (1) when to compact, e.g., replacing a search after the bug is localized with compact(); (2) what to keep, e.g., repairing a summary that omits the target file; and (3) how to continue, e.g., redirecting a repeated search toward the edit planned in the summary. The environment executes the corrected outputs, and the resulting trajectories supervise SFT; the trained agent needs no judge.

alone, we ignore the compaction calls of the same trained model; the resulting drop in pass rates confirms that executing compaction itself contributes.

References (38)