Talegorithm 1 месяц назад
Родитель
Сommit
602bc6060e

+ 3 - 0
.gitignore

@@ -47,3 +47,6 @@ htmlcov/
 # Misc
 # Misc
 .DS_Store
 .DS_Store
 Thumbs.db
 Thumbs.db
+
+# Debug output
+.trace/

+ 9 - 0
agent/debug/__init__.py

@@ -0,0 +1,9 @@
+"""
+Debug 工具模块
+
+提供 Step 树的实时查看功能,用于开发调试。
+"""
+
+from .tree_dump import StepTreeDumper, dump_tree
+
+__all__ = ["StepTreeDumper", "dump_tree"]

+ 317 - 0
agent/debug/tree_dump.py

@@ -0,0 +1,317 @@
+"""
+Step 树 Debug 输出
+
+将 Step 树以完整格式输出到文件,便于开发调试。
+
+使用方式:
+    1. 命令行实时查看:
+       watch -n 0.5 cat .trace/tree.txt
+
+    2. VS Code 打开文件自动刷新:
+       code .trace/tree.txt
+
+    3. 代码中使用:
+       from agent.debug import dump_tree
+       dump_tree(trace, steps)
+"""
+
+import json
+from datetime import datetime
+from pathlib import Path
+from typing import Any, Dict, List, Optional
+
+# 默认输出路径
+DEFAULT_DUMP_PATH = ".trace/tree.txt"
+DEFAULT_JSON_PATH = ".trace/tree.json"
+
+
+class StepTreeDumper:
+    """Step 树 Debug 输出器"""
+
+    def __init__(self, output_path: str = DEFAULT_DUMP_PATH):
+        self.output_path = Path(output_path)
+        self.output_path.parent.mkdir(parents=True, exist_ok=True)
+
+    def dump(
+        self,
+        trace: Optional[Dict[str, Any]] = None,
+        steps: Optional[List[Dict[str, Any]]] = None,
+        title: str = "Step Tree Debug",
+    ) -> str:
+        """
+        输出完整的树形结构到文件
+
+        Args:
+            trace: Trace 字典(可选)
+            steps: Step 字典列表
+            title: 输出标题
+
+        Returns:
+            输出的文本内容
+        """
+        lines = []
+
+        # 标题和时间
+        lines.append("=" * 60)
+        lines.append(f" {title}")
+        lines.append(f" Generated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
+        lines.append("=" * 60)
+        lines.append("")
+
+        # Trace 信息
+        if trace:
+            lines.append("## Trace")
+            lines.append(f"  trace_id: {trace.get('trace_id', 'N/A')}")
+            lines.append(f"  task: {trace.get('task', 'N/A')}")
+            lines.append(f"  status: {trace.get('status', 'N/A')}")
+            lines.append(f"  total_steps: {trace.get('total_steps', 0)}")
+            lines.append(f"  total_tokens: {trace.get('total_tokens', 0)}")
+            lines.append(f"  total_cost: {trace.get('total_cost', 0.0):.4f}")
+            lines.append("")
+
+        # Step 树
+        if steps:
+            lines.append("## Steps")
+            lines.append("")
+
+            # 构建树结构
+            tree = self._build_tree(steps)
+            tree_output = self._render_tree(tree, steps)
+            lines.append(tree_output)
+
+        content = "\n".join(lines)
+
+        # 写入文件
+        self.output_path.write_text(content, encoding="utf-8")
+
+        return content
+
+    def _build_tree(self, steps: List[Dict[str, Any]]) -> Dict[str, List[str]]:
+        """构建父子关系映射"""
+        # parent_id -> [child_ids]
+        children: Dict[str, List[str]] = {"__root__": []}
+
+        for step in steps:
+            step_id = step.get("step_id", "")
+            parent_id = step.get("parent_id")
+
+            if parent_id is None:
+                children["__root__"].append(step_id)
+            else:
+                if parent_id not in children:
+                    children[parent_id] = []
+                children[parent_id].append(step_id)
+
+        return children
+
+    def _render_tree(
+        self,
+        tree: Dict[str, List[str]],
+        steps: List[Dict[str, Any]],
+        parent_id: str = "__root__",
+        indent: int = 0,
+    ) -> str:
+        """递归渲染树结构"""
+        # step_id -> step 映射
+        step_map = {s.get("step_id"): s for s in steps}
+
+        lines = []
+        child_ids = tree.get(parent_id, [])
+
+        for i, step_id in enumerate(child_ids):
+            step = step_map.get(step_id, {})
+            is_last = i == len(child_ids) - 1
+
+            # 渲染当前节点
+            node_output = self._render_node(step, indent, is_last)
+            lines.append(node_output)
+
+            # 递归渲染子节点
+            if step_id in tree:
+                child_output = self._render_tree(tree, steps, step_id, indent + 1)
+                lines.append(child_output)
+
+        return "\n".join(lines)
+
+    def _render_node(self, step: Dict[str, Any], indent: int, is_last: bool) -> str:
+        """渲染单个节点的完整信息"""
+        lines = []
+
+        # 缩进和连接符
+        prefix = "  " * indent
+        connector = "└── " if is_last else "├── "
+        child_prefix = "  " * indent + ("    " if is_last else "│   ")
+
+        # 状态图标
+        status = step.get("status", "unknown")
+        status_icons = {
+            "completed": "✓",
+            "in_progress": "→",
+            "planned": "○",
+            "failed": "✗",
+            "skipped": "⊘",
+        }
+        icon = status_icons.get(status, "?")
+
+        # 类型和描述
+        step_type = step.get("step_type", "unknown")
+        description = step.get("description", "")
+
+        # 第一行:类型和描述
+        lines.append(f"{prefix}{connector}[{icon}] {step_type}: {description}")
+
+        # 详细信息
+        step_id = step.get("step_id", "")[:8]  # 只显示前 8 位
+        lines.append(f"{child_prefix}id: {step_id}...")
+
+        # 执行指标
+        if step.get("duration_ms") is not None:
+            lines.append(f"{child_prefix}duration: {step.get('duration_ms')}ms")
+        if step.get("tokens") is not None:
+            lines.append(f"{child_prefix}tokens: {step.get('tokens')}")
+        if step.get("cost") is not None:
+            lines.append(f"{child_prefix}cost: ${step.get('cost'):.4f}")
+
+        # summary(如果有)
+        if step.get("summary"):
+            summary = step.get("summary", "")
+            # 截断长 summary
+            if len(summary) > 100:
+                summary = summary[:100] + "..."
+            lines.append(f"{child_prefix}summary: {summary}")
+
+        # data 内容(格式化输出)
+        data = step.get("data", {})
+        if data:
+            lines.append(f"{child_prefix}data:")
+            data_lines = self._format_data(data, child_prefix + "  ")
+            lines.append(data_lines)
+
+        # 时间
+        created_at = step.get("created_at", "")
+        if created_at:
+            if isinstance(created_at, str):
+                # 只显示时间部分
+                time_part = created_at.split("T")[-1][:8] if "T" in created_at else created_at
+            else:
+                time_part = created_at.strftime("%H:%M:%S")
+            lines.append(f"{child_prefix}time: {time_part}")
+
+        lines.append("")  # 空行分隔
+        return "\n".join(lines)
+
+    def _format_data(self, data: Dict[str, Any], prefix: str, max_value_len: int = 200) -> str:
+        """格式化 data 字典"""
+        lines = []
+
+        for key, value in data.items():
+            # 格式化值
+            if isinstance(value, str):
+                if len(value) > max_value_len:
+                    value_str = value[:max_value_len] + f"... ({len(value)} chars)"
+                else:
+                    value_str = value
+                # 处理多行字符串
+                if "\n" in value_str:
+                    first_line = value_str.split("\n")[0]
+                    value_str = first_line + f"... ({value_str.count(chr(10))+1} lines)"
+            elif isinstance(value, (dict, list)):
+                value_str = json.dumps(value, ensure_ascii=False, indent=2)
+                if len(value_str) > max_value_len:
+                    value_str = value_str[:max_value_len] + "..."
+                # 缩进多行
+                value_str = value_str.replace("\n", "\n" + prefix + "  ")
+            else:
+                value_str = str(value)
+
+            lines.append(f"{prefix}{key}: {value_str}")
+
+        return "\n".join(lines)
+
+
+def dump_tree(
+    trace: Optional[Any] = None,
+    steps: Optional[List[Any]] = None,
+    output_path: str = DEFAULT_DUMP_PATH,
+    title: str = "Step Tree Debug",
+) -> str:
+    """
+    便捷函数:输出 Step 树到文件
+
+    Args:
+        trace: Trace 对象或字典
+        steps: Step 对象或字典列表
+        output_path: 输出文件路径
+        title: 输出标题
+
+    Returns:
+        输出的文本内容
+
+    示例:
+        from agent.debug import dump_tree
+
+        # 每次 step 变化后调用
+        dump_tree(trace, steps)
+
+        # 自定义路径
+        dump_tree(trace, steps, output_path=".debug/my_trace.txt")
+    """
+    # 转换为字典
+    trace_dict = None
+    if trace is not None:
+        trace_dict = trace.to_dict() if hasattr(trace, "to_dict") else trace
+
+    steps_list = []
+    if steps:
+        for step in steps:
+            if hasattr(step, "to_dict"):
+                steps_list.append(step.to_dict())
+            else:
+                steps_list.append(step)
+
+    dumper = StepTreeDumper(output_path)
+    return dumper.dump(trace_dict, steps_list, title)
+
+
+def dump_json(
+    trace: Optional[Any] = None,
+    steps: Optional[List[Any]] = None,
+    output_path: str = DEFAULT_JSON_PATH,
+) -> str:
+    """
+    输出完整的 JSON 格式(用于程序化分析)
+
+    Args:
+        trace: Trace 对象或字典
+        steps: Step 对象或字典列表
+        output_path: 输出文件路径
+
+    Returns:
+        JSON 字符串
+    """
+    path = Path(output_path)
+    path.parent.mkdir(parents=True, exist_ok=True)
+
+    # 转换为字典
+    trace_dict = None
+    if trace is not None:
+        trace_dict = trace.to_dict() if hasattr(trace, "to_dict") else trace
+
+    steps_list = []
+    if steps:
+        for step in steps:
+            if hasattr(step, "to_dict"):
+                steps_list.append(step.to_dict())
+            else:
+                steps_list.append(step)
+
+    data = {
+        "generated_at": datetime.now().isoformat(),
+        "trace": trace_dict,
+        "steps": steps_list,
+    }
+
+    content = json.dumps(data, ensure_ascii=False, indent=2)
+    path.write_text(content, encoding="utf-8")
+
+    return content

+ 90 - 32
agent/models/trace.py

@@ -2,7 +2,7 @@
 Trace 和 Step 数据模型
 Trace 和 Step 数据模型
 
 
 Trace: 一次完整的 LLM 交互(单次调用或 Agent 任务)
 Trace: 一次完整的 LLM 交互(单次调用或 Agent 任务)
-Step: Trace 中的一个原子操作
+Step: Trace 中的一个原子操作,形成树结构
 """
 """
 
 
 from dataclasses import dataclass, field
 from dataclasses import dataclass, field
@@ -11,14 +11,34 @@ from typing import Dict, Any, List, Optional, Literal
 import uuid
 import uuid
 
 
 
 
+# Step 类型
 StepType = Literal[
 StepType = Literal[
-    "llm_call",      # LLM 调用
-    "tool_call",     # 工具调用
-    "tool_result",   # 工具结果
-    "conclusion",    # 中间/最终结论
-    "feedback",      # 人工反馈
+    # 计划相关
+    "goal",        # 目标/计划项(可以有子 steps)
+
+    # LLM 输出
+    "thought",     # 思考/分析(中间过程)
+    "evaluation",  # 评估总结(需要 summary)
+    "response",    # 最终回复
+
+    # 工具相关
+    "action",      # 工具调用(tool_call)
+    "result",      # 工具结果(tool_result)
+
+    # 系统相关
     "memory_read",   # 读取记忆(经验/技能)
     "memory_read",   # 读取记忆(经验/技能)
     "memory_write",  # 写入记忆
     "memory_write",  # 写入记忆
+    "feedback",      # 人工反馈
+]
+
+
+# Step 状态
+Status = Literal[
+    "planned",      # 计划中(未执行)
+    "in_progress",  # 执行中
+    "completed",    # 已完成
+    "failed",       # 失败
+    "skipped",      # 跳过
 ]
 ]
 
 
 
 
@@ -28,7 +48,7 @@ class Trace:
     执行轨迹 - 一次完整的 LLM 交互
     执行轨迹 - 一次完整的 LLM 交互
 
 
     单次调用: mode="call", 只有 1 个 Step
     单次调用: mode="call", 只有 1 个 Step
-    Agent 模式: mode="agent", 多个 Steps 形成 DAG
+    Agent 模式: mode="agent", 多个 Steps 形成树结构
     """
     """
     trace_id: str
     trace_id: str
     mode: Literal["call", "agent"]
     mode: Literal["call", "agent"]
@@ -52,6 +72,9 @@ class Trace:
     uid: Optional[str] = None
     uid: Optional[str] = None
     context: Dict[str, Any] = field(default_factory=dict)
     context: Dict[str, Any] = field(default_factory=dict)
 
 
+    # 当前焦点 goal(用于 step 工具)
+    current_goal_id: Optional[str] = None
+
     # 时间
     # 时间
     created_at: datetime = field(default_factory=datetime.now)
     created_at: datetime = field(default_factory=datetime.now)
     completed_at: Optional[datetime] = None
     completed_at: Optional[datetime] = None
@@ -83,6 +106,7 @@ class Trace:
             "total_cost": self.total_cost,
             "total_cost": self.total_cost,
             "uid": self.uid,
             "uid": self.uid,
             "context": self.context,
             "context": self.context,
+            "current_goal_id": self.current_goal_id,
             "created_at": self.created_at.isoformat() if self.created_at else None,
             "created_at": self.created_at.isoformat() if self.created_at else None,
             "completed_at": self.completed_at.isoformat() if self.completed_at else None,
             "completed_at": self.completed_at.isoformat() if self.completed_at else None,
         }
         }
