feat(checkpoint):增加上下文回滚功能#547
Conversation
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: phantom5099 <245659304+phantom5099@users.noreply.github.com>
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
|
@xgopilot 给我几条comment中提到问题的修复方案 |
Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: phantom5099 <245659304+phantom5099@users.noreply.github.com>
test(checkpoint): 补充回滚链路覆盖
Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: phantom5099 <245659304+phantom5099@users.noreply.github.com>
test(checkpoint): 补充 checkpoint 覆盖
|
/review 找出纰漏,并分析与#521的偏差 |
|
|
||
| // Checkpoint 基础设施:影子仓库 + SQLite checkpoint 存储 | ||
| // 复用 sessionStore 的 *sql.DB 连接,避免 Windows 上多连接文件锁定。 | ||
| sessionDB := sessionStore.DB() |
There was a problem hiding this comment.
sessionStore.DB() is still nil until the store has performed its first ensureDB-backed operation, and BuildGatewayServerDeps has not touched the store yet. On a fresh startup this leaves checkpointStore unset, so checkpoint wiring/repair never runs and the whole feature is silently disabled. Please open the DB explicitly here or fall back to constructing the checkpoint store from DatabasePath(...).
There was a problem hiding this comment.
已修复 internal/app/bootstrap.go 冷启动时 sessionStore.DB() 仍为 nil 导致 checkpoint store 未初始化的问题。
本次修改:
BuildGatewayServerDeps现在会默认按session.DatabasePath(...)构造 checkpoint store,避免冷启动时整条 checkpoint 链路被静默跳过。- 如果 session store 已经打开了 SQLite 连接,仍然优先复用这条连接,保持原有的共享连接路径。
- 新增回归测试
TestBuildGatewayServerDepsInitializesCheckpointStoreOnFreshStartup,验证 fresh startup 下 runtime 的ListCheckpoints能正常工作。
验证:
go test ./internal/app -run 'TestBuildGatewayServerDepsInitializesCheckpointStoreOnFreshStartup|TestBuildRuntimeUsesWorkdirOverride'
代码已推送到当前工作分支,并同步创建了 fork 侧 PR:https://github.com/phantom5099/neo-code/pull/70
背景问题
NeoCode 缺乏代码回退、会话回滚与运行恢复的统一机制。用户让 agent 修改代码后发现方向错误时,只能依赖自身的 Git 或手动撤销,且会话中的 transcript、todo、plan 等状态仍保留已污染内容。具体缺口:
方案概要
三层恢复模型
Per-Turn Checkpoint
每轮 turn 开始时自动创建 checkpoint,确保用户可回退到任意一轮开始前的状态:
Restore 流程
git diff --name-status比较目标 commit 与当前工作区)pre_restore_guard快照(供 undo 使用)git checkout <hash> -- .)restoredUndo 机制
查询最近的
pre_restore_guardcheckpoint,递归调用 RestoreCheckpoint(Force=true),形成可逆链路。ResumeCheckpoint
在 phase 转换点(plan → execute → verify → stopped)持久化运行位置,每个 session 仅保留最新一条。进程重启后据此恢复精确运行状态,不再依赖猜测。
保留策略
pre_restore_guard始终可恢复降级模式
Git 不可用时降级为 session-only 模式:创建会话快照、可 restore/undo,仅跳过代码快照。reason 标记为
pre_write_degraded。ShadowRepo 自愈
Init 时执行
rev-parse --git-dir健康检查,损坏则 rename 旧目录为 .bak 并重建。启动时检查所有 checkpoint 的代码引用,丢失的标记为broken。修改范围
核心模块
internal/checkpoint/checkpoint_manager.gointernal/checkpoint/shadow_repo.gointernal/session/checkpoint_types.goRuntime
internal/runtime/checkpoint_restore.gointernal/runtime/checkpoint_resume.gointernal/runtime/checkpoint_gate.gointernal/runtime/run.gointernal/runtime/compact.gointernal/runtime/events.goGateway
internal/gateway/types.gointernal/gateway/contracts.gointernal/gateway/registry.gointernal/gateway/bootstrap.goBootstrap
internal/app/bootstrap.go其他
internal/cli/gateway_runtime_bridge.go预期收益