[KongchangAI]
· 2 min read· 1,187 words

ReTerm: Teaching Terminal Agents to Recover from Their Own Mistakes

ReTerm: Teaching Terminal Agents to Recover from Their Own Mistakes

ReTerm teaches terminal agents to recover from their own failures, improving Pass@1 by 14.6 points.

ReTerm targets the difficulty terminal agents face in recovering from command errors and implementation flaws during long-chain execution by proposing a training method centered on learning self-recovery. The team built the ReTerm-10K dataset for SFT cold-start failure-recovery demonstrations, then introduced ReTerm-GRPO, which branches samples from the agent's own failure states and rewards final task completion. Built on Qwen3.5-9B, ReTerm-9B reaches 34.8% Pass@1 on Terminal-Bench 2.0, a 14.6-point gain over the base model, with better recovery rates than standard GRPO across multiple error categories. Code, data, and models are open-sourced.

Terminal agents have already demonstrated considerable capability on complex coding tasks, but a long-overlooked weakness remains: once a task enters a long execution chain, problems such as command errors or flawed implementations often leave the agent unable to recover effectively, ultimately causing the entire task to fail. To address this pain point, the research team proposes ReTerm——a terminal agent that achieves self-improvement by "learning how to recover from its own failed tasks."

ReTerm paper source page

Error Recovery: The Real Bottleneck for Terminal Agents

Most agent capability evaluations focus on the success rate of "getting it right in one shot," but real terminal environments are full of uncertainty: missing dependencies, command syntax errors, path misjudgments, and flawed implementation logic all create obstacles mid-execution.

ReTerm's core insight is: rather than having the model repeatedly learn "correct trajectories," it is better to have it specifically learn the ability to "escape from error states." This recovery capability is especially critical in long-chain tasks, because a single-step failure propagates down the execution chain, and the later it is fixed, the greater the cost.

The paper explicitly points out that existing terminal agents often struggle to recover from command errors or incorrect implementations during long-horizon execution, which directly leads to task failure. This judgment forms the starting point of the entire work.

ReTerm-10K: Data Built Specifically for "Recovery"

To teach a model to recover, high-quality recovery demonstration data is needed first. The research team built a customized data pipeline for this purpose, producing the ReTerm-10K dataset, which specifically provides effective recovery demonstrations for the supervised fine-tuning (SFT) cold-start stage.

The design philosophy here is worth noting: the data is not simply a pile of success cases, but is organized around the "failure-recovery" relationship. In the cold-start stage, recovery demonstrations first "lead the model in the door" by shaping its behavioral patterns, giving it a preliminary tendency toward error correction rather than blind exploration from scratch. This two-stage approach of "demonstrate first, reinforce later" is consistent with common paradigms in current agent training, but the data construction is more targeted.

SFT cold start refers to using supervised fine-tuning before reinforcement learning to give the model a basic target behavioral pattern. For behaviors like "recovery" that require specific strategies, if RL exploration starts directly from scratch, the model is unlikely to stumble upon effective error-correction trajectories in a vast action space, and the reward signal will be extremely sparse. Therefore, a batch of high-quality "failure-recovery" demonstrations is first used for supervised learning to "guide" the model's behavior toward a reasonable error-correction tendency, and then RL is used to refine and generalize it. This two-stage paradigm of "demonstrate first, reinforce later" is relatively common in agent training, but what makes ReTerm special is that its cold-start data is designed specifically around recovery scenarios rather than generic task-success trajectories.

ReTerm-GRPO: "Branching" from One's Own Failures

After cold start, ReTerm introduces the online reinforcement learning algorithm ReTerm-GRPO. Its key mechanism is to "branch" from the agent's own failure states, generating multiple independent self-recovery attempts, and then providing learning signals based on whether these attempts ultimately complete the task successfully.

This means the model is not learning how others recover, but rather learning "which recovery path works for me in that situation." Compared with standard GRPO, this design of branching samples from failure states and using final task completion as the reward aligns more directly with the true objective in long-chain tasks.

GRPO (Group Relative Policy Optimization) is a reinforcement learning algorithm whose core feature is: for the same input, generate a group of candidate outputs, and then use the relative quality of this group to compute the advantage, without needing to train a separate critic/value network to estimate a baseline like PPO does. This greatly reduces training overhead and is especially suitable for language model scenarios. Standard GRPO usually samples multiple trajectories from the same initial state and compares their differences in final reward to update the policy. ReTerm-GRPO's innovation is changing the sampling starting point from the "task's initial state" to the "agent's own already-failed state," so that multiple trajectories within the group become different recovery attempts for that failure state, with the reward signal directly corresponding to whether the final task is completed.

Measured Performance: Pass@1 Improves by 14.6 Points

ReTerm is built on Qwen3.5-9B, and the resulting ReTerm-9B achieves 34.8% Pass@1 on Terminal-Bench 2.0, a 14.6 percentage point improvement over the base model.

Further analysis shows that compared with standard GRPO training, this method achieves more stable and higher recovery rates across a variety of different error categories. This point is quite critical: it indicates that the improvement does not come from overfitting to a single type of error, but from broad generalization of recovery capability across error types. For terminal tasks that require long runtimes, this cross-category robustness is more practically valuable than a number on a single benchmark.

All code, data, and models have been open-sourced and released in the GitHub repository (twothsnd/ReTerm).

Terminal-Bench 2.0 is a benchmark specifically designed to evaluate terminal agents' ability to execute tasks in real command-line environments, covering typical terminal workflows such as file operations, software installation, script writing, and system configuration. Unlike traditional code generation evaluations, it requires agents to execute multiple steps in an interactive shell, observe outputs, and adjust strategies based on feedback, so it naturally tests error recovery capability. Pass@1 measures the rate at which a model successfully completes a task on its first attempt, without allowing multiple retries, making it a strict metric for evaluating recovery capability within a single execution chain——because once recovery fails midway, Pass@1 is judged as a failure.

What This Means for Terminal Agents

ReTerm's value is not just a score improvement. It elevates "error recovery" from an implicit issue in agent capability to an independent objective that can be explicitly addressed through data construction, algorithm design, and evaluation.

For developers building practical terminal automation tools, this suggests a direction: rather than endlessly pursuing first-step accuracy, it is better to shift part of the focus of training and evaluation to "whether the agent can save itself after failure." In the real world, an agent that can recover is more reliable than one that never makes mistakes——because the latter does not exist.

One-line Summary

ReTerm teaches terminal agents to recover from their own failures, improving Pass@1 by 14.6 points.

Paragraph Summary

ReTerm addresses the pain point that terminal agents struggle to recover from command errors and implementation flaws during long-chain execution by proposing a training method centered on "learning self-recovery." The research team built the ReTerm-10K dataset specifically to provide failure-recovery demonstrations for SFT cold start; they then introduced the online reinforcement learning algorithm ReTerm-GRPO, which branches samples from the agent's own failure states to generate multiple recovery attempts and uses final task completion as the reward signal. Built on Qwen3.5-9B, ReTerm-9B achieves 34.8% Pass@1 on Terminal-Bench 2.0, a 14.6 percentage point improvement over the base model, and outperforms standard GRPO in recovery rates across multiple error categories, demonstrating generalization across error types. The code, data, and models have all been open-sourced.

#ReTerm#terminal agent#error recovery#ReTerm-GRPO#Terminal-Bench 2.0#SFT cold start#Qwen3.5-9B#reinforcement learning
Share:

Related articles