feat: 统一 Agent 失败收尾与文件回滚流程#47
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
此前 Agent 的正常结束和异常结束分别走不同的处理路径。
达到最大迭代次数、验证不通过等非异常失败可以触发文件回滚,但模型、工具链或验证器抛出异常时会直接进入
catch并返回,可能跳过文件回滚、Memory 保存和部分终止处理。此外,原有回滚逻辑依赖已经创建的 checkpoint,存在以下问题:
本次改动
1. 统一 Agent 收尾流程
将原有运行逻辑拆分为:
executeRun:负责工作区准备、模型调用、工具执行和结果验证。finalizeRun:负责文件回滚、最终状态计算、Memory 保存、结束中间件、终止事件和资源清理。模型、工具、验证和 Abort 等所有退出路径现在都会进入统一的
finalizeRun,避免异常路径绕过必要的收尾逻辑。2. 改为任务级文件回滚
ChangeTracker 现在以整个 Agent Run 为单位维护文件基线:
新增
rollbackAll,不再通过倒序执行多个累计 checkpoint 完成回滚。Checkpoint 继续用于记录修改过程、生成 Trace 和向 UI 展示 Diff,但不再作为实际回滚指令。
3. 支持未创建 checkpoint 的修改回滚
回滚逻辑现在直接读取当前 ChangeTracker 中的累计变化,不再依赖
run.changes。因此以下场景也能够正确恢复文件: