# Autonomy 3: Orchestrating Claude Code Teams

Two labs for running several Claude Code sessions as one team. The first watches the sessions and catches collisions. The second decides which model and effort level each kind of work gets.

| Lab | What it does | Session topics |
|---|---|---|
| `team_board.py` + `team_board/kit/` | Reads a task board (tasks, owners, worktrees, file footprints, dependencies) and a hook-logged activity stream from four parallel sessions. It shows the board, raises PLAN, DEPENDS, FOOTPRINT, COLLISION and STALLED alerts at the time each could have fired, and replays the kit's real PreToolUse footprint guard on the same edits. It ends with a merge plan. `ast` finds the function each edit touched, so only two edits to the same function count as a collision. | Supervision (progress visibility, collision detection, intervention, final human control), Parallelism (worktrees, merge points), Coordination (task dependencies) |
| `routing.py` | Routes eight work items by role, risk and complexity to a model alias and an effort level. It applies the org caps (`availableModels`, `maxEffortLevel`), estimates cost against an all-Opus/high baseline, prices a mid-session `/model` switch against a subagent, and writes one `.claude/agents/<role>.md` per role. `--real` runs one route for real. | Routing (model choice, effort, cost and speed, risk), Organization (planner, builder, reviewer and validator roles) |

## Run

```bash
cd labs
python3 autonomy-3/team_board.py                 # the board as of 11:20
python3 autonomy-3/team_board.py --at 10:15      # what the lead could see at 10:15
python3 autonomy-3/routing.py
python3 autonomy-3/routing.py --max-effort medium --models sonnet,haiku
python3 autonomy-3/routing.py --real             # costs money: claude -p, read-only, haiku, --effort low, capped at $0.05
```

Both labs run offline by default and write only to `autonomy-3/out/`. `team_board.py` runs the guard hook in a temp directory. `_build_fixtures.py` regenerates `board/events.jsonl` deterministically.

## Install the team-board kit

```bash
cp -r autonomy-3/team_board/kit/.claude <repo>/          # merge settings.json by hand if you have one
```

1. Edit `<repo>/.claude/team-board.json` in the main checkout as the plan changes.
2. Commit the hooks and `settings.json` so every worktree has them.
3. Add `.claude/worktrees/` and `.claude/team-board-events.jsonl` to `.gitignore`.
4. Start each task with `claude --worktree <name>`.

To watch the team, run `python3 autonomy-3/team_board.py --board <repo>/.claude/team-board.json --events <repo>/.claude/team-board-events.jsonl --repo <repo>`.

Completion comes from the `TaskCompleted` hook, which fires only where the task tools exist. By default those are on for Opus 4.7, Sonnet 4.6, Haiku 4.5 and earlier models; on newer models, start each session with `CLAUDE_CODE_ENABLE_TODO_TOOLS=1`. Without them, the board can't see a task complete. Claude Code picks its own task ids, so the board matches tasks by `task_subject`.

