Learn Claude Code
s11

自主 Agent

协作

Scan Board, Claim Tasks

413 LOC14 个工具TypeScriptTask board polling + timeout-based self-governance
Teammates scan the board and claim tasks themselves; no need for the lead to assign each one

s01 > s02 > s03 > s04 > s05 > s06 | s07 > s08 > s09 > s10 > [ s11 ] s12

"队友自己看看板, 有活就认领" -- 不需要领导逐个分配, 自组织。

Harness 层: 自治 -- 模型自己找活干, 无需指派。

问题

s09-s10 中, 队友只在被明确指派时才动。领导得给每个队友写 prompt, 任务看板上 10 个未认领的任务得手动分配。这扩展不了。

真正的自治: 队友自己扫描任务看板, 认领没人做的任务, 做完再找下一个。

一个细节: 上下文压缩 (s06) 后智能体可能忘了自己是谁。身份重注入解决这个问题。

解决方案

Teammate lifecycle with idle cycle:

+-------+
| spawn |
+---+---+
    |
    v
+-------+   tool_use     +-------+
| WORK  | <------------- |  LLM  |
+---+---+                +-------+
    |
    | stop_reason != tool_use (or idle tool called)
    v
+--------+
|  IDLE  |  poll every 5s for up to 60s
+---+----+
    |
    +---> check inbox --> message? ----------> WORK
    |
    +---> scan .tasks/ --> unclaimed? -------> claim -> WORK
    |
    +---> 60s timeout ----------------------> SHUTDOWN

Identity re-injection after compression:
  if len(messages) <= 3:
    messages.insert(0, identity_block)

工作原理

  1. 队友循环分两个阶段: WORK 和 IDLE。LLM 停止调用工具 (或调用了 idle) 时, 进入 IDLE。
async loop(name: string, role: string, prompt: string) {
  while (true) {
    // WORK phase
    if (idleRequested) break;
    // IDLE phase
    if (!resume) return;
  }
}
  1. 空闲阶段循环轮询收件箱和任务看板。
while (Date.now() - start < IDLE_TIMEOUT) {
  await sleep(POLL_INTERVAL);
  const inbox = BUS.readInbox(name);
  const unclaimed = scanUnclaimedTasks();
  if (inbox.length || unclaimed.length) return true;
}
return false;
  1. 任务看板扫描: 找 pending 状态、无 owner、未被阻塞的任务。
function scanUnclaimedTasks() {
  return loadTasks().filter((task) =>
    task.status === "pending" && !task.owner && !(task.blockedBy?.length),
  );
}
  1. 身份重注入: 上下文过短 (说明发生了压缩) 时, 在开头插入身份块。
if (messages.length <= 3) {
  messages.unshift({ role: "assistant", content: `I am ${name}. Continuing.` });
  messages.unshift(makeIdentityBlock(name, role, teamName));
}

相对 s10 的变更

组件之前 (s10)之后 (s11)
Tools1214 (+idle, +claim_task)
自治性领导指派自组织
空闲阶段轮询收件箱 + 任务看板
任务认领仅手动自动认领未分配任务
身份系统提示+ 压缩后重注入
超时60 秒空闲 -> 自动关机

试一试

cd learn-claude-code
cd agents-ts
npm install
npm run s11

试试这些 prompt:

  1. Create 3 tasks on the board, then spawn alice and bob. Watch them auto-claim.
  2. Spawn a coder teammate and let it find work from the task board itself
  3. Create tasks with dependencies. Watch teammates respect the blocked order.
  4. 输入 /tasks 查看带 owner 的任务看板
  5. 输入 /team 监控谁在工作、谁在空闲