@@ -93,19 +117,31 @@ class Step:
     """
     """
     执行步骤 - Trace 中的一个原子操作
     执行步骤 - Trace 中的一个原子操作
 
 
-    Step 之间通过 parent_ids 形成 DAG 结构
+    Step 之间通过 parent_id 形成树结构(单父节点)
     """
     """
     step_id: str
     step_id: str
     trace_id: str
     trace_id: str
     step_type: StepType
     step_type: StepType
+    status: Status
     sequence: int  # 在 Trace 中的顺序
     sequence: int  # 在 Trace 中的顺序
 
 
-    # DAG 结构(支持多父节点)
-    parent_ids: List[str] = field(default_factory=list)
+    # 树结构(单父节点)
+    parent_id: Optional[str] = None
+
+    # 内容
+    description: str = ""  # 所有节点都有,系统自动提取
 
 
     # 类型相关数据
     # 类型相关数据
     data: Dict[str, Any] = field(default_factory=dict)
     data: Dict[str, Any] = field(default_factory=dict)
 
 
+    # 仅 evaluation 类型需要
+    summary: Optional[str] = None
+
+    # 执行指标
+    duration_ms: Optional[int] = None
+    tokens: Optional[int] = None
+    cost: Optional[float] = None
+
     # 时间
     # 时间
     created_at: datetime = field(default_factory=datetime.now)
     created_at: datetime = field(default_factory=datetime.now)
 
 
@@ -115,17 +151,29 @@ class Step:
         trace_id: str,
         trace_id: str,
         step_type: StepType,
         step_type: StepType,
         sequence: int,
         sequence: int,
+        status: Status = "completed",
+        description: str = "",
         data: Dict[str, Any] = None,
         data: Dict[str, Any] = None,
-        parent_ids: List[str] = None,
+        parent_id: Optional[str] = None,
+        summary: Optional[str] = None,
+        duration_ms: Optional[int] = None,
+        tokens: Optional[int] = None,
+        cost: Optional[float] = None,
     ) -> "Step":
     ) -> "Step":
         """创建新的 Step"""
         """创建新的 Step"""
         return cls(
         return cls(
             step_id=str(uuid.uuid4()),
             step_id=str(uuid.uuid4()),
             trace_id=trace_id,
             trace_id=trace_id,
             step_type=step_type,
             step_type=step_type,
+            status=status,
             sequence=sequence,
             sequence=sequence,
-            parent_ids=parent_ids or [],
+            parent_id=parent_id,
+            description=description,
             data=data or {},
             data=data or {},
+            summary=summary,
+            duration_ms=duration_ms,
+            tokens=tokens,
+            cost=cost,
         )
         )
 
 
     def to_dict(self) -> Dict[str, Any]:
     def to_dict(self) -> Dict[str, Any]:
@@ -134,44 +182,54 @@ class Step:
             "step_id": self.step_id,
             "step_id": self.step_id,
             "trace_id": self.trace_id,
             "trace_id": self.trace_id,
             "step_type": self.step_type,
             "step_type": self.step_type,
+            "status": self.status,
             "sequence": self.sequence,
             "sequence": self.sequence,
-            "parent_ids": self.parent_ids,
+            "parent_id": self.parent_id,
+            "description": self.description,
             "data": self.data,
             "data": self.data,
+            "summary": self.summary,
+            "duration_ms": self.duration_ms,
+            "tokens": self.tokens,
+            "cost": self.cost,
             "created_at": self.created_at.isoformat() if self.created_at else None,
             "created_at": self.created_at.isoformat() if self.created_at else None,
         }
         }
 
 
 
 
 # Step.data 结构说明
 # Step.data 结构说明
 #
 #
-# llm_call:
+# goal:
+#   {
+#       "description": "探索代码库",
+#   }
+#
+# thought:
+#   {
+#       "content": "需要先了解项目结构...",
+#   }
+#
+# action:
 #   {
 #   {
-#       "messages": [...],
-#       "response": "...",
-#       "model": "gpt-4o",
-#       "prompt_tokens": 100,
-#       "completion_tokens": 50,
-#       "cost": 0.01,
-#       "tool_calls": [...]  # 如果有
+#       "tool_name": "glob_files",
+#       "arguments": {"pattern": "**/*.py"},
 #   }
 #   }
 #
 #
-# tool_call:
+# result:
 #   {
 #   {
-#       "tool_name": "search_blocks",
-#       "arguments": {...},
-#       "llm_step_id": "..."  # 哪个 LLM 调用触发的
+#       "tool_name": "glob_files",
+#       "output": ["src/main.py", ...],
+#       "title": "找到 15 个文件",
 #   }
 #   }
 #
 #
-# tool_result:
+# evaluation:
 #   {
 #   {
-#       "tool_call_step_id": "...",
-#       "result": "...",
-#       "duration_ms": 123
+#       "content": "分析完成...",
 #   }
 #   }
+#   # summary 字段存储简短总结
 #
 #
-# conclusion:
+# response:
 #   {
 #   {
-#       "content": "...",
-#       "is_final": True/False
+#       "content": "任务已完成...",
+#       "is_final": True,
 #   }
 #   }
 #
 #
 # feedback:
 # feedback:

+ 97 - 42
agent/runner.py

@@ -19,6 +19,7 @@ from agent.models.memory import Experience, Skill
 from agent.storage.protocols import TraceStore, MemoryStore, StateStore
 from agent.storage.protocols import TraceStore, MemoryStore, StateStore
 from agent.storage.skill_loader import load_skills_from_dir
 from agent.storage.skill_loader import load_skills_from_dir
 from agent.tools import ToolRegistry, get_tool_registry
 from agent.tools import ToolRegistry, get_tool_registry
+from agent.debug import dump_tree
 
 
 logger = logging.getLogger(__name__)
 logger = logging.getLogger(__name__)
 
 
@@ -60,6 +61,7 @@ class AgentRunner:
         tool_registry: Optional[ToolRegistry] = None,
         tool_registry: Optional[ToolRegistry] = None,
         llm_call: Optional[Callable] = None,
         llm_call: Optional[Callable] = None,
         config: Optional[AgentConfig] = None,
         config: Optional[AgentConfig] = None,
+        debug: bool = False,
     ):
     ):
         """
         """
         初始化 AgentRunner
         初始化 AgentRunner
@@ -71,6 +73,7 @@ class AgentRunner:
             tool_registry: 工具注册表(可选,默认使用全局注册表)
             tool_registry: 工具注册表(可选,默认使用全局注册表)
             llm_call: LLM 调用函数(必须提供,用于实际调用 LLM)
             llm_call: LLM 调用函数(必须提供,用于实际调用 LLM)
             config: Agent 配置
             config: Agent 配置
+            debug: 是否启用 debug 模式(输出 step tree 到 .trace/tree.txt)
         """
         """
         self.trace_store = trace_store
         self.trace_store = trace_store
         self.memory_store = memory_store
         self.memory_store = memory_store
@@ -78,12 +81,21 @@ class AgentRunner:
         self.tools = tool_registry or get_tool_registry()
         self.tools = tool_registry or get_tool_registry()
         self.llm_call = llm_call
         self.llm_call = llm_call
         self.config = config or AgentConfig()
         self.config = config or AgentConfig()
+        self.debug = debug
 
 
     def _generate_id(self) -> str:
     def _generate_id(self) -> str:
         """生成唯一 ID"""
         """生成唯一 ID"""
         import uuid
         import uuid
         return str(uuid.uuid4())
         return str(uuid.uuid4())
 
 
