Reinforcement learning · October 2026强化学习 · 2026 年 10 月強化学習 · 2026年10月
Terminal-RSI: From Execution to Recursive TrainingTerminal-RSI:从终端执行到递归训练Terminal-RSI:実行経験から再帰的な学習へ
Successful runs become new demonstrations; fresh attempts provide the next training signal. RSR, RPR and turn-based credit assignment connect the two.成功执行经过重写,成为新的示范;模型继续尝试,从验证结果中学习。RSR、RPR 与按回合回传的反馈,把两段训练连成循环。成功した実行を新しい手本に整え、次の試行結果から学ぶ。RSR、RPR、ターン単位の信用割り当てが二つの学習をつなぐ。
A terminal agent reads files, edits code, and runs tests through command-line tools. The complete interaction record for a task is its trajectory. Terminal-RSI alternates two uses of this experience: RSR reruns successful solutions to create demonstrations; RPR runs new tasks and uses verification outcomes to train the policy and value networks.
1 The workflow: demonstrations, then execution feedback
The Actor is the policy network that generates commands and code. The Critic is the value network: from the task and interaction history at a particular position, it predicts the return from continuing. Follow RSR across the top, then RPR below. The verifier is the program that runs checks and determines whether the task is complete.
Static diagram
A · RSR supplies demonstrations: extract a plan from a verified success, execute it in a clean environment, and keep the accepted new trajectory. B · RPR trains on new attempts: the verifier supplies reward, the Critic supplies value predictions, and GAE / TB-GAE computes advantages A and return targets Ĝ at generated positions. The Actor uses A for PPO; the Critic fits Ĝ.
RSR changes the Actor. Each new RPR phase therefore holds the Actor fixed while calibrating the inherited Critic, then trains both. The updated models produce experience for the next round—the recursive step in Recursive Self-Improvement (RSI).
The diagram includes both advantage estimators. Section 2.3 proposes TB-GAE; the first training phase documented in §2.4 uses standard GAE with γ = λ = 1.
The paper’s original diagrams and actual training order
Figure 2 · Overall Framework ↗ Figure 3 · Single Recursive Step ↗
The actual stage order is Base → RSR-1 → RPR-1 → RSR-2 → RPR-2. At the top of the original diagram, Actor: RSR-1 and Warm-up Critic Model indicate that, on first entering RPR, the policy has completed supervised learning and the value network has completed its initial warm-up.
| Step | Ordinary PPO loop | Terminal-RSI |
|---|---|---|
| Using execution experience | Sample trajectories, estimate advantages from returns, and update. | Also re-execute and filter verified successful experience for SFT. |
| After the policy update | Continue sampling and training the Actor / Critic. | Preserve the Critic across SFT and calibrate it at the start of a new RPR stage. |
| Starting the next round | Continue PPO with the updated policy. | Use the updated policy to discover experience again, then enter RSR → RPR. |
2 RSR: rerun successful experience as demonstrations
The most direct way to learn is to imitate successful attempts. Yet a successful log may contain many detours or depend on tools specific to one harness. Copying that log may not reproduce success in the deployment environment.
Recursive Self-Rewrite (RSR) first distills a successful approach into a plan, then executes it again in the target environment. The trajectory is what gets ‘rewritten’; qualifying trajectories are subsequently used for training.
Static diagram
- Collect successful experience. The current model attempts tasks in different harnesses, retaining only trajectories that fully pass verification. A harness provides tools, maintains interaction history, and controls execution and stopping conditions.
- Distill a plan. The Planner organizes key steps, checks, and recovery procedures into a runbook. A reviewer checks whether the plan works in the target environment and whether it leaks the answer.
- Execute again. With the plan, the Executor solves the same task in a clean sandbox—an isolated, resettable environment—making real tool calls and receiving new responses.
- Verify and train. Once a new trajectory passes task verification and leakage checks, remove the private plan and review dialogue, retaining the public task and real interactions. These demonstrations update the language model responsible for actions: the policy network (Actor).
The final step is Supervised Fine-Tuning (SFT): given the task and history, increase the probability of generating the demonstrated behavior. The private plan helps produce demonstrations; training teaches the model to act without that extra guidance.[1, §2.1]
Rewriting trajectories for a Markdown task
The preceding RSR paper gives a Markdown-processing example. A parser identifies structures such as headings, paragraphs, and tables for downstream code. A precompiled parser is already compiled and can be loaded directly in a compatible environment, avoiding some generation and compilation work.
The source execution took 64 turns to succeed. The distilled plan starts by looking for a compatible precompiled parser, and calls attention to output-summary and table-processing parameters. A new execution can therefore avoid some detours.
Static diagram
The same plan produced both a successful and a failed branch. A successful trajectory enters the SFT data only after passing verification.
Complete rewriting results for this task
The source execution tried generating a parser at turn 26 and used a precompiled parser at turn 51; the successful rewrite shown here used one at turn 9. Across 12 rewrites of this task, 7 succeeded, with a median of 32 turns among successful rewrites. Source: RSR §3.4.
The model generates one token at a time—a textual unit such as a character, part of a word, or punctuation. SFT learns the model-generated portions of demonstrations; tool logs serve as input rather than imitation targets. The selection flags that determine which positions contribute to the training loss form the training mask.
Three roles for the same model
In the preceding RSR paper, the same base model takes on planning, review, and execution through different prompts and contexts. The Planner organizes the approach, the Reviewer checks the plan, and the Executor calls tools to complete the task.
When producing demonstrations, the Executor may consult the private plan. Training retains only the public task and interaction history, checking whether each action can be explained by that public information. Source: RSR §2.
The corresponding SFT objective
θ denotes the trainable model parameters. πθ(yⱼ | hⱼ) is the probability that the policy with parameters θ generates demonstration token yⱼ given history hⱼ. A smaller negative log loss means a higher probability for that demonstrated choice.
This is the form for a single demonstration; training also averages over the demonstration set. hⱼ is the public history, yⱼ is the demonstration token, and mⱼ selects the generated positions to learn from. Minimizing negative log probability increases the probability of the verified demonstration under its corresponding history. Source: Equation 3.
Demonstrations teach the model an approach. On new attempts of its own, it also needs to learn from actual outcomes.
3 RPR: how verification outcomes become training signals
Recursive Policy-Refine (RPR) has the supervised-fine-tuned Actor execute tasks again, using verification outcomes as rewards to update the policy with PPO. The Critic predicts future returns from the task and interaction history, providing a baseline for advantage estimation. For how each model is trained, see PPO: training along a long-horizon trajectory.
Terminal-RSI uses programmatic verification directly, without a separately trained preference reward model; the paper sets both KL and entropy coefficients to 0. PPO still uses old-policy probabilities to control policy changes.
Static diagram
Within an RPR phase, trajectory sampling, verification, and model updates repeat. After the phase ends, the retained Actor enters the next RSR round.
Reward (R) converts verification outcomes into a training score. The paper starts with two quantities: P counts passed semantic checks—checks of task functionality, excluding auxiliary checks such as cleanup; S is the strict-success indicator: 1 for a full pass, 0 otherwise. The score is computed at the end of an execution, rather than issued anew after every command.
The earlier Family Dense Reward (FDR) uses P / D to represent partial completion, with D set in advance for the task family. This gives a signal for getting some things right. A partial-check score alone, however, need not rank full success above every failure.
The subsequent Success-Prioritized Reward (SPR) therefore uses S + P / (P + D′). The second term is always below 1, while full success adds another 1: failures score below 1 and successes at least 1. Here, D′ is a positive scale calibrated offline for each task family and held fixed within the stage.
With 8 checks passed and D′ = 10, the reward is about 0.444 without strict success and 1.444 with strict success. SPR first distinguishes full success, then compares partial completion.
Exact reward formulas, scales, and training stages
These formulas apply when the check report is valid; an invalid report makes both fall back to the strict-success indicator S. FDR uses the 90th percentile of historical total check counts within the task family for D, falling back to 82 when no family scale is available. SPR uses a positive, offline-calibrated D′ fixed within the stage.
The ordinary RPR-2 branch uses FDR; the retained Long-Reward RPR-2 branch uses SPR. Sources: §2.2, §3.1.
In RPR-1, the training side recomputes old-policy probabilities using the old Actor for PPO’s comparison of new and old probabilities. [§2.4]
4 TB-GAE: passing feedback back by turn
A task often spans many turns: the model generates a response or tool call, then receives an environmental response. Rewards arrive only at task completion, yet training needs an advantage for every earlier generated position.
Generalized Advantage Estimation (GAE) combines rewards and value predictions along the trajectory, recursively estimating advantages backward from the end. Later feedback usually decays as it propagates, so the choice of what counts as one step affects the signal reaching earlier positions.
If every text token multiplies feedback by a factor below 1, simply lengthening a tool log weakens the feedback reaching earlier positions. Masking out the training loss at log positions is not enough: logs may still participate in the advantage recursion and incur additional decay.
Turn-Boundary Skip-Observation Generalized Advantage Estimation (TB-GAE) skips tool observations in the recursion. Generated tokens within the same turn incur no decay; decay occurs only across turn boundaries.
Logs remain in the model input, influencing subsequent actions and value predictions. Positions g₁ through g₄ each use the context before their own generation, so they have different value baselines. The training system can batch these predictions after collecting the trajectory.
How log length affects decay
After one step, the difference between ‘immediate reward plus discounted future expectation’ and the previous expectation is the temporal-difference residual (TD residual, δ). Below, we track only this change at the final position and see how much reaches the first position.
The example has two turns and four generated positions. Set γ = 0.9,λ = 0.8, multiplying by 0.72 at each decay step. Drag the log-length control to compare three recursion chains.
0.72⁶0.72³0.72¹The mixed chain crosses 6 edges; skipping observations crosses 3; TB-GAE crosses just 1 turn boundary.
The values are the coefficients carrying the final change back to the start. Generated positions, values, and other inputs are fixed; only log length changes. With γ = λ = 1 checked, all three coefficients equal 1.
Three recursion chains: working through four positions
Let g₁ and g₂ belong to the first turn, and g₃ and g₄ to the second. Their pre-generation values are 0.2, 0.4, 0.5, and 0.8, respectively; reward 1 arrives only at task completion.
First compute each position’s temporal-difference residual, then accumulate backward from the end.
Static diagram
- g₄: the task ends. The reward is 1, against a prediction of 0.8, so A₄ = 1 − 0.8 = 0.20.
- g₃: still in the second turn. The value changes from 0.5 to 0.8, giving residual 0.3. There is no within-turn decay, so A₃ = (0.8 − 0.5) + 0.20 = 0.50.
- g₂: crossing back into the first turn. The residual is 0.9 × 0.5 − 0.4 = 0.05; the next advantage is multiplied by 0.9 × 0.8, giving A₂ = 0.05 + 0.72 × 0.50 = 0.41.
- g₁: still in the first turn. Again there is no within-turn decay: A₁ = (0.4 − 0.2) + 0.41 = 0.61.
| Generated position | Turn | Predicted v | Advantage A | Value target Ĝ |
|---|---|---|---|---|
| g₁ | 1 | 0.20 | 0.61 | 0.81 |
| g₂ | 1 | 0.40 | 0.41 | 0.81 |
| g₃ | 2 | 0.50 | 0.50 | 1.00 |
| g₄ | 2 | 0.80 | 0.20 | 1.00 |
The first turn’s two positions share a return target of 0.81, but their advantages are 0.61 and 0.41 because their prior expectations differ. The Actor updates probabilities using each position’s advantage; the Critic fits its corresponding return target.
Writing the calculation as a single recursion
r̃ⱼ is the reward assigned to generated position j. In this terminal-reward example, only the final position has a nonzero reward.
Enumerate only the retained generated positions j, and let vⱼ be the value before that token is generated. When the next generated position is in the same turn, set Γⱼ = Λⱼ = 1; across turns, use Γⱼ = γturn,Λⱼ = λturn. dⱼ = 1 indicates actual termination.
The terminal reward is injected once at the last generated position. With no intermediate rewards within a turn, adjacent values cancel, yielding a shared return target Uₜ. Each position subtracts its own value prediction to obtain Aⱼ = Uₜ − vⱼ. Here, the first turn’s target is U₁ = 0.9 × (0.5 + 0.8 × 0.5) = 0.81.
TB-GAE discounts by turn, applying one time-decay step per complete action. Log length and within-turn token count therefore no longer determine the number of discount steps. Sources: Equations 8–13 and Appendix C.
What happens at the boundary when a long trajectory is split?
To save GPU memory or compute in batches, a complete trajectory can be split into chunks, passing the recursion from later chunks back to earlier ones. If this example is split after g₂, retaining the next value prediction of 0.5 but zeroing the next advantage of 0.5 changes A₂ to 0.05 and A₁ to 0.25. The missing feedback is exactly 0.72 × 0.5 = 0.36.
A complete continuation must pass both the next value and the next advantage, or compute backward over the full sequence. When sampling stops early and the future has not yet been executed, use value bootstrapping—estimating future returns with the current value prediction. At true task termination, the next value and advantage are zero. Context summarization also changes the history the model reads and requires separate handling.
This section describes TB-GAE as proposed in §2.3. The first stage documented in §2.4 uses standard GAE with γ = λ = 1.
5 Calibrating values under a new policy
RSR changes how the Actor acts, while the old Critic still scores from earlier experience. If the reward formula also changes, so does the score scale. Using old predictions to estimate advantages for a new policy can miscalibrate the learning signal.
Terminal-RSI therefore retains the existing Critic and begins each RPR stage by freezing the Actor—temporarily leaving its weights unchanged—for 4 Critic-only calibration updates, before joint training. This preserves existing experience while bringing value predictions in line with the new policy and rewards.
Static diagram
The Critic, from initial warm-up to recursive training
- Initial warm-up. Freeze the RSR-1 Actor after supervised fine-tuning and train the Critic for one epoch—one pass through Scientific Workflow Reconstruction (SWR) tasks. These tasks reconstruct executable scientific programs and verify their behavior with hidden checks. The Actor supplies trajectories; the Critic learns their return targets, producing the Warm-up Critic Model.
- Entering RPR. Each RPR stage starts with the Actor fixed for 4 Critic-only calibration updates. The Actor then uses advantages for PPO updates, while the Critic regresses on return targets; the two parameter sets are trained separately.
- Entering the next round. RSR-2 fine-tunes only the Actor, retaining the RPR-1 Critic’s weights. RPR-2 calibrates the Critic against executions by the new Actor, then resumes joint training.
The paper describes this warm-up and weight handoff, but does not specify the checkpoint—the model-weight file—loaded into the Critic backbone before its first warm-up. Sources: §2.1, §2.4 and Appendix A.
6 From local tests to complete delivery
The task asks for a calculator written in Rust and C. It has two final checks: can the entry script recompile automatically after build artifacts are deleted, and do outputs for hidden inputs match the reference implementation character for character?
One execution by the initial Base model passed local arithmetic tests and rebuilt automatically, but its final-check output was 23, while the reference returned 23.0. The numbers are equal, but the strings differ. An independent execution by the later RPR-2 version passed both checks.
Static diagram
The RPR-2 execution combines Rust / C components with Python expression evaluation and passes both automatic-rebuild and output-format checks.
How often did each of the five model versions pass this task?
Each version was evaluated twice. Full passes were Base 0/2, RSR-1 0/2, RPR-1 1/2, RSR-2 2/2, and RPR-2 2/2.
终端 Agent 通过命令行读文件、改代码、跑测试。一次任务留下的完整交互记录叫执行轨迹。Terminal-RSI 用这些轨迹交替进行两种训练:RSR 重做成功经验,制作监督学习的示范;RPR 执行新任务,根据验证结果更新策略与价值网络。
1 整体流程:先学示范,再从执行中学习
图中的 Actor 是生成命令与代码的策略网络;Critic 是价值网络,根据截至某个位置的任务与交互历史,预测继续执行能获得的回报。先沿上方看 RSR,再沿下方看 RPR。验证器(verifier)是运行检查、判断任务是否完成的程序。
静态图
A · RSR 解决示范从哪里来:从通过验收的旧轨迹中提炼做法,在干净环境重做,留下可以学习的新轨迹。B · RPR 解决新尝试怎么训练:验证器给出奖励,Critic 给出价值预测,GAE / TB-GAE 算出各生成位置的优势 A 和回报目标 Ĝ。Actor 用 A 做 PPO 更新;Critic 拟合 Ĝ。
每次 RSR 都会改变 Actor。进入新的 RPR 阶段时,先固定 Actor,校准继承的 Critic,再联合训练。更新后的模型又为下一轮产生经验,这就是这里的递归自我改进(Recursive Self-Improvement,RSI)。
图中保留两种优势估计方法。论文 §2.3 提出 TB-GAE;§2.4 披露的第一训练阶段使用标准 GAE,γ = λ = 1。
对照论文原图与具体训练顺序
Figure 2 · Overall Framework ↗ Figure 3 · Single Recursive Step ↗
实际阶段顺序为 Base → RSR-1 → RPR-1 → RSR-2 → RPR-2。原图顶部的 Actor: RSR-1 与 Warm-up Critic Model 表示首次进入 RPR 时,策略已完成监督学习,价值网络已完成初始预热。
| 环节 | 普通 PPO 循环 | Terminal-RSI |
|---|---|---|
| 执行经验怎么用 | 采样轨迹,按回报计算优势并更新。 | 还把验证过的成功经验重新执行、过滤后用于 SFT。 |
| 策略更新之后 | 继续采样并训练 Actor / Critic。 | 跨 SFT 阶段保留 Critic,进入新 RPR 阶段先校准。 |
| 下一轮从哪里开始 | 用更新后的策略继续 PPO。 | 用更新后的策略重新发现经验,再进入 RSR → RPR。 |
2 RSR:把成功经验重做成示范
最直接的学习办法,是模仿成功尝试。但成功日志也可能包含许多弯路,或依赖特定框架的工具。照搬日志,未必能在部署环境里重现成功。
递归自我重写(Recursive Self-Rewrite,RSR)先把成功做法整理成计划,再在目标环境里重新执行。“重写”的是轨迹,合格轨迹随后才用于训练。
静态图
- 收集成功经验。当前模型在不同执行框架中尝试任务,只保留完整通过验证的轨迹。执行框架(harness)负责提供工具、维护交互历史、控制运行与结束条件。
- 提炼计划。规划角色(Planner)整理关键步骤、检查与恢复办法,形成操作说明 runbook。审查角色检查计划能否在目标环境使用,以及是否夹带答案。
- 重新执行。执行角色(Executor)带着计划,在干净的沙盒——隔离、可重新初始化的运行环境——里解决同一任务,实际调用工具并获得新的返回。
- 验收并训练。新轨迹通过任务验证与泄漏检查后,移除私有计划和审查对话,保留公开任务与真实交互。用这些示范更新负责行动的语言模型,即策略网络(Actor)。
最后一步叫监督微调(Supervised Fine-Tuning,SFT):给定任务和历史,提高模型生成示范行为的概率。私有计划帮助制作示范,训练则让模型在没有这份额外指导时也能行动。[1, §2.1]
Markdown 任务中的轨迹重写
RSR 前置论文给出一个处理 Markdown 的案例。语法解析器(parser)把文本识别为标题、段落、表格等结构,供后续代码处理。预编译 parser已经编译好,可在兼容环境中直接加载,省去部分生成与编译工作。
源执行花了 64 轮才成功。提炼后的计划把寻找兼容的预编译解析器作为起步策略,并提醒对齐输出摘要和表格处理参数。重做时,模型有机会少走弯路。
静态图
同一份计划产生了成功与失败两条分支。成功轨迹通过验收后,才进入 SFT 数据。
这项任务的完整重写结果
源执行在第 26 轮尝试生成 parser,第 51 轮使用预编译 parser;图中成功重写在第 9 轮就用上了它。这项任务共重写 12 次,7 次成功,成功重写的中位轮数为 32。来源:RSR §3.4。
模型逐个生成token——一个字、词的一部分或标点等文字单位。SFT 学习示范中的模型生成部分,工具日志作为输入,不作为模仿目标。决定哪些位置参与训练损失的选择标记,叫训练掩码(mask)。
同一模型的三种角色
RSR 前置论文让同一基础模型通过不同提示和上下文,分别承担规划、审查和执行。规划者整理做法,审查者检查计划,执行者调用工具完成任务。
制作示范时,Executor 可以参考私有计划;训练时,只留下公开任务与交互历史,并筛查每一步行为是否能由这些公开信息解释。来源:RSR §2。
对应的 SFT 目标
θ 表示模型的可训练参数;πθ(yⱼ | hⱼ) 表示参数为 θ 的策略网络,在历史 hⱼ 下生成示范 token yⱼ 的概率。负对数损失越小,这个示范选择的概率越大。
这里画的是单条示范的形式;训练还对示范集合取平均。hⱼ 是公开历史,yⱼ 是示范 token,mⱼ 选择需要学习的生成位置。最小化负对数概率,就是提高合格示范在相应历史下的生成概率。来源:式 3。
示范教会模型一种做法。面对自己新产生的尝试,它还需要从实际结果中学习。
3 RPR:验证结果怎样进入训练
递归策略精炼(Recursive Policy-Refine,RPR)让完成监督微调的 Actor 再次执行任务,用验证结果作为奖励,通过 PPO 更新策略。这里的 Critic 根据任务与交互历史预测后续回报,为优势估计提供基线。模型各自如何训练,见PPO:沿一条长程轨迹看训练。
Terminal-RSI 直接使用程序验收,没有另行训练偏好奖励模型;论文配置中的 KL 与熵系数均为 0。PPO 更新仍使用旧策略概率来控制策略变化。
静态图
一个 RPR 阶段会反复采样轨迹、验收并更新模型;阶段结束后,保留的 Actor 再进入下一轮 RSR。
奖励(reward,R)是把验证结果转换成的训练分数。论文从两项结果出发:P 是通过的语义检查数量,也就是与任务功能有关的检查,不含清理等辅助检查;S 是严格成功标记,完整通过为 1,否则为 0。分数在一次执行结束时计算,并非每条命令之后都重新发一次。
先前使用的家族稠密奖励(Family Dense Reward,FDR)用 P / D 表达部分完成程度,其中 D 是同类任务预先确定的尺度。这样“做对一些”也有信号。但部分检查得分本身,不一定把完整成功排在所有失败之前。
后续的成功优先奖励(Success-Prioritized Reward,SPR)因此改为 S + P / (P + D′)。后一项永远小于 1,完整成功则另加 1:失败在 1 分以下,成功至少 1 分。这里的 D′ 是按任务家族离线校准、在该阶段固定的正尺度。
通过 8 项检查、D′ = 10 时,未严格成功的奖励约为 0.444,严格成功则为 1.444。SPR 先区分是否完整成功,再比较部分完成程度。
奖励的精确公式、尺度和使用阶段
这里写的是检查报告有效时的形式;报告无效时都回退到严格成功标记 S。FDR 的 D 使用该任务家族历史检查总数的第 90 百分位,没有可用家族尺度时取 82。SPR 的 D′ 为离线校准的正数,在阶段内固定。
普通 RPR-2 分支使用 FDR,最终保留的 Long-Reward RPR-2 分支使用 SPR。来源:§2.2、§3.1。
RPR-1 的旧策略概率由训练端使用旧 Actor 重算,用于 PPO 的新旧概率比较。 [§2.4]
4 TB-GAE:按回合回传反馈
一次任务往往包含许多回合(turn):模型生成一段响应或工具调用,随后得到环境返回。奖励直到任务结束才出现,训练却需要为此前的每个生成位置计算优势。
广义优势估计(Generalized Advantage Estimation,GAE)结合沿途的奖励和价值预测,从末尾向前递推优势。递推时,后续反馈通常会逐步衰减,因而“多长算一步”会影响早先位置收到的信号。
若每经过一个文本 token 都乘一个小于 1 的系数,那么仅仅加长工具日志,也会削弱传到早先位置的反馈。关掉日志位置的训练损失还不够:日志可能仍参与优势递推,额外消耗了衰减。
回合边界跳过观测的广义优势估计(Turn-Boundary Skip-Observation Generalized Advantage Estimation,TB-GAE)从递推链里跳过工具观察;同一回合的生成 token 之间不衰减,跨回合时才衰减。
日志仍保留在模型的输入中,影响后续行动和价值预测。g₁ 到 g₄ 各自使用生成前的上下文,因而有不同的价值基线。训练系统可以在轨迹收集完成后,批量计算这些位置的预测。
日志长度对衰减的影响
一步之后,“即时奖励加上折扣后的后续预期”,与原先预期之差,叫时序差分残差(TD residual,δ)。下面只跟踪最后一个位置的这份变化,看看它传到第一个位置时还剩多少。
例子有两回合、四个生成位置。取 γ = 0.9,λ = 0.8,每次衰减乘 0.72。拖动日志长度,比较三条递推链。
0.72⁶0.72³0.72¹混合链跨 6 条边;只跳过观察跨 3 条边;TB-GAE 只跨 1 个回合边界。
图中数值是末端变化传回起点的系数。例子固定生成位置与价值等输入,只改变日志长度。勾选 γ = λ = 1,三条链的系数都为 1。
三条递推链与四个位置的逐步手算
设 g₁、g₂ 属于第一回合,g₃、g₄ 属于第二回合。四个位置生成前的价值依次为 0.2、0.4、0.5、0.8,只在任务结束时获得奖励 1。
先算各位置的时序差分残差,再从末尾逐步向前累积。
静态图
- g₄:任务结束。拿到 1 分,之前预计 0.8 分,所以 A₄ = 1 − 0.8 = 0.20。
- g₃:还在第二回合。价值从 0.5 变成 0.8,残差为 0.3;同轮不衰减,故 A₃ = (0.8 − 0.5) + 0.20 = 0.50。
- g₂:跨回第一回合。残差为 0.9 × 0.5 − 0.4 = 0.05;后续优势要乘 0.9 × 0.8,因此 A₂ = 0.05 + 0.72 × 0.50 = 0.41。
- g₁:还在第一回合。同轮继续不衰减,A₁ = (0.4 − 0.2) + 0.41 = 0.61。
| 生成位置 | 回合 | 原预测 v | 优势 A | 价值目标 Ĝ |
|---|---|---|---|---|
| g₁ | 1 | 0.20 | 0.61 | 0.81 |
| g₂ | 1 | 0.40 | 0.41 | 0.81 |
| g₃ | 2 | 0.50 | 0.50 | 1.00 |
| g₄ | 2 | 0.80 | 0.20 | 1.00 |
第一回合的两个位置共享 0.81 的回报目标,却分别有 0.61 和 0.41 的优势,因为它们原先的预期不同。Actor 用各位置的优势更新概率,Critic 拟合相应的回报目标。
把手算写成统一递推式
r̃ⱼ 表示分配到第 j 个生成位置的奖励;在这里的末尾奖励示例中,仅最后一个位置非零。
只枚举保留的生成位置 j,令 vⱼ 是该 token 生成前的价值。与下一生成位置同轮时,取 Γⱼ = Λⱼ = 1;跨轮时取 Γⱼ = γturn,Λⱼ = λturn。dⱼ = 1 表示真实终止。
终点奖励在最后一个生成位置注入一次。同轮无中间奖励时,相邻价值相互抵消,得到共同回报目标 Uₜ;每个位置分别扣除自己的价值预测,得到 Aⱼ = Uₜ − vⱼ。本例第一轮的目标为 U₁ = 0.9 × (0.5 + 0.8 × 0.5) = 0.81。
TB-GAE 以回合为折扣单位,让一次完整行动承担一次时间衰减。日志长度与回合内的 token 数由此从折扣步数中移除。来源:式 8–13及附录 C。
长轨迹被切成两块,边界该怎么办?
为节省显存或分批计算,可以把一条完整轨迹切成几块,再把后半块的递推结果传回前半块。若在本例 g₂ 后切开,只保留下一价值预测 0.5,却把下一优势 0.5 清零,A₂ 会变成 0.05,A₁ 变成 0.25,缺失的反馈正是 0.72 × 0.5 = 0.36。
完整续接需要同时传递后继价值与后继优势,或对完整序列统一反向计算。若采样提前停止、未来尚未执行,则用价值自举(bootstrap)——以当前价值预测估计未来回报。任务真正终止时,后继价值与优势取零。上下文摘要还会改变模型读取的历史,需要单独处理。
本节介绍 §2.3 提出的 TB-GAE。论文 §2.4 记录的第一阶段使用标准 GAE,γ = λ = 1。
5 新策略下的价值校准
RSR 改变了 Actor 的做法,旧 Critic 却还按之前的经验估分。若奖励公式也变了,分数尺度也随之改变。用旧预测计算新策略的优势,学习信号就可能失准。
Terminal-RSI 因而保留已有 Critic,在每个 RPR 阶段开头先冻结 Actor——暂时不更新其权重——做 4 次只更新 Critic 的校准,再联合训练。已有经验得以保留,价值预测也能跟上新的策略与奖励。
静态图
Critic 从首次预热到递归训练
- 首次预热。固定完成监督微调的 RSR-1 Actor,在科学工作流重建(Scientific Workflow Reconstruction,SWR)任务上训练 Critic 一个 epoch,即遍历一轮数据。这类任务要求重建可执行的科学程序,并用隐藏检查验证其行为。Actor 提供执行轨迹,Critic 学习相应的回报目标,得到原图中的 Warm-up Critic Model。
- 进入 RPR。每个 RPR 阶段先固定 Actor,做 4 次只更新 Critic 的校准;随后 Actor 用优势做 PPO 更新,Critic 用回报目标做价值回归,两套参数分别训练。
- 进入下一轮。RSR-2 只微调 Actor,保留 RPR-1 的 Critic 权重。到了 RPR-2,再按新 Actor 的执行结果校准 Critic,然后继续联合训练。
论文说明了上述预热和权重交接;首次预热之前,Critic 骨干加载的具体 checkpoint(模型权重文件)没有明确给出。来源:§2.1、§2.4与附录 A。
6 从局部测试到完整交付
任务要求用 Rust 和 C 写一个计算器。它有两道最终检查:删除构建产物后,入口脚本能否自动重新编译;面对隐藏输入时,输出能否与参考实现逐字一致。
初始模型 Base 的一条执行通过了本地算术测试,也做到了自动重建,却在最终检查里输出了 23,而参考输出是 23.0。数值相等,字符串仍不同。一个后期训练版本 RPR-2 的独立执行则通过了两道检查。
静态图
RPR-2 的这条执行组合了 Rust / C 组件与 Python 表达式求值,最终通过自动重建与输出格式两道检查。
五个模型版本在这项任务上各通过几次?
每个版本各评测两次,完整通过次数依次为 Base 0/2、RSR-1 0/2、RPR-1 1/2、RSR-2 2/2、RPR-2 2/2。
端末 Agent はコマンドラインでファイルを読み、コードを編集し、テストを実行する。一つの課題における対話記録全体を軌跡と呼ぶ。Terminal-RSI はこの経験を二つの学習に使う。RSR は成功した解法を再実行して手本を作り、RPR は新たな実行の検証結果から方策と価値ネットワークを学習する。
1 全体像:手本を学び、実行結果から学ぶ
Actor はコマンドやコードを生成する方策ネットワーク。Critic は、ある位置までの課題と対話履歴から、その先のリターンを予測する価値ネットワークだ。上段の RSR、下段の RPR の順に読む。検証器(verifier)は、チェックを実行して課題が完了したかを判定するプログラムである。
静的な図
A · RSR は手本を作る。検証済みの成功から計画を抽出し、初期化した環境で実行し直して、合格した新しい軌跡を残す。B · RPR は新しい試行を学習に使う。検証器の報酬と Critic の価値予測から、GAE / TB-GAE が各生成位置のアドバンテージ A とリターン目標 Ĝ を計算する。Actor は A で PPO 更新を行い、Critic は Ĝ に回帰する。
RSR によって Actor が変わるため、各 RPR 段階では Actor を固定して継承した Critic を校正してから共同学習へ進む。更新されたモデルが次の経験を作る。この循環が再帰的自己改善(Recursive Self-Improvement、RSI)である。
図は二つのアドバンテージ推定法を含む。§2.3 は TB-GAE を提案し、§2.4 に記載された最初の学習段階は標準 GAE、γ = λ = 1 を使う。
論文の原図と実際の学習順序を照らし合わせる
Figure 2 · Overall Framework ↗ Figure 3 · Single Recursive Step ↗
実際の段階順序は Base → RSR-1 → RPR-1 → RSR-2 → RPR-2です。原図上部の Actor: RSR-1 と Warm-up Critic Model は、最初にRPRへ入る時点で、方策は教師あり学習を、価値ネットワークは初回ウォームアップを完了していることを表します。
| 段階 | 通常のPPOループ | Terminal-RSI |
|---|---|---|
| 実行経験の使い方 | 軌跡をサンプリングし、収益からアドバンテージを計算して更新する。 | 検証済みの成功経験をさらに再実行し、選別してSFTに使う。 |
| 方策更新の後 | サンプリングとActor / Criticの学習を続ける。 | SFTをまたいでCriticを保持し、新しいRPR段階の冒頭で校正する。 |
| 次の反復の出発点 | 更新した方策でPPOを続ける。 | 更新した方策で経験を再び発見し、RSR → RPRに進む。 |
2 RSR:成功経験を再実行し、手本にする
最も直接的な学習方法は、成功した試行をまねることです。ただし成功ログにも遠回りが含まれ、特定の実行基盤のツールに依存する場合があります。そのまま写しても、配備先の環境で成功を再現できるとは限りません。
再帰的自己書き換え(Recursive Self-Rewrite、RSR)は、成功した方法を計画にまとめ、対象環境でもう一度実行します。「書き換える」のは軌跡であり、条件を満たした軌跡がその後の学習に使われます。
静的な図
- 成功経験を集める。現在のモデルが複数の実行基盤で課題を試み、検証に完全合格した軌跡だけを残します。実行基盤(harness)は、ツールの提供、対話履歴の管理、実行と終了条件の制御を担います。
- 計画を抽出する。Plannerが主要な手順、確認項目、復旧方法を整理し、手順書である runbookを作ります。Reviewerは対象環境で計画を使えるか、答えが紛れ込んでいないかを確認します。
- 再実行する。Executorは計画を参考に、クリーンなサンドボックス、つまり分離され再初期化できる実行環境で同じ課題を解きます。実際にツールを呼び出し、新たな応答を受け取ります。
- 検証し、学習する。新しい軌跡が課題の検証と漏洩検査に合格したら、非公開の計画とレビュー対話を除き、公開された課題と実際の対話を残します。この手本で、行動を担う言語モデルである方策ネットワーク(Actor)を更新します。
最後の段階を教師あり微調整(Supervised Fine-Tuning、SFT)と呼びます。課題と履歴を与えたときに、手本の行動を生成する確率を高めます。非公開の計画は手本を作るために使い、学習では追加の指示なしでも行動できるようにします。[1, §2.1]
Markdown課題の軌跡を書き換える
先行するRSR論文は、Markdownを処理する例を示しています。構文解析器(parser)は、後続のコードが扱えるよう、テキストから見出し、段落、表などの構造を認識します。コンパイル済みparserは、互換性のある環境ならそのまま読み込めるため、生成やコンパイルの作業を一部省けます。
元の実行は成功まで64ターンかかりました。抽出した計画では、互換性のあるコンパイル済みparserを探すことを最初の方針とし、出力要約と表処理のパラメータを合わせるよう促しています。やり直す際には、遠回りを減らせる可能性があります。
静的な図
同じ計画から、成功と失敗の両方の分岐が生まれました。成功した軌跡は、検証に合格して初めてSFTデータに入ります。
この課題の書き換え結果全体
元の実行では26ターン目にparserの生成を試み、51ターン目にコンパイル済みparserを使いました。図中の成功した書き換えでは、9ターン目で利用しています。この課題は計12回書き換えられ、そのうち7回が成功し、成功例のターン数中央値は32でした。出典:RSR §3.4。
モデルはtokenを1つずつ生成します。これは文字、単語の一部、句読点などのテキスト単位です。SFTは手本のうちモデルが生成した部分を学習し、ツールログは模倣対象ではなく入力として使います。どの位置を学習損失に含めるかを選ぶ印が学習マスク(mask)です。
同じモデルが担う3つの役割
先行するRSR論文では、同じ基盤モデルが異なるプロンプトと文脈を通じて、計画、レビュー、実行を担います。Plannerが方法を整理し、Reviewerが計画を確認し、Executorがツールを呼び出して課題を完了します。
手本を作る際、Executorは非公開の計画を参照できます。学習時には公開された課題と対話履歴だけを残し、各行動がその公開情報から説明できるかを検査します。出典:RSR §2。
対応するSFTの目的関数
θは学習可能なモデルパラメータです。πθ(yⱼ | hⱼ)は、パラメータθの方策が履歴hⱼの下で手本のtoken yⱼを生成する確率を表します。負の対数損失が小さいほど、その手本の選択確率は高くなります。
これは1つの手本に対する形です。学習では手本の集合全体でも平均を取ります。hⱼ は公開履歴、yⱼ は手本のtoken、mⱼ は学習対象の生成位置を選びます。負の対数確率を最小化すると、その履歴において検証済みの手本を生成する確率が高まります。出典:式3。
手本からモデルは方法を学びます。自ら新たに試みる際には、実際の結果からも学ぶ必要があります。
3 RPR:検証結果を学習信号にする
再帰的方策精練(Recursive Policy-Refine、RPR)では、教師あり微調整を終えたActorが課題を再び実行し、検証結果を報酬としてPPOで方策を更新します。ここでの Critic は、課題と対話履歴から将来の収益を予測し、アドバンテージ推定のベースラインを提供します。各モデルの学習方法は次の記事で説明しています:PPO:長期軌跡に沿って学習を見る。
Terminal-RSIはプログラムによる検証を直接使い、選好報酬モデルを別途学習しません。論文の設定では、KLとエントロピーの係数はいずれも0です。PPOの更新では、引き続き旧方策の確率を使って方策の変化を制御します。
静的な図
各 RPR 段階では軌跡の生成、検証、モデル更新を繰り返す。段階が終わると、継承した Actor が次の RSR に入る。
報酬(reward、R)は、検証結果を学習用の点数に変換したものです。論文では2つの値を用います。P は合格した意味的検査の数です。課題の機能に関する検査を指し、後片付けなどの補助検査は含みません。S は厳密な成功の印で、完全合格なら1、それ以外は0です。点数は実行の終了時に計算され、各コマンドの直後に毎回付与されるものではありません。
先に使われた課題ファミリー別の密な報酬(Family Dense Reward、FDR)は、 P / D で部分的な達成度を表します。 D は同じ課題ファミリーで事前に定める尺度です。これにより、一部を正しくできた場合にも信号が得られます。ただし部分検査の点数だけでは、完全成功があらゆる失敗より上に並ぶとは限りません。
そこで、後の成功優先報酬(Success-Prioritized Reward、SPR)は S + P / (P + D′)を使います。後半の項は常に1未満で、完全成功にはさらに1を加えます。失敗は1点未満、成功は少なくとも1点になります。ここで D′ は課題ファミリーごとにオフラインで校正され、その段階内では固定される正の尺度です。
8項目に合格しD′ = 10の場合、厳密な成功でなければ報酬は約0.444、厳密な成功なら1.444です。SPRはまず完全成功かどうかを分け、その後で部分的な達成度を比べます。
報酬の正確な式、尺度、適用段階
ここでの式は検査レポートが有効な場合のものです。無効な場合は、どちらも厳密な成功の印Sに戻ります。FDRのDは、その課題ファミリーの過去の検査総数の第90百分位で、利用できるファミリー尺度がなければ82を使います。SPRのD′はオフラインで校正された正数で、段階内では固定です。
通常のRPR-2分岐はFDRを使い、最終的に採用されたLong-Reward RPR-2分岐はSPRを使います。出典:§2.2、§3.1。
RPR-1では、学習側が旧Actorを使って旧方策の確率を再計算し、PPOの新旧確率の比較に用います。 [§2.4]
4 TB-GAE:ターン単位でフィードバックを遡らせる
1つの課題には、多くのターン(turn)が含まれます。モデルが応答やツール呼び出しを生成し、環境から結果を受け取る単位です。報酬は課題の終了時にしか得られませんが、学習には、それ以前の各生成位置のアドバンテージが必要です。
一般化アドバンテージ推定(Generalized Advantage Estimation、GAE)は、軌跡上の報酬と価値予測を組み合わせ、末尾から逆向きにアドバンテージを再帰計算します。後から得たフィードバックは通常、伝わる過程で減衰するため、「何を1ステップと数えるか」が前の位置に届く信号を左右します。
各テキストtokenを通るたびに1未満の係数を掛けると、ツールログを長くするだけで、前の位置に届くフィードバックが弱くなります。ログ位置の学習損失を無効にするだけでは十分ではありません。ログがアドバンテージの再帰計算に残り、追加の減衰を生むことがあるためです。
ターン境界・観測スキップ型一般化アドバンテージ推定(Turn-Boundary Skip-Observation Generalized Advantage Estimation、TB-GAE)は、再帰計算からツールの観測を飛ばします。同じターン内の生成token間では減衰せず、ターン境界を越えるときだけ減衰させます。
ログはモデルの入力には残り、次の行動や価値予測に影響します。g₁からg₄はそれぞれ生成直前の文脈を使うため、価値のベースラインが異なります。学習システムは、軌跡の収集後に各位置の予測をまとめて計算できます。
ログの長さが減衰に与える影響
1ステップ後の「即時報酬と割引後の将来予測の和」から、元の予測を引いた差を時間差分残差(TD residual、δ)と呼びます。以下では最後の位置でのこの変化だけを追い、最初の位置までどれだけ届くかを見ます。
例は2ターン、4つの生成位置です。 γ = 0.9,λ = 0.8とし、減衰のたびに 0.72を掛けます。ログ長を動かして、3種類の再帰計算を比べてください。
0.72⁶0.72³0.72¹混合列は6本の辺、観測だけを飛ばす列は3本の辺、TB-GAEは1つのターン境界を越えます。
表示値は、末端の変化を起点へ伝える係数です。生成位置や価値などの入力は固定し、ログ長だけを変えています。γ = λ = 1にチェックすると、3種類とも係数は1になります。
3種類の再帰計算と4位置の手計算
g₁、g₂を第1ターン、g₃、g₄を第2ターンとします。各位置の生成直前の価値は順に0.2、0.4、0.5、0.8で、課題終了時だけ報酬1を得ます。
まず各位置の時間差分残差を求め、末尾から順に遡って累積します。
静的な図
- g₄:課題終了。報酬は1、事前予測は0.8なので、 A₄ = 1 − 0.8 = 0.20。
- g₃:まだ第2ターン。価値が0.5から0.8へ変わるため、残差は0.3です。同じターンでは減衰しないので、 A₃ = (0.8 − 0.5) + 0.20 = 0.50。
- g₂:第1ターンへ戻る。残差は 0.9 × 0.5 − 0.4 = 0.05です。後続のアドバンテージには 0.9 × 0.8を掛けるため、 A₂ = 0.05 + 0.72 × 0.50 = 0.41。
- g₁:まだ第1ターン。同じターンなので引き続き減衰せず、A₁ = (0.4 − 0.2) + 0.41 = 0.61。
| 生成位置 | ターン | 元の予測 v | アドバンテージ A | 価値目標 Ĝ |
|---|---|---|---|---|
| g₁ | 1 | 0.20 | 0.61 | 0.81 |
| g₂ | 1 | 0.40 | 0.41 | 0.81 |
| g₃ | 2 | 0.50 | 0.50 | 1.00 |
| g₄ | 2 | 0.80 | 0.20 | 1.00 |
第1ターンの2位置は0.81という収益目標を共有しますが、事前予測が異なるため、アドバンテージは0.61と0.41です。Actorは各位置のアドバンテージで確率を更新し、Criticは対応する収益目標に適合させます。
手計算を共通の再帰式で表す
r̃ⱼは第j生成位置に割り当てる報酬です。この終端報酬の例では、最後の位置だけが非ゼロです。
保持する生成位置だけに jを付け、 vⱼ をそのtokenの生成直前の価値とします。次の生成位置が同じターンなら Γⱼ = Λⱼ = 1、ターンをまたぐなら Γⱼ = γturn,Λⱼ = λturn。dⱼ = 1 は実際の終端を表します。
終端報酬は最後の生成位置に1回だけ入れます。同じターン内に中間報酬がなければ、隣り合う価値が相殺され、共通の収益目標Uₜが得られます。各位置で自分の価値予測を引くと、Aⱼ = Uₜ − vⱼです。この例の第1ターンの目標はU₁ = 0.9 × (0.5 + 0.8 × 0.5) = 0.81です。
TB-GAEはターンを割引の単位とし、完結した1回の行動に1回の時間減衰を適用します。これにより、ログ長とターン内のtoken数が割引ステップ数から外れます。出典:式8–13および付録C。
長い軌跡を分割したら、境界をどう扱うか
GPUメモリを節約したり分割計算したりするため、完全な軌跡を複数の区間に分け、後半の再帰計算結果を前半へ渡すことがあります。この例をg₂の後で切り、次の価値予測0.5だけを残して次のアドバンテージ0.5をゼロにすると、A₂は0.05、A₁は0.25になります。失われるフィードバックは、ちょうど0.72 × 0.5 = 0.36です。
完全に接続するには、後続の価値とアドバンテージを両方渡すか、全系列をまとめて逆向きに計算する必要があります。サンプリングが早く止まり、将来部分をまだ実行していない場合には、価値のブートストラップ(bootstrap)を使い、現在の価値予測で将来の収益を見積もります。課題が実際に終了した場合、後続の価値とアドバンテージはゼロです。文脈の要約もモデルが読む履歴を変えるため、別途扱う必要があります。
この節は§2.3で提案されたTB-GAEを説明しています。論文§2.4に記載された第1段階は、γ = λ = 1の標準GAEを使います。
5 新しい方策に合わせて価値を校正する
RSRでActorの行動は変わりますが、旧Criticは以前の経験で評価を続けます。報酬式も変われば、点数の尺度も変わります。古い予測で新方策のアドバンテージを計算すると、学習信号がずれる場合があります。
そこでTerminal-RSIは既存のCriticを引き継ぎ、各RPR段階の冒頭でActorを凍結します。つまりActorの重みを一時的に更新せず、Criticだけを4回校正してから共同学習に移ります。既存の経験を残しつつ、価値予測を新しい方策と報酬に合わせます。
静的な図
Criticの初回ウォームアップから再帰学習まで
- 初回ウォームアップ。教師あり微調整を終えた RSR-1 Actor を固定し、科学ワークフロー再構築(Scientific Workflow Reconstruction、SWR)課題で Critic を 1 epoch、つまりデータを 1 巡して学習する。この課題では実行可能な科学プログラムを再構築し、隠されたチェックで動作を検証する。Actor が軌跡を供給し、Critic がリターン目標を学ぶ。その結果が原図の Warm-up Critic Model である。
- RPRに入る。各RPR段階の最初にActorを固定し、Criticだけを4回校正します。その後、Actorはアドバンテージを用いてPPOで更新し、Criticは収益目標への価値回帰を行います。2組のパラメータはそれぞれ学習されます。
- 次の反復に入る。RSR-2はActorだけを微調整し、RPR-1のCriticの重みを保持します。RPR-2では新しいActorの実行結果でCriticを校正し、その後に共同学習を続けます。
論文はこのウォームアップと重みの引き継ぎを説明していますが、初回ウォームアップ前にCriticの基盤へ読み込む具体的なcheckpoint、つまりモデル重みファイルは明記していません。出典:§2.1、§2.4と付録A。
6 局所テストから課題全体の達成へ
課題は Rust と C で計算機を作ること。最終検証は二つある。ビルド成果物を削除しても起動スクリプトが自動で再コンパイルできるか、そして未知の入力に対する出力が参照実装と一字一句一致するかを確認する。
初期モデルBaseのある実行は、ローカルの計算テストと自動再ビルドには成功しました。しかし最終検証の出力は 23で、参照出力は 23.0でした。数値は同じでも、文字列は異なります。後の学習段階のRPR-2による独立した実行は、両方の検証に合格しました。
静的な図
この RPR-2 の実行は Rust / C の部品と Python の式評価を組み合わせ、自動再ビルドと出力形式の両検証に合格した。
5つのモデル版は、この課題にそれぞれ何回合格したか
各版を2回ずつ評価した結果、完全合格はBase 0/2、RSR-1 0/2、RPR-1 1/2、RSR-2 2/2、RPR-2 2/2でした。
PPO, Along a Long-Horizon TrajectoryPPO:沿一条长程轨迹看训练長い実行軌跡から理解するPPO →
Figures and code图与代码図とコード
Download article and figure sources (JSON)下载正文与可编辑图源(JSON)本文と編集可能な図のソースをダウンロード(JSON)