Skip to content

s09 代码不是很严谨,子 agent 的循环消费可能会有运行异常。 #138

@maquannene

Description

@maquannene

我输入了以下指令: 生成两个成员,姓名分别为 A 和 B。等待 60 秒后,然后让 A 发给 B 一条消息,你好。

看 _teammate_loop 的逻辑(第 166-204 行):

def _teammate_loop(self, name: str, role: str, prompt: str):

      for _ in range(50):  # 最多循环 50 次

          inbox = BUS.read_inbox(name)

          for msg in inbox:

              messages.append({"role": "user", "content": json.dumps(msg)})

          response = client.messages.create(...)

          if response.stop_reason != "tool_use":

              break  # 没有工具调用就退出循环

问题分析:

  1. 立即执行:spawn 后,子 agent 立即开始处理 prompt
  2. 快速退出:如果 prompt 是"等待任务",agent 可能不会调用任何工具,直接返回文本说"我准备好了"
  3. 循环终止:stop_reason != "tool_use" 导致循环退出
  4. 消息丢失:60 秒后消息到达 inbox 时,子 agent 的循环早已结束

典型场景:
时间线:
t=0s: 用户输入 "生成 A 和 B,等待 60 秒后让 A 发消息给 B"
t=1s: lead spawn A 和 B,prompt 是"等待指令"
t=2s: A 的循环:读取 inbox(空)-> LLM 说"好的我准备好了" -> stop_reason="end_turn" -> 循环结束
t=2s: B 的循环:同上 -> 循环结束
t=3s: lead 说"好的,我会等待 60 秒"
...
t=60s: lead 发送消息到 A 的 inbox
t=61s: 消息静静躺在 inbox 中,但 A 的线程早已结束

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions