ReTerm: Code Agents That Learn to Recover from Their Own Failures

Researchers from Fudan University, CUHK MMLab, and Shanghai Jiao Tong University introduce ReTerm, a recursive self-improvement framework where code agents learn to diagnose and recover from their own terminal failures, turning self-generated error trajectories into training data that boosts 9B model performance on Terminal-Bench, SWE-bench, and OpenThoughts-TBLite by over 13 percentage points.

Machine Heart
Machine Heart
Machine Heart
ReTerm: Code Agents That Learn to Recover from Their Own Failures

In complex terminal tasks, a single error often cascades into a chain of futile actions — reinstalling already-installed dependencies, modifying code based on wrong assumptions — until the budget is exhausted. The ReTerm project asks whether these failures can become the very material for an agent's self-improvement.

Why Terminal Agents Get Stuck in Errors

Error recovery in a terminal is harder than fixing a reasoning answer because earlier actions mutate the environment: files change, packages install, processes start. The agent must understand what has already happened and pinpoint the root cause. The paper illustrates this with a PyStan case: import pystan fails even though the package is installed. A standard GRPO-trained model repeatedly tries import, uninstall, and reinstall, never escaping the loop. A ReTerm-GRPO model instead inspects the installation path, discovers the actual module resides under stan, and switches to import stan — a successful recovery.

ReTerm-10K: Cold-Start Recovery Demonstrations

Learning recovery only from clean successful trajectories cannot cover the messy states after real failures; injected errors may not match the model's actual mistakes. ReTerm therefore starts from the agent's own failure scenes. The team collected tasks where a base model failed, then had a stronger teacher model take over, demonstrating how to inspect the problem, fix errors, and complete the task. After verification and quality filtering, they obtained ~10,000 recovery trajectories (ReTerm-10K) for supervised fine-tuning, giving the model an initial ability to handle failure contexts before online reinforcement learning.

ReTerm-GRPO: Closing the Loop with Self-Generated Failures

ReTerm-GRPO lets the agent attempt multiple independent recovery strategies from the exact same failure state (preserved environment and action history). A task verifier judges which attempts ultimately succeed. The success/failure outcomes become the reinforcement signal that updates the policy. This creates a recursive loop: the model encounters its own errors, autonomously tries to recover, learns from the verified results, and as its capability shifts, new failures emerge to fuel the next round — jointly improving recovery skill and task-solving ability.

9B Model Gains Across Three Major Benchmarks

On three terminal and software-engineering benchmarks, ReTerm-9B improves over the base model by more than 13 percentage points:

Terminal-Bench 2.0: 20.2% → 34.8% (+14.6)

SWE-bench Verified: 45.2% → 58.4% (+13.2)

OpenThoughts-TBLite: 44.2% → 60.6% (+16.4)

The 9B model shows strong competitiveness among same-size models and even outperforms some larger models on certain benchmarks. The comparison with Qwen3.5-9B uses identical framework and evaluation settings, isolating the gain from recovery training; other model numbers come from their respective papers and may differ in configuration.

Generalization Across Frameworks and Task Domains

The improvement transfers across three different execution frameworks on Terminal-Bench 2.0, retaining gains of 6.7–12.4 percentage points, showing robustness to interaction setup changes. Benefits also extend beyond terminal tasks: AIME 2025 Pass@1 rises from 73.3% to 93.3%, SpreadsheetBench Verified from 37.8% to 48.8%, with gains on AIME 2024 and Aider Polyglot as well, indicating cross-domain generalization potential.

Ablation: Explicit Recovery Training Adds Measurable Value

On Terminal-Bench 2.0, recovery SFT lifts performance to 25.8%, standard GRPO to 28.0%, and ReTerm-GRPO to 34.8%. On top of standard RL, optimizing specifically for recovery behavior yields an additional 6.8 points. Across five error categories — planning, tool use, code implementation, verification, resource management — ReTerm-GRPO achieves higher recovery rates than both base model and standard GRPO, though only tool-use recovery exceeds 32%, leaving room for further improvement.

Turning Failures into the Next Round's Starting Point

ReTerm demonstrates a concrete recursive self-improvement path: preserve the agent's own failure scene, let it attempt recovery autonomously, and use verifiable task outcomes to train the next policy. Failures cease to be endpoints and become training material. The work provides an experimentally tractable entry point for failure-driven RSI; as the policy evolves, the model continuously faces its newly generated problems, letting recovery ability and task-solving ability co-develop in a single training process. Code, models, and data are publicly available at https://github.com/twothsnd/ReTerm and the paper at https://arxiv.org/abs/2610.18484.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Reinforcement LearningGRPOfailure recoverySWE-benchCode AgentsTerminal EnvironmentsRecursive Self-ImprovementReTerm
Machine Heart
Written by

Machine Heart

Professional AI media and industry service platform

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.