Conversation
参考 learn-claude-code (s01-s12) 体系重构 Agent 基础设施。 Phase 1 - Agent Loop + 工具层: - packages/agent_core/loop.py: 显式 AgentLoop 类,while + stop_reason 循环 - packages/agent_core/dispatcher.py: ToolDispatcher,工具注册表 + dispatch map - packages/agent_core/tools/: bash / filesystem handlers Phase 2 - 持久化 + Context: - packages/agent_core/tasks.py: TaskManager,JSON 文件持久化 + blockedBy 依赖图 - packages/agent_core/background.py: BackgroundTaskRunner,daemon 线程池 Phase 3 - Team 协作: - packages/agent_core/message_bus.py: MessageBus,JSONL 邮箱异步通信 - packages/agent_core/teammates.py: TeammateManager,持久 agent 线程管理 - packages/agent_core/protocols.py: TeamProtocols,shutdown/plan_approval FSM Skills (packages/agent_core/skills/): - agent-loop: AgentLoop 使用指南 - tool-dispatcher: 工具注册机制 - task-persistence: TaskManager 文件格式 - context-compaction: 3层压缩策略 - agent-teams: 多 agent 协作 - team-protocols: 通信协议 FSM README 引用 learn-claude-code 项目致谢。
- 添加 StreamingAgentLoop: 支持流式 LLM + SSE + 确认机制 - 添加 ConfirmationMixin: 接管 _CONFIRM_TOOLS pending 逻辑 - 添加 StreamingToolDispatcher: 适配生成器式 tool handlers - 添加 GlobalTrackerAdapter: global_tracker 兼容层 - 重构 agent_service.py: 使用新架构,保持原有接口不变 - 所有文件通过 py_compile 和 ruff check
- Context Compact (s06): 3层压缩策略,支持无限长度会话 - Layer1: 消息摘要 (LLM生成) - Layer2: 关键决策提取 - Layer3: 元信息压缩 - CompactingStreamingAgentLoop 混入类 - TodoWrite (s03): 统一任务计划接口 - TodoManager: JSON持久化,支持层级结构(parent_id) - PlannerMixin: 计划生成混入类 - 格式规范: [WHERE] [HOW] to [WHY] — expect [RESULT] - Subagents (s04): 增强型子Agent并行调度 - SubagentRunner: 独立线程执行,支持sync/async - SubagentPool: 并发控制(max_concurrent) - 全局单例 get_subagent_pool() - agent_service.py: 导出所有新模块供consumer使用 - 所有文件通过 py_compile 和 ruff check
修复的问题: 1. tool消息丢失:每个tool_result事件立即保存到DB 2. 消息累积逻辑错误:现在保存所有user/assistant/tool消息 3. SSE正则bug:修复 (.+?) DOTALL模式下可能匹配异常 4. 历史消息恢复:API层从DB加载消息传给stream_chat 5. stream_chat返回类型:改为 tuple[Iterator, conversation] 6. schemas: AgentMessage添加meta字段支持 涉及的变更: - agent.py: 重写消息保存逻辑,修复SSE解析 - agent_service.py: stream_chat/confirm_action/reject_action返回conversation - schemas.py: AgentMessage.meta字段
在 session_scope 外部访问 PipelineRun ORM 对象导致 DetachedInstanceError。 将 recent_runs 列表推导式移到 with scope 内部,确保对象 attached 时访问属性。
问题原因: - agent_core.loop.PaperMindToolResult 和 agent_tools.ToolResult 是两个同名不同模块的类 - _execute_and_emit/execute_confirmed_action/execute_and_continue 中的 isinstance 检查只匹配 PaperMindToolResult - execute_tool_stream 返回的是 agent_tools.ToolResult,导致 isinstance 返回 False - 结果:result 一直是默认值 "无结果",工具看起来"调用失败" 修复方案: - 增加 hasattr duck-typing 检查,兼容两种 ToolResult 类
🔍 OpenCode PR Review Required这是一个受保护的分支,merge 前需要进行 code review。 请运行以下命令进行 OpenCode review: 或者在 PR 页面评论 This is an automated reminder from PR Review Gate. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Changes
packages/agent_core/: 新增 Agent Harness 核心模块apps/api/routers/agent.py: 重构消息持久化逻辑packages/ai/agent_service.py: 接入 StreamingAgentLooppackages/domain/schemas.py: AgentMessage 添加 meta 字段