Skip to content

Wire Protocol

Protocol v3 carries live data from a training process to viewers over one WebSocket. The viewers are a browser tab, a Jupyter widget, the hub and the native Caliper app. From version 3.1, the stream also carries the controls of a viewer back to the script. The Header Listing embeds the header include/caliper/live_net.h, which defines the constants and the frame header. The golden frames give test data for every implementation.

Status

Version 3.1 adds controls and linked selection to 3.0. The stream works on the loopback interface only, with a session token. Network streaming explains the design decisions behind it.

Protocol v2 (caliper/live.h, shared memory) is a separate zero-copy path between two processes on the same machine. Protocol v3 sends copies of reduced snapshots over a socket. A producer reduces each snapshot on its own device before it sends it, so the copy is small.

Roles and Terms

Role What it is What it does
Producer The training process, through caliper.serve() in Python Owns the tensors, computes every data view on its own device, and sends snapshots and series values
Viewer A browser element, the Jupyter widget, or the native app's NetSource Draws what it receives. It computes nothing
Hub caliper serve, a subcommand of the caliper binary Accepts producers and viewers, holds several sessions and forwards between them

The side that opens a connection is its client. The side that accepts it is its server. A connection has one of three kinds:

Connection Client Server
Viewer to producer A viewer caliper.serve()
Viewer to hub A viewer The hub
Producer to hub A producer The hub

caliper.serve(port=0) listens on 127.0.0.1 and prints http://127.0.0.1:<port>/?token=<token>. The viewer page reads the token from its own address. It then connects to ws://<same host>/v3/stream?token=<token>. A viewer written for caliper.serve() works against the hub unchanged, because a server that holds exactly one session attaches a new viewer to it.

The pages use these terms with one meaning each:

  • A session is the stream of one producer process. A session id such as clp-4242-a1b2c3 names it in lists. The session id is not secret. (Protocol v2 calls the same string the session token.)
  • The session token is the secret of one server. A client must present it to open a connection.
  • A view is one named stream of a producer, such as fc1.weight or loss, of one kind.
  • A live view decides how the viewer draws a view: heatmap, image, points, series or histogram. The kind of a view names its live view.
  • A data view decides which data travels: a source, an index selection, a reduction and a size budget. Data Views defines it.
  • A subscription is a request of a viewer for one view under one data view. The viewer chooses its number, sub.
  • A snapshot is the reduced payload of a view at one moment, sent for one subscription.

Connection Setup

Upgrade Request

A client opens a connection with a WebSocket upgrade (RFC 6455) of GET /v3/stream?token=<session token>. The token is a query parameter because a browser cannot set headers on a WebSocket. Before the upgrade, the server makes these checks in this order:

Check Rule Answer when it fails
Path The path is exactly /v3/stream HTTP 404
Host The Host header is 127.0.0.1:<port> or localhost:<port>, where <port> is the port the server listens on. The host name is not case-sensitive. When the server listens on port 80, the forms without a port are also accepted. This stops DNS rebinding attacks HTTP 403
Origin The Origin header is absent or accepted by the rule below HTTP 403
Token The token parameter is present and equal to the session token of the server, compared in constant time. A server may accept more tokens of its own, as the hub of Caliper API does (Control Permissions) HTTP 403

The server accepts the Origin header when its value is one of these:

  1. The literal null, which sandboxed frames and some notebook front ends send.
  2. An origin <scheme>://<host> or <scheme>://<host>:<port> with the scheme http or https, any port, and the host 127.0.0.1, localhost or [::1]. The host name is not case-sensitive. This covers the page of the server itself and notebook front ends on loopback, such as JupyterLab at http://localhost:8888.
  3. vscode-webview:// followed by any authority, the origin of the webviews that show notebook output in VS Code.

The server refuses any other value, including an origin with a path or user information. A missing Origin means a client that is not a browser, such as the Python producer or the native app. When the origin is null or absent, the session token is the only protection.

The server answers a refused upgrade with the status above and closes the TCP connection. It never logs the token. The upgrade uses no subprotocol. A client sends no Sec-WebSocket-Protocol header, and the server declines permessage-deflate. Every message after the upgrade is binary. A receiver closes with code 1003 when it gets a text message.

The server may serve plain HTTP on the same port, such as the viewer page at /. This specification does not cover those requests, but they get the same Host check.

A session token has 22 to 128 characters of A-Z, a-z, 0-9, _ and -. It comes from at least 128 random bits, for example from secrets.token_urlsafe(16). The viewer page should remove the token from its address bar after it reads it.

Handshake

After the upgrade, the client sends HELLO and the server answers WELCOME. Neither side sends anything else before them, except BYE. A server closes with code 4003 when no HELLO arrives within 10 seconds. A client closes with 4003 when no WELCOME arrives within 10 seconds of its HELLO. Each side learns the minor version of the other from the header of HELLO or WELCOME.

Server Accepts Refuses
caliper.serve() Viewers Producers, with close code 4002
The hub Viewers and producers Nothing by role
The hub of Caliper API Viewers, and producers with the session token Producers on a token that may only watch (Control Permissions), with close code 4002

A server at its connection limit closes with 4005 after the HELLO. Either side may send an ERROR with fatal true just before such a close, to explain it.

Session Choice

Right after WELCOME on a viewer connection, the server sends SESSIONS, the list of the sessions it holds. It sends the list again whenever the list changes. caliper.serve() lists exactly one session, its own.

A viewer that has not sent ATTACH follows the default of the server. Whenever the viewer is not attached to a live session and the server holds exactly one live session, the server attaches the viewer to it. Thus a viewer of caliper.serve() is attached right after WELCOME, and a viewer of the hub follows a notebook kernel that restarts. A viewer that sends ATTACH chooses for itself from then on.

ATTACH names a session id. The server answers with VIEWS for that session, which confirms the attachment. If the session is unknown, it answers with ERROR unknown_session and the viewer stays as it was. ATTACH with session null detaches the viewer. The server then ends the subscriptions of the viewer and sends VIEWS with session null and no views. Attaching to another session ends every subscription of the previous one.

When an attached session ends, the hub keeps it in the list with the state ended, and the viewer keeps the last frames it drew. When the producer of caliper.serve() ends, it closes every connection with BYE shutdown and close code 1001.

Producer Connection

A producer that connects to a hub sends HELLO with the role producer and its session identity: id, script, pid and start_unix_ms. The hub keeps the proposed id unless a live session of another process holds it. In that case, the hub appends -2, -3 and so on. WELCOME carries the identity with the id the hub chose.

A producer that reconnects with the same id, pid and start time resumes its session. The hub closes the old connection, if it is still open, with BYE replaced and code 1000. The viewers stay attached.

After WELCOME, the producer sends VIEWS. On this connection the hub acts as the one viewer of the producer. It sends SUBSCRIBE, UNSUBSCRIBE, VIEW_REQUEST and EVENT, and the producer answers with VIEW_REPLY, SNAPSHOT and SERIES. The hub does these things:

  • It rewrites sub and request_id between its viewer connections and its producer connection.
  • It shares one producer subscription among viewers whose resolved data views are equal, at the largest max_rate among them.
  • It keeps the latest snapshot of every producer subscription and the retained series values, so a viewer that subscribes late gets them at once.
  • It forwards every event of the viewers to the producer.
  • From 3.1, it forwards the controls of the viewers it allows to the producer. It forwards the CONTROLS and SELECTION of the producer to the viewers of the session (Hub Forwarding).
  • It resubscribes after a producer resumes its session.

Keepalive and Closing

A peer that has received nothing for 10 seconds sends PING, and the other side answers with PONG that carries the same nonce. A peer that has received nothing for 30 seconds closes with code 4004. The protocol has its own ping because a browser cannot send WebSocket control pings.

To close, a peer sends BYE with a reason. It then closes the WebSocket with the close code that matches the reason. The code is 1000 for normal and replaced, 1001 for shutdown, and the code of the error for error.

Message Types

Every message is one frame, and Wire Frames defines the frame.

Id Name Direction Sections Answer
0x0001 hello client to server none welcome
0x0002 welcome server to client none
0x0003 bye either none
0x0004 error either none
0x0005 ping either none pong
0x0006 pong either none
0x0010 sessions server to viewer none
0x0011 attach viewer to server none views or error
0x0020 views producer to viewer or hub, hub to viewer none
0x0021 subscribe viewer to producer or hub, hub to producer none view_reply
0x0022 unsubscribe viewer to producer or hub, hub to producer none
0x0023 view_request viewer to producer or hub, hub to producer none view_reply
0x0024 view_reply producer to viewer or hub, hub to viewer none
0x0030 snapshot producer to viewer or hub, hub to viewer data, then optionally aux
0x0031 series producer to viewer or hub, hub to viewer values
0x0040 event viewer to producer or hub, hub to producer none
0x0050 controls producer to viewer or hub, hub to viewer none
0x0051 control viewer to producer or hub, hub to producer none control_reply
0x0052 control_reply producer to viewer or hub, hub to viewer none
0x0060 selection producer to viewer or hub, hub to viewer none