+    async def _dump_debug(self, trace_id: str) -> None:
+        """Debug 模式下输出 step tree"""
+        if not self.debug or not self.trace_store:
+            return
+        trace = await self.trace_store.get_trace(trace_id)
+        steps = await self.trace_store.get_trace_steps(trace_id)
+        dump_tree(trace, steps)
+
     # ===== 单次调用 =====
     # ===== 单次调用 =====
 
 
     async def call(
     async def call(
@@ -141,19 +153,21 @@ class AgentRunner:
         if trace and self.trace_store and trace_id:
         if trace and self.trace_store and trace_id:
             step = Step.create(
             step = Step.create(
                 trace_id=trace_id,
                 trace_id=trace_id,
-                step_type="llm_call",
+                step_type="thought",
                 sequence=0,
                 sequence=0,
+                status="completed",
+                description=f"LLM 调用 ({model})",
                 data={
                 data={
                     "messages": messages,
                     "messages": messages,
                     "response": result.get("content", ""),
                     "response": result.get("content", ""),
                     "model": model,
                     "model": model,
                     "tool_calls": result.get("tool_calls"),
                     "tool_calls": result.get("tool_calls"),
-                    "prompt_tokens": result.get("prompt_tokens", 0),
-                    "completion_tokens": result.get("completion_tokens", 0),
-                    "cost": result.get("cost", 0),
-                }
+                },
+                tokens=result.get("prompt_tokens", 0) + result.get("completion_tokens", 0),
+                cost=result.get("cost", 0),
             )
             )
             step_id = await self.trace_store.add_step(step)
             step_id = await self.trace_store.add_step(step)
+            await self._dump_debug(trace_id)
 
 
             # 完成 Trace
             # 完成 Trace
             await self.trace_store.update_trace(
             await self.trace_store.update_trace(
@@ -254,12 +268,15 @@ class AgentRunner:
                         trace_id=trace_id,
                         trace_id=trace_id,
                         step_type="memory_read",
                         step_type="memory_read",
                         sequence=0,
                         sequence=0,
+                        status="completed",
+                        description=f"加载 {len(experiences)} 条经验",
                         data={
                         data={
                             "experiences_count": len(experiences),
                             "experiences_count": len(experiences),
                             "experiences": [e.to_dict() for e in experiences],
                             "experiences": [e.to_dict() for e in experiences],
                         }
                         }
                     )
                     )
                     await self.trace_store.add_step(mem_step)
                     await self.trace_store.add_step(mem_step)
+                    await self._dump_debug(trace_id)
 
 
                 yield AgentEvent("memory_loaded", {
                 yield AgentEvent("memory_loaded", {
                     "experiences_count": len(experiences)
                     "experiences_count": len(experiences)
@@ -286,7 +303,7 @@ class AgentRunner:
                 tool_schemas = self.tools.get_schemas(tools)
                 tool_schemas = self.tools.get_schemas(tools)
 
 
             # 执行循环
             # 执行循环
-            parent_step_ids = []
+            current_goal_id = None  # 当前焦点 goal
             sequence = 1
             sequence = 1
             total_tokens = 0
             total_tokens = 0
             total_cost = 0.0
             total_cost = 0.0
@@ -294,7 +311,7 @@ class AgentRunner:
             for iteration in range(max_iterations):
             for iteration in range(max_iterations):
                 yield AgentEvent("step_started", {
                 yield AgentEvent("step_started", {
                     "iteration": iteration,
                     "iteration": iteration,
-                    "step_type": "llm_call"
+                    "step_type": "thought"
                 })
                 })
 
 
                 # 调用 LLM
                 # 调用 LLM
@@ -307,42 +324,49 @@ class AgentRunner:
 
 
                 response_content = result.get("content", "")
                 response_content = result.get("content", "")
                 tool_calls = result.get("tool_calls")
                 tool_calls = result.get("tool_calls")
-                tokens = result.get("prompt_tokens", 0) + result.get("completion_tokens", 0)
-                cost = result.get("cost", 0)
+                step_tokens = result.get("prompt_tokens", 0) + result.get("completion_tokens", 0)
+                step_cost = result.get("cost", 0)
 
 
-                total_tokens += tokens
-                total_cost += cost
+                total_tokens += step_tokens
+                total_cost += step_cost
 
 
                 # 记录 LLM 调用 Step
                 # 记录 LLM 调用 Step
                 llm_step_id = self._generate_id()
                 llm_step_id = self._generate_id()
                 if self.trace_store:
                 if self.trace_store:
+                    # 推断 step_type
+                    step_type = "thought"
+                    if tool_calls:
+                        step_type = "thought"  # 有工具调用的思考
+                    elif not tool_calls and iteration > 0:
+                        step_type = "response"  # 无工具调用,可能是最终回复
+
                     llm_step = Step(
                     llm_step = Step(
                         step_id=llm_step_id,
                         step_id=llm_step_id,
                         trace_id=trace_id,
                         trace_id=trace_id,
-                        step_type="llm_call",
+                        step_type=step_type,
+                        status="completed",
                         sequence=sequence,
                         sequence=sequence,
-                        parent_ids=parent_step_ids,
+                        parent_id=current_goal_id,
+                        description=response_content[:100] + "..." if len(response_content) > 100 else response_content,
                         data={
                         data={
-                            "messages": messages,
-                            "response": response_content,
+                            "content": response_content,
                             "model": model,
                             "model": model,
                             "tool_calls": tool_calls,
                             "tool_calls": tool_calls,
-                            "prompt_tokens": result.get("prompt_tokens", 0),
-                            "completion_tokens": result.get("completion_tokens", 0),
-                            "cost": cost,
-                        }
+                        },
+                        tokens=step_tokens,
+                        cost=step_cost,
                     )
                     )
                     await self.trace_store.add_step(llm_step)
                     await self.trace_store.add_step(llm_step)
+                    await self._dump_debug(trace_id)
 
 
                 sequence += 1
                 sequence += 1
-                parent_step_ids = [llm_step_id]
 
 
                 yield AgentEvent("llm_call_completed", {
                 yield AgentEvent("llm_call_completed", {
                     "step_id": llm_step_id,
                     "step_id": llm_step_id,
                     "content": response_content,
                     "content": response_content,
                     "tool_calls": tool_calls,
                     "tool_calls": tool_calls,
-                    "tokens": tokens,
-                    "cost": cost
+                    "tokens": step_tokens,
+                    "cost": step_cost
                 })
                 })
 
 
                 # 处理工具调用
                 # 处理工具调用
@@ -379,28 +403,50 @@ class AgentRunner:
                             uid=uid or ""
                             uid=uid or ""
                         )
                         )
 
 
-                        # 记录 tool_call Step
-                        tool_step_id = self._generate_id()
+                        # 记录 action Step
+                        action_step_id = self._generate_id()
                         if self.trace_store:
                         if self.trace_store:
-                            tool_step = Step(
-                                step_id=tool_step_id,
+                            action_step = Step(
+                                step_id=action_step_id,
                                 trace_id=trace_id,
                                 trace_id=trace_id,
-                                step_type="tool_call",
+                                step_type="action",
+                                status="completed",
                                 sequence=sequence,
                                 sequence=sequence,
-                                parent_ids=[llm_step_id],
+                                parent_id=llm_step_id,
+                                description=f"{tool_name}({', '.join(f'{k}={v}' for k, v in list(tool_args.items())[:2])})",
                                 data={
                                 data={
                                     "tool_name": tool_name,
                                     "tool_name": tool_name,
                                     "arguments": tool_args,
                                     "arguments": tool_args,
-                                    "result": tool_result,
                                 }
                                 }
                             )
                             )
-                            await self.trace_store.add_step(tool_step)
+                            await self.trace_store.add_step(action_step)
+                            await self._dump_debug(trace_id)
+
+                        sequence += 1
+
+                        # 记录 result Step
+                        result_step_id = self._generate_id()
+                        if self.trace_store:
+                            result_step = Step(
+                                step_id=result_step_id,
+                                trace_id=trace_id,
+                                step_type="result",
+                                status="completed",
+                                sequence=sequence,
+                                parent_id=action_step_id,
+                                description=str(tool_result)[:100] if tool_result else "",
+                                data={
+                                    "tool_name": tool_name,
+                                    "output": tool_result,
+                                }
+                            )
+                            await self.trace_store.add_step(result_step)
+                            await self._dump_debug(trace_id)
 
 
                         sequence += 1
                         sequence += 1
-                        parent_step_ids.append(tool_step_id)
 
 
                         yield AgentEvent("tool_result", {
                         yield AgentEvent("tool_result", {
-                            "step_id": tool_step_id,
+                            "step_id": result_step_id,
                             "tool_name": tool_name,
                             "tool_name": tool_name,
                             "result": tool_result
                             "result": tool_result
                         })
                         })
@@ -416,24 +462,27 @@ class AgentRunner:
                     continue  # 继续循环
                     continue  # 继续循环
 
 
                 # 无工具调用,任务完成
                 # 无工具调用,任务完成
-                # 记录 conclusion Step
-                conclusion_step_id = self._generate_id()
+                # 记录 response Step
+                response_step_id = self._generate_id()
                 if self.trace_store:
                 if self.trace_store:
-                    conclusion_step = Step(
-                        step_id=conclusion_step_id,
+                    response_step = Step(
+                        step_id=response_step_id,
                         trace_id=trace_id,
                         trace_id=trace_id,
-                        step_type="conclusion",
+                        step_type="response",
+                        status="completed",
                         sequence=sequence,
                         sequence=sequence,
-                        parent_ids=parent_step_ids,
+                        parent_id=current_goal_id,
+                        description=response_content[:100] + "..." if len(response_content) > 100 else response_content,
                         data={
                         data={
                             "content": response_content,
                             "content": response_content,
                             "is_final": True
                             "is_final": True
                         }
                         }
                     )
                     )
-                    await self.trace_store.add_step(conclusion_step)
+                    await self.trace_store.add_step(response_step)
+                    await self._dump_debug(trace_id)
 
 
                 yield AgentEvent("conclusion", {
                 yield AgentEvent("conclusion", {
-                    "step_id": conclusion_step_id,
+                    "step_id": response_step_id,
                     "content": response_content,
                     "content": response_content,
                     "is_final": True
                     "is_final": True
                 })
                 })
@@ -511,7 +560,9 @@ class AgentRunner:
             trace_id=trace_id,
             trace_id=trace_id,
             step_type="feedback",
             step_type="feedback",
             sequence=max_seq + 1,
             sequence=max_seq + 1,
-            parent_ids=[target_step_id],
+            status="completed",
+            description=f"{feedback_type}: {content[:50]}...",
+            parent_id=target_step_id,
             data={
             data={
                 "target_step_id": target_step_id,
                 "target_step_id": target_step_id,
                 "feedback_type": feedback_type,
                 "feedback_type": feedback_type,
@@ -519,6 +570,7 @@ class AgentRunner:
             }
             }
         )
         )
         await self.trace_store.add_step(feedback_step)
         await self.trace_store.add_step(feedback_step)
+        await self._dump_debug(trace_id)
 
 
         # 提取经验
         # 提取经验
         exp_id = None
         exp_id = None
@@ -538,7 +590,9 @@ class AgentRunner:
                 trace_id=trace_id,
                 trace_id=trace_id,
                 step_type="memory_write",
                 step_type="memory_write",
                 sequence=max_seq + 2,
                 sequence=max_seq + 2,
-                parent_ids=[feedback_step.step_id],
+                status="completed",
+                description=f"保存经验: {exp.condition[:30]}...",
+                parent_id=feedback_step.step_id,
                 data={
                 data={
                     "experience_id": exp_id,
                     "experience_id": exp_id,
                     "condition": exp.condition,
                     "condition": exp.condition,
@@ -546,6 +600,7 @@ class AgentRunner:
                 }
                 }
             )
             )
             await self.trace_store.add_step(mem_step)
             await self.trace_store.add_step(mem_step)
+            await self._dump_debug(trace_id)
 
 
         return exp_id
         return exp_id
 
 

+ 69 - 0
agent/skills/core.md

@@ -0,0 +1,69 @@
+---
+name: core
+type: core
+description: 核心系统功能,自动加载到 System Prompt
+---
+
+# Core Skills
+
+本文档描述 Agent 的核心系统功能。
+
+---
+
+## Step 管理
+
+你可以使用 `step` 工具来管理执行计划和进度。
+
+### 何时使用
+
+- **复杂任务**(3 个以上步骤):先制定计划再执行
+- **简单任务**:直接执行,无需计划
+
+### 创建计划
+
+当任务复杂时,先制定计划:
+
+```
+step(plan=["探索代码库", "修改配置", "运行测试"])
+```
+
+### 开始执行
+
+聚焦到某个目标开始执行:
+
+```
+step(focus="探索代码库")
+```
+
+### 完成并切换
+
+完成当前目标,提供总结,切换到下一个:
+
+```
+step(complete=True, summary="主配置在 /src/config.yaml,包含数据库连接配置", focus="修改配置")
+```
+
+### 调整计划
+
+执行中发现需要增加步骤:
+
+```
+step(plan=["备份原配置"])  # 追加新目标
+```
+
+### 查看进度
+
+查看当前执行进度:
+
+```
+read_progress()
+```
+
+---
+
+## 使用规范
+
+1. **同时只有一个目标处于执行中**:完成当前目标后再切换
+2. **summary 应简洁**:记录关键结论和发现,不要冗长
+3. **计划可调整**:根据执行情况追加或跳过目标
+4. **简单任务不需要计划**:单步操作直接执行即可

