s15: Agent Teams — One Agent Isn't Enough, Form a Team
s01 → ... → s13 → s14 → s15 → s16 → s17 → s18 → s19 → s20
"One agent isn't enough, form a team" — File-based inboxes + teammate threads.
Harness Layer: Teams — Multi-agent collaboration, message bus.
The Problem
"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.
s06's sub-agents are temps, called in for one job, then gone. Some tasks need teammates that can communicate and collaborate.
The Solution
)
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).
Sub-agent vs Teammate:
How It Works
)
MessageBus: File-Based Inboxes
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):
Why files instead of in-memory queues? Teaching code uses files because they're intuitive 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.
spawn_teammate_thread: Launching a Teammate
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:
Key design:
- 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
- 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 onshutdown_request - Auto-report on completion:
BUS.send(name, "lead", summary)sends the final result to Lead's inbox
Lead's Inbox Injection
Lead checks inbox after each main loop iteration. Teammate messages are injected into history so the LLM can see and react to them:
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.
Permission Bubbling
Teaching code omits permission bubbling. Real CC's flow (permissionSync.ts, useSwarmPermissionPoller.ts):
- Teammate encounters an operation needing approval → sends
permission_requestto Lead's inbox - Lead's
useInboxPollerdetects the request → routes to approval queue - User approves → Lead sends
permission_responseback to teammate - Teammate's
useSwarmPermissionPoller(polls every 500ms) receives reply → continue or reject
Putting It Together
Two teammates work in parallel.
Changes from s14
Try It
Try these prompts:
Spawn alice as a backend developer. Ask her to create a file called schema.sql with a users table.Check your inbox for alice's result.Spawn bob as a tester. Ask him to check if schema.sql exists and list its contents.
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?
What's Next
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.
s16 Team Protocols → Shutdown handshake and message conventions.
Deep Dive into CC Source
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.
1. No Central Message Bus, It's the Filesystem
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.
Inbox path: ~/.claude/teams/{teamName}/inboxes/{agentName}.json
Writes use proper-lockfile for concurrent write safety (up to 10 retries). Each file is a JSON array; appending reads → appends → writes back.
2. 15 Message Types
CC team communication has 15 structured message types (teammateMailbox.ts):
Text messages are wrapped in <teammate-message> XML tags for delivery to the model.
3. Permission Bubbling: Bidirectional Polling
Teaching code omits permission bubbling. Real CC's flow (permissionSync.ts):
- Teammate encounters operation needing approval → sends
permission_requestto Lead's inbox - Lead's
useInboxPoller(polls every 1s) detects request → routes toToolUseConfirmQueue - Lead's UI shows approval dialog with teammate name and color
- User approves → Lead sends
permission_responseback to teammate's inbox - Teammate's
useSwarmPermissionPoller(polls every 500ms) receives reply → continue or reject
4. Teammate Lifecycle
CC teammates are created by spawnTeammate() (spawnMultiAgent.ts):
- Spawn: Create tmux pane (or in-process), assign color, write team config
- Work:
useInboxPollerchecks inbox every 1s → submit as new turn when messages arrive - Idle: Stop hook fires → send
idle_notificationto Lead - Shutdown: Lead sends
shutdown_request→ teammate repliesshutdown_approved→ Lead cleans up
5. Team Config
Team registry at ~/.claude/teams/{teamName}/config.json (teamHelpers.ts):
Teammates cannot be nested (AgentTool.tsx:273 explicitly forbids "teammates spawning other teammates").