The types from 0x0050 are additions of 3.1. A sender sends CONTROLS, CONTROL and CONTROL_REPLY only to a peer that announced the capability controls. It sends SELECTION only to a peer that announced selection (Versioning). Type 0 is never sent. A known message sent in the wrong direction, or before HELLO and WELCOME, is a protocol error (4000). A receiver ignores a message of an unknown type.

These pages define the messages:

Flow Control

A slow viewer cannot block a producer, and a connection never blocks training. These rules apply:

  • A producer takes no snapshot of a view that no subscription names. With no subscription at all, a call costs one check, as in protocol v2.
  • A producer retains appended series values whether or not anyone subscribes, so a late viewer receives the whole curve as backfill. It does not synchronize with its device to do this. It retains at least 4096 values per series.
  • A sender hands one data message (SNAPSHOT or SERIES) at a time to its socket. It chooses the next message only after the previous one drains. A message that is not yet handed over is pending.
  • Per connection, a sender keeps at most one pending snapshot per subscription, and a newer snapshot replaces it. A viewer with one subscription per view has one pending snapshot per view.
  • Series values merge into one pending SERIES message per subscription, and no value is dropped. A message that would exceed 262,144 values is split.
  • Control messages, which are every type but SNAPSHOT and SERIES, go in order and before pending data. Among pending data, series go before snapshots, and subscriptions take turns.
  • A newer CONTROLS replaces an unsent older one. A newer SELECTION of an item space replaces an unsent older one of the same space.
  • A producer sends at most max_rate messages per second for each subscription.
  • A hub applies the same rules on each viewer connection, so one slow viewer does not slow the others or the producer.

Resource Limits

Constant Value Meaning
CALIPER_NET_MAX_FRAME_BYTES 67,108,864 (64 MiB) The largest frame
CALIPER_NET_MAX_META_BYTES 1,048,576 (1 MiB) The largest metadata
CALIPER_NET_MAX_SECTIONS 8 Sections in a frame
CALIPER_NET_MAX_JSON_DEPTH 16 Nesting of the metadata
CALIPER_NET_NAME_MAX 63 Bytes of a view, source or dim name
CALIPER_NET_SESSION_ID_MAX 63 Bytes of a session id
CALIPER_NET_TOKEN_MIN 22 Characters of a session token, at least
CALIPER_NET_TOKEN_MAX 128 Characters of a session token, at most
CALIPER_NET_MAX_VIEWS 256 Views in a session
CALIPER_NET_MAX_SOURCES 64 Sources of a view
CALIPER_NET_MAX_DIMS 8 Dims of a source
CALIPER_NET_MAX_SUBSCRIPTIONS 256 Subscriptions on a connection
CALIPER_NET_MAX_STATS 64 Entries of stats
CALIPER_NET_DEFAULT_MAX_EXTENT 512 Default max_extent
CALIPER_NET_LIMIT_MAX_EXTENT 4096 Largest max_extent
CALIPER_NET_DEFAULT_MAX_POINTS 100,000 Default max_points
CALIPER_NET_LIMIT_MAX_POINTS 1,000,000 Largest max_points
CALIPER_NET_MAX_BINS 65,536 Bins of a histogram
CALIPER_NET_SERIES_RETAIN_MIN 4096 Series values a producer retains, at least
CALIPER_NET_SERIES_MAX_VALUES 262,144 Values in one SERIES message
CALIPER_NET_MAX_EVENT_INDICES 32,768 Entries of an event's index list
CALIPER_NET_DEFAULT_RATE_HZ 60 Default max_rate
CALIPER_NET_LIMIT_RATE_HZ 240 Largest max_rate
CALIPER_NET_MAX_QUEUED_EVENTS 256 Unread events a producer keeps per connection
CALIPER_NET_HANDSHAKE_TIMEOUT_MS 10,000 Wait for HELLO or WELCOME
CALIPER_NET_PING_INTERVAL_MS 10,000 Silence before a PING
CALIPER_NET_IDLE_TIMEOUT_MS 30,000 Silence before closing
CALIPER_NET_MAX_PARAMS 64 Parameters of a session (3.1)
CALIPER_NET_MAX_ACTIONS 64 Actions of a session (3.1)
CALIPER_NET_MAX_CHOICES 64 Choices of a choice parameter (3.1)
CALIPER_NET_TEXT_MAX 1,024 Bytes of a help text, a reply's message or details_error (3.1)
CALIPER_NET_CONTROL_RATE_HZ 30 set requests a viewer sends per parameter per second, at most (3.1)
CALIPER_NET_ACTION_WAIT_MS 600,000 A queued action that has not started by then is answered timeout (3.1)
CALIPER_NET_MAX_ITEM_SPACES 16 Item spaces of a session (3.1)
CALIPER_NET_MAX_DETAIL_ITEMS 32 Items in a selection's details (3.1)
CALIPER_NET_MAX_DETAIL_FIELDS 16 Fields in a selection's details (3.1)
CALIPER_NET_DETAIL_TEXT_MAX 256 Bytes of a string in a selection's details (3.1)
CALIPER_NET_DETAILS_TIMEOUT_MS 2,000 The time the details function of the script has (3.1)
CALIPER_NET_REPLY_WAIT_MS 60,000 A hub answers a CONTROL timeout when the producer has not answered it by then (3.1)
CALIPER_NET_RELEASE_GRACE_MS 10,000 A paused run resumes when no connection that may control it has been open this long (3.1)
CALIPER_NET_ORDER_WAIT_MS 2,000 The longest the run state of a pause waits for the data of its step to go out (3.1)

With the defaults, a heatmap snapshot is at most 1 MiB, an image snapshot is at most 1 MiB, and a 2-D points snapshot with values is at most 1.2 MB.

Versioning

The major version is 3 and the minor version is 1. Both are in the header of every frame. These rules apply:

  • A receiver that sees another major version closes with code 4001 and sends nothing else. The close reason may say which major version it speaks.
  • Within major 3, a receiver ignores message types, metadata keys, capabilities, frame flags and sections it does not know. A minor version only adds such things and never changes the meaning of existing ones.
  • A sender uses an addition only when the peer knows it. The peer says so with its minor version, read from its HELLO or WELCOME, or with one of its capabilities. A flag that changes how a frame decodes goes out only after a capability announced it.
  • Capabilities are strings in HELLO, WELCOME and the session list. Version 3.0 defines events (a producer that reads viewer events) and reserves arrow_ipc for a future table kind. Version 3.1 defines controls and selection.
  • A viewer and a hub announce what they understand. A producer and a hub announce what they implement.

Version 3.1 adds two groups of features:

  • Controls, under the capability controls: the messages CONTROLS, CONTROL and CONTROL_REPLY, and the key control of WELCOME.
  • Linked selection, under the capability selection: the message SELECTION, the key items of a source dim and of a series view in VIEWS, and the keys space and items in the detail of a select event.

It also adds the error codes forbidden, unknown_control, bad_value, timeout and action_failed, which only CONTROL_REPLY carries. A peer uses each addition, including the new keys of 3.0 messages, only with a peer that announced its capability. A peer decides by capabilities and never by the minor version alone. Thus an implementation may report minor 1 before it implements both capabilities.

Every 3.0 message keeps its meaning and its encoding. As a result, a 3.0 peer and a 3.1 peer work together in both directions without the additions:

Viewer Server What happens
3.0 caliper.serve() of 3.1 The viewer announces neither capability, so it receives no CONTROLS or SELECTION and no items in VIEWS
3.1 caliper.serve() of 3.0 WELCOME announces neither capability, so the viewer shows no controls and sends no item ids
3.1 A 3.0 hub with a 3.1 producer The WELCOME of the hub announces neither capability, so neither the viewer nor the producer uses the additions. The session list may name the capabilities of the producer, but a viewer goes by the WELCOME of the server it is connected to

Close Codes

Code Name When
1000 CALIPER_NET_CLOSE_NORMAL After BYE normal or replaced
1001 CALIPER_NET_CLOSE_GOING_AWAY The process is exiting, after BYE shutdown
1003 CALIPER_NET_CLOSE_UNSUPPORTED_DATA A text message arrived
1009 CALIPER_NET_CLOSE_TOO_BIG Frame decoding step 5
4000 CALIPER_NET_CLOSE_PROTOCOL_ERROR A frame or message that breaks this specification
4001 CALIPER_NET_CLOSE_UNSUPPORTED_MAJOR Frame decoding step 3
4002 CALIPER_NET_CLOSE_ROLE_REFUSED The server does not take the role of the client
4003 CALIPER_NET_CLOSE_HANDSHAKE_TIMEOUT No HELLO or WELCOME in time
4004 CALIPER_NET_CLOSE_IDLE_TIMEOUT Nothing received for 30 seconds
4005 CALIPER_NET_CLOSE_SERVER_FULL The connection limit of the server

Error Codes

