# pool

ForgeDock's built-in remote compute pool (imported from computeshare; AGPL-3.0-or-later).

- `bin/pool/` router, agent, docker/http/store helpers and `cli.mjs` (`node bin/pool/cli.mjs router|agent|install-command ...`).
- `pool/image/` lane image recipe (Dockerfile, shims, entrypoint, `cs-cred` credential relay, `cs-relay` read-only capability relay: allowlisted commands executed on the dispatching host, #4778).
- `pool/install/` agent installers (Windows/WSL, native Linux, router).
  Native Linux agent (root, systemd): `pool/install/install-agent-linux.sh --router URL --token CODE [--name N --cpus N --mem GB --dry-run]`.
  One command on Linux or macOS (#5883, outbound, no inbound port): `forgedock pool enroll --one-liner` prints
  `curl -fsSL '<router>/install' | sh -s -- --code '<code>' [--cpus N --mem GB]` (run as root, e.g. `| sudo sh -s -- ...`). Rerunning is safe:
  an enrolled host reuses its code and agent token. macOS needs a running Docker Desktop or colima and installs a LaunchDaemon.
  Uninstall: `curl -fsSL '<router>/install' | sh -s -- --uninstall` (or `pool/install/uninstall-agent-linux.sh` / `uninstall-agent-macos.sh`,
  `--dry-run` to preview). It removes the service, this agent's labelled containers, volumes and lane images and its files; docker and node
  only when `/etc/computeshare/installed-by` records that the installer added them. Never a `docker system prune`.
  Device login (#5886): `install-agent-linux.sh --router URL --device` prints a user code and polls; on your ForgeDock machine run
  `forgedock pool link <user_code> [--account A]` and the box enrolls itself, stamped with that account. `forgedock pool status --account A`
  shows one account's boxes; `forgedock pool policy <agent> [--cpus N --mem GB --idle-only --pause]` sets a per-box policy the router pushes
  (the box owner's own `pool pause` always wins). A launch spec with `account` is placed only on that account's boxes.
- `pool/services/` opt-in caching proxies (npm, PyPI, apt) and a read-only git mirror (`docker-compose.yml`); nothing starts them. See `docs/ENV-LIBRARY.md`.
- Tests: `bin/tests/computeshare.test.mjs`.

The module boundary is the ForgeDock HTTP sandbox protocol: the router speaks it to the dispatcher and relays to agents over HTTP. Sandboxes receive only short-lived GitHub and Claude access tokens; refresh tokens stay on the router host.

Claude login choice (#5165): among the router host's logins with 30+ minutes left, the router hands out the one with the lowest 5-hour use in tokenbaton's `live.<n>` (account `n` = `~/.claude-<n>`), read from router.json `usage_source` (default `$TOKENBATON_HOME`, else `~/.tokenbaton`); validity only breaks ties. An account at or above `usage_ceiling_pct` (default 95) or at 100% weekly use is skipped; when every login is, `/agent/creds/claude` answers a typed 429 (`code: rate_limited`, `reset_at` = the earliest reset, `retry-after`), which the agent passes through and `cs-cred` prints as `usage limit hit; resets at <ISO>` so the lane defers. Without readable usage it falls back to the longest validity and logs a warning. The selected account index and usage are logged, never the token. A sample whose 5-hour reset has passed is stale (#5205): unknown usage, never 0%, ranked after fresh samples. A lane that hits a usage limit runs `cs-cred claude --limited <reset>`, which drops the sandbox's cached token and posts to `/sandboxes/<id>/creds/claude/limited`; the agent drops its own cache and forwards to the router's `POST /agent/creds/claude/limited`, which excludes the account it last handed that agent until the reset (default one hour), then the lane fetches the next login and relaunches at once. With every login excluded or blocked, the 429 carries the earliest reset, limited accounts included.

Disk reporting (#5535, all fields optional; absent from an old agent means unknown): the agent's `GET /capacity` carries `disk_free_gb` (free space under its state dir) and `sandbox_disk_mb` (`{sandbox id: MB}`, refreshed at most every 60s). The router's `GET /agents` passes `disk_free_gb` through in `agents[]` and adds `disk_used_mb` to each `sandboxes[id]`. `forgedock gc` prints the disk used per pool entry (`unknown` when absent) and `forgedock doctor` shows each agent's free disk, warning below `hygiene.min_free_gb`.

Host-disk admission (#5733, WSL agents only): on WSL the lane disk is a vhdx that grows on the Windows drive, which `disk_free_gb` (statfs inside the VM) cannot see. A WSL agent's `GET /capacity` and `GET /health` also carry `host_disk_free_gb` (free GB on the drive behind the distro: agent config `host_disk_path` when set, else the drive of the distro's `BasePath` under `HKCU\Software\Microsoft\Windows\CurrentVersion\Lxss`, else `/mnt/c`; read at most every 60s) and `host_disk_low`. Non-WSL agents, or a drive that cannot be read, report neither field and apply no gate. A launch is refused with 503 `code: pool.disk_low` (`host_disk_free_gb`, `floor_gb`, `Retry-After`) before anything starts when free space is below `min_host_disk_free_gb` (default 25) or when free minus `sandbox_disk_growth_gb` (default 3.5) × (running sandboxes + 1) is below it; `host_disk_low` uses the same test. Warm sandboxes launch through the same gate. The router skips agents reporting `host_disk_low` without posting to them; when that leaves no host it answers 503 `code: pool.disk_low` (`agents` = the skipped hosts). Both use `disk_low_retry_after_s` (default 300) for `Retry-After`, and the lane treats the 503 as a capacity deferral (`SANDBOX_CAPACITY`), not a failure.

The `forgedock pool` CLI wiring is tracked separately (#4544).

Per-repo lane tools (#5650): forge.yaml `execution.lane_tools` (`name` or `name@version`) rides in the launch spec as `lane_tools`. The agent resolves the image in `launch` (`bin/pool/lane-tools.mjs`): no tools keeps the base `forgedock-lane:<version>`; otherwise it uses `<base>-t<sha256 of the sorted list, 12 hex>`, building it once `FROM` the base with one recipe per tool (warm sandboxes keep `lane_tools` in their stored spec, so they use the same image). A tool with no recipe fails before any build (`lane_tools.unknown_recipe`, naming the tool and the known recipes); a failed or timed-out build is `lane_tools.build_failed` and the launch records the error. To add a recipe, add an entry to `RECIPES` in `bin/pool/lane-tools.mjs`: `{versioned, steps(version) -> Dockerfile instructions}` for the Debian/Ubuntu lane image; tool names appear nowhere else.

Pulled variants (#6187): `pool/image/lane-tools-sets.json` lists the `lane_tools` sets worth prebuilding. After the base image push, `publish.yml` (and `lane-image.yml` for a manual rebuild) runs `scripts/lane-variants.mjs` to build and push each as `ghcr.io/rapiercraftstudios/forgedock-lane:<version>-t<hash>` (same hash as `laneToolsTag`). A host pulls the variant like the base (IPv4 pins on an IPv6 dial error), retags it to the local tag and builds on the host only when the pull fails (a set that is not in the list is always built). The fallback build runs under the same IPv4 fallback. A failed build is never cached as a permanent 422: it is reported (`lane_tools.build_failed`, carrying the last 1500 characters of the build output) for 1, 2, 5, then 10 minutes after consecutive failures of the tag, then retried.

Priority and reservations (#5856): a host's leftover compute can run lanes while its own workload keeps priority. Agent config `agent.json`: `reservations: [{"name":"marcode","cpus":10,"mem_gb":20}]`, `idle_only: true` (for a box with no reservations: borrow lanes run only while owner load and free memory are above the floor). Sandboxes carry `spec.priority` (`reserved` with `spec.reservation`, `normal`, `borrow`; lanes are `borrow`). Admission: reserved is always admitted up to its reservation (borrowed lanes are throttled via `--cpu-shares`, then SIGTERMed with `drain_grace_s`, and rescheduled by the dispatcher); borrow may use reserved-but-idle capacity; normal stays outside reservations. `/capacity` adds `cpus_free_unreserved`, `cpus_borrowable` and `reservations[]` so the router prefers hosts with free non-reserved capacity. Run one agent per docker daemon (a second agent would reap the first one's sandboxes).

Tunnel-only hosts (no Tailscale): `forgedock pool enroll --agent-url http://127.0.0.1:<port> [--cpus N --mem GB]` prints `AGENT_URL=... pool/install/install-agent-linux.sh --router ... --token CODE`; the router then reaches the agent at that URL (for example a reverse SSH tunnel).