+ 1 - 1
agent/storage/memory_impl.py

@@ -85,7 +85,7 @@ class MemoryTraceStore:
     async def get_step_children(self, step_id: str) -> List[Step]:
     async def get_step_children(self, step_id: str) -> List[Step]:
         children = []
         children = []
         for step in self._steps.values():
         for step in self._steps.values():
-            if step_id in step.parent_ids:
+            if step.parent_id == step_id:
                 children.append(step)
                 children.append(step)
         children.sort(key=lambda s: s.sequence)
         children.sort(key=lambda s: s.sequence)
         return children
         return children

+ 58 - 34
docs/README.md

@@ -185,13 +185,18 @@ class Trace:
 class Step:
 class Step:
     step_id: str
     step_id: str
     trace_id: str
     trace_id: str
-    step_type: StepType  # "llm_call", "tool_call", "tool_result", ...
-    parent_ids: List[str] = field(default_factory=list)
+    step_type: StepType    # "goal", "thought", "action", "result", "evaluation", "response"
+    status: Status         # "planned", "in_progress", "completed", "failed", "skipped"
+    parent_id: Optional[str] = None  # 树结构(单父节点)
+    description: str = ""            # 系统自动提取
     data: Dict[str, Any] = field(default_factory=dict)
     data: Dict[str, Any] = field(default_factory=dict)
+    summary: Optional[str] = None    # 仅 evaluation 类型需要
 ```
 ```
 
 
 **实现**:`agent/models/trace.py:Step`
 **实现**:`agent/models/trace.py:Step`
 
 
+**详细设计**:参考 [`docs/step-tree.md`](./step-tree.md)
+
 ---
 ---
 
 
 ## 模块详情
 ## 模块详情
@@ -213,6 +218,13 @@ class Step:
 
 
 **使用示例**:`examples/subagent_example.py`
 **使用示例**:`examples/subagent_example.py`
 
 
+### [Step 树与 Context 管理](./step-tree.md)
+- Step 类型:goal、action、result、evaluation
+- Step 状态:planned、in_progress、completed、failed、skipped
+- 树结构:统一表达计划和执行
+- step 工具:计划管理和进度更新
+- Context 压缩:基于树结构的历史消息压缩
+
 ### [工具系统](./tools.md)
 ### [工具系统](./tools.md)
 - 工具定义和注册
 - 工具定义和注册
 - 双层记忆管理
 - 双层记忆管理
@@ -304,51 +316,40 @@ messages = prompt.build_messages(text="...", images="img.png")
 
 
 ### Skills(技能库)
 ### Skills(技能库)
 
 
-**存储**:Markdown 文件 + 环境配置代码
-
-```
-./agent/skills/                # Skills 目录
-├── browser_use/              # browser-use skill
-│   ├── browser-use.md        # 使用文档
-│   ├── setup.py              # 环境配置(依赖检查和安装)
-│   └── __init__.py           # 模块导出
-└── [其他 skills]/
-```
-
-**格式**:
+**分类**:
 
 
-```markdown
----
-name: error-handling
-description: Error handling best practices
----
+| 类型 | 加载位置 | 加载时机 |
+|------|---------|---------|
+| **Core Skill** | System Prompt | Agent 启动时自动 |
+| **普通 Skill** | 对话消息 | 模型调用 `skill` 工具时 |
 
 
-## When to use
-- Analyzing error logs
-- Debugging production issues
+**目录结构**:
 
 
-## Guidelines
-- Look for stack traces first
-- Check error frequency
-- Group by error type
+```
+./agent/skills/
+├── core.md                   # Core Skill(自动加载到 System Prompt)
+└── browser_use/              # 普通 Skill(按需加载到对话消息)
+    ├── browser-use.md
+    ├── setup.py
+    └── __init__.py
 ```
 ```
 
 
-**加载**:通过 `skill` 工具动态加载
+**Core Skill**(`agent/skills/core.md`):
+- 核心系统功能:Step 管理、进度追踪
+- 框架自动注入到 System Prompt
+
+**普通 Skill**:通过 `skill` 工具动态加载
 
 
-Agent 在需要时调用 `skill` 工具:
 ```python
 ```python
-# Agent 运行时
+# Agent 运行时调用
 await tools.execute("skill", {"skill_name": "browser-use"})
 await tools.execute("skill", {"skill_name": "browser-use"})
-# 自动检查环境依赖,加载使用文档
+# 内容注入到对话历史
 ```
 ```
 
 
-工具会读取文件并返回内容,注入到对话历史中。
-
 **实现**:
 **实现**:
 - `agent/storage/skill_loader.py:SkillLoader` - Markdown 解析器
 - `agent/storage/skill_loader.py:SkillLoader` - Markdown 解析器
 - `agent/tools/builtin/skill.py:skill()` - skill 工具实现
 - `agent/tools/builtin/skill.py:skill()` - skill 工具实现
 - `agent/tools/builtin/skill.py:list_skills()` - 列出可用 skills
 - `agent/tools/builtin/skill.py:list_skills()` - 列出可用 skills
-- `agent/skills/*/setup.py` - 环境配置(可选,每个 skill 可自定义)
 
 
 **详细文档**:参考 [`docs/skills.md`](./skills.md)
 **详细文档**:参考 [`docs/skills.md`](./skills.md)
 
 
@@ -485,6 +486,28 @@ agent/
 
 
 ---
 ---
 
 
+## Debug 工具
+
+开发调试时可实时查看 Step 树:
+
+```python
+from agent.debug import dump_tree
+
+# 每次 step 变化后调用
+dump_tree(trace, steps)
+```
+
+```bash
+# 终端实时查看
+watch -n 0.5 cat .trace/tree.txt
+```
+
+**实现**:`agent/debug/tree_dump.py`
+
+**详细说明**:参考 [`docs/step-tree.md`](./step-tree.md#debug-工具)
+
+---
+
 ## 测试
 ## 测试
 
 
 详见 [测试指南](./testing.md)
 详见 [测试指南](./testing.md)
@@ -513,7 +536,8 @@ GEMINI_API_KEY=xxx pytest tests/e2e/ -v -m e2e
 | 概念 | 定义 | 存储 | 实现 |
 | 概念 | 定义 | 存储 | 实现 |
 |------|------|------|------|
 |------|------|------|------|
 | **Trace** | 一次任务执行 | 文件系统(JSON) | `models/trace.py` |
 | **Trace** | 一次任务执行 | 文件系统(JSON) | `models/trace.py` |
-| **Step** | 执行步骤 | 文件系统(JSON) | `models/trace.py` |
+| **Step** | 执行步骤(树结构) | 文件系统(JSON) | `models/trace.py` |
+| **Goal Step** | 计划项/目标 | Step 的一种类型 | `models/trace.py` |
 | **Sub-Agent** | 专门化的子代理 | 独立 Trace | `tools/builtin/task.py` |
 | **Sub-Agent** | 专门化的子代理 | 独立 Trace | `tools/builtin/task.py` |
 | **AgentDefinition** | Agent 类型定义 | 配置文件/代码 | `models/agent.py` |
 | **AgentDefinition** | Agent 类型定义 | 配置文件/代码 | `models/agent.py` |
 | **Skill** | 能力描述(Markdown) | 文件系统 | `storage/skill_fs.py` |
 | **Skill** | 能力描述(Markdown) | 文件系统 | `storage/skill_fs.py` |

+ 162 - 0
docs/decisions.md

@@ -347,6 +347,168 @@ async def advanced_search(
 
 
 ---
 ---
 
 
+## 11. Step 树结构 vs DAG
+
+### 问题
+Step 之间的关系应该是树(单父节点)还是 DAG(多父节点)?
+
+### 方案对比
+
+| 方案 | 优点 | 缺点 |
+|------|------|------|
+| **DAG(多父节点)** | 能精确表达并行汇合 | 复杂,难以折叠/展开 |
+| **树(单父节点)** | 简单,天然支持折叠 | 并行汇合需要其他方式表达 |
+
+### 决策
+**选择:树结构(单父节点)**
+
+**理由**:
+1. **可视化友好**:树结构天然支持折叠/展开
+2. **足够表达**:并行工具调用可以是同一父节点的多个子节点
+3. **简化实现**:不需要处理复杂的 DAG 遍历
+
+**实现**:`Step.parent_id: Optional[str]`(单个值,不是列表)
+
+---
+
+## 12. 计划管理:统一到 Step 树 vs 独立 TODO 列表
+
+### 问题
+Agent 的计划(TODO)应该如何管理?
+
+### 方案对比
+
+| 方案 | 优点 | 缺点 |
+|------|------|------|
+| **独立 TODO 列表**(OpenCode 方式) | 简单,与执行分离 | 计划与执行无结构化关联 |
+| **统一到 Step 树** | 计划和执行在同一结构中,可追踪关联 | 稍复杂 |
+
+### 决策
+**选择:统一到 Step 树**
+
+**设计**:
+- `Step.status = "planned"` 表示计划中的步骤
+- `Step.step_type = "goal"` 表示计划项/目标
+- 模型通过 `step` 工具管理计划
+
+**理由**:
+1. **统一模型**:不需要额外的 TODO 数据结构
+2. **可追踪**:执行步骤自动关联到计划项
+3. **可视化**:计划和执行在同一棵树中展示
+
+**参考**:OpenCode 的 `todowrite`/`todoread` 工具(`src/tool/todo.ts`)
+
+---
+
+## 13. Summary 生成策略
+
+### 问题
+哪些 Step 需要生成 summary?
+
+### 决策
+**选择:仅 evaluation 类型节点需要 summary**
+
+**理由**:
+1. **避免浪费**:不是每个 step 都需要总结
+2. **有意义的总结**:evaluation 是对一组操作的评估,值得总结
+3. **节省资源**:减少 LLM 调用次数
+
+**实现**:
+- `Step.summary` 字段可选
+- 仅在 `step_type == "evaluation"` 时填充
+- `tool_call`/`tool_result` 不需要 summary,直接从 `data` 提取关键信息
+
+---
+
+## 14. Context 压缩策略
+
+### 问题
+当消息历史过长时,如何压缩?
+
+### 决策
+**选择:基于树结构的分层压缩**
+
+**设计**:
+- **Todo 格式(简略)**:仅选择 `goal` 类型节点
+- **历史压缩格式(详细)**:选择 `goal` + `result` + `evaluation` 节点
+
+**触发时机**:
+- 正常情况:模型通过工具按需读取进度
+- 压缩时(context 超 70%):自动注入详细历史摘要
+
+**理由**:
+1. **信息分层**:不同用途需要不同详略程度
+2. **节点选择**:关键是选择哪些节点,而非每个节点展示什么
+3. **按需读取**:正常情况不浪费 context
+
+---
+
+## 15. Step 元数据设置策略
+
+### 问题
+Step 的元数据(step_type、description、parent_id 等)如何设置?
+
+### 方案对比
+
+| 方案 | 优点 | 缺点 |
+|------|------|------|
+| **LLM 显式输出** | 准确 | 需要 LLM 配合特定格式,增加复杂度 |
+| **系统自动推断** | 简单,不需要 LLM 额外输出 | 可能不够准确 |
+| **混合** | 平衡准确性和简洁性 | 需要明确划分 |
+
+### 决策
+**选择:系统自动推断为主,显式工具调用为辅**
+
+**设计**:
+- **系统自动记录**:`step_id`、`parent_id`、`tokens`、`cost`、`duration_ms`、`created_at`
+- **系统推断**:`step_type`(基于输出内容)、`description`(从输出提取)
+- **显式声明**(通过 step 工具):`goal`、`evaluation`(summary)
+
+**step_type 推断规则**:
+1. 有工具调用 → `action`
+2. 调用 step 工具且 complete=True → `evaluation`
+3. 调用 step 工具且 plan 不为空 → `goal`
+4. 最终回复 → `response`
+5. 默认 → `thought`
+
+**理由**:
+1. **简化 LLM 负担**:不需要输出特定格式的元数据
+2. **step 工具是显式意图**:计划和评估通过工具明确声明
+3. **其他类型自动推断**:`thought`、`action`、`result`、`response` 可从输出内容判断
+
+---
+
+## 16. Skill 分层:Core Skill vs 普通 Skill
+
+### 问题
+Step 工具等核心功能如何让 Agent 知道?
+
+### 方案对比
+
+| 方案 | 优点 | 缺点 |
+|------|------|------|
+| **写在 System Prompt** | 始终可见 | 每次消耗 token,内容膨胀 |
+| **作为普通 Skill** | 按需加载 | 模型不知道存在就不会加载 |
+| **分层:Core + 普通** | 核心功能始终可见,其他按需 | 需要区分两类 |
+
+### 决策
+**选择:Skill 分层**
+
+**设计**:
+- **Core Skill**:`agent/skills/core.md`,自动注入到 System Prompt
+- **普通 Skill**:`agent/skills/{name}/`,通过 `skill` 工具加载到对话消息
+
+**理由**:
+1. **核心功能必须可见**:Step 管理等功能,模型需要始终知道
+2. **避免 System Prompt 膨胀**:只有核心内容在 System Prompt
+3. **普通 Skill 按需加载**:领域知识在需要时才加载,节省 token
+
+**实现**:
+- Core Skill:框架在 `build_system_prompt()` 时自动读取并拼接
+- 普通 Skill:模型调用 `skill` 工具时返回内容到对话消息
+
+---
+
 ## 总结
 ## 总结
 
 
 这些设计决策的核心原则:
 这些设计决策的核心原则:

+ 35 - 2
docs/skills.md

@@ -1,8 +1,41 @@
 # Skills 使用指南
 # Skills 使用指南
 
 
-Skills 是 Agent 的领域知识库,存储在 Markdown 文件中。Agent 通过 `skill` 工具按需加载。
+Skills 是 Agent 的领域知识库,存储在 Markdown 文件中。
 
 
-## Skill 文件格式
+---
+
+## Skill 分类
+
+| 类型 | 加载位置 | 加载时机 | 文件位置 |
+|------|---------|---------|---------|
+| **Core Skill** | System Prompt | Agent 启动时自动加载 | `agent/skills/core.md` |
+| **普通 Skill** | 对话消息 | 模型调用 `skill` 工具时 | `agent/skills/{name}/` |
+
+### Core Skill
+
+核心系统功能,每个 Agent 都需要了解:
+
+- Step 管理(计划、执行、进度)
+- 其他系统级功能
+
+**位置**:`agent/skills/core.md`
+
+**加载方式**:框架自动注入到 System Prompt
+
+### 普通 Skill
+
+特定领域能力,按需加载:
+
+- browser_use(浏览器自动化)
+- 其他领域 skills
+
+**位置**:`agent/skills/{name}/`
+
+**加载方式**:模型调用 `skill` 工具
+
+---
+
+## 普通 Skill 文件格式
 
 
 ```markdown
 ```markdown
 ---
 ---

+ 471 - 0
docs/step-tree.md

@@ -0,0 +1,471 @@
+# Step 树结构与 Context 管理
+
+> 本文档描述 Agent 执行过程的结构化记录、计划管理和 Context 压缩机制。
+
+---
+
+## 设计目标
+
+1. **可视化**:支持执行路径的树状展示,可折叠/展开
+2. **计划管理**:统一表达"已执行"和"计划中"的步骤
+3. **Context 优化**:基于树结构压缩历史消息,节省 token
+
+---
+
+## 核心设计:Step 树
+
+### Step 类型
+
+```python
+StepType = Literal[
+    # 计划相关
+    "goal",        # 目标/计划项(可以有子 steps)
+
+    # LLM 输出
+    "thought",     # 思考/分析(中间过程)
+    "evaluation",  # 评估总结(需要 summary)
+    "response",    # 最终回复
+
+    # 工具相关
+    "action",      # 工具调用(tool_call)
+    "result",      # 工具结果(tool_result)
+]
+```
+
+| 类型 | 来源 | 说明 |
+|------|------|------|
+| `goal` | LLM(通过 step 工具) | 设定目标/计划 |
+| `thought` | LLM | 中间思考,不产生工具调用 |
+| `evaluation` | LLM | 对一组操作的总结,需要 summary |
+| `response` | LLM | 最终给用户的回复 |
+| `action` | System | LLM 决定调用工具,系统记录 |
+| `result` | System | 工具执行结果 |
+
+### Step 状态
+
+```python
+Status = Literal[
+    "planned",      # 计划中(未执行)
+    "in_progress",  # 执行中
+    "completed",    # 已完成
+    "failed",       # 失败
+    "skipped",      # 跳过
+]
+```
+
+### Step 模型
+
+```python
+@dataclass
+class Step:
+    step_id: str
+    trace_id: str
+    step_type: StepType
+    status: Status
+    sequence: int
+
+    # 树结构(单父节点)
+    parent_id: Optional[str] = None
+
+    # 内容
+    description: str                      # 所有节点都有
+    data: Dict[str, Any] = field(default_factory=dict)
+
+    # 仅 evaluation 类型需要
+    summary: Optional[str] = None
+
+    # 执行指标
+    duration_ms: Optional[int] = None
+    cost: Optional[float] = None
+    tokens: Optional[int] = None
+
+    # 时间
+    created_at: datetime = field(default_factory=datetime.now)
+```
+
+**关键点**:
+- `parent_id` 是单个值(树结构),不是列表(DAG)
+- `summary` 仅在 `evaluation` 类型节点填充,不是每个节点都需要
+- `planned` 状态的 step 相当于 TODO item
+
+---
+
+## 树结构示例
+
+```
+Trace
+├── goal: "探索代码库" (completed)
+│   ├── thought: "需要先了解项目结构"
+│   ├── action: glob_files
+│   ├── result: [15 files...]
+│   ├── thought: "发现配置文件,需要查看内容"
+│   ├── action: read_file
+│   ├── result: [content...]
+│   └── evaluation: "主配置在 /src/config.yaml" ← summary
+│
+├── goal: "修改配置" (in_progress)
+│   ├── action: read_file
+│   └── result: [content...]
+│
+└── goal: "运行测试" (planned)
+```
+
+### Parent 关系规则
+
+| Step 类型 | parent 是谁 |
+|----------|------------|
+| `goal` | 上一个 `goal`(或 None) |
+| `thought` | 当前 `in_progress` 的 `goal` |
+| `action` | 当前 `in_progress` 的 `goal` |
+| `result` | 对应的 `action` |
+| `evaluation` | 所属的 `goal` |
+| `response` | 当前 `in_progress` 的 `goal`(或 None) |
+
+---
+
+## 元数据设置
+
+### 系统自动记录
+
+以下字段由系统自动填充,不需要 LLM 参与:
+
+```python
+step_id: str          # 自动生成
+parent_id: str        # 根据当前 focus 的 goal 自动设置
+step_type: StepType   # 根据 LLM 输出推断(见下)
+sequence: int         # 递增序号
+tokens: int           # API 返回
+cost: float           # 计算得出
+duration_ms: int      # 计时
+created_at: datetime  # 当前时间
+```
+
+### Step 类型推断
+
+系统根据 LLM 输出内容自动推断类型,不需要显式声明:
+
+```python
+def infer_step_type(llm_response) -> StepType:
+    # 有工具调用 → action
+    if llm_response.tool_calls:
+        return "action"
+
+    # 调用了 step 工具且 complete=True → evaluation
+    if called_step_tool(llm_response, complete=True):
+        return "evaluation"
+
+    # 调用了 step 工具且 plan 不为空 → goal
+    if called_step_tool(llm_response, plan=True):
+        return "goal"
+
+    # 最终回复(无后续工具调用,对话结束)
+    if is_final_response(llm_response):
+        return "response"
+
+    # 默认:中间思考
+    return "thought"
+```
+
+### description 提取
+
+`description` 字段由系统从 LLM 输出中提取:
+
+| Step 类型 | description 来源 |
+|----------|-----------------|
+| `goal` | step 工具的 plan 参数 |
+| `thought` | LLM 输出的第一句话(或截断) |
+| `action` | 工具名 + 关键参数 |
+| `result` | 工具返回的 title 或简要输出 |
+| `evaluation` | step 工具的 summary 参数 |
+| `response` | LLM 输出的第一句话(或截断) |
+
+---
+
+## 计划管理工具
+
+### step 工具
+
+模型通过 `step` 工具管理执行进度:
+
+```python
+@tool
+def step(
+    plan: Optional[List[str]] = None,     # 添加 planned goals
+    focus: Optional[str] = None,          # 切换焦点到哪个 goal
+    complete: bool = False,               # 完成当前 goal
+    summary: Optional[str] = None,        # 评估总结(配合 complete)
+):
+    """管理执行步骤"""
+```
+
+### 使用示例
+
+```python
+# 1. 创建计划
+step(plan=["探索代码库", "修改配置", "运行测试"])
+
+# 2. 开始执行第一个
+step(focus="探索代码库")
+
+# 3. [执行各种 tool_call...]
+
+# 4. 完成并切换到下一个
+step(complete=True, summary="主配置在 /src/config.yaml", focus="修改配置")
+
+# 5. 中途调整计划
+step(plan=["备份配置"])  # 追加新的 goal
+```
+
+### 状态变化
+
+```
+调用 step(plan=["A", "B", "C"]) 后:
+├── goal: "A" (planned)
+├── goal: "B" (planned)
+└── goal: "C" (planned)
+
+调用 step(focus="A") 后:
+├── goal: "A" (in_progress) ← 当前焦点
+├── goal: "B" (planned)
+└── goal: "C" (planned)
+
+调用 step(complete=True, summary="...", focus="B") 后:
+├── goal: "A" (completed)
+│   └── evaluation: "..." ← 自动创建
+├── goal: "B" (in_progress) ← 新焦点
+└── goal: "C" (planned)
+```
+
+---
+
+## Context 管理
+
+### 信息分层
+
+不同用途需要不同的信息粒度:
+
+| 用途 | 选择哪些节点 | 详略程度 |
+|------|-------------|---------|
+| **Todo 列表** | 仅 `goal` 类型 | 简略:描述 + 状态 |
+| **历史压缩** | `goal` + `result` + `evaluation` | 详细:包含关键结果 |
+
+### Todo 格式(简略)
+
+```python
+def to_todo_string(tree: StepTree) -> str:
+    lines = []
+    for goal in tree.filter(step_type="goal"):
+        icon = {"completed": "✓", "in_progress": "→", "planned": " "}[goal.status]
+        lines.append(f"[{icon}] {goal.description}")
+    return "\n".join(lines)
+```
+
+输出:
+```
+[✓] 探索代码库
+[→] 修改配置
+[ ] 运行测试
+```
+
+### 历史压缩格式(详细)
+
+```python
+def to_history_string(tree: StepTree) -> str:
+    lines = []
+    for goal in tree.filter(step_type="goal"):
+        status_label = {"completed": "完成", "in_progress": "进行中", "planned": "待做"}
+        lines.append(f"[{status_label[goal.status]}] {goal.description}")
+
+        if goal.status == "completed":
+            # 选择关键结果节点
+            for step in goal.children():
+                if step.step_type == "result":
+                    lines.append(f"  → {extract_brief(step.data)}")
+                elif step.step_type == "evaluation":
+                    lines.append(f"  总结: {step.summary}")
+
+    return "\n".join(lines)
+```
+
+输出:
+```
+[完成] 探索代码库
+  → glob_files: 找到 15 个文件
+  → read_file(config.yaml): db_host=prod.db.com
+  总结: 主配置在 /src/config.yaml,包含数据库连接配置
+
+[进行中] 修改配置
+  → read_file(config.yaml): 已读取
+
+[待做] 运行测试
+```
+
+### 压缩触发
+
+```python
+def build_messages(messages: List, tree: StepTree) -> List:
+    # 正常情况:不压缩
+    if estimate_tokens(messages) < MAX_CONTEXT * 0.7:
+        return messages
+
+    # 超限时:用树摘要替代历史详情
+    history_summary = tree.to_history_string()
+    summary_msg = {"role": "assistant", "content": history_summary}
+
+    # 保留最近的详细消息
+    return [summary_msg] + recent_messages(messages)
+```
+
+### 按需读取
+
+模型可通过工具读取当前进度,而非每次都注入:
+
+```python
+@tool
+def read_progress() -> str:
+    """读取当前执行进度"""
+    return tree.to_todo_string()
+```
+
+**策略**:
+- 正常情况:模型通过 `read_progress` 按需读取(省 context)
+- 压缩时:自动注入详细历史摘要(保证不丢失)
+
+---
+
+## 可视化支持
+
+树结构天然支持可视化:
+
+- **折叠**:折叠某个 `goal` 节点 → 隐藏其子节点
+- **展开**:展示子节点详情
+- **回溯**:`failed` 或 `skipped` 状态的分支
+- **并行**:同一 `goal` 下的多个 `action`(并行工具调用)
+
+### 边的信息
+
+可视化时,边(连接线)可展示:
+- 执行时间:`Step.duration_ms`
+- 成本:`Step.cost`
+- 简要描述:`Step.description`
+
+---
+
+## 与 OpenCode 的对比
+
+| 方面 | OpenCode | 本设计 |
+|------|----------|--------|
+| 计划存储 | Markdown 文件 + Todo 列表 | Step 树(`planned` 状态) |
+| 计划与执行关联 | 无结构化关联 | 统一在树结构中 |
+| 进度读取 | `todoread` 工具 | `read_progress` 工具 |
+| 进度更新 | `todowrite` 工具 | `step` 工具 |
+| Context 压缩 | 无 | 基于树结构自动压缩 |
+
+**参考**:OpenCode 的实现见 `src/tool/todo.ts`、`src/session/prompt.ts`
+
+---
+
+## Debug 工具
+
+### 实时查看 Step 树
+
+开发调试时,可通过 `dump_tree` 将完整的 Step 树输出到文件:
+
+```python
+from agent.debug import dump_tree
+
+# 每次 step 变化后调用
+dump_tree(trace, steps)
+
+# 自定义路径
+dump_tree(trace, steps, output_path=".debug/my_trace.txt")
+```
+
+### 查看方式
+
+```bash
+# 方式1:终端实时刷新
+watch -n 0.5 cat .trace/tree.txt
+
+# 方式2:VS Code 打开(自动刷新)
+code .trace/tree.txt
+```
+
+### 输出示例
+
+```
+============================================================
+ Step Tree Debug
+ Generated: 2024-01-15 14:30:25
+============================================================
+
+## Trace
+  trace_id: abc123
+  task: 修改配置文件
+  status: running
+  total_steps: 5
+  total_tokens: 1234
+  total_cost: 0.0150
+
+## Steps
+
+├── [✓] goal: 探索代码库
+│   id: a1b2c3d4...
+│   duration: 1234ms
+│   tokens: 500
+│   cost: $0.0050
+│   data:
+│     description: 探索代码库
+│   time: 14:30:10
+│
+│   ├── [✓] thought: 需要先了解项目结构
+│   │   id: e5f6g7h8...
+│   │   data:
+│   │     content: 让我先看看项目的目录结构...
+│   │   time: 14:30:11
+│   │
+│   ├── [✓] action: glob_files
+│   │   id: i9j0k1l2...
+│   │   duration: 50ms
+│   │   data:
+│   │     tool_name: glob_files
+│   │     arguments: {"pattern": "**/*.py"}
+│   │   time: 14:30:12
+│   │
+│   └── [✓] result: 找到 15 个文件
+│       id: m3n4o5p6...
+│       data:
+│         output: ["src/main.py", "src/config.py", ...]
+│       time: 14:30:12
+│
+└── [→] goal: 修改配置
+    id: q7r8s9t0...
+    time: 14:30:15
+```
+
+### JSON 格式输出
+
+用于程序化分析:
+
+```python
+from agent.debug import dump_json
+
+dump_json(trace, steps)  # 输出到 .trace/tree.json
+```
+
+**实现**:`agent/debug/tree_dump.py`
+
+---
+
+## 实现位置
+
+- Step 模型:`agent/models/trace.py:Step`(待更新)
+- step 工具:`agent/tools/builtin/step.py`(待实现)
+- read_progress 工具:`agent/tools/builtin/step.py`(待实现)
+- Context 压缩:`agent/context/compressor.py`(待实现)
+- Debug 工具:`agent/debug/tree_dump.py`(已实现)
+- **Core Skill**:`agent/skills/core.md`(已实现)
+
+## 未来扩展
+- 重试原因、重试次数、是否降级/兜底
+- 为什么选择某个动作\是否触发了skills、系统prompt中的策略

+ 0 - 3
examples/__init__.py

@@ -1,3 +0,0 @@
-"""
-Examples 包 - 使用样例
-"""

+ 0 - 62
examples/browser_use_setup_demo.py

@@ -1,62 +0,0 @@
-"""
-Browser-Use 自动设置演示
-
-展示如何使用自动检查和安装工具
-"""
-
-import asyncio
-from agent.skills.browser_use import (
-    check_browser_use,
-    install_browser_use_chromium
-)
-from agent.tools.builtin import skill
-
-
-async def demo():
-    """演示 browser-use 设置流程"""
-
-    print("=" * 60)
-    print("Browser-Use 自动设置演示")
-    print("=" * 60)
-
-    # 1. 加载 skill(会自动检查依赖)
-    print("\n1. 加载 browser-use skill(自动检查依赖)")
-    result = await skill(skill_name="browser-use")
-    print(f"✅ {result.title}")
-    if "⚠️" in result.output:
-        print("   检测到缺失的依赖,输出中包含安装提示")
-
-    # 2. 手动检查依赖
-    print("\n2. 手动检查依赖状态")
-    result = await check_browser_use()
-    print(f"✅ {result.title}")
-    print(f"   CLI 已安装: {result.metadata.get('cli_installed', False)}")
-    print(f"   Chromium 已安装: {result.metadata.get('chromium_installed', False)}")
-    print(f"   状态: {result.metadata.get('status', 'unknown')}")
-
-    # 3. 自动安装 Chromium(如果需要)
-    if not result.metadata.get("chromium_installed", False):
-        print("\n3. 安装 Chromium 浏览器(可选)")
-        print("   注意:这会下载 200-300MB 数据")
-
-        # 用户确认
-        confirm = input("   是否继续安装?(y/N): ")
-        if confirm.lower() == "y":
-            result = await install_browser_use_chromium()
-            print(f"   {result.title}")
-            if result.metadata.get("installed"):
-                print("   ✅ 安装成功")
-            else:
-                print("   ❌ 安装失败,请查看输出")
-        else:
-            print("   跳过安装")
-    else:
-        print("\n3. Chromium 已安装,跳过")
-
-    print("\n" + "=" * 60)
-    print("演示完成!")
-    print("=" * 60)
-
-
-if __name__ == "__main__":
-    asyncio.run(demo())

+ 5 - 2
examples/feature_extract/run.py

@@ -17,6 +17,7 @@ load_dotenv()
 
 
 from agent.prompts import SimplePrompt
 from agent.prompts import SimplePrompt
 from agent.runner import AgentRunner
 from agent.runner import AgentRunner
+from agent.storage import MemoryTraceStore
 from agent.llm.providers.gemini import create_gemini_llm_call
 from agent.llm.providers.gemini import create_gemini_llm_call
 
 
 
 
@@ -56,7 +57,9 @@ async def main():
     # 4. 创建 Agent Runner
     # 4. 创建 Agent Runner
     print("4. 创建 Agent Runner...")
     print("4. 创建 Agent Runner...")
     runner = AgentRunner(
     runner = AgentRunner(
-        llm_call=create_gemini_llm_call()
+        trace_store=MemoryTraceStore(),
+        llm_call=create_gemini_llm_call(),
+        debug=True  # 启用 debug,输出到 .trace/tree.txt
     )
     )
 
 
     # 5. 调用 Agent
     # 5. 调用 Agent
@@ -67,7 +70,7 @@ async def main():
         messages=messages,
         messages=messages,
         model=prompt.config.get('model', 'gemini-2.5-flash'),
         model=prompt.config.get('model', 'gemini-2.5-flash'),
         temperature=float(prompt.config.get('temperature', 0.3)),
         temperature=float(prompt.config.get('temperature', 0.3)),
-        trace=False  # 暂不记录 trace
+        trace=True  # 启用 trace,配合 debug 输出 step tree
     )
     )
 
 
     # 6. 输出结果
     # 6. 输出结果