Code Meaning
bad_request A field is missing or has the wrong type
unknown_session ATTACH named a session the server does not hold
unknown_view A message named a view the session does not have
unknown_subscription VIEW_REQUEST named a sub that is not open
bad_data_view A data view did not resolve
not_attached A viewer of a hub subscribed, or sent CONTROL, before it was attached
unsupported A known feature the receiver does not support, such as a section encoding, a pause of a script without a checkpoint, or a CONTROL for a session whose producer lacks controls or has ended
limit A limit was reached, such as the number of subscriptions
internal The producer failed while it computed a data view
forbidden The connection may not control: a hub without --allow-control, or a server that refuses controls
unknown_control A CONTROL named a parameter or an action that the script has not registered
bad_value A set value of the wrong type, a fraction for an integer, or a choice that is not listed
timeout A queued action did not start within CALIPER_NET_ACTION_WAIT_MS
action_failed The action raised an error, and the message gives its text

Security

Network streaming stays on the local machine, on the loopback interface and with the session token. A server listens on 127.0.0.1 only. Before every upgrade, it checks the Host header, the Origin header and the session token. The token appears in a URL, so it can reach the history of a browser. It protects against other pages and other users on the machine, but not against someone who can read the files of the user. Controls need nothing more than the stream. Whoever holds the token of caliper.serve() may control, unless the script turns controls off.

The stream carries data, viewer events and, from 3.1, control requests. It never carries host commands or the command layer, and no message carries code. A CONTROL can only name a parameter or an action that the script registered, matched exactly. The producer checks and clamps every value, and an action takes no argument from the viewer. The only code that a message makes a producer run is such a registered action, at a checkpoint on the training thread. One exception exists in the 3.2 draft: PROGRAM, under the capability programs, carries the WGSL source of a GPU program. A producer that announced programs runs it only on the GPU, after the sandbox checks, with read-only bindings of its tensors, and never on the CPU (Wire protocol 3.2). A hub forwards controls only when it starts with --allow-control, and only to the producer of the session of the viewer.

Golden Frames

The folder tests/fixtures/wire_v3 holds test data that every implementation decodes in its own tests. These implementations are the Python codec, the native NetSource, the hub and the TypeScript viewer.

File Contents
<name>.bin The bytes of one frame of a 3.0 sender, with minor 0 in its header
<name>.json What a decoder must find: the header fields, section_lengths, meta_offset, the metadata object, and for each section its name, dtype, shape, offset, length and values
ping_future.bin, unknown_type.bin Frames from a later minor version, with unknown flags, keys and type, which a 3.0 decoder accepts and partly ignores
v3_1/<name>.bin, v3_1/<name>.json The frames that 3.1 adds, with minor 1: one for each new message type and op, and 3.0 messages with the keys that 3.1 adds
invalid/<name>.bin A frame every decoder rejects
invalid/expect.json For each invalid frame, the reason and the close code

The folder has one frame per message type, a few more for sections and options, and a set of frames to reject. The 3.0 frames keep minor 0, as a 3.0 sender wrote them, so an implementation that knows only 3.0 keeps passing. An implementation that encodes a decoded frame again to compare bytes uses the minor version of the frame. It reads v3_1/ after it implements 3.1. A 3.0 decoder finds those frames well formed, with the new types unknown to it. The worked example decodes one of these frames by hand.

Header Listing

/* ===========================================================================
 * caliper/live_net.h: wire protocol v3, live data streaming over a WebSocket.
 *
 * This header defines the constants and the frame header of protocol v3. The
 * reference page docs/reference/wire-protocol.md specifies every message and
 * embeds this file verbatim; tests/fixtures/wire_v3 holds the golden frames
 * that every implementation (python/caliper/_wire.py, the native NetSource,
 * the hub, the TypeScript viewer) decodes in its tests. Plain C99 and C++, no
 * platform headers, no functions to link: other languages port it by hand.
 *
 * Protocol v2 (caliper/live.h, shared memory) is unchanged. v2 stays the
 * zero-copy path between processes on one machine; v3 sends copies of reduced
 * snapshots over a socket to browsers, notebooks, the hub and the native app.
 *
 * ROLES
 *   producer  the training process (caliper.serve() in Python). It owns the
 *             tensors and computes every data view on its own device.
 *   viewer    a browser element, the Jupyter widget or the native app's
 *             NetSource. It draws what it receives and computes nothing.
 *   hub       `caliper serve`: producers connect out to it, viewers connect to
 *             it, and it forwards between them. It holds several sessions.
 *   A connection has a CLIENT (the side that opened it: a viewer, or a producer
 *   connecting to a hub) and a SERVER (caliper.serve() or the hub).
 *
 * ENDPOINT AND HANDSHAKE
 *   One WebSocket endpoint, CALIPER_NET_PATH, on 127.0.0.1 only. The session
 *   token travels as the query parameter CALIPER_NET_TOKEN_PARAM of the upgrade
 *   request. Before the upgrade the server answers HTTP 404 for another path,
 *   and HTTP 403 when the Host header is not "127.0.0.1:<port>" or
 *   "localhost:<port>", when the Origin header is not accepted (the page
 *   states the Origin rule exactly), or when the token is missing or wrong
 *   (compared in constant time). No subprotocol, no permessage-deflate. Then
 *   the client sends HELLO, the server answers WELCOME, and nothing but BYE
 *   may precede them.
 *
 * FRAMES
 *   Every WebSocket message is ONE binary frame (a text message closes the
 *   connection with CALIPER_NET_CLOSE_UNSUPPORTED_DATA). All integers are
 *   little-endian. A frame is, in this order:
 *     1. CaliperNetFrameHeader, CALIPER_NET_HEADER_BYTES (24) bytes;
 *     2. section_count uint32 section lengths in bytes;
 *     3. meta_len bytes of metadata: one UTF-8 JSON object (RFC 8259), at
 *        least 2 bytes, no byte order mark, nesting depth at most
 *        CALIPER_NET_MAX_JSON_DEPTH;
 *     4. section_count payload sections. Section i starts at the first offset
 *        that is a multiple of CALIPER_NET_SECTION_ALIGN (8), counted from the
 *        first byte of the frame, at or after the end of the part before it
 *        (the metadata for i = 0). The sender fills the gaps with zero bytes;
 *        the receiver ignores them.
 *   frame_len is the total: the end of the last section, or the end of the
 *   metadata when there is none. No bytes follow.
 *   When section_count > 0 the metadata key "sections" is an array of
 *   section_count descriptors {"name": string, "dtype": CaliperNetDType,
 *   "shape": [int...], "encoding": CaliperNetEncoding (optional, 0)}; names are
 *   unique in the frame. A RAW section holds prod(shape) * item size bytes,
 *   row-major, little-endian. "sections" is reserved in every message.
 *
 * DECODING ORDER (every implementation applies these checks in this order, so
 * all of them reject a bad frame with the same close code):
 *   1. fewer than 8 bytes                                -> PROTOCOL_ERROR
 *   2. magic != CALIPER_NET_MAGIC                        -> PROTOCOL_ERROR
 *   3. major != CALIPER_NET_PROTOCOL_MAJOR               -> UNSUPPORTED_MAJOR
 *   4. fewer than 24 bytes                               -> PROTOCOL_ERROR
 *   5. frame_len > CALIPER_NET_MAX_FRAME_BYTES, meta_len > CALIPER_NET_MAX_META_BYTES
 *      or section_count > CALIPER_NET_MAX_SECTIONS      -> TOO_BIG
 *   6. message length != frame_len                       -> PROTOCOL_ERROR
 *   7. the layout above does not end exactly at frame_len -> PROTOCOL_ERROR
 *   8. metadata not a JSON object as stated above        -> PROTOCOL_ERROR
 *   9. "sections" missing, with another count, a malformed descriptor, or a
 *      RAW section of a known dtype whose length disagrees with its shape
 *                                                        -> PROTOCOL_ERROR
 *   minor, flags, reserved and the padding bytes are never grounds to reject.
 *   A frame that passes is well formed. A well-formed frame whose message is
 *   invalid (a missing field, an unknown view, a bad data view) is answered
 *   with an ERROR message and the connection stays open.
 *
 * VERSIONING
 *   Major 3. A peer closes with CALIPER_NET_CLOSE_UNSUPPORTED_MAJOR on another
 *   major. Within major 3 a receiver ignores message types, metadata keys,
 *   capabilities, frame flags and sections it does not know, and a minor
 *   version only adds those. A sender uses an addition only when the peer's
 *   minor (the header of its HELLO or WELCOME) or one of its capabilities
 *   says the peer knows it.
 *   3.1 adds controls (capability "controls": the run state, parameters and
 *   actions a script registers, CONTROLS, CONTROL, CONTROL_REPLY and the
 *   WELCOME key "control") and linked selection (capability "selection":
 *   item spaces in VIEWS, "space" and "items" in a select event, SELECTION).
 *   Every 3.1 addition, new keys of 3.0 messages included, is used only with
 *   a peer that announced its capability; the minor alone never enables one.
 *   3.0 messages keep their meaning and encoding.
 *
 * FLOW CONTROL
 *   Nothing in the protocol lets a slow viewer block a producer. Per connection
 *   a sender keeps at most ONE undelivered snapshot per subscription (a view and
 *   its data view) and replaces it with a newer one; series values are
 *   coalesced into one pending SERIES message per subscription and never
 *   dropped; control messages go in order and first. A producer takes no
 *   snapshot of a view that no subscription names. Series are the exception
 *   that makes backfill possible: a producer retains appended values whether or
 *   not anyone subscribes, without synchronising with the device.
 *
 * CONTROLS (3.1)
 *   A script registers parameters and actions and calls a checkpoint in its
 *   training loop. A viewer sends CONTROL (pause, resume, step, set a
 *   parameter, run an action); the producer validates it, applies it on the
 *   training thread at the next checkpoint, answers the sender with
 *   CONTROL_REPLY and announces the new state to every viewer with CONTROLS.
 *   A hub forwards CONTROL to the session's producer only when it was started
 *   to allow control; WELCOME's "control" tells a viewer whether it may.
 *
 * SECURITY
 *   Loopback (127.0.0.1) and the session token only, until a dedicated security
 *   review. The stream carries data, viewer events (selections, picks, hovers
 *   that Python code can read) and, from 3.1, control requests that name a
 *   parameter or an action the script registered. It never carries host
 *   commands or the command layer. No message carries code, and the only code
 *   a message makes a producer run is an action the script registered, by its
 *   exact name and with no argument from the viewer. One exception: PROGRAM
 *   (3.2 draft, capability "programs") carries the WGSL source of a GPU
 *   program. A producer that announced "programs" runs it only on the GPU,
 *   after the sandbox checks, with read-only bindings of its tensors, and
 *   never on the CPU.
 * ===========================================================================*/
