goal_models.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. """
  2. Goal 数据模型
  3. Goal: 执行计划中的目标节点
  4. GoalTree: 目标树,管理整个执行计划
  5. GoalStats: 目标统计信息
  6. """
  7. from dataclasses import dataclass, field
  8. from datetime import datetime
  9. from typing import Dict, Any, List, Optional, Literal
  10. import json
  11. # Goal 状态
  12. GoalStatus = Literal["pending", "in_progress", "completed", "abandoned"]
  13. # Goal 类型
  14. GoalType = Literal["normal", "agent_call"]
  15. @dataclass
  16. class GoalStats:
  17. """目标统计信息"""
  18. message_count: int = 0 # 消息数量
  19. total_tokens: int = 0 # Token 总数
  20. total_cost: float = 0.0 # 总成本
  21. preview: Optional[str] = None # 工具调用摘要,如 "read_file → edit_file → bash"
  22. def to_dict(self) -> Dict[str, Any]:
  23. return {
  24. "message_count": self.message_count,
  25. "total_tokens": self.total_tokens,
  26. "total_cost": self.total_cost,
  27. "preview": self.preview,
  28. }
  29. @classmethod
  30. def from_dict(cls, data: Dict[str, Any]) -> "GoalStats":
  31. return cls(
  32. message_count=data.get("message_count", 0),
  33. total_tokens=data.get("total_tokens", 0),
  34. total_cost=data.get("total_cost", 0.0),
  35. preview=data.get("preview"),
  36. )
  37. @dataclass
  38. class Goal:
  39. """
  40. 执行目标
  41. 使用扁平列表 + parent_id 构建层级结构。
  42. agent_call 类型用于标记启动了 Sub-Trace 的 Goal。
  43. """
  44. id: str # 内部唯一 ID,纯自增("1", "2", "3"...)
  45. description: str # 目标描述
  46. reason: str = "" # 创建理由(为什么做)
  47. parent_id: Optional[str] = None # 父 Goal ID(层级关系)
  48. type: GoalType = "normal" # Goal 类型
  49. status: GoalStatus = "pending" # 状态
  50. summary: Optional[str] = None # 完成/放弃时的总结
  51. # agent_call 特有
  52. sub_trace_ids: Optional[List[Dict[str, str]]] = None # 启动的 Sub-Trace 信息 [{"trace_id": "...", "mission": "..."}]
  53. agent_call_mode: Optional[str] = None # "explore" | "delegate" | "sequential"
  54. sub_trace_metadata: Optional[Dict[str, Dict[str, Any]]] = None # Sub-Trace 元数据
  55. # 统计(后端维护,用于可视化边的数据)
  56. self_stats: GoalStats = field(default_factory=GoalStats) # 自身统计(仅直接关联的 messages)
  57. cumulative_stats: GoalStats = field(default_factory=GoalStats) # 累计统计(自身 + 所有后代)
  58. # 相关知识(自动检索注入)
  59. knowledge: Optional[List[Dict[str, Any]]] = None # 相关知识列表
  60. created_at: datetime = field(default_factory=datetime.now)
  61. def to_dict(self) -> Dict[str, Any]:
  62. """转换为字典"""
  63. return {
  64. "id": self.id,
  65. "description": self.description,
  66. "reason": self.reason,
  67. "parent_id": self.parent_id,
  68. "type": self.type,
  69. "status": self.status,
  70. "summary": self.summary,
  71. "sub_trace_ids": self.sub_trace_ids,
  72. "agent_call_mode": self.agent_call_mode,
  73. "sub_trace_metadata": self.sub_trace_metadata,
  74. "self_stats": self.self_stats.to_dict(),
  75. "cumulative_stats": self.cumulative_stats.to_dict(),
  76. "knowledge": self.knowledge,
  77. "created_at": self.created_at.isoformat() if self.created_at else None,
  78. }
  79. @classmethod
  80. def from_dict(cls, data: Dict[str, Any]) -> "Goal":
  81. """从字典创建"""
  82. created_at = data.get("created_at")
  83. if isinstance(created_at, str):
  84. created_at = datetime.fromisoformat(created_at)
  85. self_stats = data.get("self_stats", {})
  86. if isinstance(self_stats, dict):
  87. self_stats = GoalStats.from_dict(self_stats)
  88. cumulative_stats = data.get("cumulative_stats", {})
  89. if isinstance(cumulative_stats, dict):
  90. cumulative_stats = GoalStats.from_dict(cumulative_stats)
  91. return cls(
  92. id=data["id"],
  93. description=data["description"],
  94. reason=data.get("reason", ""),
  95. parent_id=data.get("parent_id"),
  96. type=data.get("type", "normal"),
  97. status=data.get("status", "pending"),
  98. summary=data.get("summary"),
  99. sub_trace_ids=data.get("sub_trace_ids"),
  100. agent_call_mode=data.get("agent_call_mode"),
  101. sub_trace_metadata=data.get("sub_trace_metadata"),
  102. self_stats=self_stats,
  103. cumulative_stats=cumulative_stats,
  104. knowledge=data.get("knowledge"),
  105. created_at=created_at or datetime.now(),
  106. )
  107. @dataclass
  108. class GoalTree:
  109. """
  110. 目标树 - 管理整个执行计划
  111. 使用扁平列表 + parent_id 构建层级结构
  112. """
  113. mission: str # 总任务描述
  114. goals: List[Goal] = field(default_factory=list) # 扁平列表(通过 parent_id 构建层级)
  115. current_id: Optional[str] = None # 当前焦点 goal ID
  116. _next_id: int = 1 # 内部 ID 计数器(私有字段)
  117. created_at: datetime = field(default_factory=datetime.now)
  118. def find(self, goal_id: str) -> Optional[Goal]:
  119. """按 ID 查找 Goal"""
  120. for goal in self.goals:
  121. if goal.id == goal_id:
  122. return goal
  123. return None
  124. def find_by_display_id(self, display_id: str) -> Optional[Goal]:
  125. """按显示 ID 查找 Goal(如 "1", "2.1", "2.2")"""
  126. for goal in self.goals:
  127. if self._generate_display_id(goal) == display_id:
  128. return goal
  129. return None
  130. def find_parent(self, goal_id: str) -> Optional[Goal]:
  131. """查找指定 Goal 的父节点"""
  132. goal = self.find(goal_id)
  133. if not goal or not goal.parent_id:
  134. return None
  135. return self.find(goal.parent_id)
  136. def get_children(self, parent_id: Optional[str]) -> List[Goal]:
  137. """获取指定父节点的所有子节点"""
  138. return [g for g in self.goals if g.parent_id == parent_id]
  139. def get_current(self) -> Optional[Goal]:
  140. """获取当前焦点 Goal"""
  141. if self.current_id:
  142. return self.find(self.current_id)
  143. return None
  144. def _generate_id(self) -> str:
  145. """生成新的 Goal ID(纯自增)"""
  146. new_id = str(self._next_id)
  147. self._next_id += 1
  148. return new_id
  149. def _generate_display_id(self, goal: Goal) -> str:
  150. """生成显示序号(1, 2, 2.1, 2.2...)"""
  151. if not goal.parent_id:
  152. # 顶层目标:找到在同级中的序号
  153. siblings = [g for g in self.goals if g.parent_id is None and g.status != "abandoned"]
  154. try:
  155. index = [g.id for g in siblings].index(goal.id) + 1
  156. return str(index)
  157. except ValueError:
  158. return "?"
  159. else:
  160. # 子目标:父序号 + "." + 在同级中的序号
  161. parent = self.find(goal.parent_id)
  162. if not parent:
  163. return "?"
  164. parent_display = self._generate_display_id(parent)
  165. siblings = [g for g in self.goals if g.parent_id == goal.parent_id and g.status != "abandoned"]
  166. try:
  167. index = [g.id for g in siblings].index(goal.id) + 1
  168. return f"{parent_display}.{index}"
  169. except ValueError:
  170. return f"{parent_display}.?"
  171. def _find_next_pending_goal(self, completed_goal_id: str) -> Optional[Goal]:
  172. """
  173. 完成 goal 后,自动查找下一个应该执行的 pending goal
  174. 查找顺序:
  175. 1. 同级的下一个 pending goal
  176. 2. 父级的下一个 pending goal
  177. 3. 任意顶层 pending goal
  178. Args:
  179. completed_goal_id: 刚完成的 goal ID
  180. Returns:
  181. 下一个 pending goal,如果没有则返回 None
  182. """
  183. completed_goal = self.find(completed_goal_id)
  184. if not completed_goal:
  185. return None
  186. # 1. 查找同级的下一个 pending goal
  187. siblings = self.get_children(completed_goal.parent_id)
  188. found_current = False
  189. for sibling in siblings:
  190. if sibling.id == completed_goal_id:
  191. found_current = True
  192. continue
  193. if found_current and sibling.status == "pending":
  194. return sibling
  195. # 2. 如果有父级,查找父级的下一个 pending goal
  196. if completed_goal.parent_id:
  197. return self._find_next_pending_goal(completed_goal.parent_id)
  198. # 3. 查找任意顶层 pending goal
  199. for goal in self.goals:
  200. if goal.parent_id is None and goal.status == "pending":
  201. return goal
  202. return None
  203. def add_goals(
  204. self,
  205. descriptions: List[str],
  206. reasons: Optional[List[str]] = None,
  207. parent_id: Optional[str] = None
  208. ) -> List[Goal]:
  209. """
  210. 添加目标
  211. 如果 parent_id 为 None,添加到顶层
  212. 如果 parent_id 有值,添加为该 goal 的子目标
  213. """
  214. if parent_id:
  215. parent = self.find(parent_id)
  216. if not parent:
  217. raise ValueError(f"Parent goal not found: {parent_id}")
  218. # 创建新目标
  219. new_goals = []
  220. for i, desc in enumerate(descriptions):
  221. goal_id = self._generate_id()
  222. reason = reasons[i] if reasons and i < len(reasons) else ""
  223. goal = Goal(
  224. id=goal_id,
  225. description=desc.strip(),
  226. reason=reason,
  227. parent_id=parent_id
  228. )
  229. self.goals.append(goal)
  230. new_goals.append(goal)
  231. return new_goals
  232. def add_goals_after(
  233. self,
  234. target_id: str,
  235. descriptions: List[str],
  236. reasons: Optional[List[str]] = None
  237. ) -> List[Goal]:
  238. """
  239. 在指定 Goal 后面添加兄弟节点
  240. 新创建的 goals 与 target 有相同的 parent_id,
  241. 并插入到 goals 列表中 target 的后面。
  242. """
  243. target = self.find(target_id)
  244. if not target:
  245. raise ValueError(f"Target goal not found: {target_id}")
  246. # 创建新 goals(parent_id 与 target 相同)
  247. new_goals = []
  248. for i, desc in enumerate(descriptions):
  249. goal_id = self._generate_id()
  250. reason = reasons[i] if reasons and i < len(reasons) else ""
  251. goal = Goal(
  252. id=goal_id,
  253. description=desc.strip(),
  254. reason=reason,
  255. parent_id=target.parent_id # 同层级
  256. )
  257. new_goals.append(goal)
  258. # 插入到 target 后面(调整 goals 列表顺序)
  259. target_index = self.goals.index(target)
  260. for i, goal in enumerate(new_goals):
  261. self.goals.insert(target_index + 1 + i, goal)
  262. return new_goals
  263. def focus(self, goal_id: str) -> Goal:
  264. """切换焦点到指定 Goal,并将其状态设为 in_progress"""
  265. goal = self.find(goal_id)
  266. if not goal:
  267. raise ValueError(f"Goal not found: {goal_id}")
  268. # 更新状态
  269. if goal.status == "pending":
  270. goal.status = "in_progress"
  271. self.current_id = goal_id
  272. return goal
  273. def complete(self, goal_id: str, summary: str, clear_focus: bool = True) -> Goal:
  274. """
  275. 完成指定 Goal
  276. Args:
  277. goal_id: 要完成的目标 ID
  278. summary: 完成总结
  279. clear_focus: 如果完成的是当前焦点,是否清除焦点(默认 True)
  280. """
  281. goal = self.find(goal_id)
  282. if not goal:
  283. raise ValueError(f"Goal not found: {goal_id}")
  284. goal.status = "completed"
  285. goal.summary = summary
  286. # 如果完成的是当前焦点,根据参数决定是否清除焦点
  287. if clear_focus and self.current_id == goal_id:
  288. # 不直接清空,尝试自动切换到下一个 pending goal
  289. next_goal = self._find_next_pending_goal(goal_id)
  290. if next_goal:
  291. self.current_id = next_goal.id
  292. else:
  293. self.current_id = None
  294. # 检查是否所有兄弟都完成了,如果是则自动完成父节点
  295. if goal.parent_id:
  296. siblings = self.get_children(goal.parent_id)
  297. all_completed = all(g.status == "completed" for g in siblings)
  298. if all_completed:
  299. parent = self.find(goal.parent_id)
  300. if parent and parent.status != "completed":
  301. # 自动级联完成父节点
  302. parent.status = "completed"
  303. if not parent.summary:
  304. parent.summary = "所有子目标已完成"
  305. return goal
  306. def abandon(self, goal_id: str, reason: str) -> Goal:
  307. """放弃指定 Goal"""
  308. goal = self.find(goal_id)
  309. if not goal:
  310. raise ValueError(f"Goal not found: {goal_id}")
  311. goal.status = "abandoned"
  312. goal.summary = reason
  313. # 如果放弃的是当前焦点,尝试自动切换到下一个 pending goal
  314. if self.current_id == goal_id:
  315. next_goal = self._find_next_pending_goal(goal_id)
  316. if next_goal:
  317. self.current_id = next_goal.id
  318. else:
  319. self.current_id = None
  320. return goal
  321. def to_prompt(self, include_abandoned: bool = False, include_summary: bool = False) -> str:
  322. """
  323. 格式化为 Prompt 注入文本
  324. Args:
  325. include_abandoned: 是否包含已废弃的目标
  326. include_summary: 是否显示 completed/abandoned goals 的 summary 详情
  327. False(默认)= 精简视图,用于日常周期性注入
  328. True = 完整视图(含 summary),用于压缩时提供上下文
  329. 展示策略:
  330. - 过滤掉 abandoned 目标(除非明确要求)
  331. - 完整展示所有顶层目标
  332. - 完整展示当前 focus 目标的父链及其所有子孙
  333. - 其他分支的子目标折叠显示(只显示数量和状态)
  334. - include_summary=True 时不折叠,全部展开并显示 summary
  335. """
  336. lines = []
  337. lines.append(f"**Mission**: {self.mission}")
  338. if self.current_id:
  339. current = self.find(self.current_id)
  340. if current:
  341. display_id = self._generate_display_id(current)
  342. lines.append(f"**Current**: {display_id} {current.description}")
  343. lines.append("")
  344. lines.append("**Progress**:")
  345. # 获取当前焦点的祖先链(从根到当前节点的路径)
  346. current_path = set()
  347. if self.current_id:
  348. goal = self.find(self.current_id)
  349. while goal:
  350. current_path.add(goal.id)
  351. if goal.parent_id:
  352. goal = self.find(goal.parent_id)
  353. else:
  354. break
  355. def format_goal(goal: Goal, indent: int = 0) -> List[str]:
  356. # 跳过废弃的目标(除非明确要求包含)
  357. if goal.status == "abandoned" and not include_abandoned:
  358. return []
  359. prefix = " " * indent
  360. # 状态图标
  361. if goal.status == "completed":
  362. icon = "[✓]"
  363. elif goal.status == "in_progress":
  364. icon = "[→]"
  365. elif goal.status == "abandoned":
  366. icon = "[✗]"
  367. else:
  368. icon = "[ ]"
  369. # 生成显示序号
  370. display_id = self._generate_display_id(goal)
  371. # 当前焦点标记
  372. current_mark = " ← current" if goal.id == self.current_id else ""
  373. result = [f"{prefix}{icon} {display_id}. {goal.description}{current_mark}"]
  374. # 显示 summary:include_summary=True 时全部显示,否则只在焦点路径上显示
  375. if goal.summary and (include_summary or goal.id in current_path):
  376. result.append(f"{prefix} → {goal.summary}")
  377. # 显示相关知识:仅在当前焦点 goal 显示
  378. if goal.id == self.current_id and goal.knowledge:
  379. result.append(f"{prefix} 📚 相关知识 ({len(goal.knowledge)} 条):")
  380. for idx, k in enumerate(goal.knowledge[:3], 1):
  381. k_id = k.get('id', 'N/A')
  382. k_content = k.get('content', '').strip()
  383. result.append(f"{prefix} {idx}. [{k_id}] {k_content}")
  384. # 递归处理子目标
  385. children = self.get_children(goal.id)
  386. # include_summary 模式下不折叠,全部展开
  387. if include_summary:
  388. for child in children:
  389. result.extend(format_goal(child, indent + 1))
  390. return result
  391. # 判断是否需要折叠
  392. # 如果当前 goal 或其子孙在焦点路径上,完整展示
  393. should_expand = goal.id in current_path or any(
  394. child.id in current_path for child in self._get_all_descendants(goal.id)
  395. )
  396. if should_expand or not children:
  397. # 完整展示子目标
  398. for child in children:
  399. result.extend(format_goal(child, indent + 1))
  400. else:
  401. # 折叠显示:只显示子目标的统计
  402. non_abandoned = [c for c in children if c.status != "abandoned"]
  403. if non_abandoned:
  404. completed = sum(1 for c in non_abandoned if c.status == "completed")
  405. in_progress = sum(1 for c in non_abandoned if c.status == "in_progress")
  406. pending = sum(1 for c in non_abandoned if c.status == "pending")
  407. status_parts = []
  408. if completed > 0:
  409. status_parts.append(f"{completed} completed")
  410. if in_progress > 0:
  411. status_parts.append(f"{in_progress} in progress")
  412. if pending > 0:
  413. status_parts.append(f"{pending} pending")
  414. status_str = ", ".join(status_parts)
  415. result.append(f"{prefix} ({len(non_abandoned)} subtasks: {status_str})")
  416. return result
  417. # 处理所有顶层目标
  418. top_goals = self.get_children(None)
  419. for goal in top_goals:
  420. lines.extend(format_goal(goal))
  421. return "\n".join(lines)
  422. def _get_all_descendants(self, goal_id: str) -> List[Goal]:
  423. """获取指定 Goal 的所有子孙节点"""
  424. descendants = []
  425. children = self.get_children(goal_id)
  426. for child in children:
  427. descendants.append(child)
  428. descendants.extend(self._get_all_descendants(child.id))
  429. return descendants
  430. def to_dict(self) -> Dict[str, Any]:
  431. """转换为字典"""
  432. return {
  433. "mission": self.mission,
  434. "goals": [g.to_dict() for g in self.goals],
  435. "current_id": self.current_id,
  436. "_next_id": self._next_id,
  437. "created_at": self.created_at.isoformat() if self.created_at else None,
  438. }
  439. @classmethod
  440. def from_dict(cls, data: Dict[str, Any]) -> "GoalTree":
  441. """从字典创建"""
  442. goals = [Goal.from_dict(g) for g in data.get("goals", [])]
  443. created_at = data.get("created_at")
  444. if isinstance(created_at, str):
  445. created_at = datetime.fromisoformat(created_at)
  446. return cls(
  447. mission=data["mission"],
  448. goals=goals,
  449. current_id=data.get("current_id"),
  450. _next_id=data.get("_next_id", 1),
  451. created_at=created_at or datetime.now(),
  452. )
  453. def rebuild_for_rewind(self, cutoff_time: datetime) -> "GoalTree":
  454. """
  455. 为 Rewind 重建干净的 GoalTree
  456. 以截断点消息的 created_at 为界:
  457. - 保留 created_at <= cutoff_time 的所有 goals(无论状态)
  458. - 丢弃 cutoff_time 之后创建的 goals
  459. - 将被保留的 in_progress goals 重置为 pending
  460. - 清空 current_id,让 Agent 重新选择焦点
  461. Args:
  462. cutoff_time: 截断点消息的创建时间
  463. Returns:
  464. 新的干净 GoalTree
  465. """
  466. surviving_goals = []
  467. for goal in self.goals:
  468. if goal.created_at <= cutoff_time:
  469. surviving_goals.append(goal)
  470. # 清理 parent_id 引用:如果 parent 不在存活列表中,设为 None
  471. surviving_ids = {g.id for g in surviving_goals}
  472. for goal in surviving_goals:
  473. if goal.parent_id and goal.parent_id not in surviving_ids:
  474. goal.parent_id = None
  475. # 将 in_progress 重置为 pending(回溯后需要重新执行)
  476. if goal.status == "in_progress":
  477. goal.status = "pending"
  478. new_tree = GoalTree(
  479. mission=self.mission,
  480. goals=surviving_goals,
  481. current_id=None,
  482. _next_id=self._next_id,
  483. created_at=self.created_at,
  484. )
  485. return new_tree
  486. def save(self, path: str) -> None:
  487. """保存到 JSON 文件"""
  488. with open(path, "w", encoding="utf-8") as f:
  489. json.dump(self.to_dict(), f, ensure_ascii=False, indent=2)
  490. @classmethod
  491. def load(cls, path: str) -> "GoalTree":
  492. """从 JSON 文件加载"""
  493. with open(path, "r", encoding="utf-8") as f:
  494. data = json.load(f)
  495. return cls.from_dict(data)