fix(agent): 回退时恢复会话级附加目录的代码文件#456
Merged
Merged
Conversation
rewindSession 收集 attachedDirs 时只取了工作区级目录,遗漏了 sessionMeta.attachedDirectories 与 attachedFiles,导致 rewindFilesFromSnapshot 中 allowedDirs 判断这些绝对路径越界并 continue 跳过,备份存在却不写回——表现为"会话记录回退了,代码没回退"。 抽取 collectAttachedDirectories 聚合临时附加 + 会话级 + 工作区级所有来源,sendMessage 与 rewindSession 共用同一份聚合逻辑,保证 SDK 写 file-history-snapshot 的目录范围与回退时校验的目录范围始终一致。
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.
Summary
修复 Agent 模式下「回退到此处」会话记录回退了、但会话级附加目录里的代码文件没有跟随回退的问题。
根因
发消息(
sendMessage)传给 SDK 的additionalDirectories包含 5 类来源(用户当次输入、会话级附加目录/文件、工作区级附加目录/文件、工作区文件目录),SDK 据此用绝对路径在file-history-snapshot中记录这些目录里被读写的文件备份。但
rewindSession收集rewindAttachedDirs时只取了工作区级附加目录 + 工作区文件目录,遗漏了:sessionMeta.attachedDirectories(会话级附加目录 ← 用户最常用的代码目录)sessionMeta.attachedFiles的父目录getWorkspaceAttachedFiles(workspaceSlug)的父目录rewindFilesFromSnapshot中的路径越界保护(agent-session-manager.ts:1078)发现备份文件的绝对路径不在allowedDirs内,就console.warn('rewindFiles: 拒绝路径越界 ...')然后continue静默跳过——备份明明存在却不写回。修复
抽取
collectAttachedDirectories(...)统一聚合三类来源(临时附加 + 会话级 + 工作区级),sendMessage与rewindSession共用同一份逻辑,保证 SDK 写入快照时使用的目录范围与回退时校验的目录范围始终一致,不会再因为漂移而被越界保护误伤。Test plan
bun run typecheck全包通过rewindFiles: 拒绝路径越界 ...,且rewindFiles: 恢复 /path/to/file ← backup-xxx实际执行,文件内容回到目标快照状态🤖 Generated with Claude Code