#ifndef CALIPER_LIVE_NET_H
#define CALIPER_LIVE_NET_H

#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

/* --- version, endpoint ---------------------------------------------------- */
#define CALIPER_NET_PROTOCOL_MAJOR 3u
#define CALIPER_NET_PROTOCOL_MINOR 1u
#define CALIPER_NET_MAGIC          0x574c4143u   /* bytes "CALW" as a little-endian uint32 */
#define CALIPER_NET_PATH           "/v3/stream"
#define CALIPER_NET_TOKEN_PARAM    "token"

/* --- frame layout ---------------------------------------------------------- */
#define CALIPER_NET_HEADER_BYTES   24u
#define CALIPER_NET_SECTION_ALIGN  8u

/* The fixed header at offset 0 of every frame. The struct matches the bytes on
 * a little-endian machine; read it with memcpy (a received buffer need not be
 * aligned). It is followed by uint32_t section_len[section_count]. */
typedef struct CaliperNetFrameHeader {
    uint32_t magic;          /* offset  0: CALIPER_NET_MAGIC                        */
    uint16_t major;          /* offset  4: CALIPER_NET_PROTOCOL_MAJOR               */
    uint16_t minor;          /* offset  6: the sender's minor version               */
    uint16_t type;           /* offset  8: CaliperNetMessageType                    */
    uint16_t flags;          /* offset 10: CALIPER_NET_FRAME_*; none in 3.1, so 0   */
    uint32_t frame_len;      /* offset 12: bytes of the whole frame, header included */
    uint32_t meta_len;       /* offset 16: bytes of the JSON metadata               */
    uint16_t section_count;  /* offset 20: 0..CALIPER_NET_MAX_SECTIONS              */
    uint16_t reserved;       /* offset 22: 0 when sent, ignored when received       */
} CaliperNetFrameHeader;

/* C99 has no static_assert: an array of negative size fails the build. */
typedef char caliper_net_header_size_check[sizeof(CaliperNetFrameHeader) == 24 ? 1 : -1];

/* Frame flags: none are defined in 3.1. A receiver ignores bits it does not
 * know; a flag that changes how a frame decodes is only sent to a peer that
 * announced the capability for it. */
#define CALIPER_NET_FRAME_FLAGS_NONE 0x0000u

/* --- message types (header.type) -------------------------------------------
 * Direction: C = the client of the connection, S = its server; V viewer,
 * P producer, H hub. Type 0 is never sent. */
typedef enum CaliperNetMessageType {
    CALIPER_NET_MSG_HELLO        = 0x0001,  /* C -> S   role, capabilities, producer identity */
    CALIPER_NET_MSG_WELCOME      = 0x0002,  /* S -> C   role, capabilities, session identity  */
    CALIPER_NET_MSG_BYE          = 0x0003,  /* either   reason, then the sender closes        */
    CALIPER_NET_MSG_ERROR        = 0x0004,  /* either   code, message, fatal                  */
    CALIPER_NET_MSG_PING         = 0x0005,  /* either   nonce                                 */
    CALIPER_NET_MSG_PONG         = 0x0006,  /* either   the ping's nonce                      */
    CALIPER_NET_MSG_SESSIONS     = 0x0010,  /* S -> V   the sessions the server holds         */
    CALIPER_NET_MSG_ATTACH       = 0x0011,  /* V -> S   pick a session (null detaches)        */
    CALIPER_NET_MSG_VIEWS        = 0x0020,  /* P -> V/H, H -> V   every view, resent on change */
    CALIPER_NET_MSG_SUBSCRIBE    = 0x0021,  /* V -> P/H, H -> P   sub, view, rate, data view   */
    CALIPER_NET_MSG_UNSUBSCRIBE  = 0x0022,  /* V -> P/H, H -> P   sub                          */
    CALIPER_NET_MSG_VIEW_REQUEST = 0x0023,  /* V -> P/H, H -> P   a new data view for a sub    */
    CALIPER_NET_MSG_VIEW_REPLY   = 0x0024,  /* P -> V/H, H -> V   resolved data view or error  */
    CALIPER_NET_MSG_SNAPSHOT     = 0x0030,  /* P -> V/H, H -> V   sections "data" (+ "aux")    */
    CALIPER_NET_MSG_SERIES       = 0x0031,  /* P -> V/H, H -> V   section "values"             */
    CALIPER_NET_MSG_EVENT        = 0x0040,  /* V -> P/H, H -> P   select, pick, hover          */
    /* 3.1, capability "controls" */
    CALIPER_NET_MSG_CONTROLS     = 0x0050,  /* P -> V/H, H -> V   run state, parameters, actions */
    CALIPER_NET_MSG_CONTROL      = 0x0051,  /* V -> P/H, H -> P   pause, resume, step, set, run */
    CALIPER_NET_MSG_CONTROL_REPLY = 0x0052, /* P -> V/H, H -> V   the answer to one CONTROL     */
    /* 3.1, capability "selection" */
    CALIPER_NET_MSG_SELECTION    = 0x0060   /* P -> V/H, H -> V   the selection of an item space */
} CaliperNetMessageType;

/* --- live view kinds (values of CaliperLiveKind, caliper/live.h) ----------- */
typedef enum CaliperNetKind {
    CALIPER_NET_KIND_HEATMAP   = 1,   /* data (H,W) f32                                  */
    CALIPER_NET_KIND_IMAGE     = 2,   /* data (H,W,4) u8 RGBA                            */
    CALIPER_NET_KIND_POINTS    = 3,   /* data (N,D) f32, D = points_dims 2|3; aux (N,) f32 */
    CALIPER_NET_KIND_SERIES    = 4,   /* SERIES messages only: values (M,) f32           */
    CALIPER_NET_KIND_HISTOGRAM = 5    /* data (B,) f32 bin counts over [stats0, stats1]  */
} CaliperNetKind;

/* --- section dtypes (values of CaliperDType, caliper/tensor.h) -------------
 * 3.1 producers send F32 and U8 payloads only (I64 nowhere yet); the rest are
 * defined so that a receiver can name what it refuses. */
typedef enum CaliperNetDType {
    CALIPER_NET_DTYPE_F32  = 0,   /* 4 bytes */
    CALIPER_NET_DTYPE_F16  = 1,   /* 2 bytes */
    CALIPER_NET_DTYPE_BF16 = 2,   /* 2 bytes */
    CALIPER_NET_DTYPE_I64  = 3,   /* 8 bytes */
    CALIPER_NET_DTYPE_I32  = 4,   /* 4 bytes */
    CALIPER_NET_DTYPE_U8   = 5    /* 1 byte  */
} CaliperNetDType;

/* --- section encodings ---------------------------------------------------- */
typedef enum CaliperNetEncoding {
    CALIPER_NET_ENCODING_RAW       = 0,   /* a raw little-endian array (dtype, shape)  */
    CALIPER_NET_ENCODING_ARROW_IPC = 1    /* reserved for a future table kind; unused  */
} CaliperNetEncoding;

/* --- colormaps (ids 0..15 of the shared colormap table) ------------------- */
#define CALIPER_NET_COLORMAP_VIRIDIS  0
#define CALIPER_NET_COLORMAP_MAGMA    1
#define CALIPER_NET_COLORMAP_RDBU     2
#define CALIPER_NET_COLORMAP_GREYS    3
#define CALIPER_NET_COLORMAP_CIVIDIS  4
#define CALIPER_NET_COLORMAP_INFERNO  5
#define CALIPER_NET_COLORMAP_PLASMA   6
#define CALIPER_NET_COLORMAP_TURBO    7
#define CALIPER_NET_COLORMAP_TWILIGHT 8
#define CALIPER_NET_COLORMAP_COOLWARM 9
#define CALIPER_NET_COLORMAP_BATLOW   10
#define CALIPER_NET_COLORMAP_VIK      11
#define CALIPER_NET_COLORMAP_ROMA     12
#define CALIPER_NET_COLORMAP_OSLO     13
#define CALIPER_NET_COLORMAP_LAJOLLA  14
#define CALIPER_NET_COLORMAP_BERLIN   15

/* --- snapshot flags (metadata "flags"; values of CALIPER_LIVE_SNAP_*) ------ */
#define CALIPER_NET_SNAP_AUTO_VMIN 0x1u   /* vmin = stats[0], else the fixed "vmin" */
#define CALIPER_NET_SNAP_AUTO_VMAX 0x2u   /* vmax = stats[1], else the fixed "vmax" */
#define CALIPER_NET_SNAP_RAW       0x4u   /* heatmap/image drawn 1:1 (tests)         */

/* --- WebSocket close codes -------------------------------------------------- */
#define CALIPER_NET_CLOSE_NORMAL            1000  /* after a BYE                              */
#define CALIPER_NET_CLOSE_GOING_AWAY        1001  /* the process is exiting                   */
#define CALIPER_NET_CLOSE_UNSUPPORTED_DATA  1003  /* a text message                           */
#define CALIPER_NET_CLOSE_TOO_BIG           1009  /* decoding step 5                          */
#define CALIPER_NET_CLOSE_PROTOCOL_ERROR    4000  /* a frame or message breaking this spec    */
#define CALIPER_NET_CLOSE_UNSUPPORTED_MAJOR 4001  /* decoding step 3                          */
#define CALIPER_NET_CLOSE_ROLE_REFUSED      4002  /* the server does not take this role       */
#define CALIPER_NET_CLOSE_HANDSHAKE_TIMEOUT 4003  /* no HELLO (or WELCOME) in time            */
#define CALIPER_NET_CLOSE_IDLE_TIMEOUT      4004  /* nothing received for IDLE_TIMEOUT_MS     */
#define CALIPER_NET_CLOSE_SERVER_FULL       4005  /* the server's connection limit            */

/* --- strings used as JSON values ------------------------------------------ */
#define CALIPER_NET_ROLE_VIEWER   "viewer"
#define CALIPER_NET_ROLE_PRODUCER "producer"
#define CALIPER_NET_ROLE_HUB      "hub"

#define CALIPER_NET_SESSION_LIVE  "live"
#define CALIPER_NET_SESSION_ENDED "ended"

#define CALIPER_NET_BYE_NORMAL    "normal"     /* done; closes with 1000                */
#define CALIPER_NET_BYE_SHUTDOWN  "shutdown"   /* the process is exiting; 1001          */
#define CALIPER_NET_BYE_REPLACED  "replaced"   /* the producer reconnected to the hub   */
#define CALIPER_NET_BYE_ERROR     "error"      /* after a fatal ERROR                   */

#define CALIPER_NET_ERR_BAD_REQUEST          "bad_request"           /* missing or invalid field */
#define CALIPER_NET_ERR_UNKNOWN_SESSION      "unknown_session"
#define CALIPER_NET_ERR_UNKNOWN_VIEW         "unknown_view"
#define CALIPER_NET_ERR_UNKNOWN_SUBSCRIPTION "unknown_subscription"
#define CALIPER_NET_ERR_BAD_DATA_VIEW        "bad_data_view"
#define CALIPER_NET_ERR_NOT_ATTACHED         "not_attached"
#define CALIPER_NET_ERR_UNSUPPORTED          "unsupported"
#define CALIPER_NET_ERR_LIMIT                "limit"
#define CALIPER_NET_ERR_INTERNAL             "internal"
/* 3.1: CONTROL_REPLY errors */
#define CALIPER_NET_ERR_FORBIDDEN            "forbidden"       /* this connection may not control */
#define CALIPER_NET_ERR_UNKNOWN_CONTROL      "unknown_control" /* no parameter or action of that name */
#define CALIPER_NET_ERR_BAD_VALUE            "bad_value"       /* a value of the wrong type or choice */
#define CALIPER_NET_ERR_TIMEOUT              "timeout"         /* a queued action did not start in time */
#define CALIPER_NET_ERR_ACTION_FAILED        "action_failed"   /* the action raised; message says why */

#define CALIPER_NET_EVENT_SELECT "select"
#define CALIPER_NET_EVENT_PICK   "pick"
#define CALIPER_NET_EVENT_HOVER  "hover"

#define CALIPER_NET_REDUCE_MEAN "mean"
#define CALIPER_NET_REDUCE_SUM  "sum"
#define CALIPER_NET_REDUCE_MAX  "max"
#define CALIPER_NET_REDUCE_MIN  "min"
#define CALIPER_NET_REDUCE_NORM "norm"         /* sqrt(sum(x * x)) */

#define CALIPER_NET_POOL_MEAN "mean"           /* budget "pool": heatmaps; images always mean */
#define CALIPER_NET_POOL_MAX  "max"
#define CALIPER_NET_POOL_MIN  "min"

#define CALIPER_NET_CAP_EVENTS    "events"     /* producer: Python code reads viewer events */
#define CALIPER_NET_CAP_ARROW_IPC "arrow_ipc"  /* reserved                                  */
#define CALIPER_NET_CAP_CONTROLS  "controls"   /* 3.1: run state, parameters and actions    */
#define CALIPER_NET_CAP_SELECTION "selection"  /* 3.1: item spaces and linked selection     */

/* 3.1: CONTROLS "run.state" */
#define CALIPER_NET_RUN_NONE      "none"       /* the script has called no checkpoint yet   */
#define CALIPER_NET_RUN_RUNNING   "running"
#define CALIPER_NET_RUN_PAUSING   "pausing"    /* a pause waits for the next checkpoint     */
#define CALIPER_NET_RUN_PAUSED    "paused"     /* the training thread waits in a checkpoint */
#define CALIPER_NET_RUN_STEPPING  "stepping"   /* runs steps_left steps, then pauses        */

/* 3.1: CONTROL "op" */
#define CALIPER_NET_OP_PAUSE      "pause"
#define CALIPER_NET_OP_RESUME     "resume"
#define CALIPER_NET_OP_STEP       "step"       /* "count" steps (default 1), then pause     */
#define CALIPER_NET_OP_SET        "set"        /* parameter "name" to "value"               */
#define CALIPER_NET_OP_RUN        "run"        /* action "name", no argument                */

/* 3.1: parameter "type" */
#define CALIPER_NET_PARAM_NUMBER  "number"     /* finite, clamped to [min, max]             */
#define CALIPER_NET_PARAM_INTEGER "integer"    /* whole, clamped to [min, max]              */
#define CALIPER_NET_PARAM_BOOLEAN "boolean"
#define CALIPER_NET_PARAM_CHOICE  "choice"     /* one of "choices"                          */

/* 3.1: CONTROL_REPLY "status" */
#define CALIPER_NET_STATUS_OK     "ok"         /* done (an action: finished without error)  */
#define CALIPER_NET_STATUS_QUEUED "queued"     /* an action waits; one more reply follows   */
#define CALIPER_NET_STATUS_ERROR  "error"      /* refused or failed: see "error"            */

/* --- limits and defaults ---------------------------------------------------- */
#define CALIPER_NET_MAX_FRAME_BYTES     (64u << 20)  /* any frame                          */
#define CALIPER_NET_MAX_META_BYTES      (1u << 20)   /* the metadata of any frame          */
#define CALIPER_NET_MAX_SECTIONS        8u
#define CALIPER_NET_MAX_JSON_DEPTH      16u          /* nested objects and arrays          */
#define CALIPER_NET_NAME_MAX            63u          /* bytes: view, source, dim names     */
#define CALIPER_NET_SESSION_ID_MAX      63u          /* bytes of [A-Za-z0-9._-]            */
#define CALIPER_NET_TOKEN_MIN           22u          /* chars of [A-Za-z0-9_-], >= 128 bits */
#define CALIPER_NET_TOKEN_MAX           128u
#define CALIPER_NET_MAX_VIEWS           256u         /* per session                        */
#define CALIPER_NET_MAX_SOURCES         64u          /* per view                           */
#define CALIPER_NET_MAX_DIMS            8u           /* per source                         */
#define CALIPER_NET_MAX_SUBSCRIPTIONS   256u         /* per connection                     */
#define CALIPER_NET_MAX_STATS           64u          /* "stats" entries of a snapshot      */
#define CALIPER_NET_DEFAULT_MAX_EXTENT  512u         /* heatmap/image rows and columns     */
#define CALIPER_NET_LIMIT_MAX_EXTENT    4096u
#define CALIPER_NET_DEFAULT_MAX_POINTS  100000u
#define CALIPER_NET_LIMIT_MAX_POINTS    1000000u
#define CALIPER_NET_MAX_BINS            65536u
#define CALIPER_NET_SERIES_RETAIN_MIN   4096u        /* values a producer retains, at least */
#define CALIPER_NET_SERIES_MAX_VALUES   262144u      /* values in one SERIES message       */
#define CALIPER_NET_MAX_EVENT_INDICES   32768u       /* entries of an event index list     */
#define CALIPER_NET_DEFAULT_RATE_HZ     60u          /* subscribe "max_rate" default       */
#define CALIPER_NET_LIMIT_RATE_HZ       240u         /* larger rates are clamped           */
#define CALIPER_NET_MAX_QUEUED_EVENTS   256u         /* a producer drops the oldest beyond */
#define CALIPER_NET_HANDSHAKE_TIMEOUT_MS 10000u
#define CALIPER_NET_PING_INTERVAL_MS    10000u       /* ping after this long without input */
#define CALIPER_NET_IDLE_TIMEOUT_MS     30000u       /* close after this long without input */
/* 3.1 */
#define CALIPER_NET_MAX_PARAMS          64u          /* parameters of a session            */
#define CALIPER_NET_MAX_ACTIONS         64u          /* actions of a session               */
#define CALIPER_NET_MAX_CHOICES         64u          /* choices of a choice parameter      */
#define CALIPER_NET_TEXT_MAX            1024u        /* bytes: help texts, reply messages  */
#define CALIPER_NET_PARAM_TEXT_MAX      4096u        /* UTF-8 bytes: free-text values      */
#define CALIPER_NET_CONTROL_RATE_HZ     30u          /* "set" requests per parameter, at most */
#define CALIPER_NET_ACTION_WAIT_MS      600000u      /* a queued action not started by then fails */
#define CALIPER_NET_MAX_ITEM_SPACES     16u          /* item spaces of a session           */
#define CALIPER_NET_MAX_DETAIL_ITEMS    32u          /* items in a selection's details     */
#define CALIPER_NET_MAX_DETAIL_FIELDS   16u          /* fields in a selection's details    */
#define CALIPER_NET_DETAIL_TEXT_MAX     256u         /* bytes of a string in the details   */
#define CALIPER_NET_DETAILS_TIMEOUT_MS  2000u        /* the script's details callback, at most */
#define CALIPER_NET_REPLY_WAIT_MS       60000u       /* a hub answers an unanswered CONTROL timeout */
#define CALIPER_NET_RELEASE_GRACE_MS    10000u       /* a paused run with no controller resumes */
#define CALIPER_NET_ORDER_WAIT_MS       2000u        /* a run state waits this long for data, at most */

/* Item size in bytes of a CaliperNetDType, 0 for an unknown one. */
static inline uint32_t caliper_net_dtype_size(int32_t dtype) {
    switch (dtype) {
        case CALIPER_NET_DTYPE_F32:  return 4u;
        case CALIPER_NET_DTYPE_F16:  return 2u;
        case CALIPER_NET_DTYPE_BF16: return 2u;
        case CALIPER_NET_DTYPE_I64:  return 8u;
        case CALIPER_NET_DTYPE_I32:  return 4u;
        case CALIPER_NET_DTYPE_U8:   return 1u;
        case 6:                      return 2u;   /* CALIPER_NET_DTYPE_U16, the 3.2 block */
        default:                     return 0u;
    }
}

/* n rounded up to CALIPER_NET_SECTION_ALIGN (n <= CALIPER_NET_MAX_FRAME_BYTES). */
static inline uint32_t caliper_net_align(uint32_t n) {
    return (n + (CALIPER_NET_SECTION_ALIGN - 1u)) & ~(CALIPER_NET_SECTION_ALIGN - 1u);
}

/* ===========================================================================
 * PROTOCOL 3.2 DRAFT: THE SCENES SUBSET
 *
 * Provisional proposals: the minor version 2, the
 * capabilities, the kinds scene (6), text (7) and bars (8), the messages
 * 0x0070 to 0x0074, 0x0078 to 0x007B and 0x0080 to 0x0082, the dtype u16 (6), the error codes
 * and the limits below. Each is one line here, so
 * a renumbering changes one line (the dtype also has one line in
 * caliper_net_dtype_size above). docs/reference/wire-protocol-3-2.md states
 * what is built of them: the codecs build the whole draft (every part of
 * SCENE_UPDATE, split updates, SCENE_TRANSFORMS, SCENE_CLEAR, SCENE_CLASSES,
 * stamps, TEXT, bar charts and the recording messages); producers, the hub
 * and viewers build the scenes. Senders keep writing
 * CALIPER_NET_PROTOCOL_MINOR in their frames until 3.2 is confirmed; the 3.2
 * golden frames (tests/fixtures/wire_v3/v3_2) carry CALIPER_NET_MINOR_3_2. A
 * peer decides by the capability, never the minor. Every 3.1 message keeps
 * its meaning and encoding. CaliperDType (sdk/include/caliper/tensor.h) has
 * no CALIPER_DT_U16 yet: the SDK owns it, so the draft depends on a later SDK addition.
 * ===========================================================================*/
#define CALIPER_NET_MINOR_3_2            2u
#define CALIPER_NET_CAP_SCENES           "scenes"     /* kind scene and its messages      */
#define CALIPER_NET_KIND_SCENE           6            /* the scene messages                */
#define CALIPER_NET_MSG_SCENE_UPDATE     0x0070u      /* P -> V/H, H -> V   one part of one entity */
#define CALIPER_NET_MSG_SCENE_TRANSFORMS 0x0071u      /* P -> V/H, H -> V   entity transforms    */
#define CALIPER_NET_MSG_SCENE_CLEAR      0x0072u      /* P -> V/H, H -> V   clear an entity or reset */
#define CALIPER_NET_MSG_SCENE_CLASSES    0x0073u      /* P -> V/H, H -> V   the class table      */
#define CALIPER_NET_DTYPE_U16            6            /* 2 bytes: depth and segmentation images */

/* SCENE_UPDATE "part" */
#define CALIPER_NET_PART_POINTS          "points"
#define CALIPER_NET_PART_LINES           "lines"
#define CALIPER_NET_PART_ARROWS          "arrows"
#define CALIPER_NET_PART_BOXES           "boxes"
#define CALIPER_NET_PART_MESH            "mesh"
#define CALIPER_NET_PART_IMAGE           "image"
#define CALIPER_NET_PART_CAMERA          "camera"
/* lines "mode" and "color_per" */
#define CALIPER_NET_LINES_SEGMENTS       "segments"   /* vertices 2k and 2k + 1: segment k  */
#define CALIPER_NET_LINES_STRIPS         "strips"     /* section strip_lengths i32 (S,)     */
#define CALIPER_NET_LINES_EDGES          "edges"      /* section indices i32 (E, 2)         */
#define CALIPER_NET_COLOR_PER_VERTEX     "vertex"
#define CALIPER_NET_COLOR_PER_STRIP      "strip"
#define CALIPER_NET_COLOR_PER_EDGE       "edge"
/* image "form" and "format" */
#define CALIPER_NET_FORM_COLOR           "color"        /* pixels u8 (H, W, 4) or (H, W)  */
#define CALIPER_NET_FORM_DEPTH           "depth"        /* pixels f32 or u16 (H, W)       */
#define CALIPER_NET_FORM_SEGMENTATION    "segmentation" /* pixels u16 or u8 (H, W)        */
#define CALIPER_NET_FORM_ENCODED         "encoded"      /* pixels u8 (B,): one JPEG or PNG */
#define CALIPER_NET_FORMAT_JPEG          "jpeg"
#define CALIPER_NET_FORMAT_PNG           "png"
/* camera "convention" */
#define CALIPER_NET_CONVENTION_RDF       "rdf"        /* x right, y down, z forward         */
#define CALIPER_NET_CONVENTION_RUB       "rub"        /* x right, y up, looking along -z    */
/* SCENE_CLEAR "parts" also takes "transform" */
#define CALIPER_NET_PART_TRANSFORM       "transform"
/* VIEWS "scene" {"space", "up", "units", "view_camera"} */
#define CALIPER_NET_SPACE_3D             "3d"
#define CALIPER_NET_SPACE_2D             "2d"

