pi-protocol
Runtime-neutral schemas, CBOR encoding, and byte-stream framing for the experimental pi protocol.
Protocol version 1 uses binary messages with this wire layout:
- A four-byte unsigned big-endian payload length.
- One definite-length CBOR item containing the message.
The first client message is always hello, containing PROTOCOL_VERSION. Subsequent messages use correlated request/response envelopes and server event envelopes. Session and server snapshots are authoritative. Progress events are transient UI hints and must not be reduced into authoritative state. Transports complete authentication before protocol bytes are exchanged.
Session lists contain SessionMetadata, the normalized durable metadata available without acquiring a session runtime. Only id and createdAt are required; updatedAt, parentSessionId, sessionName, and cwd are included when supported by the backing store. Runtime state such as phase, model, thinking level, attachment, and locking appears only in an acquired SessionSnapshot.
This package does not implement JSONL session files. JSONL session persistence belongs to pi_coding_agent; pi_protocol frames CBOR messages for the socket protocol.
Validated message API
encode_client_message() and encode_server_message() validate a message and return a complete framed bytes object. The incremental decoders accept arbitrary fragmentation or coalescing, so they work with streams, sockets, and custom byte transports.
from pi_protocol import (
PROTOCOL_VERSION,
create_server_message_decoder,
encode_client_message,
)
hello = {
"type": "hello",
"version": PROTOCOL_VERSION,
}
outbound = encode_client_message(hello)
decoder = create_server_message_decoder(max_frame_length=1024 * 1024)
for message in decoder.push(incoming_chunk):
handle_server_message(message)
decoder.end()
ClientMessageDecoder and ServerMessageDecoder are also available directly. Schema violations, malformed CBOR, and invalid framing throw ProtocolValidationError. Validation errors do not retain rejected payloads.
parse_client_message() and parse_server_message() only validate already-decoded Python values. They do not parse JSON strings.
The TypeScript package builds TypeBox schemas. The Python port expresses those schemas as JSON Schema draft 2020-12 dictionaries in src/pi_protocol/schemas.py and validates them with jsonschema.Draft202012Validator in src/pi_protocol/codec.py. Field names are on-the-wire names and stay camelCase.
Transport support
Every transport carries the same complete bytes: [uint32-be CBOR length][CBOR payload]. Transports may split or coalesce those bytes arbitrarily.
This package does not bundle a transport. Consumers provide a byte-stream transport that preserves byte order and reports stream closure. Custom transports must handle arbitrary frame fragmentation and coalescing.
All transports are untrusted. Configure matching frame limits and enforce access controls appropriate for the transport before exposing a connection to the protocol. Unix sockets can use filesystem permissions, while network transports can authenticate during connection establishment.
Encoding and framing
encode_cbor() and decode_cbor() implement the protocol's strict RFC 8949 subset. encode_frame() and FrameDecoder handle framing independently of schemas and CBOR.
The CBOR subset supports:
Noneand booleans- finite numbers, with integers restricted to JavaScript's safe range and non-integers encoded as float64
- UTF-8 strings
bytesbyte strings- definite-length arrays
- definite-length maps represented by
dictobjects with unique string keys
JSON-valued protocol fields reject CBOR byte strings and non-plain objects. Top-level non-values, non-finite or unsafe numbers, malformed UTF-8, tags, indefinite-length items, trailing data, excessive nesting, oversized values, cycles, and unknown object properties are rejected.
Default limits are 16 MiB per CBOR payload/frame, 1,000,000 array elements or map entries, and 64 nested item levels. Options can configure these limits. A frame decoder validates the declared length before buffering payload bytes.
All schemas reject unknown object properties. The protocol is experimental and has no compatibility guarantees.
Development
From the repository root:
uv sync --all-packages
uv run pytest packages/pi-protocol
uv run ruff check packages/pi-protocol
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pp_rpc_protocol-0.1.0.tar.gz.
File metadata
- Download URL: pp_rpc_protocol-0.1.0.tar.gz
- Upload date:
- Size: 23.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.10.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f55e352492d844c8fc79db9feae600c4bdf66d1b4200861a09a19547d6f6f755
|
|
| MD5 |
1399050e84a51f1fb757ce8d4b97d76d
|
|
| BLAKE2b-256 |
35067fffede75d32ccc320b5ec933efe3ab812fbd22b471b7866591182aea695
|
File details
Details for the file pp_rpc_protocol-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pp_rpc_protocol-0.1.0-py3-none-any.whl
- Upload date:
- Size: 16.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.10.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c92d54d95f854d7afab67e621300147aab7ebb8d9be03cd30bb48a71fe9c337b
|
|
| MD5 |
94e5fa32eb64138bd3c0d4c3600157d0
|
|
| BLAKE2b-256 |
75d6b75c4953d3ef02c2301006075a18b7a5c358c383b2166cae9a33dbc084fb
|