The footprint guard is defense in depth. It fails closed (`onFailure: "block"`, and it denies when it can't read the board), and it resolves `..` before matching a path. But a Bash command can still write anywhere the permission rules and sandbox allow. And the guard knows a session's task only from the input `cwd`, which moves when Claude runs `cd` or enters another worktree.

Event times keep their date, so a log that crosses midnight still orders correctly; times on the board are UTC. `--at HH:MM` means that time on the last day in the log, and `--at` also takes a full timestamp.

## What you'll see

`team_board.py`, at 11:20:

```text
[1] Board
pending                        in progress                         completed
─────────────────────────────  ──────────────────────────────────  ──────────────────────────────────
T6 reviewer (0 edits, last -)  T3 builder-c (2 edits, last 10:45)  T1 builder-a (2 edits, last 10:16)
                               T5 builder-a (1 edits, last 10:52)  T2 builder-b (3 edits, last 10:38)
                                                                   T4 builder-d (2 edits, last 10:21)
[2] Alerts, in the order they could have fired
   plan  PLAN      T2 and T4 both claim tinyapp/pricing.py and nothing orders them
         -> narrow one footprint, or make T2 depend on T4
  10:08  DEPENDS   builder-b edited tinyapp/cart.py for T2 while its dependency T1 is in progress
         -> pause gift-checkout until T1 completes, then rebase on T1's branch
  10:14  COLLISION tinyapp/pricing.py::apply_discount rewritten in codes-case (10:07) and gift-checkout (10:14)
         -> stop one session (claude stop <id>, or message the teammate); merge T4 first, then rebase T2 and rerun the check
  10:22  FOOTPRINT builder-b (T2) edited tinyapp/__init__.py, T5's file
         -> move the change into T5
  10:30  COLLISION tinyapp/__init__.py::module level rewritten in gift-checkout (10:22) and gift-export (10:30)
         -> stop one session (claude stop <id>, or message the teammate); merge T2 first, then rebase T5 and rerun the check
  11:20  STALLED   builder-c has been silent on T3 for 35 min (last activity 10:45)
         -> check it (claude attach <id> / claude logs <id>), then nudge or reassign
```

The guard replay turns those detections into prevention, and it also shows the guard's limit:

```text
[4] Prevention: the kit's footprint guard replayed on the same edits (PreToolUse, real hook)
at     worktree       file                     guard       why
10:07  codes-case     tinyapp/pricing.py       no opinion
10:14  gift-checkout  tinyapp/pricing.py       no opinion
10:22  gift-checkout  tinyapp/__init__.py      deny        tinyapp/__init__.py belongs to T5 (Export the gift card API, owner bui
  The guard follows the board. Where the plan lets two tasks own a file, it allows both edits: fix the plan.
[5] Merge plan (dependencies first, then completion order; a human merges)
#  task  branch (worktree)  completed    before merging                     after
1  T1    gift-model         10:16        -                                  run the integration check
2  T4    codes-case         10:21        -                                  run the integration check
3  T2    gift-checkout      10:38        rebase on T4 first: same function  run the integration check
-  T3    gift-receipt       in progress  not ready                          wait
-  T5    gift-export        in progress  not ready                          wait
  after each merge: python3 -m unittest -q on the integration branch; stop at the first failure
```

`routing.py`. Builders never drop below Sonnet. Risk raises the model, and complexity raises the effort:

```text
task                role       model   effort  fallback  est. cost  baseline  saved
find-callers        explorer   haiku   low     sonnet    $0.008     $0.456    98%
plan-gift-cards     planner    opus    xhigh   sonnet    $1.204     $1.004    -20%
build-gift-cards    builder    sonnet  medium  opus      $0.936     $2.352    60%
fix-typo            builder    sonnet  low     opus      $0.084     $0.246    66%
migrate-rounding    builder    opus    xhigh   sonnet    $4.176     $3.376    -24%
review-diff         reviewer   opus    xhigh   sonnet    $0.848     $0.688    -23%
summarize-failures  validator  haiku   low     sonnet    $0.006     $0.324    98%
label-issues        triager    haiku   low     sonnet    $0.028     $2.032    99%
  total $7.29 vs $10.48 baseline (30% less). Illustrative: list prices from common/llm.py and assumed output tokens per effort level.
  The role table and the risk/complexity adjustments are this lab's heuristics. Documented: the aliases, the availableModels and maxEffortLevel caps, where each route is set, and the cache behavior.
  Aliases resolve per provider: on Bedrock and Agent Platform `sonnet` is Sonnet 4.5 and `haiku` is Haiku 4.5, and on Foundry `opus` is Opus 4.6 (no xhigh). Pin ANTHROPIC_DEFAULT_*_MODEL there (on Bedrock with us.anthropic.… inference-profile IDs) before trusting these prices.
```

The subagent files list only tools the role will really have. On macOS, Linux and WSL, a subagent gets Glob and Grep back only when its `tools` list leaves out Bash, so the builder and the validator search through Bash:

```text
[4] Subagent files (copy into .claude/agents/ to use them)
  builder.md: tools: Read, Edit, Write, Bash, model: sonnet, effort: medium
  explorer.md: tools: Read, Grep, Glob, model: haiku, effort: low
  planner.md: tools: Read, Grep, Glob, model: opus, effort: high
  reviewer.md: tools: Read, Grep, Glob, model: opus, effort: high
  triager.md: tools: Read, Grep, Glob, model: haiku, effort: low
  validator.md: tools: Read, Bash, model: haiku, effort: low
```

When the org caps leave a role nothing at or above its floor, the route is blocked rather than quietly downgraded, and its agent file isn't written. With `--models haiku`:

```text
build-gift-cards    builder    -      -       -         blocked    $2.352    -
fix-typo            builder    -      -       -         blocked    $0.246    -
migrate-rounding    builder    -      -       -         blocked    $3.376    -
  total $0.10 vs $4.50 baseline (98% less, 3 blocked task(s) left out). Illustrative: list prices from common/llm.py and assumed output tokens per effort level.
  build-gift-cards: builder: sonnet/medium (writing code stays on a strong model); availableModels allows nothing at or above the sonnet floor: blocked by policy
  builder.md: not written: every task for this role is blocked by the org caps
```

```text
  review-diff: reviewer: opus/high (a fresh, strong reader catches what the builder missed); risk high: model and effort up one -> fable/xhigh; availableModels doesn't allow fable: using opus
[3] Switching the main session (sonnet) vs a subagent, per task with another model
task                model change     /model mid-session (cache write)  subagent brief (cache write)
plan-gift-cards     sonnet -> opus   $0.400                            $0.040
migrate-rounding    sonnet -> opus   $0.600                            $0.040
```

A real run of the `find-callers` route. This is one of the two real calls made while building these labs: Claude Code v2.1.303 on Amazon Bedrock, read-only tools, `--model haiku --effort low`, `--max-budget-usd 0.05`, in a temp copy of `common/samples/tiny-app`. The output below was recorded from that run; the current script also prints an `estimate for this route` line:

```text
[5] Real run of the find-callers route (the prompt below is that task)
  real run: claude -p --model haiku --effort low, read-only, --max-turns 4, --max-budget-usd 0.05
  outcome: success (terminal_reason=completed, stop_reason=end_turn)
  turns: 2
  model responses: 2 (3 assistant events)
  tool calls: 1 {'Grep': 1}  errors=0
  cost (estimate): $0.001866
  usage: {'input_tokens': 4, 'output_tokens': 306, 'cache_creation_input_tokens': 12737, 'cache_read_input_tokens': 12043}
  models that ran: claude-haiku-5-5
  answer: Three call sites: - `tinyapp/cart.py:54` - `tests/test_pricing.py:30` - `tests/test_pricing.py:33`
```

The shared `common/cc.print_summary` now prints the outcome line in the docs' order, stop_reason first ([ResultError](https://code.claude.com/docs/en/agent-sdk/python#resulterror)): for this run it reads `outcome: stop_reason=end_turn, terminal_reason=completed, subtype=success`.

`claude-haiku-5-5` ran because that machine pins `ANTHROPIC_DEFAULT_HAIKU_MODEL`. Unpinned, `haiku` on Bedrock is Haiku 4.5, at a different price. The run may also leave an empty `~/.claude/projects/<temp dir>/memory/` folder behind.

The real run cost about a quarter of the route's $0.008 estimate. It took 2 turns, not 6, with a 12.7k context instead of 30k. That gap is why exercise 2 asks you to put your own result events into `tasks.json`.

## Formats and where they come from

| Encoded format | Source (fetched 2026-10-20) |
|---|---|
| Hook payloads in `board/events.jsonl` (common fields; `SessionStart`, `PostToolUse`, `Stop`, `TaskCompleted` input) and the PreToolUse decision the guard prints | https://code.claude.com/docs/en/hooks |
| `tool_input` for Edit (`file_path`, `old_string`, `new_string`) and Write (`file_path`, `content`); Bash `tool_response` (`stdout`, `stderr`, `interrupted`) | https://code.claude.com/docs/en/agent-sdk/typescript#tool-input-types |
| Worktree layout (`<repo>/.claude/worktrees/<name>/`) | https://code.claude.com/docs/en/worktrees |
| Task states (`pending`, `in progress`, `completed`), `TaskCompleted` | https://code.claude.com/docs/en/agent-teams |
| Which models have the task tools (and so `TaskCompleted`) by default; `CLAUDE_CODE_ENABLE_TODO_TOOLS` | https://code.claude.com/docs/en/tools-reference#task-tool-availability |
| `claude stop`, `attach`, `logs` | https://code.claude.com/docs/en/agent-view |
| Model aliases, `opusplan`, effort levels, `--fallback-model` | https://code.claude.com/docs/en/model-config , https://code.claude.com/docs/en/cli-reference |
| Alias resolution on Bedrock, and pinning `ANTHROPIC_DEFAULT_*_MODEL` | https://code.claude.com/docs/en/amazon-bedrock |
| `availableModels`, `maxEffortLevel` | https://code.claude.com/docs/en/settings-reference |
| Subagent frontmatter (`name`, `description`, `tools`, `model`, `effort`); per-invocation model and effort | https://code.claude.com/docs/en/sub-agents |
| Glob and Grep absent by default on macOS/Linux/WSL; a subagent gets them back only when its `tools` leave out Bash | https://code.claude.com/docs/en/tools-reference#glob-tool-behavior |
| Prompt cache per model; switching models invalidates it, spawning a subagent doesn't | https://code.claude.com/docs/en/prompt-caching |

`board.json`, the `{"logged_at", "payload"}` wrapper written by `board_log.py`, `routing/tasks.json` and `routing/policy.json` are lab formats. Routing costs are estimates: list prices come from `common/llm.py`, and the output tokens per effort level are assumptions you should replace with your own numbers.
