编程-学习-思考

s17:Autonomous Agents——自己看板,自己认领

s17:Autonomous Agents——自己看板,自己认领

“自己看板,自己认领。” ——空闲时轮询,有活就干。

问题

s16 的队友能通信、能握手关机。但每个队友等 Lead 分配任务——如果任务看板上有 10 个未认领任务,Lead 得手动 assign 10 次。这不能扩展。

队友应该自己看任务看板,发现没人做的任务就认领,做完再找下一个。

解决方案

新增三样:

  • idle_poll:空闲时每 5 秒轮询
  • scan_unclaimed_tasks:扫描看板上可认领的任务
  • 自动认领:找到任务就 claim,不用 Lead 操心

队友生命周期从两阶段变成三阶段:

阶段 行为 退出条件
WORK inbox → LLM → 工具循环 stop_reason != tool_use
IDLE 每 5s 轮询 inbox + 任务板 60s 超时或收到 shutdown
SHUTDOWN 发 summary,退出 —

工作原理

idle_poll: 空闲轮询

队友完成当前任务后不退出,进入 IDLE 阶段——每 5 秒检查一次有没有新工作:

IDLE_POLL_INTERVAL = 5   # seconds
IDLE_TIMEOUT = 60         # seconds

def idle_poll(name, messages, role) -> str:
    """Return 'work', 'shutdown', or 'timeout'."""
    for _ in range(IDLE_TIMEOUT // IDLE_POLL_INTERVAL):
        time.sleep(IDLE_POLL_INTERVAL)

        # ① 检查收件箱(优先)
        inbox = BUS.read_inbox(name)
        if inbox:
            for msg in inbox:
                if msg.get("type") == "shutdown_request":
                    return "shutdown"
            # 普通消息注入上下文,回到 WORK
            messages.append(...)
            return "work"

        # ② 扫描任务看板
        unclaimed = scan_unclaimed_tasks()
        if unclaimed:
            task = unclaimed[0]
            result = claim_task(task["id"], name)
            if "Claimed" in result:
                messages.append(...)
                return "work"
    return "timeout"

inbox 优先(可能包含 shutdown_request 等协议消息),任务板其次。

scan_unclaimed_tasks: 扫描任务看板

找 pending 状态、无 owner、所有依赖已完成的任务:

def scan_unclaimed_tasks() -> list[dict]:
    unclaimed = []
    for f in sorted(TASKS_DIR.glob("task_*.json")):
        task = json.loads(f.read_text())
        if (task.get("status") == "pending"
                and not task.get("owner")
                and can_start(task["id"])):
            unclaimed.append(task)
    return unclaimed

三个条件:pending、无 owner、所有 blockedBy 依赖已完成。

队友生命周期: WORK → IDLE → SHUTDOWN

# Outer loop: WORK → IDLE cycle
while True:
    # WORK phase (最多 10 轮 LLM 调用)
    for _ in range(10):
        # 检查 inbox、处理协议消息、调 LLM、执行工具
        ...
        if response.stop_reason != "tool_use":
            break

    # IDLE phase
    idle_result = idle_poll(name, messages, role)
    if idle_result == "shutdown": break
    if idle_result == "timeout": break

# SHUTDOWN: 发 summary 给 Lead
BUS.send(name, "lead", summary, "result")

关键设计:

  • 外层 while True:WORK 和 IDLE 交替进行
  • 内层 for 10:WORK 阶段最多 10 轮 LLM 调用
  • IDLE 超时 60 秒:超时后发送 summary 并退出
  • shutdown_request 两阶段都能响应

完整流程

1. Lead → create_task("创建数据库 schema")
2. Lead → create_task("写 API 路由")
3. Lead → create_task("写单元测试")
4. Lead → spawn_teammate("alice", "backend", "你是后端开发者")
5. Lead → spawn_teammate("bob", "backend", "你是后端开发者")

6. alice 启动 → WORK: 空转 → IDLE
7. bob 启动 → WORK: 空转 → IDLE

8. alice IDLE → scan → "创建数据库 schema" → claim → WORK
9. bob IDLE → scan → "写 API 路由" → claim → WORK

10. alice WORK: write_file("schema.sql") → complete_task → IDLE
11. alice IDLE → scan → "写单元测试" → claim → WORK
12. alice 完成 → IDLE → 60s 无新任务 → SHUTDOWN

两个队友并行认领、并行工作。Lead 只需创建任务和启动队友,不需要手动分配。

对比上一章

组件 s16 s17
任务分配 Lead 手动 assign 队友自动认领
IDLE 每 1s 轮询 inbox 每 5s 轮询 inbox + 任务板,60s 超时
claim_task 无 owner 检查 拒绝已有 owner 的任务
队友工具 5 8 (+list_tasks, claim_task, complete_task)

试一下

python s17_autonomous_agents/code.py

试试: Create 3 tasks on the board, then spawn alice and bob. Watch them auto-claim and work.

观察:队友是否自动认领了未分配的任务?有 blockedBy 依赖的任务是否在前置完成后被正确认领?空闲超时后是否自动关机?

接下来

队友自组织了。但 Alice 和 Bob 都在同一个目录下工作——Alice 改 config.py,Bob 也改 config.py,互相覆盖。

s18 Worktree Isolation → 每个任务有自己的工作目录,互不干扰。