Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

An open protocol between chess programs and the nodes that serve them: chess engines, and
voice modules that speak and listen. It is JSON-RPC 2.0 over a child process's stdin/stdout,
one message per line, and later over WebSocket with TLS.
one message per line, or over TCP with TLS 1.3 to a node on another computer, with pairing
and discovery on the local network (the spec's section 3).

CNP goes further than UCI. A search can be limited to some root moves or to a score window. An
engine's techniques can be switched on and off for teaching. A search can ask for progress,
Expand Down
1 change: 1 addition & 0 deletions crates/cnp-conformance/src/bin/cnp-example-node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ fn answer(request: &Request, state: &mut State, limits: &Limits) -> Response {
// A real node lists its domains here.
capabilities: Capabilities::default(),
limits: limits.clone(),
resources: None,
};
Response::ok::<Initialize>(id, &result)
}
Expand Down
17 changes: 17 additions & 0 deletions crates/cnp/src/lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,23 @@ pub struct InitializeResult {
/// How far it will go.
#[serde(default)]
pub limits: Limits,
/// What the node was started with. Missing from nodes that don't say.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub resources: Option<Resources>,
}

/// What a node was started with: facts for a client that chooses among nodes, such as one that
/// asks a rack of machines which should search. Not limits: no request is refused for them, and
/// none can change them.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Resources {
/// The search threads the node uses.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub threads: Option<u32>,
/// The hash memory the node uses, in megabytes.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub hash_mb: Option<u64>,
}

/// The node program: what it is, and where its source is.
Expand Down
30 changes: 30 additions & 0 deletions crates/cnp/tests/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,3 +725,33 @@ fn move_text_is_two_squares_and_maybe_a_promotion() {
assert!(!engine::is_move_text(bad), "{bad}");
}
}

#[test]
fn resources_are_optional_in_initializes_result_and_say_what_a_node_runs_with() {
let older = json!({
"protocol": "1.0",
"node": {"name": "n", "version": "1", "license": "MIT"},
"capabilities": {}
});
let result: lifecycle::InitializeResult = serde_json::from_value(older.clone()).unwrap();
assert_eq!(result.resources, None);
// A node that doesn't say leaves the field out, so its answer is what it always was.
assert_eq!(
serde_json::to_value(&result).unwrap().get("resources"),
None
);

let mut newer = older;
newer["resources"] = json!({"threads": 8, "hashMb": 1024, "somethingNew": true});
let result: lifecycle::InitializeResult = serde_json::from_value(newer).unwrap();
assert_eq!(
result.resources,
Some(lifecycle::Resources {
threads: Some(8),
hash_mb: Some(1024)
})
);
// Either may be missing.
let partial: lifecycle::Resources = serde_json::from_value(json!({"threads": 2})).unwrap();
assert_eq!(partial.hash_mb, None);
}
73 changes: 73 additions & 0 deletions crates/cnp/tests/transport_examples.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//! The examples of the network transport in the specification (sections 3.2 and 3.3) are valid
//! JSON of the shapes the text describes, so the spec's examples can't drift from its rules.
//!
//! They are the `transport` blocks of `spec/cnp.md`: one message a line, after `→ ` (from the
//! client) or `← ` (from the node). They aren't JSON-RPC messages (except the `Busy` refusal,
//! which is), so they are not the `cnp` blocks the other example test checks.

use serde_json::Value;

const SPEC: &str = include_str!("../../../spec/cnp.md");

/// The messages of the `transport` blocks: (from the client, the message).
fn messages() -> Vec<(bool, Value)> {
let mut found = Vec::new();
let mut inside = false;
for line in SPEC.lines() {
match line.trim_end() {
"```transport" => inside = true,
"```" => inside = false,
text if inside => {
let (client, json) = match (text.strip_prefix("→ "), text.strip_prefix("← ")) {
(Some(json), _) => (true, json),
(_, Some(json)) => (false, json),
_ => panic!("a transport line starts with an arrow: {text}"),
};
found.push((client, serde_json::from_str(json).expect("valid JSON")));
}
_ => {}
}
}
found
}

