自己找活幹的 agent
「The agent finds work itself.」不需要人類派單,它會從任務板上自己拿活。
從 "被叫醒" 到 "自己找活"
s09 / s10 的 teammate 是被動的:要有人通過郵箱 send message,它才開始乾;做完就 idle,再被叫醒再乾。
s11 的 autonomous teammate 主動:沒活的時候也不真的停下,它會每 5 秒掃一次 .tasks/ 目錄——看到沒主的 task 就 claim_task 認領,開始乾。
Teammate lifecycle:
+-------+
| spawn |
+---+---+
|
v
+-------+
| WORK | <-- LLM tool_use 循环
+---+---+
|
| stop_reason != tool_use OR tool_use("idle")
v
+-------+
| IDLE | poll 每 5s,共 12 次 = 60s 超时
+---+---+
|
+--> inbox 有新消息? → resume WORK
|
+--> scan .tasks/,发现未认领 → claim → resume WORK
|
+--> 60s 什么都没等到 → status = shutdown,线程退出
Auto-claim · 從任務板"拿單"
核心函數:
def scan_unclaimed_tasks() -> list: 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 not task.get("blockedBy")): unclaimed.append(task) return unclaimed
三條濾波 = pending + 無 owner + 無 blockedBy。缺一不可。
claim 動作本身有鎖(_claim_lock),防止兩個 agent 同時看到同一個 unclaimed task 都去搶——lock 內再讀一次、檢查 owner 是否還是空、然後 "in_progress" 標記+ owner 寫入磁碟。
模擬多個 agent 同時掃任務板
3 個 teammate 在 idle 狀態,3 個 task 等待認領。點「下一輪掃描」看誰先搶到。
Identity 重註入 · 壓縮後還知道自己是誰
s06 講 auto_compact 會把 messages 換成 summary。 autonomous agent 跑個把小時很容易觸發-壓縮後模型不知道自己叫什麼、是什麼角色。
s11 的招數:claim 到新 task 時,如果 len(messages) ≤ 3(說明剛被壓縮過),自動在頭部插入 identity 塊:
if len(messages) <= 3: messages.insert(0, { "role": "user", "content": f"<identity>You are 'alice', role: coder, team: my-team. Continue your work.</identity>", }) messages.insert(1, {"role":"assistant", "content": f"I am alice. Continuing."})
這個模式叫做 identity reinjection--用人為構造的一輪對話「告訴模型它是誰」,讓它找回角色感。