← All Labs Lab 11 / 28 Pi coordinates several A2A agents
Lab 11 · Durable agents · Learned

Pi coordinates several A2A agents

The pi CLI discovers, authenticates to and drives two remote A2A habitats through pi-a2a, each with its own bearer token, and a model running pi delegates to both.

Learned Needs: tool-calling Smoke: pnpm run demo:multi-a2a Run: pnpm run demo:multi-a2a Source: examples/11-a2a-coordinator

Reproducible end-to-end test and interactive sandbox for driving multiple remote A2A 1.0 agents from the pi coding agent via @leocavalcante/pi-a2a.

Each run boots two isolated HTTP + SQLite DurableHabitat A2A 1.0 servers (researcher and reviewer) on separate loopback ports with per-agent bearer tokens, verifies /.well-known/agent-card.json discovery and fail-closed 401 auth, writes an isolated temporary agents.json, and drives both agents through pi.

Clean-Machine Single Command

zsh
./examples/11-a2a-coordinator/clean-machine.sh

Checks Node (>= 22) and pnpm, installs workspace dependencies if node_modules is absent, installs @earendil-works/pi-coding-agent globally if pi is missing, and runs the full automated verification with zero API keys required.

Workspace Recipes

Run automated verification (offline fauxProvider agent backends):

zsh
pnpm run demo:multi-a2a

Keep both A2A agents running and drop into an interactive pi TUI connected to both:

zsh
pnpm run demo:multi-a2a -- --interactive

Run with a real model backend plus an LLM-orchestrated multi-agent Pi turn. The habitats and the coordinating pi both use --model (default mycel:deepseek/deepseek-v4.1-flash; models go only through Mycel, D-28). For mycel: models the lab writes an isolated pi agent directory whose models.json defines the Mycel provider, so ~/.pi/agent is never touched:

zsh
MYCEL_URL=https://mycel.thefocus.ai pnpm run demo:multi-a2a -- --live --model mycel:deepseek/deepseek-v4.1-flash

Standalone Setup (Connecting to Any Remote A2A Agents Outside This Repo)

  1. Install Pi and the A2A extension:
zsh
pnpm add -g @earendil-works/pi-coding-agent
pi install npm:@leocavalcante/pi-a2a
  1. Configure multiple remote A2A endpoints in ~/.pi/a2a/agents.json (each agent needs its own origin — port or host — so /.well-known/agent-card.json resolves distinctly):
json
{
  "agents": {
    "researcher": {
      "url": "https://researcher.example.com",
      "bearerTokenEnv": "RESEARCHER_A2A_TOKEN"
    },
    "reviewer": {
      "url": "https://reviewer.example.com",
      "bearerTokenEnv": "REVIEWER_A2A_TOKEN"
    }
  }
}
  1. Start pi and either run manual commands (/a2a list, /a2a send researcher ..., /a2a send reviewer ...) or ask Pi's model to delegate across them via a2a_agents and a2a_send.

Next: a coordinator mesh (not built)

The next step puts a coordinator DurableHabitat between pi and the peers. Open questions:

  • Coordinator vs. flat Pi delegation: Is it cleaner when Pi talks directly to 3 leaf agents (flat agents.json), or when Pi delegates once to a Coordinator Habitat equipped with createA2ATools?
  • Busy-peer behavior: When two tasks hit the same peer conversation simultaneously, how does UNSUPPORTED_OPERATION (busy: true) behave and should the coordinator retry, fork, or spawn another peer?
  • Loop / self-delegation prevention: Does a peer mesh prevent coordinator -> researcher -> coordinator infinite loops and keep credentials isolated per directional peer token?

Prompt for that step:

Visual Target Wireframe ASCII Layout
Build a peer-to-peer A2A mesh lab under `examples/lab-07-a2a-mesh/` extending the pattern in `examples/11-a2a-coordinator/e2e.ts` so a Coordinator `DurableHabitat` discovers and delegates to specialist A2A peers using `createA2ATools` from `@leocavalcante/pi-a2a/durable` (re-exported by `@umwelten/protocols`), while Pi talks to the Coordinator (or directly to peers).

Read these files first:
- `examples/11-a2a-coordinator/e2e.ts`
- `packages/protocols/src/index.ts`
- `packages/protocols/node_modules/@leocavalcante/pi-a2a/README.md` (section "Private discovery and delegation")
- `packages/protocols/node_modules/@leocavalcante/pi-a2a/src/durable-tools.ts`
- `packages/habitat/src/durable/durable-habitat.ts`

Requirements:
1. Create `examples/lab-07-a2a-mesh/run.ts` (plus `README.md`) supporting default offline `fauxProvider` mode, `--interactive`, and `--live --model <provider:model>`.
2. Start 3 isolated `DurableHabitat` A2A 1.0 HTTP servers on separate loopback ports (`127.0.0.1:0`):
   - `researcher` & `reviewer`: specialist agents with distinct per-agent bearer tokens.
   - `coordinator`: configured with `createA2ATools({ self: "coordinator", ... })` (reading a peer `agents.json` in `tmpRoot` pointing to `researcher` and `reviewer` with `bearerTokenEnv`) so the coordinator itself can list `a2a_agents` and delegate via `a2a_send`.
