Skip to content

feat(agent): Agent Harness 架构重构 + 消息持久化修复#11

Merged
Color2333 merged 6 commits intomainfrom
dev
Mar 20, 2026
Merged

feat(agent): Agent Harness 架构重构 + 消息持久化修复#11
Color2333 merged 6 commits intomainfrom
dev

Conversation

@Color2333
Copy link
Copy Markdown
Owner

Summary

  • 参考 learn-claude-code (s01-s12) 重构 Agent 核心架构
  • 实现 Context Compact(3层压缩)、TodoWrite(统一任务计划)、Subagents(并行调度)
  • 重构 agent_service 使用 StreamingAgentLoop + 新架构
  • 修复消息持久化丢失、SSE解析bug
  • 修复 get_system_status SQLAlchemy session detached bug
  • 修复 StreamingAgentLoop 忽略 tool result 的严重 bug

Changes

  • packages/agent_core/: 新增 Agent Harness 核心模块
  • apps/api/routers/agent.py: 重构消息持久化逻辑
  • packages/ai/agent_service.py: 接入 StreamingAgentLoop
  • packages/domain/schemas.py: AgentMessage 添加 meta 字段

参考 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 类
@github-actions
Copy link
Copy Markdown

🔍 OpenCode PR Review Required

这是一个受保护的分支,merge 前需要进行 code review。

请运行以下命令进行 OpenCode review:

/oc review https://github.com/Color2333/PaperMind/pull/$PR_NUM

或者在 PR 页面评论 /oc 来触发 OpenCode review。


This is an automated reminder from PR Review Gate.

@Color2333 Color2333 merged commit 21852ca into main Mar 20, 2026
2 checks passed
@Color2333 Color2333 deleted the dev branch March 20, 2026 05:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant