# Phase network protocols and wire formats

Every Phase node communicates over `libp2p 0.57` with Noise XX encryption (TCP and Circuit Relay v2) or TLS 1.3 (QUIC v1). The networking substrate (`crates/phase-net`) is completely workload-neutral: it registers eight custom `/phase/*` protocols alongside standard libp2p discovery and NAT traversal behaviours.

## Transport and swarm bounds (`phase-net`)

- **Transports:** TCP + Noise XX + Yamux, QUIC v1 (TLS 1.3), Circuit Relay v2 Client + Noise XX + Yamux.
- **Idle connection timeout:** `60s`.
- **Connection limits (`connection_limits::Behaviour`):** max `32` pending inbound, `32` pending outbound, `128` established inbound, `128` established outbound, `4` per peer, `256` total.
- **Standard libp2p behaviours:**
  - **Kademlia DHT:** Forced `KademliaMode::Server` with `MemoryStore`; key `1..=512 B`, value `1..=64 KiB`, `256` values / `4 MiB` per `get_record`, `64` providers per query, `32` concurrent provider ops, `10s` op timeout.
  - **Identify:** Protocol `/phase/reachability/1.0.0`, `agent_version = "phase-net/<version>"`, signed peer records enabled, filters observed addresses to reject unspecified/multicast/broadcast/zero-port/unix/memory addresses.
  - **Ping:** `15s` interval / `15s` timeout keepalives; immediately closes unresponsive connections on ping failure so roaming laptops and helpers recover without waiting for stale OS TCP timeouts.
  - **Circuit Relay v2:** Client enabled by default with background self-healing re-reservation on listener close or address expiry; once a direct or DCUtR connection is established to a peer, redundant relayed circuits to that peer are pruned automatically. Server strictly restricted to `ReachabilityRole::Infrastructure` (defaults: `128` reservations / `2` per peer / `1h`; `256` circuits / `4` per peer / `2m` / `64 MiB`).
  - **DCUtR:** Direct hole-punching upgrade (disabled when `--outbound-only`).
  - **AutoNAT v2:** Client enabled by default (disabled when `--outbound-only`); Server restricted to `Infrastructure`.
  - **mDNS & LAN Provenance:** Local LAN peer discovery (disabled when `--outbound-only`). Both mDNS-discovered addresses and explicit LAN `--bootstrap-peer` addresses preserve provenance so local routes stay usable in Kademlia alongside public seeds.
  - **Rendezvous:** Client enabled (`discover` limit `1..=256`, `lucidd` namespace `"phase-lucid-workers"`); Server explicitly disabled (`None`) in `lucidd` infrastructure mode pending upstream registration-count quotas.

## Custom `/phase/*` libp2p protocols