3. Exercise and print a crisp timeline for all 3 edge cases in default automated mode (plus interactive Pi test if `--interactive` is passed):
   - Happy-path multi-hop delegation: `pi` CLI (using `PI_A2A_CONFIG` pointing to `coordinator`) sends one prompt -> `coordinator` invokes `a2a_send` to `researcher` and `reviewer` -> synthesizes final response back to `pi`.
   - Busy-peer rejection drill: force `reviewer` into an in-flight delayed turn, send a second peer message while busy, confirm `busy` rejection is surfaced cleanly without hanging, then retry cleanly after the first completes.
   - Loop protection check: verify self-delegation (`coordinator -> coordinator`) fails safely (`SELF_DELEGATION`).
4. Ensure all child `pi` calls are async (`spawn`, never `spawnSync`, so loopback servers never deadlock!), tear down servers/habitats/`tmpRoot` cleanly in `finally`, and pass strict `tsc --noEmit`.

Prompt

Coding-Agent Prompt · Ready to Paste
Stand up two isolated A2A 1.0 DurableHabitat peers (researcher and reviewer) on loopback, verify fail-closed 401 auth and Agent Card discovery (/.well-known/agent-card.json), write an isolated agents.json config, drive /a2a list, /a2a send, and /a2a result end-to-end through the real Pi CLI (@leocavalcante/pi-a2a), and pair it with a clean-machine shell verifier.
Works in Pi, Claude Code, Codex & Antigravity Offline mock first → --live opt-in

What we learned

The answer

The real pi CLI with @leocavalcante/pi-a2a drives two remote DurableHabitat A2A agents. /a2a list, /a2a send and /a2a result work against both, each agent accepts only its own bearer token, and live on DeepSeek V4.1 Flash through Mycel a model running in pi called a2a_agents, messaged both agents and summarized their answers.

What going through it found

  • The live coordinator turn could never have worked. It started pi without --model, so pi used its own default model, which had no key: "No API key found for the selected model". The habitats did use --model, so everything up to the coordinator turn passed. pi now gets the same model.
  • pi has no Mycel provider. @umwelten/pi knows mycel:, but the pi CLI only knows providers from its catalog and models.json. The lab writes a models.json with a mycel provider (openai-completions, apiKey: "$MYCEL_API_KEY", X-Mycel-End-User, cacheControlFormat: "anthropic") into a temp dir and passes --agent-dir, so the user's ~/.pi/agent is untouched. Anyone pointing their own pi at Mycel needs the same entry; @umwelten/pi could ship it as a pi extension.
  • The live turn didn't check delegation. It only required pi to exit 0, so a model that answered without calling the agents would pass. It now counts A2A POSTs per agent during the turn and requires both to be above zero.
  • Only a missing token was tested. The lab now also sends the researcher's token to the reviewer and requires 401.
  • The recorded live pass didn't reproduce. A later review reran it. The coordinator prompt said "call a2a_send on both" but gave no message, so DeepSeek called a2a_agents and then refused ("a2a_send requires a concrete message"). The prompt now gives each agent a concrete message (Redis-cache risks for researcher, a token-leak audit for reviewer) and asks for a 3-bullet summary.
  • The delegation counter counted rejected requests. It counted every POST to /a2a before auth, so a 401 counted as delegation. It now counts only responses with status 200.
  • The default --model was a direct vendor model (google:gemini-4-hydrogen), which breaks D-28 (models only through Mycel). It is now mycel:deepseek/deepseek-v4.1-flash.
  • /a2a send had a fixed 60 s timeout. A live habitat turn under load can take longer. --live now uses 120 s, the same as the coordinator turn.

Gotchas

  • One a2a_send is several HTTP requests. pi-a2a sends, then polls the task, so one delegation shows up as many A2A POSTs (12 and 18 accepted POSTs in the 2026-11-30 live run). Count deliveries, not requests.
  • Agents keep context across sends. pi-a2a reuses each agent's contextId, so the coordinator turn's answers refer to the earlier /a2a send messages. A fresh question needs a new context.
  • Each agent needs its own origin. /.well-known/agent-card.json is per origin, so two agents can't share a host and port.
  • The pi CLI must be installed globally (clean-machine.sh does it). With pnpm, put $PNPM_HOME/bin on PATH.

Not tested

  • A coordinator habitat between pi and the agents, busy-agent rejection and self-delegation. That is the "coordinator mesh" prompt in the README.
  • Agents on other machines, and TLS.

Smoke run

Visual Target Wireframe ASCII Layout
2. Verified Agent Card discovery + fail-closed 401 auth (no token, and another agent's token).
   - PASS /a2a list -> ["researcher","reviewer"]
   - PASS /a2a send researcher -> [researcher] Analyzes architecture tradeoffs and risks — handled request: …
   - PASS /a2a send reviewer   -> [reviewer] Audits auth boundaries and failure modes — handled request: …
   - PASS /a2a result researcher … -> TASK_STATE_COMPLETED
ALL MULTI-A2A PI CHECKS PASSED.

Live, 2026-11-30, after the fixes above (MYCEL_URL=https://mycel.thefocus.ai pnpm exec tsx examples/11-a2a-coordinator/e2e.ts --live, default model mycel:deepseek/deepseek-v4.1-flash): all of the above with real answers, then the coordinator turn called both agents and summarized them in 3 bullets: PASS model delegated to both agents (A2A POSTs: researcher 12, reviewer 18), ALL MULTI-A2A PI CHECKS PASSED. That was one pass. Stability across repeated runs is not yet shown. An earlier recorded pass (researcher 5, reviewer 6) did not reproduce, for the prompt reason above.