Connection¶
A worker holds one WebSocket connection to the server at /proto. All frames are binary and encoded by the Proto derive. A session moves through five stages: version agreement -> handshake -> authorization -> capabilities -> job loop.
sequenceDiagram
participant W as Worker
participant S as Server
W->>S: version range
S->>W: version range
W->>S: InitConnection { capabilities, id }
S->>W: AuthChallenge { peers }
W->>S: AuthResponse { tokens }
S->>W: InitAck { authorized_peers, failed_peers }
W->>S: RunningJobs { assignments }
W->>S: WorkerCapabilities
loop job loop
W->>S: RequestJob
S->>W: AssignJob
end
Handshake¶
Four steps make up the handshake:
| Step | Message | Content |
|---|---|---|
| 1 | InitConnection |
capabilities, the worker's persistent id |
| 2 | AuthChallenge |
Peers that registered this worker ID without url |
| 3 | AuthResponse |
A token for each challenged peer |
| 4 | InitAck or Reject |
Authorized peers, failed peers with reasons |
Each worker stores its ID in /var/lib/gradient-worker/worker-id. The server can match reconnects by that ID, reject duplicates and label the worker in the UI and logs.
Handshake messages can be at most MAX_HANDSHAKE_MESSAGE_SIZE (64 KiB) in size. A larger message can end the session with close code 1002, before any decoding.
Rejections: The server can end a session with the codes below. A connected worker can reject the server with 400, 401, 403 or 503, see Server-Initiated Handshake.
| Code | Reason |
|---|---|
400 |
An unexpected message during the handshake |
401 |
unknown worker id or wrong token: the same answer for an unknown worker ID, a deactivated worker and a wrong token, so a client can't tell them apart before it proves a token |
403 |
worker is deactivated (sent to an already connected worker after its last registration is deactivated), no project grants this team's workers, or the server is not accepting connections |
495 |
project has no cache subscribed: every authorized project is lacking a cache |
496 |
worker already connected |
Server-Initiated Handshake¶
The server can open a connection to each registration and team worker with a url. Server tokens plus the worker's TLS certificate prove both sides.
sequenceDiagram
participant S as Server
participant W as Worker
S->>W: Authenticate { worker_id, tokens }
W->>S: InitConnection { capabilities, id }
S->>W: InitAck { authorized_peers, failed_peers }
| Step | Message | Content |
|---|---|---|
| 1 | Authenticate |
The worker ID and one (peer, token) pair for each registration with a stored token at that URL |
| 2 | InitConnection or Reject |
401 on an unknown worker ID or a wrong token, 400 on any other first message |
| 3 | InitAck or Reject |
The projects whose tokens the worker accepted, with no challenge round |
A side that refuses the handshake can reply with these codes.
| Code | Reason |
|---|---|
400 |
A first message other than InitConnection |
401 |
An unknown worker ID or a wrong token |
403 |
the link is disabled, the upstream is not allowed to run |
503 |
the upstream is unreadable right now, the allowance is not checked yet |
- Server connections use only
wss://URLs. Connections tows://URLs work only for loopback hosts, so tokens never travel over the network unencrypted. - Worker URLs on private, loopback, or link-local addresses fail at registration and before every connection unless administrators set
proto.allowWorkerUrlsOnPrivateNetworks. Server connections go only to addresses that the server inspected. - Workers compare the hash in
services.gradient.worker.acceptedServerTokensFileon each worker with the server token. Discoverable workers with no such file accept all servers. NixOS modules needservices.gradient.worker.allowDiscoverableWithoutAcceptedServerTokensFile. - A server-connected session that reauths receives
AuthUpdate, neverAuthChallenge. - Sessions for a server connection end with
AuthResponsefrom the worker. - Registrations and team workers with
urlcarry the address that the server can dial. The server can't authenticate incoming connections with those worker IDs and tokens. The server can consider only registrations and team workers withouturlfor an incoming handshake and a reauthentication challenge. - A reauth that discovers a new registration ends the session. The next connection can carry the new project's token.
- Projects granting a team's workers join the running session. All of them share the team worker's single token for the team ID.
- A registration cannot reuse the worker ID of a team worker. Each Gradient.CI worker ID can belong to exactly one registration or team worker.
- The server can hold the last failure of each worker ID in memory as that worker's offline reason.
- The server can hold a failure from before the token check only for a registered worker ID.
Capabilities¶
Two sides of a connection negotiate capabilities during the handshake. A capability counts as active only when both sides support it. The server alone can enable core and cache.
| Capability | Meaning |
|---|---|
core |
The server side of the protocol. Always enabled on the server, disabled on workers |
cache |
Acting as a binary cache. Always enabled on the server |
network |
Internet access for clones, flake inputs, substitutions and fixed-output builds |
secrets |
Receiving project credentials such as the SSH key |
exec |
Declared and stored, with no behavior attached yet |
eval |
Running flake evaluations |
build |
Running Nix builds |
federate |
Reserved. Part of the handshake, with no behavior attached yet |
Capability flags mark feature support. They do not stand in for protocol versions.
Authorization¶
A peer is anything registering a worker, such as a project, a cache or a proxy. Both sides consent. Peers register the worker ID and workers hold the peer's token.
- A peer registered the worker ID. The server generated a token for that pair.
- The peers file (
services.gradient.worker.peersFile) is a list ofpeer_id:tokenlines. - The server can challenge every registering peer on connect and check each token on its own.
| Peer | Granted Access |
|---|---|
| Project | Jobs from the project's tasks |
| Cache | Serving and pulling from the cache |
| Proxy | Membership in the proxy's worker pool |
- The remaining peers keep the session open after some failed tokens.
- A failure of every token can reject the session.
- A project without a cache subscription can move to
failed_peersin the reply. - The reply is
495when no peer remained. - Reauth can add peers without reconnecting.
- The server can send
AuthChallengeto a connected worker when a peer registered the worker. - A cache subscription by a project is a reason for a reauth of the project's workers.
- The worker can send
ReauthRequestwhen its peers file changed. - Both end in
AuthUpdateand reauth can never revoke granted peers. - A single connection per worker ID. The server can reject a second connection with
496. - The server can drop a connection silent for
proto.workerHeartbeatTimeoutSecs(120 s). - The worker can drop a connection with data unacknowledged for 120 s and reconnect.
- The worker can abandon a connect after 10 s and a handshake after 15 s without an answer.
- A project's worker list can mark a worker as live when that worker authenticated for the project.
Build Timeouts¶
The timeout_secs and max_silent_secs of a BuildSpec follow the derivation's own timeout and maxSilent. The fallbacks are build.defaultTimeoutSecs (14400) and build.defaultMaxSilentSecs (3600). A limit is off at a value of 0. A build past a limit is a FailedTimeout. Evaluations have no timeout.
Server Restart¶
Running builds and evaluations survive a lost connection and a server restart. Queued jobs survive as well.
| Side | Behavior |
|---|---|
| Worker | Keeps its running jobs. Reconnects with backoff that starts at 1 s, doubles each try, and caps at 60 s |
| Worker | Appends LogChunk payloads to /var/lib/gradient-worker/job-logs/ while detached. At most 1 GiB of unsent log per job. Older bytes give way to one bytes of log dropped marker line |
| Worker | Queues every other job report in memory. Only the newest BuildProgress or EvalProgress of a job stays in the queue |
| Worker | Queues again every JobCompleted and JobFailed sent during the lost session. A dead socket can swallow them unnoticed. An AbortJob or a new assignment of the job drops the copy |
| Worker | Performs a full handshake, then sends RunningJobs with the assignment of each running job, an empty list included |
| Worker | Sends the queued reports first. Replays each buffered log next to the job loop, from the offset already sent |
| Worker | The worker can send its store summary and then one RequestJob for each kind |
| Server | Runs recover_interrupted_work before the first session. Assignments outside cluster attempts stay open. Their builds remain Building |
| Server | Keeps the open assignments of a disconnected worker for proto.assignmentReclaimGraceSecs (600 s). The grace of a worker lost by a running server ends with its remaining maxLifetime at the latest |
| Server | Reattaches every listed assignment that is still open and belongs to this worker. Sends AbortJob for every other listed one |
| Server | Re-queues at once every open assignment of the worker absent from RunningJobs, as a lost worker |
| Server | Drops every report whose assignment_id does not match the live dispatch of that job in the scheduler core |
- Cluster members and aborted jobs stay out of
RunningJobs. Cluster members abort at the end of the session. RunningJobscan also list a job with its finalJobCompletedorJobFailedstill in the queue.- Jobs of a server-dialed session end with the session. Such sessions never reclaim jobs.
- Workers empty
job-logs/at process start. No job can outlive a worker process restart. - Log bytes in flight at the moment the connection dropped can be missing from the build log.
Server Stop¶
SIGTERMshut down every server session at the same time.- Workers keep their running jobs and list them in
RunningJobsafter the reconnect. - Startup recovery can re-queue the interrupted work without an open assignment on the next start.
- Workers reconnect after the server is back.
- A
Drainingmessage from the server can terminate the session. The worker process stay alive.
Version Agreement¶
- Both sides send this 8-byte frame before the first message. Future versions leave it alone.
- The
ProtoSockettype can agree on the first send or receive. Both sides are sending first and reading second. - The agreed version is the lower of both newest versions. Every later frame is for that version.
- Two ranges without overlap can end the session with close code
1002andno shared protocol version: ours 30..=30, peer 31..=32. - A first frame without
GRADcame from protocol 26 or older. The session can end withpeer protocol is older than 30. - Supported versions start at 67. Peers older than 67 share no version with the other side. The session can end with close code
1002. - Both sides are logging the reason at the
warnlevel. - A server connecting to a worker can keep the reason as the worker's offline reason.
gradient_wire::PROTO_VERSIONSis the supported range, computed from the#[proto]annotations.- The agreed version for feature checks is available through the
writer.version()call.
Changing a Message¶
| Change | Annotation | Effect |
|---|---|---|
| New field | #[proto(N)] |
The field exist since version N. The oldest supported version rise to N |
| New field with a fallback | #[proto(N, default)] |
Older peers omit the field. Decoding fill in Default::default() |
| New variant | #[proto(N)] on the variant |
The variant get appended at the end. Sending it to a peer below N trigger an EncodeError |
| Removed or reordered field or variant, changed meaning | #[proto(oldest = N)] on ClientMessage and ServerMessage |
The oldest supported version rise to N |
- A field without
defaultcount as mandatory. A missingdefaultbreak compatibility, not correctness. - New variant can only be appended. Placing a variant before an older one fail to compile.
- Supported versions N get one file each.
gradient-wire/schema/v{N}.txtcan record the wire shape of that version along with variant names and field names. Theschematest can fail when two variants or two fields of the same type swap. Renames don't change bytes on the wire, so update the names by hand in all schema files. cargo run --example wire_schemacan write the file for a new version and remove files below the oldest version. Existing files remain untouched.
Cache Sessions¶
A read-only session for one cache is available through the /cache/{cache}/proto endpoint. It serves the same frames as a normal session.
| Aspect | Rule |
|---|---|
| Public cache | Credentials are not required. The setting proto.anonymousCache.enable can turn this off |
| Private cache | The upgrade request needs the header Authorization: GRAD<key> |
| Handshake | The client sends InitConnection. The server answers with InitAck and no peers, without a challenge |
| Allowed | Requests permitted here are CacheQuery in Normal or Pull mode, and NarRequest. Everything else gets a 403 rejection |
| Limits | Each IP can hold proto.anonymousCache.maxConnectionsPerIp (32) anonymous connections. All sessions also count toward proto.maxConnections (256). Anonymous sessions also count toward http.anonymousWebsocketMaxConnectionsPerIp (256) per address. Anonymous sessions on IPv6 count per /64 network. Each message is limited by SAFE_INFLIGHT_MESSAGE_SIZE (2 MiB), which fits a CacheQuery with 1000 paths |
| Idle | The server closes the session after 120 s without a NAR transfer |
Implementation¶
- A single handshake state machine in
gradient-wire/src/session/handshake.rsis the driver of every session. - The server can run
as_authorityfor an incoming worker andas_dialerfor a worker the server connected to. The worker can runas_peerandas_dialed. - The cache session can agree on a version like every other session.
- The code in
session/frame.rscan split each socket into a typed reader and a writer. The writer can hold a control lane and a bulk lane. - Control frames go first. A bulk batch can hold no more than 256 KiB.
- A full bulk queue can never block control replies.
- Chunk payloads (
NarPush,UploadChunk,EvalCacheChunk,LogChunk) arrive at the handler as slices of the frame, without a copy. - The
client::dialfunction can disable Nagle's algorithm on every socket. Small control frames go out without waiting for a delayed ACK.