| Protocol ID | Transport & Wire Framing | Size & Timeout Bounds | Admission & Auth Gates |
|---|---|---|---|
| `/phase/job-relay/2.0.0` | Raw `libp2p_stream`, 4-byte BE length-prefixed **postcard** frames (`JobRelayStreamOpen`, `JobRelayStreamFrame`, `JobRelayStreamControl`: `Cancel` / `ReceiptAck`); inner manifest/event/receipt are `serde_json`. Tool fields (`tools`, `tool_choice`, `tool_calls`, `tool_call_id`, `name`, `parallel_tool_calls`) use `skip_serializing_if` so non-tool manifests remain byte-identical to v0.2.0. | Open ≤ `256 KiB` (+1 KiB wire), Event ≤ `1 MiB` (+4 KiB wire), Receipt ≤ `256 KiB`, Reason ≤ `1 KiB`; idle `250ms..=60s` (default `30s`), deadline ≤ `15m` (default HTTP/relay job TTL `10m`). | Shared stream pool (`64` global / `4` per peer). Fail-closed `JobRelayStreamValidator` enforces exact `job_id`, contiguous `sequence` from 0, `Event` → `Receipt` ordering. `lucidd` enforces `verify_for_remote_execution()`, `signer_pubkey → PeerId` binding, default-deny allowlist/enrollment, replay cache, token/prompt/tool caps, fair FIFO `SlotQueue` (`16` total / `4` local-reserved / `2` per peer / `120s` wait cap), and `PolicyEngine`. |
| `/phase/job-relay/1.0.0` | `request_response::cbor` outer (`JobRelayRequest` / `JobRelayResponse`); inner `SignedManifest<JobSpec>`, `Vec<JobEvent>`, `SignedReceipt<JobResult>` are `serde_json`. | Request ≤ `256 KiB`, response ≤ `8 MiB`, `5m` timeout. | Legacy batch compatibility (fail-fast when busy). Shares `prepare_inbound_relay` authz, concurrency gate, and replay cache with v2. |
| `/phase/blob/1.0.0` | Raw `libp2p_stream`, 4-byte BE length-prefixed **postcard** frames (`BlobStreamRequest` → `Accepted` / `Rejected` / `Chunk*` / `Eof`). | Chunk `1..=64 KiB`, metadata ≤ `4 KiB`, idle `250ms..=60s` (default `30s`), deadline ≤ `1h`. | Stream pool (`64` / `4`) + fixed-window per-peer egress cap `16 MiB/s`. `BlobStreamValidator` enforces exact `content_id`, sequential cursor offsets, and `Eof` at `total_size`. `lucidd::ContentPlane` adds `BlobServeGate` (`4` global / `1` per stripe across 256 stripes), rejects non-empty metadata, and serves only verified installed CIDs when `--serve-model-content` is enabled. |
| `/phase/shard-stage/1.0.0` | Raw `libp2p_stream`, 4-byte BE length-prefixed **postcard** frames (`ShardStageRequest` → `ShardStageResponse`: `Completed` / `Rejected` / `Failed`); inner plan & receipt are `serde_json`. | Plan ≤ `256 KiB`, payload ≤ `8 MiB`, receipt ≤ `64 KiB`, reason ≤ `1 KiB`; idle `250ms..=120s` (default `30s`), deadline ≤ `15m`. | Stream pool (`64` / `4`). Enforces default-deny coordinator authz (`coordinator_pubkey_hex → caller_peer`), verified `SignedShardPartitionPlan`, Tier-1 `TrustedCircle` only, exact `stage_index` + local `PeerId`/pubkey binding, and `PolicyEngine::should_serve`. |
| `/phase/shard-rpc/1.0.0` | Raw `libp2p_stream`: 4-byte BE length-prefixed **postcard** handshake (`ShardRpcOpen` ≤ 256 B → `ShardRpcDecision` ≤ 1280 B), then **raw spliced bidirectional byte stream** (`64 KiB` splice buffer). | Idle `1s..=12h` (default `2h`, with `15s` ping dead-peer eviction), session `1s..=24h` (default `12h`). | Dedicated pools: `32`/`8` pending handshakes, `32`/`8` inbound sessions, `128` outbound. Synchronous `peer_filter` drops non-enrolled peers before reading 1 byte; **never** honors `allow_unauthenticated_jobs`; requires live `cluster_enabled = true` and `!manual_pause`, re-checked every `250 ms` with immediate teardown on revocation. |
| `/phase/capacity-offer/1.0.0` | Raw `libp2p_stream`, 4-byte BE length-prefixed **postcard** request/response wrapping JSON `CapacityOfferRequest` (nonce only, no model ID) → `SignedCapacityOffer` or `Refused`. | Request ≤ `512 B`, offer payload ≤ `4032 B` (`4 KiB` max frame), reason ≤ `256 B`; `2s` read / `5s` write / `5s` handler / `10s` client timeout. | Pools: `16` inbound (`4`/peer), `4` outbound, `5s` per-peer rate limit. Enrolled peers only (`cluster_enabled = true`, `!manual_pause`). Coordinator strictly verifies Ed25519 signature (`signer_pubkey → helper_peer_id`), audience (`coordinator_peer_id`), echoed 32-byte nonce, `60s` max TTL (`5s` skew), `usable_bytes ≤ 1 TiB`, and matching runtime SHA-256. |
| `/phase/job-offer/1.0.0` | `request_response::json` (`JobOffer` → `JobResponse`). | `64 KiB` max request & response. | Legacy WASM capability check (arch, runtime prefix, CPU, memory). No signature or submitter authz gate. |
| `/phase/reachability/1.0.0` | libp2p Identify protobuf (`new_with_signed_peer_record`). | Standard Identify bounds. | Rejects non-observable addresses (unspecified, multicast, broadcast, zero-port, unix, memory). |

