编程-学习-思考

s16:Team Protocols——队友之间要有约定

s16:Team Protocols——队友之间要有约定

“队友之间要有约定。” ——请求-响应模式驱动协商。

问题

s15 的队友能干活了,但协调是松散的:Lead 发消息,队友回复,没有结构化协议。

两个场景暴露了问题:

关机:Lead 想让 Alice 关机。直接杀线程,Alice 写了一半的文件留在磁盘上。需要握手:Lead 发请求,Alice 确认收尾后关机。

计划审批:Bob 想重构认证模块,属于高风险操作。应该先让 Lead 看 Bob 的计划,审批通过后再动手。

这两个场景结构完全一样:一方发请求,另一方给回复,请求和回复通过同一个 ID 关联,有状态机追踪:pending → approved / rejected。

解决方案

新增三样:

  • ProtocolState:请求状态追踪
  • dispatch_message:按消息类型路由到处理器
  • match_response:通过 request_id 关联回复与请求

两种协议一套机制:

协议 方向 用途
shutdown_request / response Lead → 队友 体面关机握手
plan_approval_request / response 队友 → Lead 计划审批

工作原理

ProtocolState: 请求状态

@dataclass
class ProtocolState:
    request_id: str      # 唯一 ID,如 "req_004281"
    type: str            # "shutdown" | "plan_approval"
    sender: str          # 发起方
    target: str          # 接收方
    status: str          # pending | approved | rejected
    payload: str         # 计划文本或关机原因
    created_at: float    # 时间戳

pending_requests: dict[str, ProtocolState] = {}

四步协议流程(以关机为例)

① Lead 发请求
   req_id = new_request_id()
   pending_requests[req_id] = ProtocolState(type="shutdown", status="pending")
   BUS.send("lead", "alice", "shutdown_request",
            metadata={"request_id": req_id})

② 队友收到 → dispatch
   inbox = BUS.read_inbox("alice")
   → 路由到 handle_shutdown_request()

③ 队友回复
   BUS.send("alice", "lead", "shutdown_response",
            metadata={"request_id": req_id, "approve": True})

④ Lead 收响应 → match
   match_response("shutdown_response", req_id, approve=True)
   pending_requests[req_id].status = "approved"

dispatch_message: 按类型路由

def handle_inbox_message(name, msg, messages):
    msg_type = msg.get("type", "message")
    req_id = msg.get("metadata", {}).get("request_id", "")

    if msg_type == "shutdown_request":
        BUS.send(name, "lead", "Shutting down.", "shutdown_response",
                 {"request_id": req_id, "approve": True})
        return True   # 停止循环

    if msg_type == "plan_approval_response":
        approve = msg["metadata"].get("approve", False)
        messages.append({"role": "user",
            "content": "[Plan approved]" if approve else "[Plan rejected]"})
    return False  # 继续循环

match_response: 类型校验

不只按 request_id 找状态,还会校验响应类型是否匹配请求类型:

def match_response(response_type, request_id, approve):
    state = pending_requests.get(request_id)
    if not state: return
    if state.type == "shutdown" and response_type != "shutdown_response":
        return  # type mismatch
    if state.type == "plan_approval" and response_type != "plan_approval_response":
        return
    if state.status != "pending":
        return  # already resolved
    state.status = "approved" if approve else "rejected"

一个 shutdown_response 不会意外 approve 一个 plan_approval 请求。

队友 idle loop

s15 的队友跑完 10 轮就退出。s16 的队友在 LLM 返回非 tool_use 后进入 idle 等待:轮询 inbox,收到 shutdown_request 就响应退出,收到新消息就继续工作:

LLM 返回非 tool_use
  → idle: 每秒轮询 inbox
  → 收到 shutdown_request → 回复 shutdown_response → 退出
  → 收到新消息 → 注入 messages → 继续 LLM turn

完整关机流程

1. Lead → spawn_teammate("alice", "backend", "创建 config.py")
2. alice → write_file("config.py") → 完成 → idle
3. Lead → shutdown_request("alice")
4. alice idle 轮询收到 → shutdown_response → 退出
5. Lead consume_lead_inbox → match_response → status = "approved"

对比上一章

组件 s15 s16
协调方式 松散文本消息 结构化请求-响应协议
请求追踪 无 ProtocolState + pending_requests
消息路由 全部当文本处理 dispatch_message 按类型分发
关机 自然退出或杀线程 request_id 握手机制
队友生命周期 最多 10 轮 idle loop(等待 inbox 消息)

试一下

python s16_team_protocols/code.py

试试:

  1. Spawn alice as a backend dev. Ask her to create a file. Then request her shutdown.
  2. Spawn bob with a refactoring task. Have him submit a plan first.

观察:关机握手是否完整(请求 → 确认 → 关机)?pending_requests 的状态是否正确转换?request_id 是否在请求和响应之间保持一致?

接下来

现在 Lead 必须给每个队友分配任务。”Alice 做这个,Bob 做那个”。任务看板上有 10 个未认领的任务,Lead 得手动 assign。

能不能让队友自己看板、自己认领?Lead 只需要创建任务,队友自己发现、自己认领、自己完成。

s17 Autonomous Agents → 队友自组织,不需要领导分配。