完成时间:2026-02-04
本次重构移除了旧的 branch 概念,采用统一的 Trace 模型,每个 Sub-Agent 都是完全独立的 Trace。
将基于 branch 的设计重构为基于独立 Trace 的设计:
.trace/{trace_id}/branches/{branch_id}/.trace/{parent_id}@{mode}-{timestamp}-{seq}/agent/execution/trace_id.py
generate_trace_id() - 生成主 Trace UUIDgenerate_sub_trace_id(parent_id, mode) - 生成 Sub-Trace IDparse_parent_trace_id(trace_id) - 解析父 Trace IDis_sub_trace(trace_id) - 判断是否为 Sub-Traceextract_mode(trace_id) - 提取运行模式tests/test_trace_id.pyagent/execution/models.py)parent_trace_id: Optional[str] 字段parent_goal_id: Optional[str] 字段to_dict() 方法context: Dict[str, Any] 字段存在agent/execution/models.py)branch_id 字段create() 方法签名to_dict() 方法agent/goal/models.py)branch_id 字段branch_ids 字段GoalType 从 "explore_start" | "explore_merge" 改为 "normal" | "agent_call"sub_trace_ids: Optional[List[str]] 字段agent_call_mode: Optional[str] 字段explore_start_id, merge_summary, selected_branch 字段to_dict() 和 from_dict() 方法agent/goal/models.py 删除 BranchContext 类agent/goal/__init__.py 移除导出BranchStatus 类型定义agent/execution/fs_store.py)移除的方法(11 个):
_get_branches_dir()_get_branch_dir()_get_branch_meta_file()_get_branch_goal_file()_get_branch_messages_dir()create_branch()get_branch()get_branch_goal_tree()update_branch_goal_tree()update_branch()list_branches()更新的方法:
create_trace() - 不再创建 branches/ 目录add_message() - 移除 branch_id 逻辑_update_goal_stats() - 移除 branch_id 逻辑_get_affected_goals() - 移除 branch_id 逻辑get_trace_messages() - 移除 branch_id 参数get_messages_by_goal() - 移除 branch_id 参数update_message() - 移除 branch_id 逻辑get_message() - 不再扫描 branches/ 目录更新的导入:
from agent.goal.models import GoalTree, Goal, BranchContext, GoalStats
改为 from agent.goal.models import GoalTree, Goal, GoalStatsagent/execution/protocols.py)移除的方法签名(6 个):
create_branch()get_branch()get_branch_goal_tree()update_branch_goal_tree()update_branch()list_branches()更新的方法签名:
get_trace_messages() - 移除 branch_id 参数get_messages_by_goal() - 移除 branch_id 参数更新的导入:
from agent.goal.models import GoalTree, Goal, BranchContext
改为 from agent.goal.models import GoalTree, Goal2f8d3a1c-4b6e-4f9a-8c2d-1e5b7a9f3c4d
2f8d3a1c-4b6e-4f9a-8c2d-1e5b7a9f3c4d@explore-20260204220012-001
{parent_id}@{mode}-{timestamp}-{seq}✅ 零碰撞风险:使用完整 UUID ✅ 可精确追溯:从 Sub-Trace ID 直接看到完整父 ID ✅ 无需冲突检测:实现简单,不依赖外部状态 ✅ 信息完整:一眼看出触发者、模式、时间 ✅ 线程安全:序号生成器使用锁保护
.trace/
├── abc123/
│ ├── meta.json
│ ├── goal.json
│ ├── messages/
│ ├── branches/ ❌ 已移除
│ │ ├── A/
│ │ └── B/
│ └── events.jsonl
.trace/
├── 2f8d3a1c-4b6e-4f9a-8c2d-1e5b7a9f3c4d/ # 主 Trace
│ ├── meta.json # parent_trace_id: null
│ ├── goal.json
│ ├── messages/
│ └── events.jsonl
│
├── 2f8d3a1c...@explore-20260204220012-001/ # Sub-Trace A
│ ├── meta.json # parent_trace_id: "2f8d3a1c..."
│ ├── goal.json # 独立的 GoalTree
│ ├── messages/
│ └── events.jsonl
│
└── 2f8d3a1c...@explore-20260204220012-002/ # Sub-Trace B
└── ...
python3 -c "from agent.execution.fs_store import FileSystemTraceStore"
# ✅ 成功
fs_store.py 中添加 goal_added 事件fs_store.py 中添加 goal_updated 事件fs_store.py 中添加 goal_completed 事件agent/goal/explore.py - explore 工具agent/goal/delegate.py - delegate 工具sub_trace_started 和 sub_trace_completed 事件agent/execution/api.py REST 端点
BranchDetailResponse 模型TraceDetailResponse 使用 sub_tracesget_trace() 端点查询 Sub-Tracesbranch_id 参数/branches/{branch_id} 端点agent/execution/websocket.py 事件格式
connected 事件:查询 Sub-Traces 而非 branchesbroadcast_branch_started()、broadcast_branch_goal_added()、broadcast_branch_completed()、broadcast_explore_completed() 函数broadcast_sub_trace_started() 和 broadcast_sub_trace_completed() 函数docs/trace-api.md - 完整重写,移除所有 branch 引用docs/decisions.md - 更新 explore 工具描述docs/context-comparison.md - 更新执行流程描述frontend/API.md - 更新 Trace ID 格式,移除 branch_id 字段agent/execution/protocols.py - 移除注释中的 branch 引用agent/execution/trace_id.py - Trace ID 生成工具tests/test_trace_id.py - 单元测试agent/goal/explore.py - explore 工具实现agent/goal/delegate.py - delegate 工具实现agent/execution/models.py - Trace 和 Message 模型agent/goal/models.py - Goal 模型agent/goal/__init__.py - 导出列表agent/execution/fs_store.py - 存储实现agent/execution/protocols.py - 协议定义agent/execution/api.py - REST API 端点agent/execution/websocket.py - WebSocket 事件docs/context-management.md - 设计文档docs/refactor-plan.md - 重构计划BranchContext 类BranchStatus 类型Message.branch_id 字段Goal.branch_id 字段Goal.branch_ids 字段Goal.explore_start_id 字段Goal.merge_summary 字段Goal.selected_branch 字段.trace/ 目录中的旧数据(包含 branches/)如需使用,需要手动处理BranchContext 或 branch_id 的地方需要更新sub_trace_started/sub_trace_completed 替代旧的 branch 事件)本次重构已全面完成从 branch 概念到统一 Trace 模型的迁移:
重构已全部完成,系统已经可以正常使用新的统一 Trace 模型。