|
| 1 | +# s15: Agent Teams — One Agent Isn't Enough, Form a Team |
| 2 | + |
| 3 | +[中文](README.md) · [English](README.en.md) · [日本語](README.ja.md) |
| 4 | + |
| 5 | +s01 → ... → s13 → s14 → `s15` → [s16](../s16_team_protocols/) → s17 → s18 → s19 |
| 6 | +> *"One agent isn't enough, form a team"* — File-based inboxes + teammate threads. |
| 7 | +> |
| 8 | +> **Harness Layer**: Teams — Multi-agent collaboration, message bus. |
| 9 | +
|
| 10 | +--- |
| 11 | + |
| 12 | +## The Problem |
| 13 | + |
| 14 | +"Refactor the entire backend" touches auth, database layer, API routes, and tests. One agent working on API routes no longer has auth module details in context. The context window is limited, a single agent can't cover every module. |
| 15 | + |
| 16 | +s06's sub-agents are temps, called in for one job, then gone. Some tasks need teammates that can communicate and collaborate. |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | +## The Solution |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +Teaching code carries forward S14's capabilities (prompt assembly, task system, background execution, cron scheduling). To stay focused on the team mechanism, it omits full error recovery, memory, and skill systems. Added: **MessageBus** (file-based inboxes), **spawn_teammate_thread** (launch teammate threads), **inbox injection** (Lead receives teammate messages and injects into history). |
| 25 | + |
| 26 | +Sub-agent vs Teammate: |
| 27 | + |
| 28 | +| | s06 Sub-agent | s15 Teammate | |
| 29 | +|---|---|---| |
| 30 | +| Lifetime | One-shot, destroyed after use | Multi-turn (teaching: 10 rounds; real CC: idle loop) | |
| 31 | +| Communication | Only returns conclusion | Async inbox, communicate anytime | |
| 32 | +| Context | Fully isolated | Shared via messages | |
| 33 | +| Count | One lead + occasional sub-agent | One Lead + multiple teammates | |
| 34 | + |
| 35 | +--- |
| 36 | + |
| 37 | +## How It Works |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +### MessageBus: File-Based Inboxes |
| 42 | + |
| 43 | +Each agent (including Lead and teammates) has a `.jsonl` inbox. Send = append a JSON line to the target's file. Read = read file + delete (consumption): |
| 44 | + |
| 45 | +```python |
| 46 | +class MessageBus: |
| 47 | + def send(self, from_agent: str, to_agent: str, |
| 48 | + content: str, msg_type: str = "message"): |
| 49 | + msg = {"from": from_agent, "to": to_agent, |
| 50 | + "content": content, "type": msg_type, |
| 51 | + "ts": time.time()} |
| 52 | + inbox = MAILBOX_DIR / f"{to_agent}.jsonl" |
| 53 | + with open(inbox, "a") as f: |
| 54 | + f.write(json.dumps(msg) + "\n") |
| 55 | + |
| 56 | + def read_inbox(self, agent: str) -> list[dict]: |
| 57 | + inbox = MAILBOX_DIR / f"{agent}.jsonl" |
| 58 | + if not inbox.exists(): |
| 59 | + return [] |
| 60 | + msgs = [json.loads(line) for line in inbox.read_text().splitlines()] |
| 61 | + inbox.unlink() # consume: read + delete |
| 62 | + return msgs |
| 63 | +``` |
| 64 | + |
| 65 | +Why files instead of in-memory queues? Teaching code uses files because they're直观 and observable across threads. Real CC also uses file inboxes (`~/.claude/teams/{team}/inboxes/`) but adds `proper-lockfile` for concurrent write safety. The teaching version's `read_inbox` has a read + unlink race, concurrent reads could lose messages, acceptable for teaching purposes. |
| 66 | + |
| 67 | +### spawn_teammate_thread: Launching a Teammate |
| 68 | + |
| 69 | +Lead calls the `spawn_teammate` tool to start a teammate. The teammate runs in its own daemon thread with its own system prompt, messages, and simplified tool set: |
| 70 | + |
| 71 | +```python |
| 72 | +def spawn_teammate_thread(name: str, role: str, prompt: str) -> str: |
| 73 | + system = f"You are '{name}', a {role}. Use tools to complete tasks." |
| 74 | + |
| 75 | + def run(): |
| 76 | + messages = [{"role": "user", "content": prompt}] |
| 77 | + sub_tools = [bash, read_file, write_file, send_message] |
| 78 | + for _ in range(10): # max 10 rounds |
| 79 | + inbox = BUS.read_inbox(name) |
| 80 | + if inbox: |
| 81 | + messages.append({"role": "user", |
| 82 | + "content": f"<inbox>{json.dumps(inbox)}</inbox>"}) |
| 83 | + response = client.messages.create( |
| 84 | + model=MODEL, system=system, messages=messages[-20:], |
| 85 | + tools=sub_tools, max_tokens=8000) |
| 86 | + # ... execute tools, process results |
| 87 | + # Send final summary to Lead |
| 88 | + BUS.send(name, "lead", summary, "result") |
| 89 | + |
| 90 | + threading.Thread(target=run, daemon=True).start() |
| 91 | +``` |
| 92 | + |
| 93 | +Key design: |
| 94 | +- **Simplified tool set**: bash, read, write, send_message. Teaching code omits tasks and cron to focus on communication. Real CC teammates also have TaskCreate, TaskUpdate, etc., the task system is shared across the team |
| 95 | +- **Teaching: 10 rounds max**: prevents infinite loops. Real CC uses idle loop: after each round, send `idle_notification`, wait for inbox messages, resume on arrival, exit only on `shutdown_request` |
| 96 | +- **Auto-report on completion**: `BUS.send(name, "lead", summary)` sends the final result to Lead's inbox |
| 97 | + |
| 98 | +### Lead's Inbox Injection |
| 99 | + |
| 100 | +Lead checks inbox after each main loop iteration. Teammate messages are injected into history so the LLM can see and react to them: |
| 101 | + |
| 102 | +```python |
| 103 | +# After main loop iteration |
| 104 | +inbox = BUS.read_inbox("lead") |
| 105 | +if inbox: |
| 106 | + inbox_text = "\n".join( |
| 107 | + f"From {m['from']}: {m['content'][:200]}" for m in inbox) |
| 108 | + history.append({"role": "user", |
| 109 | + "content": f"[Inbox]\n{inbox_text}"}) |
| 110 | +``` |
| 111 | + |
| 112 | +Teaching code injects in the user input loop. Real CC is more refined, Lead's `useInboxPoller` checks every 1 second, submitting messages as new turns without waiting for user input. |
| 113 | + |
| 114 | +### Permission Bubbling |
| 115 | + |
| 116 | +Teaching code omits permission bubbling. Real CC's flow (`permissionSync.ts`, `useSwarmPermissionPoller.ts`): |
| 117 | + |
| 118 | +1. Teammate encounters an operation needing approval → sends `permission_request` to Lead's inbox |
| 119 | +2. Lead's `useInboxPoller` detects the request → routes to approval queue |
| 120 | +3. User approves → Lead sends `permission_response` back to teammate |
| 121 | +4. Teammate's `useSwarmPermissionPoller` (polls every 500ms) receives reply → continue or reject |
| 122 | + |
| 123 | +### Putting It Together |
| 124 | + |
| 125 | +``` |
| 126 | +1. Lead: "Build the backend: one agent isn't enough, form a team" |
| 127 | +2. Lead → spawn_teammate("alice", "backend dev", "Create database schema") |
| 128 | +3. Lead → spawn_teammate("bob", "frontend dev", "Write API client") |
| 129 | +4. Alice thread starts → her own LLM call → bash "python manage.py migrate" |
| 130 | +5. Bob thread starts → his own LLM call → write_file("client.ts", ...) |
| 131 | +6. Alice done → BUS.send("alice", "lead", "Schema done: users, orders tables") |
| 132 | +7. Bob done → BUS.send("bob", "lead", "Client written with types") |
| 133 | +8. Lead next iteration → inbox injected into history → LLM sees both results |
| 134 | +``` |
| 135 | + |
| 136 | +Two teammates work in parallel. |
| 137 | + |
| 138 | +--- |
| 139 | + |
| 140 | +## Changes from s14 |
| 141 | + |
| 142 | +| Component | Before (s14) | After (s15) | |
| 143 | +|-----------|-------------|-------------| |
| 144 | +| Agent count | 1 | 1 Lead + N teammate threads | |
| 145 | +| Communication | None | MessageBus + .mailboxes/*.jsonl | |
| 146 | +| New classes | — | MessageBus, active_teammates dict | |
| 147 | +| New functions | — | spawn_teammate_thread, run_send_message, run_check_inbox | |
| 148 | +| Lead tools | 11 (s14) | + spawn_teammate, send_message, check_inbox (14) | |
| 149 | +| Teammate tools | — | bash, read_file, write_file, send_message (4) | |
| 150 | +| Permissions | Local decisions | Teaching code omits (real CC has bubbling) | |
| 151 | + |
| 152 | +--- |
| 153 | + |
| 154 | +## Try It |
| 155 | + |
| 156 | +```sh |
| 157 | +cd learn-claude-code |
| 158 | +python s15_agent_teams/code.py |
| 159 | +``` |
| 160 | + |
| 161 | +Try these prompts: |
| 162 | + |
| 163 | +1. `Spawn alice as a backend developer. Ask her to create a file called schema.sql with a users table.` |
| 164 | +2. `Check your inbox for alice's result.` |
| 165 | +3. `Spawn bob as a tester. Ask him to check if schema.sql exists and list its contents.` |
| 166 | + |
| 167 | +What to observe: How does Lead spawn teammates? What do the `.mailboxes/` JSONL files look like? After teammates finish, is Lead's inbox injected into history? |
| 168 | + |
| 169 | +--- |
| 170 | + |
| 171 | +## What's Next |
| 172 | + |
| 173 | +Teammates can work and communicate. But if Lead wants Alice to shut down, killing the thread outright could leave half-written files. A graceful shutdown protocol is needed: Lead sends shutdown_request, teammate wraps up and exits. |
| 174 | + |
| 175 | +s16 Team Protocols → Shutdown handshake and message conventions. |
| 176 | + |
| 177 | +<details> |
| 178 | +<summary>Deep Dive into CC Source</summary> |
| 179 | + |
| 180 | +> The following is a complete analysis based on CC source code `spawnMultiAgent.ts`, `useInboxPoller.ts` (969 lines), `useSwarmPermissionPoller.ts` (330 lines), `teammateMailbox.ts`, `teamHelpers.ts`. |
| 181 | +
|
| 182 | +### 1. No Central Message Bus, It's the Filesystem |
| 183 | + |
| 184 | +Teaching code uses a `MessageBus` class to send and receive messages. Real CC is more direct, each agent writes directly to other agents' inbox files. |
| 185 | + |
| 186 | +Inbox path: `~/.claude/teams/{teamName}/inboxes/{agentName}.json` |
| 187 | + |
| 188 | +Writes use `proper-lockfile` for concurrent write safety (up to 10 retries). Each file is a JSON array; appending reads → appends → writes back. |
| 189 | + |
| 190 | +### 2. 15 Message Types |
| 191 | + |
| 192 | +CC team communication has 15 structured message types (`teammateMailbox.ts`): |
| 193 | + |
| 194 | +| Type | Direction | Purpose | |
| 195 | +|------|-----------|---------| |
| 196 | +| `plain text` | Both ways | Normal inter-teammate communication | |
| 197 | +| `idle_notification` | Teammate→Lead | Teammate finished a turn, now idle | |
| 198 | +| `permission_request` | Teammate→Lead | Teammate needs operation approval | |
| 199 | +| `permission_response` | Lead→Teammate | Lead's approval result | |
| 200 | +| `plan_approval_request` | Teammate→Lead | Teammate submits plan for review | |
| 201 | +| `plan_approval_response` | Lead→Teammate | Lead's plan review | |
| 202 | +| `shutdown_request` | Lead→Teammate | Request graceful shutdown | |
| 203 | +| `shutdown_approved` | Teammate→Lead | Confirm shutdown | |
| 204 | +| `shutdown_rejected` | Teammate→Lead | Reject shutdown (with reason) | |
| 205 | +| `task_assignment` | Lead→Teammate | Assign a task | |
| 206 | +| `team_permission_update` | Lead→Teammate | Broadcast permission changes | |
| 207 | +| `mode_set_request` | Lead→Teammate | Change teammate's permission mode | |
| 208 | +| `sandbox_permission_*` | Both ways | Network permission request/reply | |
| 209 | +| `teammate_terminated` | System | Teammate removed notification | |
| 210 | + |
| 211 | +Text messages are wrapped in `<teammate-message>` XML tags for delivery to the model. |
| 212 | + |
| 213 | +### 3. Permission Bubbling: Bidirectional Polling |
| 214 | + |
| 215 | +Teaching code omits permission bubbling. Real CC's flow (`permissionSync.ts`): |
| 216 | + |
| 217 | +1. **Teammate** encounters operation needing approval → sends `permission_request` to Lead's inbox |
| 218 | +2. **Lead's** `useInboxPoller` (polls every 1s) detects request → routes to `ToolUseConfirmQueue` |
| 219 | +3. Lead's UI shows approval dialog with teammate name and color |
| 220 | +4. User approves → Lead sends `permission_response` back to teammate's inbox |
| 221 | +5. **Teammate's** `useSwarmPermissionPoller` (polls every 500ms) receives reply → continue or reject |
| 222 | + |
| 223 | +### 4. Teammate Lifecycle |
| 224 | + |
| 225 | +CC teammates are created by `spawnTeammate()` (`spawnMultiAgent.ts`): |
| 226 | + |
| 227 | +1. **Spawn**: Create tmux pane (or in-process), assign color, write team config |
| 228 | +2. **Work**: `useInboxPoller` checks inbox every 1s → submit as new turn when messages arrive |
| 229 | +3. **Idle**: Stop hook fires → send `idle_notification` to Lead |
| 230 | +4. **Shutdown**: Lead sends `shutdown_request` → teammate replies `shutdown_approved` → Lead cleans up |
| 231 | + |
| 232 | +### 5. Team Config |
| 233 | + |
| 234 | +Team registry at `~/.claude/teams/{teamName}/config.json` (`teamHelpers.ts`): |
| 235 | + |
| 236 | +```json |
| 237 | +{ |
| 238 | + "name": "my-team", |
| 239 | + "leadAgentId": "lead@my-team", |
| 240 | + "members": [{ |
| 241 | + "agentId": "researcher@my-team", |
| 242 | + "name": "researcher", |
| 243 | + "agentType": "general-purpose", |
| 244 | + "color": "blue", |
| 245 | + "isActive": true |
| 246 | + }] |
| 247 | +} |
| 248 | +``` |
| 249 | + |
| 250 | +Teammates cannot be nested (`AgentTool.tsx:273` explicitly forbids "teammates spawning other teammates"). |
| 251 | + |
| 252 | +</details> |
| 253 | + |
| 254 | +<!-- translation-sync: zh@v1, en@v1, ja@v1 --> |
0 commit comments