+ 0 - 188
examples/tools_complete_demo.py

@@ -1,188 +0,0 @@
-"""
-完整工具系统使用示例
-
-演示基础工具和高级工具的使用。
-"""
-
-import asyncio
-from agent.tools.builtin import (
-    read_file,
-    edit_file,
-    write_file,
-    bash_command,
-    glob_files,
-    grep_content
-)
-
-
-async def demo_basic_tools():
-    """演示基础工具(Python 实现)"""
-
-    print("=" * 60)
-    print("基础工具演示")
-    print("=" * 60)
-
-    # 1. 读取文件
-    print("\n1. 读取文件")
-    result = await read_file(file_path="README.md", limit=20)
-    print(f"✅ {result.title}")
-    print(f"   前 5 行: {result.output[:200]}...")
-
-    # 2. 搜索文件
-    print("\n2. Glob 搜索")
-    result = await glob_files(pattern="**/*.py", path="agent/tools")
-    print(f"✅ {result.title}")
-    print(f"   找到 {result.metadata['count']} 个文件")
-
-    # 3. 内容搜索
-    print("\n3. Grep 搜索")
-    result = await grep_content(
-        pattern="async def",
-        path="agent/tools/builtin",
-        include="*.py"
-    )
-    print(f"✅ {result.title}")
-    print(f"   找到 {result.metadata['matches']} 个匹配")
-
-    # 4. 执行命令
-    print("\n4. Bash 命令")
-    result = await bash_command(
-        command="git status --short",
-        timeout=10
-    )
-    print(f"✅ {result.title}")
-    print(f"   退出码: {result.metadata['exit_code']}")
-
-    # 5. 编辑文件(演示智能匹配)
-    print("\n5. 智能编辑(9 种策略)")
-
-    # 创建测试文件
-    test_content = """
-def hello():
-    print("Hello")
-
-def world():
-    print("World")
-"""
-    await write_file(file_path="/tmp/test_edit.py", content=test_content)
-
-    # 编辑:忽略缩进(会使用 IndentationFlexibleReplacer)
-    result = await edit_file(
-        file_path="/tmp/test_edit.py",
-        old_string='def hello():\nprint("Hello")',  # 缩进不同
-        new_string='def hello():\n    print("Hello, World!")'
-    )
-    print(f"✅ {result.title}")
-    print(f"   Diff:\n{result.metadata['diff'][:200]}...")
-
-
-async def demo_advanced_tools():
-    """演示高级工具(Bun 适配器)"""
-
-    print("\n" + "=" * 60)
-    print("高级工具演示(需要 Bun)")
-    print("=" * 60)
-
-    try:
-        from agent.tools.advanced import webfetch, lsp_diagnostics
-
-        # 1. 网页抓取
-        print("\n1. 网页抓取 (HTML -> Markdown)")
-        result = await webfetch(
-            url="https://example.com",
-            format="markdown"
-        )
-        print(f"✅ {result.title}")
-        print(f"   内容长度: {len(result.output)} 字符")
-
-        # 2. LSP 诊断
-        print("\n2. LSP 诊断")
-        result = await lsp_diagnostics(
-            file_path="agent/tools/builtin/edit.py"
-        )
-        print(f"✅ {result.title}")
-        print(f"   诊断结果: {result.output[:200]}...")
-
-    except Exception as e:
-        print(f"⚠️  高级工具需要 Bun 运行时: {e}")
-        print("   安装: curl -fsSL https://bun.sh/install | bash")
-
-
-async def demo_edit_strategies():
-    """演示 edit_file 的 9 种匹配策略"""
-
-    print("\n" + "=" * 60)
-    print("edit_file 策略演示")
-    print("=" * 60)
-
-    test_cases = [
-        {
-            "name": "策略 1: 精确匹配",
-            "content": "DEBUG = True\nVERBOSE = False",
-            "old": "DEBUG = True",
-            "new": "DEBUG = False"
-        },
-        {
-            "name": "策略 2: 忽略行首尾空白",
-            "content": "  DEBUG = True  \nVERBOSE = False",
-            "old": "DEBUG = True",  # 无空白
-            "new": "DEBUG = False"
-        },
-        {
-            "name": "策略 4: 空白归一化",
-            "content": "DEBUG  =   True",
-            "old": "DEBUG = True",  # 单空格
-            "new": "DEBUG = False"
-        },
-        {
-            "name": "策略 5: 灵活缩进",
-            "content": """
-def foo():
-    if True:
-        print("hello")
-""",
-            "old": "if True:\nprint(\"hello\")",  # 无缩进
-            "new": "if True:\n    print(\"world\")"
-        }
-    ]
-
-    for i, test in enumerate(test_cases, 1):
-        print(f"\n{i}. {test['name']}")
-
-        # 创建测试文件
-        test_file = f"/tmp/test_strategy_{i}.py"
-        await write_file(file_path=test_file, content=test["content"])
-
-        # 执行编辑
-        try:
-            result = await edit_file(
-                file_path=test_file,
-                old_string=test["old"],
-                new_string=test["new"]
-            )
-            print(f"   ✅ 成功匹配")
-        except Exception as e:
-            print(f"   ❌ 失败: {e}")
-
-
-async def main():
-    """运行所有演示"""
-
-    print("\n🚀 工具系统完整演示\n")
-
-    # 基础工具
-    await demo_basic_tools()
-
-    # 编辑策略
-    await demo_edit_strategies()
-
-    # 高级工具
-    await demo_advanced_tools()
-
-    print("\n" + "=" * 60)
-    print("演示完成!")
-    print("=" * 60)
-
-
-if __name__ == "__main__":
-    asyncio.run(main())

