Lesson 09 · 协作

多个 agent 通过文件邮箱通信

从 subagent 到 teammate:一次性 → 持久;无名 → 有名字;无通信 → 文件邮箱。

⏱ 약 12 분 · 📝 3 개 인터랙티브 컴포넌트 · 🧑‍💻 기반 shareAI-lab · s09_agent_teams.py

Subagent 和 Teammate 有什么区别?

S04 的 subagent 简单粗暴:spawn → 跑 → 返回 summary → 死。一次性,没有名字,父子之间没有双向通信。

s09 的 teammate 是一个有名字、能被反复唤醒、能收发消息的独立 agent:

subagent (s04):  spawn -> execute -> return -> destroyed
teammate (s09):  spawn -> work -> idle -> work -> ... -> shutdown

两个机制的用途不同:

  • subagent 适合「做一件具体探索」(比如 review 一个 PR)。
  • teammate 适合「持续承担一个角色」(比如长期的 reviewer,每当有新 commit 就被叫醒)。

邮箱是一个 JSONL 文件

团队成员之间怎么通信?s09 用的是最土的机制:append-only JSONL 文件

.team/
  config.json          # 团队花名册
  inbox/
    alice.jsonl        # 给 alice 的信都 append 到这里
    bob.jsonl
    lead.jsonl

send 就是 open("alice.jsonl", "a").write(msg);read 就是读完整个文件、parse JSONL、然后 truncate 清空(drain 语义)。

为什么用文件,不用内存队列?文件天然持久。agent 重启、进程崩溃、甚至机器重启——邮件都还在。加上它可读可 grep,调试体验非常好。

看一条消息从 lead 到 alice 的完整流程

下面 widget 让你给 alice 发一条消息,看每一步磁盘上发生了什么。

5 种消息类型

s09 定义了 5 种消息类型(VALID_MSG_TYPES),但只实现了前两个,后三个在 s10 协议课才补上:

  • message — 普通文字消息。
  • broadcast — 发给除自己外所有 teammate。
  • shutdown_request / shutdown_response — 请求/响应优雅关机(s10)。
  • plan_approval_response — 计划审批(s10)。

为什么声明了但不实现?因为 s09 想把「协议扩展」做成开放式——消息类型是枚举,新加一个只需要在字典里加一条、在 _exec 路由里处理。

Interactive

Widget 1 · Mail Flow · 一条消息从 lead 到 alice

点 Send,追踪文件系统上的每个步骤:lead.send → append alice.jsonl → alice 下一轮读 inbox → truncate。

Interactive

Widget 2 · Team Config · 谁在线、状态什么

.team/config.json 是团队花名册。做几个操作(spawn / message / shutdown),看配置怎么变。

操作
.team/config.json

        
Interactive

Widget 3 · Subagent vs Teammate · 决定用哪个

6 个任务场景,决定用 subagent (spawn-then-die) 还是 teammate (spawn-then-live)。思考任务是否"一锤子买卖"。

答对 0 / 6