#define CALIPER_NET_PATH_MAX             255u         /* bytes of an entity path            */
#define CALIPER_NET_PATH_SEGMENTS_MAX    16u          /* segments of an entity path         */
#define CALIPER_NET_UNITS_MAX            15u          /* bytes of a scene's unit note       */
#define CALIPER_NET_MAX_ENTITIES         1024u        /* entities of a scene                */
#define CALIPER_NET_MAX_BATCHES          2048u        /* batches of a scene                 */
#define CALIPER_NET_MAX_BATCH_POINTS     4194304u     /* points, line or mesh vertices, edges, triangles */
#define CALIPER_NET_MAX_BATCH_ITEMS      1048576u     /* boxes, arrows or line strips of a batch */
#define CALIPER_NET_MAX_LABELS           4096u        /* labels of a batch, of NAME_MAX bytes */
#define CALIPER_NET_MAX_CLASSES          4096u        /* entries of a class table           */
#define CALIPER_NET_MAX_CLASS_ID         65535u       /* the largest class id               */
#define CALIPER_NET_MAX_IMAGE_SIDE       8192u        /* pixels on a side of an image or texture */
#define CALIPER_NET_MAX_ENCODED_BYTES    16777216u    /* bytes of an encoded frame          */
#define CALIPER_NET_MAX_TRANSFORMS       1024u        /* entity transforms in one SCENE_TRANSFORMS */
#define CALIPER_NET_MAX_TIMELINES        8u           /* timelines of a session: values of a stamp */
#define CALIPER_NET_STAMP_ABSENT         (-0x7fffffffffffffffLL - 1)   /* a stamp value: absent on that timeline */
/* the scene data view's budget: default and largest value of each key
 * (max_points shares CALIPER_NET_DEFAULT_MAX_POINTS and _LIMIT_MAX_POINTS) */
#define CALIPER_NET_SCENE_DEFAULT_MAX_ITEMS    100000u
#define CALIPER_NET_SCENE_LIMIT_MAX_ITEMS      1048576u
#define CALIPER_NET_SCENE_DEFAULT_MAX_VERTICES 1000000u
#define CALIPER_NET_SCENE_LIMIT_MAX_VERTICES   4194304u
#define CALIPER_NET_SCENE_DEFAULT_MAX_EXTENT   1024u
#define CALIPER_NET_SCENE_LIMIT_MAX_EXTENT     8192u

/* --- timelines, text logs, bar charts, split updates and recordings -------- */
#define CALIPER_NET_CAP_TIMELINES        "timelines"  /* VIEWS "timelines", stamps, "static"  */
#define CALIPER_NET_CAP_TEXT             "text"       /* kind text and TEXT                 */
#define CALIPER_NET_CAP_BARS             "bars"       /* kind bars, a SNAPSHOT of kind 8    */
#define CALIPER_NET_CAP_RECORDINGS       "recordings" /* the recording messages and keys    */
#define CALIPER_NET_KIND_TEXT            7            /* TEXT messages only                 */
#define CALIPER_NET_KIND_BARS            8            /* data (B,) f32, colors (B, 4) u8    */
#define CALIPER_NET_MSG_TEXT             0x0074u      /* P -> V/H, H -> V   new lines of a text log */
#define CALIPER_NET_MSG_RECORDINGS       0x0078u      /* S -> V   the recordings a server can open */
#define CALIPER_NET_MSG_RECORDING_OPEN   0x0079u      /* V -> S   open one; answered by VIEWS or ERROR */
#define CALIPER_NET_MSG_CURSOR           0x007Au      /* V -> S   move the viewer's cursor      */
#define CALIPER_NET_MSG_TIME_STATE       0x007Bu      /* S -> V   the state of the cursor       */
#define CALIPER_NET_ERR_UNKNOWN_RECORDING "unknown_recording"
#define CALIPER_NET_ERR_BAD_RECORDING    "bad_recording"
#define CALIPER_NET_ERR_NOT_SEEKABLE     "not_seekable"
#define CALIPER_NET_ERR_BAD_CURSOR       "bad_cursor"
#define CALIPER_NET_TIMELINE_SEQUENCE    "sequence"
#define CALIPER_NET_TIMELINE_TIME        "time"
#define CALIPER_NET_LEVEL_DEBUG          0            /* TEXT "levels"                      */
#define CALIPER_NET_LEVEL_INFO           1
#define CALIPER_NET_LEVEL_WARNING        2
#define CALIPER_NET_LEVEL_ERROR          3
#define CALIPER_NET_CURSOR_SEEK          "seek"       /* CURSOR "op"                        */
#define CALIPER_NET_CURSOR_PLAY          "play"
#define CALIPER_NET_CURSOR_PAUSE         "pause"
#define CALIPER_NET_CURSOR_STEP          "step"
#define CALIPER_NET_CURSOR_FOLLOW        "follow"
#define CALIPER_NET_PACE_RECORDED        "recorded"
#define CALIPER_NET_PACE_EVEN            "even"
#define CALIPER_NET_TIME_FOLLOWING       "following"  /* TIME_STATE "state"                 */
#define CALIPER_NET_TIME_PAUSED          "paused"
#define CALIPER_NET_TIME_PLAYING         "playing"
#define CALIPER_NET_STAMP_TEXT_MAX       20u          /* characters of stamp text           */
#define CALIPER_NET_MAX_PIECES           16u          /* pieces of a split update           */
#define CALIPER_NET_MAX_UPDATE_BYTES     536870912u   /* bytes of a whole split update      */
#define CALIPER_NET_MAX_INCOMPLETE_UPDATES 2u         /* split updates a receiver holds per subscription */
#define CALIPER_NET_TEXT_LINE_MAX        4096u        /* bytes of a text line               */
#define CALIPER_NET_TEXT_MAX_LINES       65536u       /* lines in one TEXT                  */
#define CALIPER_NET_TEXT_RETAIN_MIN      4096u        /* lines a producer retains per text log */
#define CALIPER_NET_MAX_BARS             4096u        /* bars of a snapshot                 */
#define CALIPER_NET_MAX_RECORDINGS       1024u        /* recordings in RECORDINGS           */
#define CALIPER_NET_MAX_RECORDED         64u          /* VIEWS "recorded": data views of a view */
#define CALIPER_NET_MAX_CURSOR_STEP      1000u        /* CURSOR "count", either way         */
#define CALIPER_NET_CURSOR_RATE_HZ       30u          /* CURSOR messages a viewer sends a second */
#define CALIPER_NET_TIME_STATE_RATE_HZ   30u          /* TIME_STATE a server sends a second while playing */
#define CALIPER_NET_MIN_SPEED            0.0625       /* playback speed, 1/16                */
#define CALIPER_NET_MAX_SPEED            64u
#define CALIPER_NET_RECORDING_LINGER_MS  600000u      /* an opened recording without viewers stays listed */

/* --- GPU programs and interventions (design/gpu-programs.md) ---------------
 * "programs": a viewer sends PROGRAM (set or remove one program) only to a
 * producer that announced it, and the producer answers each PROGRAM and
 * reports state changes with PROGRAM_STATE. "interventions": the producer's
 * CONTROLS holds the key "interventions" ({points, active}), and a viewer
 * sends CONTROL with op "intervene" and the key "intervene" ({set: {point,
 * op, from_step, until_step}}, {clear: point} or {clear_all: true}). A
 * producer announces both only under caliper.watch(model, optimizer), beside
 * "controls". A hub forwards PROGRAM, as CONTROL, only when it allows control.
 * A PROGRAM with op "params" carries the keys request_id, op, id and "params"
 * (a JSON object of 1 to 16 names, each a WGSL field name of 1 to 63 bytes,
 * [A-Za-z_][A-Za-z0-9_]*, not starting with "__", each value a finite
 * number), no "declaration" and no section. The producer applies the values
 * at the next pass of that program and answers with PROGRAM_STATE carrying
 * the request_id. A "set" declaration may also hold "params" of the same form,
 * an output {"kind": "tensor", "shape": [..]} (1 to 4 integers, each at least
 * 1, product at most 1,000,000; no other output keys for that kind) and
 * "builtin" (a string of 1 to 32 bytes such as "surface@1", for a program the
 * app makes for an operator). A program id is either 1 to 55 bytes of
 * [a-z0-9_-] starting with a letter, or the operator form
 * "operator.<id>.<n>": the prefix "operator.", a spec id (a lower-case letter
 * then lower-case letters, digits, _ or -), a dot and 1 to 5 decimal digits,
 * the whole at most 55 bytes.
 * docs/reference/wire-protocol-3-2.md states the keys and checks. */