+ 0 - 581
examples/tools_examples.py

@@ -1,581 +0,0 @@
-"""
-工具系统完整示例
-
-本文件展示 @tool 装饰器的所有用法,包括:
-
-## 基础功能
-1. 最简形式
-2. 带 i18n 展示信息
-3. 带可编辑参数
-4. 需要用户确认
-5. 带 context 参数
-6. 同步工具
-7. 复杂返回类型
-
-## 高级功能
-8. 域名过滤(URL Patterns)
-9. 敏感数据处理(<secret> 占位符 + TOTP)
-10. 工具使用统计
-11. 组合所有功能
-
-注意:
-- uid 参数会由框架自动注入,不需要用户传递
-- context 参数用于传递额外上下文(如浏览器会话、当前 URL 等)
-- 返回值可以是字符串、字典或 ToolResult
-"""
-
-import asyncio
-import json
-from typing import List, Dict, Any, Optional
-from agent import tool, ToolResult, ToolContext, get_tool_registry
-
-
-# ============================================================
-# 基础功能示例
-# ============================================================
-
-# 1. 最简形式
-@tool()
-async def hello_world(name: str, uid: str = "") -> Dict[str, str]:
-	"""
-	最简单的工具示例
-
-	Args:
-		name: 要问候的名字
-		uid: 用户ID(自动注入)
-
-	Returns:
-		包含问候语的字典
-	"""
-	return {"greeting": f"Hello, {name}!"}
-
-
-# 2. 带 i18n 展示信息的工具
-@tool(
-	display={
-		"zh": {
-			"name": "搜索内容",
-			"params": {
-				"query": "搜索关键词",
-				"limit": "返回数量"
-			}
-		},
-		"en": {
-			"name": "Search Content",
-			"params": {
-				"query": "Search query",
-				"limit": "Number of results"
-			}
-		}
-	}
-)
-async def search_content(
-	query: str,
-	limit: int = 10,
-	uid: str = ""
-) -> List[Dict[str, Any]]:
-	"""
-	搜索用户的内容
-
-	使用语义搜索查找相关内容。display 参数用于前端展示:
-	- 工具名称会根据用户语言显示为"搜索内容"或"Search Content"
-	- 参数名称也会相应翻译
-
-	Args:
-		query: 搜索查询文本
-		limit: 返回结果数量(默认10)
-		uid: 用户ID(自动注入)
-
-	Returns:
-		搜索结果列表,每个包含 id, title, content, score
-	"""
-	# 实际实现中会调用向量搜索
-	return [
-		{
-			"id": "doc_001",
-			"title": f"关于 {query} 的文档",
-			"content": f"这是与 {query} 相关的内容...",
-			"score": 0.95
-		}
-	]
-
-
-# 3. 带可编辑参数的工具
-@tool(
-	editable_params=["query", "filters"],
-	display={
-		"zh": {
-			"name": "高级搜索",
-			"params": {
-				"query": "搜索关键词",
-				"filters": "过滤条件",
-				"sort_by": "排序方式"
-			}
-		}
-	}
-)
-async def advanced_search(
-	query: str,
-	filters: Optional[Dict[str, Any]] = None,
-	sort_by: str = "relevance",
-	limit: int = 20,
-	uid: str = ""
-) -> Dict[str, Any]:
-	"""
-	高级搜索工具(允许用户编辑参数)
-
-	editable_params 指定哪些参数允许用户在 LLM 生成后编辑:
-	- LLM 会先生成 query 和 filters
-	- 用户可以在确认前修改这些参数
-	- 适用于搜索、创建等需要用户微调的场景
-
-	Args:
-		query: 搜索查询
-		filters: 过滤条件(如 {"type": "note", "date_range": "7d"})
-		sort_by: 排序方式(relevance/date/title)
-		limit: 返回数量
-		uid: 用户ID(自动注入)
-
-	Returns:
-		搜索结果和元数据
-	"""
-	return {
-		"results": [
-			{"id": "1", "title": "Result 1", "score": 0.9},
-			{"id": "2", "title": "Result 2", "score": 0.8},
-		],
-		"total": 42,
-		"query": query,
-		"filters_applied": filters or {},
-		"sort_by": sort_by
-	}
-
-
-# 4. 需要用户确认的危险操作
-@tool(
-	requires_confirmation=True,
-	display={
-		"zh": {
-			"name": "删除内容",
-			"params": {
-				"content_id": "内容ID",
-				"permanent": "永久删除"
-			}
-		}
-	}
-)
-async def delete_content(
-	content_id: str,
-	permanent: bool = False,
-	uid: str = ""
-) -> Dict[str, Any]:
-	"""
-	删除内容(需要用户确认)
-
-	requires_confirmation=True 表示这是一个危险操作:
-	- LLM 调用此工具时,不会立即执行
-	- 会先向用户展示操作详情,等待确认
-	- 用户确认后才会真正执行
-
-	适用场景:删除操作、发送消息、修改重要设置、任何不可逆操作
-
-	Args:
-		content_id: 要删除的内容ID
-		permanent: 是否永久删除(False=移到回收站)
-		uid: 用户ID(自动注入)
-
-	Returns:
-		删除结果
-	"""
-	return {
-		"success": True,
-		"content_id": content_id,
-		"permanent": permanent,
-		"message": f"内容 {content_id} 已{'永久删除' if permanent else '移到回收站'}"
-	}
-
-
-# 5. 带 context 参数的工具
-@tool(
-	display={
-		"zh": {"name": "获取相关推荐", "params": {"top_k": "推荐数量"}}
-	}
-)
-async def get_recommendations(
-	top_k: int = 5,
-	uid: str = "",
-	context: Optional[Dict[str, Any]] = None
-) -> List[Dict[str, Any]]:
-	"""
-	获取相关推荐(使用 context 获取额外信息)
-
-	context 参数用于传递执行上下文,由框架自动注入:
-	- 当前阅读位置 (current_location)
-	- 当前会话 ID (session_id)
-	- 排除的内容 ID (exclude_ids)
-
-	Args:
-		top_k: 返回推荐数量
-		uid: 用户ID(自动注入)
-		context: 执行上下文(自动注入)
-
-	Returns:
-		推荐列表
-	"""
-	current_location = None
-	if context:
-		current_location = context.get("current_location")
-
-	return [
-		{
-			"id": "rec_001",
-			"title": "推荐内容 1",
-			"reason": f"基于当前位置 {current_location}" if current_location else "基于您的兴趣"
-		}
-	]
-
-
-# 6. 同步工具(非 async)
-@tool()
-def format_text(
-	text: str,
-	format_type: str = "markdown",
-	uid: str = ""
-) -> str:
-	"""
-	格式化文本(同步工具)
-
-	不需要 async 的工具可以定义为普通函数。
-	框架会自动检测并正确调用。
-
-	适用于:纯计算操作、文本处理、不需要 I/O 的操作
-
-	Args:
-		text: 要格式化的文本
-		format_type: 格式类型(markdown/plain/html)
-		uid: 用户ID(自动注入)
-
-	Returns:
-		格式化后的文本
-	"""
-	if format_type == "markdown":
-		return f"**{text}**"
-	elif format_type == "html":
-		return f"<p>{text}</p>"
-	else:
-		return text
-
-
-# 7. 使用 ToolResult 的工具
-@tool()
-async def analyze_content(
-	content_id: str,
-	analysis_types: Optional[List[str]] = None,
-	uid: str = ""
-) -> ToolResult:
-	"""
-	分析内容(使用 ToolResult)
-
-	ToolResult 支持双层记忆管理:
-	- output: 完整结果(可能很长)
-	- long_term_memory: 简短摘要(永久保存)
-
-	Args:
-		content_id: 要分析的内容ID
-		analysis_types: 分析类型列表(sentiment/keywords/summary)
-		uid: 用户ID(自动注入)
-
-	Returns:
-		ToolResult 包含分析结果
-	"""
-	types = analysis_types or ["sentiment", "keywords"]
-
-	result = {
-		"content_id": content_id,
-		"analyses": {}
-	}
-
-	if "sentiment" in types:
-		result["analyses"]["sentiment"] = {
-			"score": 0.8,
-			"label": "positive",
-			"confidence": 0.92
-		}
-
-	if "keywords" in types:
-		result["analyses"]["keywords"] = [
-			{"word": "AI", "weight": 0.9},
-			{"word": "学习", "weight": 0.7}
-		]
-
-	return ToolResult(
-		title=f"Analysis of {content_id}",
-		output=json.dumps(result, indent=2, ensure_ascii=False),
-		long_term_memory=f"Analyzed {content_id}: {', '.join(types)}",
-		metadata={"types": types}
-	)
-
-
-# ============================================================
-# 高级功能示例
-# ============================================================
-
-# 8. 域名过滤示例
-@tool(url_patterns=["*.google.com", "www.google.*"])
-async def google_search(query: str, uid: str = "") -> ToolResult:
-	"""
-	Google 搜索(仅在 Google 页面可用)
-
-	使用 url_patterns 限制工具只在特定域名显示。
-	在 Google 页面时,此工具会出现在可用工具列表中。
-	在其他页面时,此工具会被过滤掉。
-
-	Args:
-		query: 搜索查询
-		uid: 用户ID(自动注入)
-
-	Returns:
-		搜索结果
-	"""
-	return ToolResult(
-		title="Google Search",
-		output=f"Searching Google for: {query}",
-		long_term_memory=f"Searched Google for '{query}'"
-	)
-
-
-@tool(url_patterns=["*.github.com"])
-async def create_github_issue(
-	title: str,
-	body: str,
-	uid: str = ""
-) -> ToolResult:
-	"""
-	创建 GitHub Issue(仅在 GitHub 页面可用)
-
-	Args:
-		title: Issue 标题
-		body: Issue 内容
-		uid: 用户ID(自动注入)
-
-	Returns:
-		创建结果
-	"""
-	return ToolResult(
-		title="Issue Created",
-		output=f"Created issue: {title}",
-		long_term_memory=f"Created GitHub issue: {title}"
-	)
-
-
-@tool()  # 无 url_patterns,所有页面都可用
-async def take_screenshot(uid: str = "") -> ToolResult:
-	"""截图(所有页面都可用)"""
-	return ToolResult(
-		title="Screenshot",
-		output="Screenshot taken",
-		attachments=["screenshot_001.png"]
-	)
-
-
-# 9. 敏感数据处理示例
-@tool(url_patterns=["*.github.com"])
-async def github_login(
-	username: str,
-	password: str,
-	totp_code: str,
-	uid: str = ""
-) -> ToolResult:
-	"""
-	GitHub 登录(支持敏感数据占位符)
-
-	LLM 会输出类似:
-	{
-		"username": "user@example.com",
-		"password": "<secret>github_password</secret>",
-		"totp_code": "<secret>github_2fa_bu_2fa_code</secret>"
-	}
-
-	执行时会自动替换为实际值。
-
-	Args:
-		username: 用户名
-		password: 密码(可以是占位符)
-		totp_code: TOTP 验证码(可以是占位符,自动生成)
-		uid: 用户ID(自动注入)
-
-	Returns:
-		登录结果
-	"""
-	# 注意:password 和 totp_code 在到达这里时已经被替换
-	return ToolResult(
-		title="Login Successful",
-		output=f"Logged in as {username}",
-		long_term_memory=f"Logged in to GitHub as {username}"
-	)
-
-
-# 10. 组合所有功能
-@tool(
-	url_patterns=["*.example.com"],
-	requires_confirmation=True,
-	editable_params=["message"],
-	display={
-		"zh": {
-			"name": "发送认证消息",
-			"params": {
-				"recipient": "接收者",
-				"message": "消息内容",
-				"api_key": "API密钥"
-			}
-		}
-	}
-)
-async def send_authenticated_message(
-	recipient: str,
-	message: str,
-	api_key: str,
-	ctx: ToolContext,
-	uid: str = ""
-) -> ToolResult:
-	"""
-	发送消息(组合多个功能)
-
-	展示所有高级功能:
-	- 仅在 example.com 可用(域名过滤)
-	- 需要用户确认(危险操作)
-	- 消息可编辑(用户微调)
-	- API key 使用敏感数据占位符
-	- 使用 ToolContext 获取上下文
-
-	Args:
-		recipient: 接收者
-		message: 消息内容
-		api_key: API密钥(可以是占位符)
-		ctx: 工具上下文
-		uid: 用户ID(自动注入)
-
-	Returns:
-		发送结果
-	"""
-	# api_key 会从 <secret>api_key</secret> 替换为实际值
-	# ctx 包含 page_url, browser_session 等信息
-
-	return ToolResult(
-		title="Message Sent",
-		output=f"Sent to {recipient}: {message}",
-		long_term_memory=f"Sent message to {recipient} on {ctx.page_url}",
-		metadata={"recipient": recipient}
-	)
-
-
-# ============================================================
-# 使用示例
-# ============================================================
-
-async def main():
-	registry = get_tool_registry()
-
-	print("=" * 60)
-	print("工具系统完整示例")
-	print("=" * 60)
-
-	# ============================================================
-	# 示例 1:基础工具调用
-	# ============================================================
-	print("\n1. 基础工具调用")
-	print("-" * 60)
-
-	result = await registry.execute("hello_world", {"name": "Alice"})
-	print(f"hello_world: {result}")
-
-	result = await registry.execute("search_content", {"query": "Python", "limit": 5})
-	print(f"search_content: {result}")
-
-	# ============================================================
-	# 示例 2:域名过滤
-	# ============================================================
-	print("\n\n2. 域名过滤示例")
-	print("-" * 60)
-
-	# 在 Google 页面
-	google_url = "https://www.google.com/search?q=test"
-	google_tools = registry.get_tool_names(google_url)
-	print(f"在 {google_url} 可用的工具:")
-	print(f"  包含 google_search: {'google_search' in google_tools}")
-
-	# 在 GitHub 页面
-	github_url = "https://github.com/user/repo"
-	github_tools = registry.get_tool_names(github_url)
-	print(f"\n在 {github_url} 可用的工具:")
-	print(f"  包含 create_github_issue: {'create_github_issue' in github_tools}")
-	print(f"  包含 google_search: {'google_search' in github_tools}")
-
-	# ============================================================
-	# 示例 3:敏感数据处理
-	# ============================================================
-	print("\n\n3. 敏感数据处理示例")
-	print("-" * 60)
-
-	# 配置敏感数据
-	sensitive_data = {
-		"*.github.com": {
-			"github_password": "my_secret_password",
-			"github_2fa_bu_2fa_code": "JBSWY3DPEHPK3PXP"  # TOTP secret
-		}
-	}
-
-	# 模拟 LLM 输出(包含占位符)
-	llm_output_args = {
-		"username": "user@example.com",
-		"password": "<secret>github_password</secret>",
-		"totp_code": "<secret>github_2fa_bu_2fa_code</secret>"
-	}
-
-	print("LLM 输出的参数(包含占位符):")
-	print(f"  {llm_output_args}")
-
-	# 执行工具(自动替换敏感数据)
-	result = await registry.execute(
-		"github_login",
-		llm_output_args,
-		context={"page_url": "https://github.com/login"},
-		sensitive_data=sensitive_data
-	)
-
-	print(f"\n执行结果(密码已替换):")
-	print(f"  {result}")
-
-	# ============================================================
-	# 示例 4:工具统计
-	# ============================================================
-	print("\n\n4. 工具统计示例")
-	print("-" * 60)
-
-	# 模拟多次调用
-	for i in range(5):
-		await registry.execute("google_search", {"query": f"test {i}"})
-
-	await registry.execute("take_screenshot", {})
-	await registry.execute("take_screenshot", {})
-
-	# 查看统计
-	stats = registry.get_stats()
-	print("工具使用统计:")
-	for tool_name, tool_stats in stats.items():
-		if tool_stats["call_count"] > 0:
-			print(f"\n  {tool_name}:")
-			print(f"    调用次数: {tool_stats['call_count']}")
-			print(f"    成功率: {tool_stats['success_rate']:.1%}")
-			print(f"    平均执行时间: {tool_stats['average_duration']:.3f}s")
-
-	# 获取 Top 工具
-	print("\n\nTop 3 最常用工具:")
-	top_tools = registry.get_top_tools(limit=3, by="call_count")
-	for i, tool_name in enumerate(top_tools, 1):
-		tool_stats = stats[tool_name]
-		print(f"  {i}. {tool_name} ({tool_stats['call_count']} 次调用)")
-
-
-if __name__ == "__main__":
-	asyncio.run(main())

+ 15 - 12
tests/test_runner.py

@@ -13,7 +13,7 @@ from agent import (
     tool,
     tool,
     get_tool_registry,
     get_tool_registry,
 )
 )
-from reson_agent.storage import MemoryTraceStore, MemoryMemoryStore
+from agent.storage import MemoryTraceStore, MemoryMemoryStore
 
 
 
 
 # 测试工具
 # 测试工具
@@ -21,7 +21,7 @@ from reson_agent.storage import MemoryTraceStore, MemoryMemoryStore
     editable_params=["query"],
     editable_params=["query"],
     display={"zh": {"name": "测试搜索", "params": {"query": "关键词"}}}
     display={"zh": {"name": "测试搜索", "params": {"query": "关键词"}}}
 )
 )
-async def test_search(query: str, limit: int = 10, uid: str = "") -> dict:
+async def mock_search(query: str, limit: int = 10, uid: str = "") -> dict:
     """测试搜索工具"""
     """测试搜索工具"""
     return {"results": [f"结果: {query}"], "count": 1}
     return {"results": [f"结果: {query}"], "count": 1}
 
 
@@ -43,7 +43,7 @@ async def mock_llm_call(
             "tool_calls": [{
             "tool_calls": [{
                 "id": "call_123",
                 "id": "call_123",
                 "function": {
                 "function": {
-                    "name": "test_search",
+                    "name": "mock_search",
                     "arguments": '{"query": "测试查询"}'
                     "arguments": '{"query": "测试查询"}'
                 }
                 }
             }],
             }],
@@ -74,14 +74,17 @@ class TestTraceAndStep:
     def test_step_create(self):
     def test_step_create(self):
         step = Step.create(
         step = Step.create(
             trace_id="trace_123",
             trace_id="trace_123",
-            step_type="llm_call",
+            step_type="thought",
             sequence=0,
             sequence=0,
-            data={"response": "hello"}
+            status="completed",
+            description="测试步骤",
+            data={"content": "hello"}
         )
         )
         assert step.step_id is not None
         assert step.step_id is not None
         assert step.trace_id == "trace_123"
         assert step.trace_id == "trace_123"
-        assert step.step_type == "llm_call"
-        assert step.data["response"] == "hello"
+        assert step.step_type == "thought"
+        assert step.status == "completed"
+        assert step.data["content"] == "hello"
 
 
 
 
 class TestMemoryStore:
 class TestMemoryStore:
@@ -136,18 +139,18 @@ class TestToolRegistry:
 
 
     def test_tool_registered(self):
     def test_tool_registered(self):
         registry = get_tool_registry()
         registry = get_tool_registry()
-        assert registry.is_registered("test_search")
+        assert registry.is_registered("mock_search")
 
 
     def test_get_schemas(self):
     def test_get_schemas(self):
         registry = get_tool_registry()
         registry = get_tool_registry()
-        schemas = registry.get_schemas(["test_search"])
+        schemas = registry.get_schemas(["mock_search"])
         assert len(schemas) == 1
         assert len(schemas) == 1
-        assert schemas[0]["function"]["name"] == "test_search"
+        assert schemas[0]["function"]["name"] == "mock_search"
 
 
     @pytest.mark.asyncio
     @pytest.mark.asyncio
     async def test_execute_tool(self):
     async def test_execute_tool(self):
         registry = get_tool_registry()
         registry = get_tool_registry()
-        result = await registry.execute("test_search", {"query": "hello"}, uid="test")
+        result = await registry.execute("mock_search", {"query": "hello"}, uid="test")
         assert "结果" in result
         assert "结果" in result
 
 
 
 
@@ -202,7 +205,7 @@ class TestAgentRunner:
         events = []
         events = []
         async for event in runner.run(
         async for event in runner.run(
             task="请搜索相关内容",
             task="请搜索相关内容",
-            tools=["test_search"],
+            tools=["mock_search"],
             agent_type="test"
             agent_type="test"
         ):
         ):
             events.append(event)
             events.append(event)