#[test]
fn the_transport_examples_are_the_messages_the_text_describes() {
let all = messages();
assert_eq!(all.len(), 5, "pair, paired, busy, probe, reply");
let by = |key: &str, value: &str| {
all.iter()
.find(|(_, m)| m[key] == value)
.unwrap_or_else(|| panic!("no {key}={value} example"))
.clone()
};
let (client, pair) = by("cnp", "pair");
assert!(client);
assert_eq!(pair["code"].as_str().unwrap().len(), 8);
assert!(!by("cnp", "paired").0);
let (client, probe) = by("cnp", "probe");
assert!(client && probe["v"] == 1);

let (client, reply) = by("cnp", "here");
assert!(!client && reply["v"] == 1);
assert!(reply["name"].as_str().unwrap().len() <= 32);
assert!((1..=65535).contains(&reply["port"].as_u64().unwrap()));
assert!(reply["pairing"].is_boolean());
// The fingerprint is 32 pairs of uppercase hexadecimal digits with colons: 95 characters.
let fingerprint = reply["fingerprint"].as_str().unwrap();
assert_eq!(fingerprint.len(), 95);
assert!(fingerprint.split(':').count() == 32);
assert!(fingerprint.split(':').all(|p| {
p.len() == 2
&& p.bytes()
.all(|b| b.is_ascii_hexdigit() && !b.is_ascii_lowercase())
}));
// The reply is never longer than the 320-byte probe it answers.
assert!(reply.to_string().len() <= 320);

let busy = all
.iter()
.find(|(_, m)| m["error"]["code"] == -32013)
.expect("the Busy refusal");
assert!(busy.1["id"].is_null());
}
127 changes: 117 additions & 10 deletions spec/cnp.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,106 @@ input and output.
([6.3](#63-limits)). A node answers it with an `InvalidRequest` error whose `id` is `null`,
and reads on from the next line.

### 3.2 The network
### 3.2 The network: TLS over TCP

A node MAY also serve clients on other computers. The messages, and their framing, are those of
[3.1](#31-stdinstdout): one line of UTF-8 JSON each, ending in `\n`. They travel over a TCP
connection protected by TLS 1.3. A node is started with this transport only by a person who
asks for it; **a node MUST NOT listen beyond its own computer without TLS and the checks below**.

- **Port.** A node SHOULD listen on TCP port **4026** unless told another. A client is told the
port by the person, or by discovery ([3.3](#33-finding-a-node-on-the-local-network)).
- **TLS 1.3 only,** with a certificate on **both** sides. A node MUST require a client
certificate. Certificates are self-signed and made on first use; there is no authority.
- **A node is recognised by its fingerprint,** the SHA-256 of its certificate's DER encoding,
written as 32 pairs of uppercase hexadecimal digits separated by colons
(`AB:CD:…`, 95 characters). No name, chain or date in a certificate is checked, but each
side MUST verify the handshake's signature, which proves the peer holds the certificate's
private key. A client that has paired with a node (below) MUST refuse a connection whose
certificate has any other fingerprint.
- **A node serves only approved clients.** It keeps a list of client fingerprints and checks the
client's after the handshake. A person removes a client from the list to revoke it, and the
node MUST apply that at the client's next connection. A client that isn't on the list MAY send
one pairing request (below) and nothing else; the node otherwise closes the connection
**without replying**, so that a refusal tells a guesser nothing.
- **Pairing** puts a client on the list, once:
1. The person at the node starts pairing. The node shows its fingerprint and a **one-time
code** of eight digits. The code MUST work for one client only, for at most five minutes,
and the window MUST close after at most three wrong codes.
2. The client connects, not yet knowing the node's fingerprint, and reads the fingerprint
from the certificate it is shown. **The client MUST show it to the person and wait for
them to confirm it is the one the node displays, before it sends the code.** This
comparison is what stops someone in the middle; the code alone does not.
3. The client sends one line, at most 1,024 bytes, and the node answers `paired` and closes
the connection, or closes it without a word. The client then connects again as an approved
client:

```transport
→ {"cnp": "pair", "code": "12345678", "name": "Carlos's iMac"}
← {"cnp": "paired"}
```

`name` is optional, a label the node keeps beside the fingerprint to tell clients apart.
The code is typed by a person, so a node SHOULD ignore spaces and hyphens in it.
- **One client at a time.** A node MAY serve one approved client at once. When another
approved client connects, the node sends one `Busy` error with a `null` id and closes the
connection:

```transport
← {"jsonrpc": "2.0", "id": null, "error": {"code": -32013, "message": "this node is serving another client"}}
```

- **Disconnecting cancels.** When a client's connection ends, for any reason, the node cancels
the client's searches and forgets its state. Nothing resumes on a later connection, and ids
belong to one connection.
- **Deadlines.** A node SHOULD close a connection that has not finished its handshake and sent
its first message within about ten seconds, counting the whole of it, not each read. It
MUST NOT close a connection merely for being silent afterwards, because a client waiting
for a long search sends nothing. It SHOULD use TCP keepalive to find a client that has
vanished.
- **Limits.** A node on the network SHOULD advertise a `maxMessageBytes` much smaller than the
default ([6.3](#63-limits)), and SHOULD limit how many connections it greets at once, in
all and from one address, and ignore an address that keeps failing.

WebSocket over TLS, for programs that can't open a TCP connection, is still reserved
([13](#13-reserved-for-later-versions)).

### 3.3 Finding a node on the local network

A client MAY look for nodes with a UDP probe. This only says *where a node claims to be*:
a client never trusts it, and connects and checks the fingerprint as in
[3.2](#32-the-network-tls-over-tcp). A node answers probes only when its owner has asked it to.

- **Probe.** A datagram of exactly **320 bytes**: the JSON below, padded with spaces. The client
sends it to UDP port **4026**, at the broadcast address of each of its network interfaces
and at `255.255.255.255` (which on some systems reaches only one interface), and MAY send it
again once. A node ignores a datagram of any other size or content.
- **Reply.** One JSON object, sent to the address and port the probe came from, never longer
than the probe:

```transport
→ {"cnp": "probe", "v": 1}
← {"cnp": "here", "v": 1, "name": "the study", "protocol": "1.0", "port": 4026, "fingerprint": "AB:CD:EF:01:23:45:67:89:AB:CD:EF:01:23:45:67:89:AB:CD:EF:01:23:45:67:89:AB:CD:EF:01:23:45:67:89", "pairing": false}
```

CNP over WebSocket with TLS, with credentials given when the connection opens, is reserved for
a later minor version ([13](#13-reserved-for-later-versions)). The messages will be the same.
| Field | Type | |
| --- | --- | --- |
| `name` | string | A name its owner chose, at most 32 bytes. Not the computer's name |
| `protocol` | string | The CNP version it speaks, `major.minor` |
| `port` | integer | The TCP port, on the address the reply came from |
| `fingerprint` | string | Its certificate's fingerprint |
| `pairing` | boolean | Whether a pairing window is open now |

A client ignores a reply that is longer than a probe, isn't this JSON, has a `port` of zero,
or has a fingerprint that doesn't read.
- **No amplification and no leaks.** A source address can be forged, so a reply MUST NOT be
longer than the probe it answers. A node SHOULD answer at most a few probes a second to one
address (cb2026: 4) and a few dozen in all (40), and bound the memory that counts them. A
reply MUST NOT contain anything private: no hostname, user name, path or hardware.
- **Opt-in.** A node MUST NOT answer probes unless it also requires TLS and approved clients.
- **Limits of broadcast.** It reaches the local network segment only. It does not cross a
router or VLAN, many Wi-Fi access points keep clients from seeing each other, and a firewall
can block the port; a client MUST also let a person type an address. IPv4 only for now.

## 4. Messages

Expand Down Expand Up @@ -153,7 +249,7 @@ already been answered, or that never existed, does nothing: the two messages can
```text
client node
initialize {protocol, client} ────────────▶
◀────────────── {protocol, node, capabilities, limits}
◀────────────── {protocol, node, capabilities, limits, resources?}
… requests, notifications, ping at any time …
shutdown ─────────────────────────────────▶ ends its work
◀────────────── null
Expand Down Expand Up @@ -181,6 +277,7 @@ Result:
| `node` | object | The node: `name`, `version`, `license` (an [SPDX](https://spdx.org/licenses/) expression), and optionally `author` and `source` (a URL to its source code) |
| `capabilities` | object | What the node can do ([6.1](#61-capabilities)) |
| `limits` | object | How far it will go ([6.3](#63-limits)) |
| `resources` | object | Optional. What the node was started with, for a client that chooses among nodes: `threads`, the search threads it uses, and `hashMb`, its hash memory in megabytes. Either may be missing. They are facts, not limits: a request can't change them, and none is refused for them |

The version spoken is the client's major version, with the lower of the two minor versions. If
the node doesn't speak the client's major version, it answers with `IncompatibleVersion`, whose
Expand Down Expand Up @@ -212,7 +309,7 @@ first, and 1 otherwise. The end of the node's input means the same as `exit`.

```cnp
→ {"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"protocol": "1.0", "client": {"name": "Solrac", "version": "0.2.0"}}}
← {"jsonrpc": "2.0", "id": 1, "result": {"protocol": "1.0", "node": {"name": "cb2026", "version": "0.9.0", "license": "MIT OR Apache-2.0", "source": "https://github.com/cjus/chessengine"}, "capabilities": {"engine": {"multipv": true, "window": true, "rootMoves": true, "strength": true, "techniques": true, "deterministic": true, "clock": true, "mate": true, "chess960": true, "progress": true}}, "limits": {"maxDepth": 128, "maxTimeMs": 3600000, "maxMultipv": 8, "maxConcurrentSearches": 4}}}
← {"jsonrpc": "2.0", "id": 1, "result": {"protocol": "1.0", "node": {"name": "cb2026", "version": "0.9.0", "license": "MIT OR Apache-2.0", "source": "https://github.com/cjus/chessengine"}, "capabilities": {"engine": {"multipv": true, "window": true, "rootMoves": true, "strength": true, "techniques": true, "deterministic": true, "clock": true, "mate": true, "chess960": true, "progress": true}}, "limits": {"maxDepth": 128, "maxTimeMs": 3600000, "maxMultipv": 8, "maxConcurrentSearches": 4}, "resources": {"threads": 8, "hashMb": 1024}}}
→ {"jsonrpc": "2.0", "id": 2, "method": "engine/search", "params": {"position": {"fen": "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", "moves": ["e2e4", "e7e5"]}, "limits": {"timeMs": 1000}, "progressMs": 250, "game": "g1"}}
← {"jsonrpc": "2.0", "method": "engine/progress", "params": {"id": 2, "depth": 12, "selDepth": 19, "score": {"cp": 24}, "pv": ["g1f3", "b8c6", "f1b5"], "nodes": 412530, "nps": 1650120, "timeMs": 250}}
← {"jsonrpc": "2.0", "id": 2, "result": {"bestMove": "g1f3", "ponder": "b8c6", "score": {"cp": 27}, "pv": ["g1f3", "b8c6", "f1b5", "a7a6"], "depth": 16, "selDepth": 27, "nodes": 1702214, "timeMs": 1000, "stopped": false, "stats": {"hashFull": 87}}}
Expand Down Expand Up @@ -931,15 +1028,19 @@ its own outside the ranges below.

## 10. Security

A node started by a client trusts that client. A node on the network trusts no one. Version 1.0
already holds both to these rules, so that the network can be added without changing them:
A node started by a client trusts that client. A node on the network trusts no one: it uses TLS
with both sides' certificates, serves only clients a person approved, and says nothing to anyone
else ([3.2](#32-the-network-tls-over-tcp)). Both are held to these rules, which are the same on
any transport:

- **No file paths in any request.** Opening books, tablebases and models are the node's own
configuration. A request that can name a file is how a server is turned against itself.
- **Advertised limits** for depth, nodes, time, lines, concurrent searches, trees and message
size. Requests beyond them are refused, not trimmed. A node bounds what it keeps for a client,
the trees of [7.8](#78-the-search-tree), and drops the oldest rather than run out of memory.
- **Credentials never travel in messages,** and are never logged.
- **Credentials never travel in messages,** and are never logged. A pairing code is the one
secret a client sends, once, in the pairing request of [3.2](#32-the-network-tls-over-tcp),
after the person has confirmed the node's fingerprint; it is never kept after use.

## 11. Versions

Expand All @@ -962,8 +1063,8 @@ conforms to CNP 1.0 if it passes every session that applies to it. See `conforma

These names are reserved, so that nothing else uses them:

- **WebSocket over TLS** ([3.2](#32-the-network)), with credentials given when the connection
opens, never in messages.
- **WebSocket over TLS**, for programs that can't open a TCP connection ([3.2](#32-the-network-tls-over-tcp)
specifies TLS over TCP), with credentials given when the connection opens, never in messages.
- **Distributed search:** `worker/register`, which a worker sends after connecting out to a
coordinator, with its capabilities and hardware. Either side may then send requests.
- **Streamed speech:** `voice/speak` sending its audio piece by piece.
Expand All @@ -972,6 +1073,12 @@ These names are reserved, so that nothing else uses them:

## 14. Changes

- **1.0 (draft), 2026-11-05**, added to the draft as pondering was: **the network**
([3.2](#32-the-network-tls-over-tcp), [3.3](#33-finding-a-node-on-the-local-network)): TLS 1.3
over TCP with self-signed certificates pinned by fingerprint on both sides, pairing with a
one-time code, one client at a time, and discovery by a UDP probe; and `resources` in
`initialize`'s result, so a client can tell a node with 64 threads from one with 1. Both are
optional, so a node or client that doesn't know them is unaffected (cjus/chessengine#209).
- **1.0 (draft)**, 2026-10-21: the first version. Before it was final: `chess960` in a
position, so a Chess960 game says so; and listening (`listen`, `voice/listen`,
`voice/audio`, `voice/speechStarted`, `voice/heard`, and the purpose `listen`), so a player
Expand Down
Loading