## Protocol flow details

### `/phase/job-relay/2.0.0`

1. Requester opens a stream and sends `JobRelayStreamOpen` containing `job_id` and `SignedManifest<JobSpec>` (`serde_json`).
2. Worker validates authorization, tool/prompt bounds, replay cache, manifest expiry/clock skew, and policy, then acquires a slot in the shared FIFO `SlotQueue` (busy refusals happen *before* replay-cache admission so callers can retry).
3. Worker sends `Accepted` and streams zero-wire-break `JobEvent::Progress` heartbeats (`kind: "queued"` with position/depth while waiting, `"dispatched"` when the slot opens, and `"model_load"` / prefill keepalives while loading or prefilling past the 30-second idle timer). On every queue heartbeat and again right before dispatch, the worker re-checks manifest expiry, submitter authorization, pause policy, and `max_concurrent_remote_jobs`.
4. Worker streams ordered `Event { sequence: 0, 1, ... }` frames containing text `OutputChunk`s and—for tool turns—index-ordered `"tool_call"` chunks followed by `Completion::ToolCalls`.
5. Requester may send `JobRelayStreamControl::Cancel` at any time (for example, clicking **Stop** in Desktop Chat) to free its queue spot or abort inference cleanly.
6. Upon completion, worker sends a final `Receipt` frame with the Ed25519 `SignedReceipt<JobResult>`; requester replays the chunk commitment and verifies all four bindings (plus `ToolCallPolicy` before releasing any tool calls) before upgrading `X-Lucid-Trust-Label`.

### `/phase/shard-stage/1.0.0`

Coordinator sends `ShardStageRequest` containing a verified `SignedShardPartitionPlan`, `stage_index`, and opaque stage activation payload to each stage worker in sequence. Each worker validates that it owns `stage_index`, forwards the payload to its local loopback stage bridge (`--shard-stage-bridge-port`) with `x-lucid-plan-id`, `x-lucid-job-id`, `x-lucid-model-cid`, `x-lucid-stage-index`, and `x-lucid-layer-range` headers, signs a `SignedShardStageReceipt` binding input and output commitments, and returns the stage output.

## Complete Kademlia DHT key namespaces

Every DHT record is verified at the application layer by Ed25519 signature (Kademlia storage itself is untrusted).

| DHT Key Pattern | Kademlia Op | Owner & Purpose |
|---|---|---|
| `/phase/capability/<arch>/<kind_label>` | `start_providing` / `get_providers` | `phase-net` — workload capability discovery |
| `/phase/<channel>/<arch>/manifest` | `put_record` / `get_record` | `phase-artifact-server` / `plasm` — Phase Boot channel manifests |
| `/phase/blob/<sha256_hex>` | `put_record` / `get_record` | `phase-artifact-server` — HTTP blob source hints |
| `phase/model/<32-byte ModelCid>` | `put_record` / `get_record` | `lucidd` — `SignedModelAdvertisement` (schema v2, ≤16 KiB, 15m max TTL) |
| `phase/model-alias/v1/<SHA256("phase:model-alias-key:v1\0" \|\| alias)>` | `put_record` / `get_record` | `lucidd` — `SignedAliasRecord` (schema v1, ≤4 KiB, 24h max TTL, sequence-ordered) |
| `phase/content-provider/v1/<32-byte ModelCid>` | `put_record` / `get_record` | `lucidd` — `SignedContentProviderRecord` (schema v1, ≤4 KiB, 24h max TTL, PeerId-bound) |
| `phase/lucid/catalog/providers/v1` | `start_providing` / `get_providers` | `lucidd` — catalog publisher provider discovery |
| `phase/lucid/catalog/publisher/v1/<publisher>` | `put_record` / `get_record` | `lucidd` — `SignedCatalog` (schema v1, ≤64 models, ≤64 KiB, 5m TTL) |
| `phase/lucid-enrollment/request/v1/<issuer>/<id>` | `put_record` / `get_record` | `lucidd` — doubly signed circle `EnrollmentRequest` mailbox |
| `phase/lucid-enrollment/approval/v1/<issuer>/<id>/<requester>` | `put_record` / `get_record` | `lucidd` — issuer-signed circle `Approval` mailbox |
