# AnySwarm — Coding Agent Setup Runbook

> **Audience:** Coding Agents (Claude Code, Codex, Cursor, Antigravity, etc.) setting up `@anyswarm/core` on a user's machine.
> **Core rule:** Never install system software, modify global PATH/shell configs, or start a paid LLM run without explaining what is needed and getting the user's explicit approval first.

---

## Phase 1 — Read-Only Environment Probe

Run these read-only checks first. Do not install or modify anything during this phase.

```bash
uname -sm
node --version 2>/dev/null || echo "node: missing"
npm --version 2>/dev/null || echo "npm: missing"
git --version 2>/dev/null || echo "git: missing"
docker info --format '{{.OSType}}/{{.Architecture}}' 2>/dev/null || echo "docker: unavailable"
which pi 2>/dev/null || echo "pi: missing"
which codex 2>/dev/null || echo "codex: missing"
which anyswarm 2>/dev/null || echo "anyswarm: missing"
```

Also check whether the user already has model credentials in their environment (without printing secret values):

```bash
for v in ANYSWARM_MODEL_URL ANYSWARM_MODEL_ID ANYSWARM_MODEL_TOKEN OPENAI_API_KEY ANTHROPIC_API_KEY; do
  [ -n "${!v}" ] && echo "$v=set" || echo "$v=unset"
done
```

Summarize the probe results concisely for the user before moving to Phase 2.

---

## Phase 2 — Confirm Missing Prerequisites with the User

AnySwarm requires:
1. **OS:** Linux or macOS (Apple Silicon or Intel).
2. **Node.js `>= 22.19`** (Node 24 LTS recommended) and **npm**.
3. **Git**.
4. **A running Linux container engine:**
   - **macOS:** [Docker Desktop for Mac](https://docs.docker.com/desktop/setup/install/mac-install/) or [OrbStack](https://orbstack.dev/download), installed and started. Note: `brew install docker` only installs the CLI and is **not** enough.
   - **Linux:** Docker Engine (`docker.io` or official `docker-ce`) running, with the current user able to reach the Docker socket.
5. **At least one in-sandbox executor backend:**
   - **Pi** (`npm install -g @earendil-works/pi-coding-agent`) — lightweight default, supports both Responses and Chat Completions endpoints.
   - **Codex** (`npm install -g @openai/codex`) — requires an OpenAI Responses-compatible endpoint.

### Approval Gate (Required)

If any prerequisite above is missing or stopped, **stop and ask the user** before making changes:
- List each missing item and why AnySwarm needs it.
- Propose the concrete install method for their OS (e.g., `nvm`/`fnm` vs Homebrew for Node; asking the user to download/start Docker Desktop or OrbStack on macOS; choosing Pi, Codex, or both).
- Wait for the user to approve or complete manual steps (such as starting Docker Desktop) before proceeding.

---

## Phase 3 — Install `@anyswarm/core` and Verify Runtime (No Model Cost)

Once prerequisites are ready and approved, install the CLI and initialize the local OpenSandbox + Worker environment:

```bash
npm install -g @anyswarm/core@latest
anyswarm --version
```

If npm reports `EACCES`, ask the user whether to install into a user-writable prefix (`npm install -g --prefix "$HOME/.local" @anyswarm/core@latest` and add `$HOME/.local/bin` to `PATH`).

Then run setup (pass `--backend pi` or `--backend codex` if both are installed so setup does not block on an interactive prompt):

```bash
anyswarm setup --backend pi
# or: anyswarm setup --backend codex
```

Verify the local Runtime, OpenSandbox container, Host Gateway, and file export without spending model tokens:

```bash
anyswarm doctor --smoke
```

Expect `"ok": true` and `"infrastructure": "ready"`.
If running on a remote Linux server over SSH, remind the user they can view traces locally via:

```bash
ssh -N -L 8787:127.0.0.1:8787 <user>@<host>
```

---

## Phase 4 — Optional Test Run (Ask Before Spending Tokens)

Once `anyswarm doctor --smoke` passes, **do not automatically call a paid LLM**.
Instead, report that installation is complete and ask the user:

> AnySwarm is installed and verified locally (`doctor --smoke` passed). Would you like to run a live test demo? (This will call your configured LLM API.)
>
> 1. **Quick Demo — Subagents Repository Handoff (~2–5 min):** Spawns a Root agent and two isolated child sandboxes to inspect a repository in parallel and export `report.md`, then opens `anyswarm trace`.
> 2. **Advanced Demo — AutoResearch (Local Lightweight Mode, ~10–15 min):** Spawns competing research peers in isolated sandboxes with a host-controlled evaluator to iteratively improve code and score patches.
> 3. **Skip for now:** Stop here; you are ready to build or eject a Harness.

Only after the user picks an option and confirms their model endpoint (`ANYSWARM_MODEL_URL`, `ANYSWARM_MODEL_ID`, `ANYSWARM_MODEL_TOKEN`) should you run the chosen demo:

### Option 1: Quick Subagents Handoff Demo

```bash
git clone https://github.com/sindresorhus/p-limit.git ./handoff-input
anyswarm eject subagents --dir ./handoff
cd ./handoff && npm install
npm start -- --config ./handoff-config.ts --cwd ../handoff-input \
  --prompt 'Prepare an engineering handoff for this repository: split entry points/checks and core scheduling/tests across two subagents, cross-check citations, and write report.md.' \
  --output-dir ../runs
anyswarm trace --root ../runs --port 8787
```

### Option 2: Advanced AutoResearch Local Demo

```bash
anyswarm eject autoresearch --dir ./research-local
cd ./research-local && npm install
```

Ask the user which local directory and scoring script (`eval.py` or shell command) they want to optimize, and confirm the peer count, time window (e.g., 10–15 minutes), and evaluation budget before starting the run. After completion, inspect results with:

```bash
anyswarm trace --root ./runs --port 8787
```
