SWE-MEM: Learning Adaptive Memory Management for Long-Horizon Coding Agents¶
Conference: EMNLP 2026
Paper: ACL Anthology
Area: LLM Reasoning
Keywords: Coding Agents, Long-Horizon Reasoning, Adaptive Memory Management, Curriculum Learning, Memory-Aware GRPO
TL;DR¶
SWE-MEM introduces a proactive and on-demand memory management framework for long-horizon software engineering agents, equipping models with a flexible memory tool to autonomously decide compression timing, arbitrary span selection, and progressive summarization. Combined with curriculum fine-tuning and Memory-aware GRPO, it reaches 43.4% and 60.2% resolve rates on SWE-Bench Verified using 4B and 30B models under a strict 32K context budget, outperforming prior fixed-window and folding baselines while reducing overall token consumption.
Background & Motivation¶
Long-horizon software engineering (SWE) agents deployed in automated repository-level bug fixing and code maintenance typically adopt the classic ReAct-style append-only paradigm. In this mode, the agent iteratively accumulates textual reasoning traces, code tool invocations, and environment feedback into its active conversation context. However, realistic SWE tasks involve dozens to hundreds of interaction rounds, where verbose compiler traces, multi-page test logs, and broad codebase directory listings rapidly inflate the trajectory length. Even when extended context windows are technically supported, massive volumes of low-utility intermediate exploration disperse model attention, causing severe distraction and performance degradation on core reasoning paths.
Prior attempts to alleviate context window pressure primarily rely on passive, rule-driven compression workflows. These include threshold-based summarization (which triggers a monolithic compression of the entire historical prefix once an arbitrary token limit is hit), subtask-level context folding (which encapsulates completed subtask spans), and sliding-window methods such as SWE-Compressor (which retains the most recent \(k\) rounds and summarizes older history). While these techniques successfully cap the peak active context, they suffer from two fundamental deficiencies. First, rigid heuristic rules fail to respect the heterogeneous information value inherent to code interaction histories: verbose execution logs or irrelevant file explorations can safely be compressed at any time, whereas early inspection of root-cause code or bug-reproducing scripts must be preserved verbatim for downstream patching. Second, existing methods optimize context reduction in isolation rather than end-to-end task-solving efficiency, frequently stripping critical debugging state and forcing the agent to re-explore the repository, which paradoxically inflates total token expenditure and interaction turns.
The root cause of these limitations is that memory management has historically been decoupled as an external post-processing mechanism rather than treated as an endogenous, learnable action in the agent's decision space. The authors argue that coding agents must possess the agency to assess their own cognitive load, proactively identify compressible spans, and distill operational memories without breaking task continuity. Core idea: Model memory management as an autonomous agent-callable tool (compress) that decides when to compress, what arbitrary span to compress, and how to synthesize high-level summaries alongside remaining work plans, optimized end-to-end via synthesized proactive trajectories, two-stage curriculum fine-tuning, and Memory-aware GRPO with checkpoint-based trajectory splitting and step-level credit assignment.
Method¶
Overall Architecture¶
SWE-MEM provides an end-to-end training and inference framework comprising a flexible memory tool interface, a dual-trigger data synthesis pipeline, two-stage curriculum supervised fine-tuning (Curriculum SFT), and reinforcement learning via Memory-aware GRPO (MGRPO). During online problem-solving, the agent interacts with the workspace while actively monitoring its context state. When compression is needed, the agent invokes compress, rewriting the selected trajectory span into a structured summary while appending a forward-looking task agenda, thereby ensuring seamless and noise-free context continuation.
%%{init: {'flowchart': {'rankSpacing': 24, 'nodeSpacing': 28, 'padding': 6, 'wrappingWidth': 400}}}%%
flowchart TD
A["Long-Horizon SWE Task Input<br/>Issue Description & Repository Context"] --> B["Adaptive Memory Tool Interface<br/>Autonomous When/What/How Decision Space"]
B --> C["Dual-Trigger Synthesis & Filtering<br/>Budget Pressure + Proactive Semantic Triggers"]
C --> D["Two-Stage Curriculum SFT<br/>Format Mastery → Proactive Behavior Reinforcement"]
D --> E["Memory-Aware GRPO Policy Optimization<br/>Checkpoint Splitting + Token Loss Aggregation + Step Credit Masking"]
E --> F["Efficient Inference Deployment<br/>Superior Task Resolution within Compact 32K Window"]
Key Designs¶
1. Adaptive Memory Management Tool Interface: Eliminating Rigid Boundary Constraints
Existing memory techniques either execute coarse-grained prefix replacement or bind compression to rigid structural boundaries (e.g., subtasks or recent windows), failing to accommodate complex, non-monotonic software debugging workflows. To give agents full autonomy, SWE-MEM formalizes memory management as an explicit tool call: compress(analysis, start_step_number, end_step_number, content, remaining_work). Here, analysis forces the agent to articulate an internal reflection regarding debugging milestones, potential regressions, and unaddressed risks. The arguments start_step_number (\(s\)) and end_step_number (\(e\)) define an arbitrary historical window \([s, e]\), enabling the agent to surgically excise verbose intermediate outputs while leaving crucial early file inspections and recent active thoughts untouched. The content (\(c\)) parameter replaces the excised span with an informative high-level distillation, while remaining_work (\(f\)) appends a precise, actionable roadmap to the end of the context to preserve momentum:
$\(C'_t = [p, \{a_1, o_1\}, \dots, \{a_{s-1}, o_{s-1}\}, \{c\}, \{a_{e+1}, o_{e+1}\}, \dots, \{a_{t-1}, o_{t-1}\}, \{f\}]\)$
Importantly, because a previously inserted summary \(\{c\}\) can itself be encompassed within future compression windows, the agent can recursively compress older history as the task extends, maintaining temporal coherence without context explosion.
2. Dual-Trigger Trajectory Synthesis & Quality Filtering: Balancing Budget Pressure and Proactive Semantic Cleanup
To bootstrap the agent's capability to invoke memory operations without reliance on proprietary models during inference, high-quality supervision trajectories must be synthesized. SWE-MEM decouples rollout generation: standard coding actions are sampled from the target base model, while memory decisions and summary arguments are synthesized via a proprietary teacher model. The context monitor incorporates two distinct trigger pathways: (1) Context Budget Pressure, implemented via a soft probabilistic schedule where compression probability \(p_t\) is activated when remaining budget drops below 20% and monotonically ramps up to 1 at 5% remaining capacity, avoiding the artificial bias of hard thresholds; and (2) Proactive Memory Management, driven by an LLM-as-a-judge evaluating three semantic states: subtask completion (collapsing exploratory details of solved milestones), low information density (compressing consecutive verbose logs or barren directory listings), and critical focus degradation (alerting and refocusing the agent when a generated patch fails tests across repeated attempts). To prevent training on corrupt interactions, rule-based data filtering masks defective assistant steps—such as improper retention ratios (\(<20\%\) or \(>80\%\)), overly brief compression spans (\(<5\) rounds without long logs), and range index misalignments—preserving valid steps without discarding entire trajectories.
3. Two-Stage Curriculum Supervised Fine-Tuning: Overcoming the Cold-Start Barrier of Proactive Invocation
Proactive semantic compression is inherently harder to acquire than budget-driven compression because it demands deep contextual reasoning about task progression rather than tracking numeric token counters. If trained monolithically, models readily gravitate toward simple budget-triggered compression while ignoring proactive opportunities. SWE-MEM addresses this through a structured two-stage curriculum. In Stage 1, the model is trained across the entire filtered synthetic corpus to internalize the tool's syntactic signature, parameter order, and baseline budget-driven summarization. In Stage 2, a specialized dataset is curated by isolating trajectories featuring proactive memory actions and subjecting them to rubric-based LLM verification (auditing whether the reflection correctly identifies process flaws and whether the summary faithfully matches the plan). To avoid catastrophic forgetting of budget awareness, an equal volume of verified budget-compression trajectories is balanced into the set. This curriculum boosts the share of proactive tool calls from 2.3% to 16.1% in the 4B model and from 4.1% to 14.7% in the 30B model.
4. Reinforcement Learning with Memory-Aware GRPO: Checkpoint Splitting and Step-Level Credit Assignment
Standard Group Relative Policy Optimization (GRPO) distributes credit based solely on the final test execution reward of a completed trajectory. In memory-augmented agents, however, an intermediate compression step irreversibly alters the context state for all subsequent decisions. Assigning an undifferentiated global reward penalizes high-quality memory operations if subsequent code edits fail, or mistakenly reinforces malformed summaries if the agent resolves the issue by chance. SWE-MEM introduces Memory-aware GRPO (MGRPO) to resolve these optimization biases. First, Memory-aware Trajectory Splitting places checkpoints at each memory event, decomposing a rollout into \(K\) sub-trajectories \(\tau^{(1)}, \dots, \tau^{(K)}\) where each segment starts from the exact compressed prefix \(C'_{t_{k-1}}\) that would be observed during online inference, resolving training-inference distribution mismatch. Second, two-stage loss aggregation calculates the token-level average within each rollout before averaging across the batch: $\(\mathcal{L} = \frac{1}{S} \sum_{i=1}^{S} \bar{\ell}_i, \quad \bar{\ell}_i = \frac{1}{N_i} \sum_{k=1}^{K_i} \sum_{t=1}^{T_{i,k}} \ell_{i,k,t}\)$ which prevents extremely long, inefficient rollouts from dominating the gradient update. Third, Step-level Credit Masking applies binary masks \(m_{i,k,t}\) to filter out low-quality memory actions, penalize late memory invocations (occurring only after budget drops below \(\tau=0.05\) in successful trajectories), and isolate the final overflow sub-trajectory to specifically penalize non-compressing steps in out-of-context failures.
Loss & Training¶
The MGRPO objective applies step-level credit masking across group-normalized advantages: $\(J_{\text{MGRPO}} = \mathbb{E}_i \left[ \frac{1}{M_i} \sum_{k=1}^{K_i} \sum_{t=1}^{T_{i,k}} m_{i,k,t} \min\left( r_{i,k,t} \hat{A}_i, \, \text{clip}(r_{i,k,t}, 1-\epsilon, 1+\epsilon) \hat{A}_i \right) \right]\)$ where \(M_i = \sum_{k=1}^{K_i} \sum_{t=1}^{T_{i,k}} m_{i,k,t}\) denotes the count of valid, unmasked tokens in rollout \(i\), \(r_{i,k,t}\) represents the token probability ratio, and \(\hat{A}_i\) is the rollout-level advantage normalized across 8 sampled rollouts per problem instance. - SFT Implementation: Models are trained using the VeOmni distributed framework with global batch size 128, cosine learning rate schedule initialized at \(2 \times 10^{-5}\) across 3 epochs, followed by Stage-2 curriculum tuning at \(2 \times 10^{-6}\) for 1 epoch. - RL Implementation: RL fine-tuning utilizes the Verl framework with batch size 64, sampling 8 rollouts per instance at temperature 1.0, learning rate \(2 \times 10^{-6}\), late compression threshold \(\tau=0.05\), DAPO clipping bounds (clip-low=0.2, clip-high=0.27), and a maximum step cap of 150. R3 Rollout Routing Replay is incorporated for the 30B backbone to maintain replay consistency, without entropy regularization or KL divergence penalties.
Key Experimental Results¶
Main Results¶
Evaluation is conducted on SWE-Bench Verified (500 verified real-world GitHub issues) using Qwen3-4B-Instruct and Qwen3-Coder-30B-A3B as backbones integrated into OpenHands, with context windows constrained to 32,768 tokens (32K). SWE-MEM is evaluated against ReAct baselines, threshold-compression agents, and context-folding models.
| Method / Model | Base Model | Scaffold | Length Limit | Resolve Rate (%) |
|---|---|---|---|---|
| <10B Models | ||||
| Qwen3-4B-Instruct (ReAct) | – | OpenHands | 128k | 7.0 |
| SWE-Gym-7B | Qwen2.5-Coder-7B | OpenHands | 32k | 10.6 |
| SWE-Mirror-7B | Qwen2.5-Coder-7B | MOpenHands | 128k | 22.8 |
| SWE-Dev-7B | Qwen2.5-Coder-7B | OpenHands | 160k | 23.4 |
| SWE-Master-4B | Qwen3-4B-Instruct | OpenHands | 128k | 33.4 |
| Klear-Agent-8B | Qwen3-8B | Mini-SWE-agent-plus | 64k | 39.4 |
| SWE-Lego-8B | Qwen3-8B | OpenHands | 128k | 42.2 |
| SWE-MEM SFT | Qwen3-4B-Instruct | OpenHands | 32k | 41.6 |
| SWE-MEM SFT+RL | Qwen3-4B-Instruct | OpenHands | 32k | 43.4 |
| ~30B Models | ||||
| Qwen3-Coder-30B-A3B (ReAct) | – | OpenHands | 256k | 51.6 |
| Qwen3-Coder-30B-A3B (ReAct) | – | OpenHands | 32k | 38.6 |
| Seed-OSS-36B (ReAct) | – | OpenHands | 327k | 55.2 |
| SWE-Gym-32B | Qwen2.5-Coder-32B | OpenHands | 32k | 20.6 |
| SWE-Mirror-32B | Qwen2.5-Coder-32B | MOpenHands | 128k | 52.2 |
| SWE-Lego-32B | Qwen3-32B | OpenHands | 128k | 52.6 |
| Threshold-Compression Agent | Qwen2.5-Coder-32B | OpenHands | 64k | 53.8 |
| Threshold-Compression Agent | Seed-OSS-36B | OpenHands | 32k | 55.0 |
| Context Folding | Seed-OSS-36B | OpenHands | 32k | 58.0 |
| SWE-Compressor (150 step) | Qwen2.5-Coder-32B | OpenHands | 64k | 54.8 |
| SWE-Compressor | Qwen2.5-Coder-32B | OpenHands | 64k | 57.8 |
| SWE-MEM Workflow-only | Qwen3-Coder-30B-A3B | OpenHands | 32k | 58.4 |
| SWE-MEM SFT (150 step) | Qwen3-Coder-30B-A3B | OpenHands | 32k | 57.2 |
| SWE-MEM SFT | Qwen3-Coder-30B-A3B | OpenHands | 32k | 58.8 |
| SWE-MEM SFT+RL | Qwen3-Coder-30B-A3B | OpenHands | 32k | 60.2 |
The efficiency comparison on SWE-Bench Verified demonstrates that SWE-MEM achieves peak resolve rates while maintaining the lowest total token expenditure:
| Method | Resolve Rate (%) | Token Usage | Relative Token Usage | Average Step | Relative Step Usage |
|---|---|---|---|---|---|
| Threshold-Compression Agent | 53.8 | 5.18M | 203.9% | – | – |
| Threshold-Compression Agent | 55.0 | – | – | 74.9 | 151.3% |
| Context Folding | 58.0 | – | – | 96.5 | 194.9% |
| SWE-Compressor (150 step) | 54.8 | 1.89M | 96.4% | – | – |
| SWE-Compressor | 57.8 | 2.75M | 108.3% | – | – |
| SWE-MEM SFT (150 step) | 57.2 | 0.86M | 89.0% | 73.8 | 119.6% |
| SWE-MEM SFT | 58.8 | 0.91M | 94.7% | 77.7 | 125.9% |
| SWE-MEM SFT+RL | 60.2 | 0.91M | 94.7% | 77.0 | 123.5% |
Ablation Study¶
The ablation study dissects core architectural and training design choices on Qwen3-Coder-30B-A3B and controlled workflow configurations:
| Configuration / Setting | Evaluation Benchmark / Backbone | Resolve Rate (%) | Mechanism & Finding |
|---|---|---|---|
| Full SWE-MEM SFT+RL | OpenHands, 32K (30B) | 60.2 | Complete framework with adaptive span, proactive triggers, and MGRPO |
| w/ Vanilla GRPO | OpenHands, 32K (30B) | ~58.9 | Lacks step-level credit masking, leading to noisy credit assignment |
| SFT Only (No RL) | OpenHands, 32K (30B) | 58.8 | Demonstrates the substantial contribution of curriculum SFT alone |
| w/ Threshold Trigger | OpenHands, 32K (30B) | 57.4 | Replaces proactive triggering with passive budget threshold |
| w/ Full Summarization | OpenHands, 32K (30B) | 57.6 | Replaces selective span selection with monolithic prefix summarization |
| Vanilla SFT (Stage 1 only) | OpenHands, 32K (4B) | 40.8 (vs 41.6) | Omitting Stage 2 curriculum hurts proactive invocation acquisition |
| Remove Memory Tool (Vanilla 256K) | OpenHands, 256K (4B) | 37.6 (vs 43.4) | Large window suffers from distraction; memory filtering aids reasoning |
| Controlled Workflow: Context Folding | Q3C-30B, OpenHands, 32K | 54.6 | Rigid subtask boundaries under identical backbone and budget |
| Controlled Workflow: SWE-Compressor | Q3C-30B, OpenHands, 32K | 56.0 | Fixed sliding window discarding valuable early code traces |
| Controlled Workflow: Threshold Comp. | Q3C-30B, OpenHands, 32K | 56.6 | Passive budget compression without selective targeting |
| Controlled Workflow: SWE-MEM | Q3C-30B, OpenHands, 32K | 58.4 | Untuned model using SWE-MEM workflow outperforms all workflow baselines |
Generalization to multi-language and longer-horizon settings is verified on SWE-Bench Multilingual and SWE-Bench Pro: - SWE-Bench Multilingual: Qwen3-4B-Instruct gains +11.7% (7.3% at 256K \(\rightarrow\) 19.0% at 32K); Qwen3-Coder-30B gains +5.4% (35.3% at 256K \(\rightarrow\) 40.7% at 32K). - SWE-Bench Pro: Qwen3-4B-Instruct improves from 2.6% to 15.2%; Qwen3-Coder-30B advances from 28.9% to 31.7%.
Key Findings¶
- Noise Elimination Outweighs Context Window Size: When the memory tool is removed and the fine-tuned 4B model is given an expansive 256K context window, performance plunges from 43.4% to 37.6%. This proves that memory management provides essential cognitive hygiene by excising distracting noise, establishing that context cleanliness is more critical than raw context capacity.
- Breaking the Token Inflation Curse: Whereas conventional threshold compression inflates total token consumption to 203.9% of the ReAct baseline due to loss of operational memory and subsequent re-exploration, SWE-MEM reduces total tokens to 94.7% (0.91M tokens vs. 0.96M for ReAct). Selective span retention preserves critical debugging state while eliminating irrelevant bloat.
- Behavioral Profiling and Failure Attribution: In runtime profiling, 83.9% of memory operations are triggered by budget pressure and 16.1% by proactive semantic triggers (66.25% subtask completion, 25.0% low information density, 8.75% critical refocusing). Manual inspection of 50 failed trajectories reveals that 49 failures (98%) are bounded by the underlying model's programming capability rather than faulty memory compression; only 1 failure was caused by a summary introducing an incorrect constraint.
Highlights & Insights¶
- Memory as an Autonomous Action Space: Rather than treating context compression as an external scaffolding rule, SWE-MEM internalizes memory management into the agent's generative action space, executing progress analysis, range selection, summarization, and forward planning in a single call.
- Curriculum-Gated Proactive Behaviors: By balancing rubric-verified proactive samples with successful budget-compressed trajectories, the two-stage curriculum SFT enables small and medium models to acquire nuanced, semantic-aware compression without succumbing to passive length-trigger bias.
- Trajectory Splitting with Credit Assignment: MGRPO decouples long-horizon trajectories into online-aligned sub-trajectories and applies rule-based step-level credit masks, preventing false attribution across non-Markovian context modifications and establishing a sound RL recipe for state-altering agent tools.
Limitations & Future Work¶
- Interaction Step Overhead and Latency: Although input token consumption is reduced, invoking memory operations adds explicit generation turns (average steps increase by ~20-25%). Furthermore, rewriting the context history disrupts KV-cache prefix sharing, resulting in an overall wall-clock latency increase of approximately 25.04%. Future work should integrate position-independent KV-caching architectures (such as EPIC) to mitigate system-level serving overhead.
- Rule-Based Masking Imperfections: The current credit assignment masking relies on heuristic checks (token retention ratio, span length, threshold timing). These rules cannot detect semantically defective summaries that remain syntactically sound, leaving room for learned process reward models (PRMs) to refine step-level credit.
Related Work & Insights¶
- vs Context Folding (Sun et al., 2025): Context Folding compresses history strictly at subtask completion boundaries, which can prematurely fold code snippets needed later. SWE-MEM allows arbitrary, non-contiguous span selection and recursive re-summarization, achieving higher resolve rates with far fewer interaction steps (77.0 vs 96.5).
- vs SWE-Compressor (Liu et al., 2025): SWE-Compressor enforces a rigid sliding window that protects only recent \(k\) rounds. This sacrifices vital early inspection context while retaining verbose recent test dumps. SWE-MEM decouples recency from relevance, selectively excising low-utility segments anywhere in the history.
- vs SWE-Master / SWE-Lego (Song et al., 2026; Tao et al., 2026): While contemporary SWE agents scale context windows to 128K-160K using extensive post-training recipes, SWE-MEM establishes that an active 32K window equipped with adaptive memory management can outperform 128K baselines, providing a compute-efficient paradigm for deployment.
Rating¶
- Novelty: ⭐⭐⭐⭐⭐ [Pioneers autonomous, arbitrary-span memory management for SWE agents alongside trajectory-split MGRPO]
- Experimental Thoroughness: ⭐⭐⭐⭐⭐ [Extensive evaluations across 4B/30B backbones, 3 major benchmarks, controlled workflow baselines, and granular manual error analysis]
- Writing Quality: ⭐⭐⭐⭐⭐ [Clear mathematical formalization, compelling architectural exposition, and honest presentation of efficiency trade-offs]
- Value: ⭐⭐⭐⭐⭐ [Offers a practical, high-efficiency paradigm for deploying autonomous coding agents under real-world context and compute constraints]