#define CALIPER_NET_CAP_PROGRAMS         "programs"      /* PROGRAM and PROGRAM_STATE        */
#define CALIPER_NET_CAP_INTERVENTIONS    "interventions" /* CONTROLS "interventions", CONTROL op "intervene" */
#define CALIPER_NET_MSG_PROGRAM          0x0080u      /* V -> P/H, H -> P   set or remove one program; section "wgsl" */
#define CALIPER_NET_MSG_PROGRAM_STATE    0x0081u      /* P -> V/H, H -> V   the status items of the programs */
#define CALIPER_NET_PROGRAM_SET          "set"        /* PROGRAM "op": install or replace   */
#define CALIPER_NET_PROGRAM_REMOVE       "remove"     /* PROGRAM "op"                       */
#define CALIPER_NET_PROGRAM_PARAMS       "params"     /* PROGRAM "op": new parameter values, no source */
#define CALIPER_NET_SECTION_WGSL         "wgsl"       /* u8 (B,): the WGSL source as UTF-8  */
#define CALIPER_NET_OP_INTERVENE         "intervene"  /* CONTROL "op", key "intervene"      */
#define CALIPER_NET_INTERVENE_ABLATE     "ablate"     /* legacy zero-output intervention */
#define CALIPER_NET_MAX_PROGRAMS         16u          /* programs of a producer; items of PROGRAM_STATE */
#define CALIPER_NET_PROGRAM_ID_MAX       55u          /* bytes of an id, [a-z0-9_-] from a letter, or operator.<id>.<n>: "program.<id>" fits NAME_MAX */
#define CALIPER_NET_PROGRAM_SOURCE_MAX   65536u       /* bytes of the section "wgsl"        */
#define CALIPER_NET_PROGRAM_STATE_RATE_HZ 2u          /* unrequested PROGRAM_STATE a second, at most */
#define CALIPER_NET_POINT_MAX            255u         /* bytes of an intervention point     */
#define CALIPER_NET_MAX_POINTS_LISTED    4096u        /* CONTROLS "interventions" "points"  */
#define CALIPER_NET_MAX_INTERVENTIONS    64u          /* CONTROLS "interventions" "active"  */
/* the PROGRAM set declaration's bounds: each count that sizes a buffer */
#define CALIPER_NET_PROGRAM_MAX_INPUTS   8u           /* "inputs"                           */
#define CALIPER_NET_PROGRAM_MAX_COUNT    1000000u     /* geometry "max_count"               */
#define CALIPER_NET_PROGRAM_SIDE_MAX     2048u        /* pixels "width" and "height"        */
#define CALIPER_NET_PROGRAM_TIME_MS_MAX  50u          /* "limits" "time_ms"                 */
#define CALIPER_NET_PROGRAM_MEMORY_MB_MAX 512u        /* "limits" "memory_mb"               */
#define CALIPER_NET_PROGRAM_EVERY_MAX    10000u       /* "limits" "every", and the cadence N */
#define CALIPER_NET_PROGRAM_WORKGROUPS_MAX 65535u     /* each of the three "workgroups"     */
/* 3.2 draft, programmable graphics (design/programmable-graphics.md, Resource Message).
 * "resources": a producer under caliper.watch(model, optimizer) announces it
 * beside "programs", a viewer that draws pieces announces it in HELLO, and a
 * hub announces it to both. A PROGRAM "set" of an instance of a piece holds
 * "piece" ({name, version, hash}) and "manifest" (a JSON object), and may
 * hold "bind", "params", "panel" and "segments", in place of "declaration",
 * "scene" and "sha256"; it carries the sections "common", "derive" and
 * "draw" (each UTF-8, with the imports resolved) in place of "wgsl", with
 * "derive" or "draw" or both. A viewer sends one only to a producer that
 * announced "resources". After a derive pass, the producer sends each
 * published resource that the pass wrote as one RESOURCE with the section
 * "resource" to each viewer or hub that announced "resources". It sends the
 * draw half of each installed program to such a peer as PROGRAM "set" with
 * request_id 0 and all three sections, when the peer attaches and after each
 * install (the pin's hash covers derive.wgsl, thus a viewer checks it only
 * with that section), and PROGRAM "remove" with request_id 0 after a
 * removal. RESOURCE holds "program", "resource", "step" (the run's step at
 * the pass), "count" (valid elements), "stride" (bytes of one element, a
 * multiple of 4) and, for a texture, "width" and "height" (1 to
 * CALIPER_NET_RESOURCE_SIDE_MAX each) with an optional "format" (rgba8unorm when
 * absent), stride 4 and count the 32-bit words of its texels in row order:
 * width * height for rgba8unorm and r32uint, (width * height + 3) / 4 for
 * r8unorm. A buffer of narrow elements holds "element" ("u8" or "u16") and
 * "elements", its exact element count, with stride 4 and count the words,
 * (elements * bytes + 3) / 4. Its section holds exactly count * stride bytes.
 * A version larger than one frame travels as 2 to CALIPER_NET_MAX_PIECES
 * RESOURCE parts, in order, each with the whole version's keys and the key
 * "split" ({version, part, parts, first}): the sender's counter of versions
 * in parts, this part's number from 0, the number of parts, and the first
 * element of the version that the part's section holds. A part holds whole
 * elements; the parts hold the version's elements in order, without gap or
 * overlap. A receiver puts the parts together, holds at most
 * CALIPER_NET_MAX_INCOMPLETE_UPDATES incomplete versions per connection, and
 * drops one whose next part has not come after CALIPER_NET_RESOURCE_PART_WAIT
 * further RESOURCE frames, or when part 0 of a newer version of the same
 * resource comes.
 * PROGRAM_STATE gives an instance the status kind "piece".
 * docs/reference/wire-protocol-3-2.md states the keys and checks. */
#define CALIPER_NET_CAP_RESOURCES        "resources"  /* RESOURCE, and PROGRAM of a piece          */
/* 3.2 draft, the piece form of SCENE_UPDATE: an entity part's arrays travel as the RESOURCE
 * versions of a scene program "scene.<n>" (1 to 9 digits), an instance of the part's library
 * piece, sent just before the update that names them with the keys program, piece, resources
 * and arrays. A producer sends the form only to a peer that announced the capability. */
#define CALIPER_NET_CAP_SCENE_PIECES     "scene_pieces" /* the piece form of SCENE_UPDATE   */
#define CALIPER_NET_SCENE_PROGRAM_PREFIX "scene."     /* the id of a scene program: scene.<n> */
#define CALIPER_NET_MSG_RESOURCE         0x0082u      /* P -> V/H, H -> V   one version of one published resource */
#define CALIPER_NET_SECTION_DERIVE       "derive"     /* u8 (B,): the derive half's WGSL    */
#define CALIPER_NET_SECTION_DRAW         "draw"       /* u8 (B,): the draw half's WGSL      */
#define CALIPER_NET_SECTION_COMMON       "common"     /* u8 (B,): the lib modules and common.wgsl */
#define CALIPER_NET_SECTION_RESOURCE     "resource"   /* u8 (B,): the valid bytes of one resource */
#define CALIPER_NET_STATUS_PIECE         "piece"      /* PROGRAM_STATE "kind" of an instance */
#define CALIPER_NET_MAX_RESOURCES        8u           /* resources of one program           */
/* A resource of 64 MiB less 64 KiB fits one frame of CALIPER_NET_MAX_FRAME_BYTES with its metadata. */
#define CALIPER_NET_RESOURCE_MAX_BYTES   67043328u    /* bytes of the section "resource"    */
/* A buffer version stays within the default WebGPU storage binding limit. */
#define CALIPER_NET_RESOURCE_VERSION_MAX_BYTES 134217728u /* bytes of a buffer version in parts */
#define CALIPER_NET_RESOURCE_TEXTURE_VERSION_MAX_BYTES 268435456u /* bytes of a texture version in parts */
#define CALIPER_NET_RESOURCE_MAX_PARTS   32u          /* 8 MiB chunks of a full RGBA8 texture */
#define CALIPER_NET_RESOURCE_PART_WAIT   256u         /* RESOURCE frames a receiver waits for the next part */
#define CALIPER_NET_RESOURCE_STRIDE_MAX  256u         /* "stride": the bytes of one element */
#define CALIPER_NET_RESOURCE_SIDE_MAX    8192u        /* a texture's "width" and "height": WebGPU's default largest 2-D texture */
#define CALIPER_NET_TEXTURE_RGBA8        "rgba8unorm" /* "format": four 8-bit channels, one word a texel */
#define CALIPER_NET_TEXTURE_R8           "r8unorm"    /* "format": one 8-bit channel, four texels a word */
#define CALIPER_NET_TEXTURE_R32UINT      "r32uint"    /* "format": one 32-bit unsigned integer a texel */
#define CALIPER_NET_ELEMENT_U8           "u8"         /* "element": four elements a word     */
#define CALIPER_NET_ELEMENT_U16          "u16"        /* "element": two elements a word      */
#define CALIPER_NET_PIECE_NAME_MAX       32u          /* bytes of "piece" "name", [a-z][a-z0-9_]* */
#define CALIPER_NET_PIECE_VERSION_MAX    1000000u     /* "piece" "version", from 1          */
#define CALIPER_NET_PIECE_COMMON_MAX     589824u      /* bytes of "common": 8 lib modules and common.wgsl of 64 KiB each */
#define CALIPER_NET_PIECE_SEGMENTS_MAX   16u          /* items of "segments"                */
#define CALIPER_NET_TENSOR_NAME_MAX      255u         /* bytes of an input's "source"       */

/* Structured interpretation metadata uses CONTROL and CONTROL_REPLY framing.
 * Both peers announce interpretation. Requests carry interpretation={version:1,
 * method,params}; successful replies carry result as an object. Model work runs
 * at a run boundary. queued has one later ok/error reply with the same ID. */
#define CALIPER_NET_CAP_TEXT_PARAMS "text_params"
#define CALIPER_NET_PARAM_TEXT "text"
#define CALIPER_NET_CAP_INTERPRETATION "interpretation"
#define CALIPER_NET_OP_INTERPRET "interpret"
#define CALIPER_NET_INTERPRET_MAX_BYTES 262144u
#define CALIPER_NET_INTERPRET_STRING_MAX 4096u

#ifdef __cplusplus
}  /* extern "C" */
#endif
#endif /* CALIPER_LIVE_NET_H */