Skip to content

[bug] Orchestrator 调度 sub-agent 时上下文丢失 #251

@hrygo

Description

@hrygo

问题描述

Orchestrator 模式下,第二轮对话调度 sub-agent 时没有传递上一轮对话的上下文,导致搜索结果为空。

复现步骤

  1. 用户发送"最近记了什么笔记" → 路由到 memo agent → 搜索成功(10条结果)
  2. 用户发送"总结这些笔记" → 路由到 Orchestrator → Orchestrator 调度 memo agent → 搜索结果为 0

日志分析

第一轮:直接路由到 memo(有上下文)

msg="react: tool execution completed" tool=memo_search status=success duration_ms=8
result_count=10   ✅ 找到 10 条

第二轮:Orchestrator 调度 memo(丢失上下文)

executor: executing task ... purpose=检索相关笔记内容并进行总结
memo_filter_only result_count=0        ❌ 搜索结果为 0
memo_list_only result_count=50         ⚠️ 改为列出全部 50 条

详细时间线

时间 Block 事件 详情
11:44:54.581 - 用户发送消息 "最近记了什么笔记"
11:44:55.574 369 Block 创建 round_number=0
11:44:55.578 369 thinking #1 LLM 推理开始
11:44:58.076 369 tool_use memo_search, query="最近7天"
11:44:58.085 369 tool_result success, result_count=10
11:44:58.085 369 thinking #2 LLM 推理继续
11:45:37.759 369 answer #1-12 最终回答 (1321 chars)
11:45:37.773 369 Block 状态 status=completed
11:45:46.669 - 用户发送消息 "总结这些笔记"
11:45:46.684 370 Block 创建 round_number=1
11:45:46.690 370 Orchestrator decomposer start
11:46:02.531 370 任务分解 task_count=1, agent=memo
11:46:02.532 370 Executor 开始执行 task t1
11:46:20.340 370 tool_use memo_search
11:46:20.347 370 tool_result result_count=0
11:46:36.542 370 tool_result memo_list_only, result_count=50
11:47:04.847 370 Task 完成 duration=62316ms
11:47:04.871 370 Orchestrator 完成

字符时间线图

时间轴            Block 369                              Block 370
                  ├──────────────────────────────────────┤         ├────────────┤
                  │                                      │         │            │
                  │  think#1                             │         │            │
                  │    ├─ memo_search ─┤                 │         │            │
                  │    │  result=10 ✓  │                 │         │            │
                  │    │               │                 │         │            │
                  │    │              think#2            │         │            │
                  │    │                 │               │         │            │
                  │    │                 └─ answer ──────┤         │            │
                  │    │                              completed  │            │
                  │    │                                       │            │
                  │    │                                       │ think#1    │
                  │    │                                       │   ├─memo   │
                  │    │                                       │   │ search  │
                  │    │                                       │   │ 0 ❌   │
                  │    │                                       │   │       │
                  │    │                                       │   │ think#2│
                  │    │                                       │   │   ├─list│
                  │    │                                       │   │   │ 50  │
                  │    │                                       │   │   └─────┤
                  │    │                                       │            │
11:44:54     11:44:58    11:45:30         11:45:38        11:46:00    11:46:40    11:47:05
   │             │            │              │               │            │           │
   └─ 用户输入   └─ 工具调用  └─ LLM 生成回答          └─ 任务分解   └─ 搜索失败  └─ 完成
       "最近..."     "最近7天"     1321字符                t1 开始      降级list    Orch

根本原因

Orchestrator 在 executor.go 中调度 sub-agent 时,没有将当前 conversation 的 pending message(包含前一轮 block 369 的内容)传递给 sub-agent。

对比:

  • 直接路由:executeAgent 调用 contextBuilder.Build() → 有历史消息
  • Orchestrator 调度:executor.executeTask没有构建历史上下文

关键发现

┌─────────────────────────────────────────────────────────────────────────────┐
│  问题:第二轮 Orchestrator 调度时上下文丢失                                   │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│  Block 369 (第一轮)                                                        │
│  ├─ 有完整上下文 → memo_search("最近7天") → 10 条结果 ✅                    │
│                                                                             │
│  Block 370 (第二轮)                                                        │
│  ├─ Orchestrator 调度 memo                                                 │
│  ├─ 无第一轮上下文 → memo_search("最近7天") → 0 条结果 ❌                   │
│  └─ 降级到 memo_list_only → 50 条(全部)                                  │
│                                                                             │
│  时间对比:                                                                  │
│  ├─ 第一轮搜索: 2ms (11:44:58.077 → 11:44:58.085)                        │
│  └─ 第二轮搜索: 6ms (11:46:20.340 → 11:46:20.347) → 0 条                  │
│      ↓                                                                     │
│      搜索失败后再次调用 list,10ms (11:46:36.531 → 11:46:36.542)           │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘

修复方向

ai/agents/orchestrator/executor.goexecuteTask 方法中,需要在调用 sub-agent 前注入当前 conversation 的上下文:

// 应该在 executor 调用 memo agent前添加类似逻辑
previousBlocks := context.GetRecentMessages(conversation_id)
agentMessages := append(previousBlocks, userInput)

相关 Issue

测试用例

-- 验证修复后,第二轮 Orchestrator 调度应能访问第一轮的搜索结果上下文
-- 输入1: "最近记了什么笔记" 
-- 输入2: "总结这些笔记"  # 应能访问第一轮搜索到的 10 条笔记

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions