# Protocol

Version 2. Reference implementation: `shared/pool/src/protocol.rs` (types),
`server/pool-hub` (hub), `client/node/src/pool` (node). JSON messages use
`"type"` as the tag, in snake case.

## Hub endpoints

| Method | Path | Auth | |
|--------|------|------|-|
| GET | `/pool/v1/ws` | challenge | Node control session (WebSocket, text frames) |
| GET | `/pool/v1/link` | signed | Open or accept a link (WebSocket, binary frames) |
| POST | `/pool/v1/route` | signed | Which head serves requests now |
| GET | `/pool/v1/status` | none | Short pool state |
| GET | `/pool/v1/stats` | none | Full public state: model, nodes, traffic, history |
| GET | `/pool/v1/events` | none | `stats` as server-sent events, event name `stats` |
| GET | `/pool/v1/history?window=` | none | Aggregates over `1h`, `6h` (one-minute buckets), `24h`, `7d` (15 min), `30d` (hours) or `all` (days): requests by plan and outcome, tokens in and out, output rate, availability, machines, clusters, lent memory; `totals` for the window, `totals_24h`, `totals_all`, and `available` (windows with enough history) |

Public endpoints show the first 12 characters of node ids and no IP
addresses. A node's `name` in `/pool/v1/stats` is always an alias,
`node-` and the first six characters of its id: the name a node sends in
its hello is often its host name and never leaves the hub.
While a member downloads its model, its entry carries `download`
(`done_bytes`, `total_bytes`), as the node reports them.

`lent_bytes` in the status is memory **offered**: the sum of the
`budget_bytes` and `ram_budget_bytes` of the nodes that are lending (or,
once a model is planned, of the cluster's members). It counts from the moment a node lends, while
its model is still downloading; it is not traffic served. `state` says what
the pool does with it: `forming` while the members download and load,
`serving` once the head answers.

A node has two ids. The **node id** is the public half of its identity key, in hex
(`identity.key`): the hub knows a node by it, it signs the hello and the
links, and it is the id on the dashboard. The **p2p id** belongs to a
separate key (`p2p.key`) used only for direct links: it names the node's
iroh endpoint, appears as `Direct links ready id=…` in the log and in the
address lookup it publishes, and reaches other members only inside the
opaque `peer` address the hub passes on.

## Signed requests

Four headers:

| Header | Value |
|--------|-------|
| `x-hyverk-node` | node id: Ed25519 public key, 64 hex characters |
| `x-hyverk-ts` | Unix seconds; refused beyond 120 s of the hub's clock |
| `x-hyverk-nonce` | 16 random bytes, hex; each one accepted once |
| `x-hyverk-sig` | Ed25519 signature, hex, over the message below |

Signed message, lines joined with `\n`:

```
hyverk-req-v1
<METHOD in upper case>
<path, with the query string when there is one>
<ts>
<nonce>
<sha256 of the body, hex; of the empty string for GET>
```

## Control session

1. Node opens `wss://llm.meshkore.com/pool/v1/ws`.
2. Hub: `{"type":"challenge","protocol":2,"nonce":"<hex>"}`.
3. Node: `{"type":"hello", ...}` with `sig` over
   `hyverk-hello-v1\n<nonce>\n<node_id>`, plus `name`, `version`, `os`,
   `host` (memory and accelerator), `budget_bytes`, `lending`, `direct`
   (null unless the node opts into LAN RPC), `cached_models`, `paused`
   when lending is off because of the power source, and `ram_budget_bytes`:
   system RAM lent next to a discrete GPU, on top of `budget_bytes`. Its
   presence, even as 0, says the node runs two-device workers and drives
   them as head; the hub counts RAM only when every lending node sends it.
4. Hub: `welcome` or `reject` with a reason.

Then, hub to node:

- `assign`: generation, model, and role, tagged by `role`:
  `{"role":"head","peers":[{"node_id":"...","direct":null}],"split_bytes":[...]}`
  or `{"role":"worker","mem_limit_bytes":...}`. A worker with a part in
  system RAM gets `ram_bytes` (included in `mem_limit_bytes`) and serves its
  GPU and then the CPU; the head then sees that peer with `"devices":2`, and
  `split_bytes` has one entry per device: the head's, then each peer's GPU
  part and RAM part in order.
- `release`: stop the engine of a generation.
- `link`: another member opened a link to this node: `stream`, `from`,
  `purpose` (`rpc` or `api`), `generation`.

Node to hub:

- `lending`: the owner switched lending or changed the budget (with
  `ram_budget_bytes` as in the hello), or the
  power source paused or resumed it. `paused` is `on_battery` or
  `low_power` while paused that way, absent otherwise; the dashboard shows
  it, and hubs that predate it ignore it.
- `engine`: `downloading`, `starting`, `ready`, `failed`, `stopped`.
- `stats`: from the head every 5 s: request and token counters, generation
  speed.

Both sides ping; a session silent for 60 s is closed.

## Links

A link carries one TCP connection between two nodes through the hub.

1. Opener: signed `GET /pool/v1/link?to=<node id>&purpose=rpc|api&generation=<n>`,
   upgraded to WebSocket. The hub checks the plan (see
   [Security](security.md)), then sends the target a `link` message.
2. Target: signed `GET /pool/v1/link?accept=<stream>` within 10 s.
3. The hub pairs the two sockets and copies binary frames both ways. Each
   frame is raw bytes of the TCP stream, 4 MiB at most. A close on either
   side closes the other.

Refusals before the upgrade: `401` bad signature, `403` not allowed or not
connected, `429` rate limited or too many links, `503` target gone or the
pool is not serving.

On top of a link:

- `rpc`: the llama.cpp RPC protocol, from the head's `llama-server` to the
  worker's `ggml-rpc-server`.
- `api`: HTTP/1.1 from a node to the head. Only `POST /v1/chat/completions`
  and `POST /v1/completions` are served.

## Route

`POST /pool/v1/route` with body `{}`:

```json
{"generation": 3, "model_id": "qwen3.6-35b-a3b-q4km", "model_name": "Qwen3.6 35B-A3B (Q4_K_M)",
 "head": "<node id>", "context": 32768}
```

`503` with the pool status when nothing is serving.
