sandkiln-protocol is a contract, not a feature. The host-side sandkiln-vmm crate and the in-VM sandkiln-guest-agent binary both depend on it, and nothing else does. It has two dependencies (serde with derive, and serde_json), no knowledge of what a vsock is, and no knowledge of HTTP or Firecracker. If a change needs a socket, a process, or a path, it does not belong here.
This post is the crate-level tour. The vsock protocol and guest agent post covers how the messages travel and how the two ends avoid hanging; the docs-site page vsock and the host-guest wire protocol has the prose introduction and a captured exchange. Here the focus is the shape of the crate itself: what it exports, what invariants each type carries, and how the tests keep the contract from drifting. It assumes you have read the system overview.
Public API surface
Everything public comes fromcore/crates/protocol/src/lib.rs. The crate root declares two private modules and re-exports from them:
core/crates/protocol/src/lib.rs:6-10.) Nothing in framing or messages is reachable except through those re-exports, so a consumer cannot name a path like sandkiln_protocol::messages::Request. That keeps module layout an internal choice.
The
encode_*/decode_* helpers are deliberately thin wrappers over serde_json::to_vec and from_slice. Their purpose is to keep the JSON choice an implementation detail: the CodecError alias says in one line that the wire encoding is not part of the public contract, only the byte-level framing and message shapes are. Each helper’s doc comment also names which side calls it (encode_request is “host-side client”, decode_request is “guest agent side”), which doubles as a map of who speaks which half of the protocol.
Module tour
The crate is three files and 696 lines in total.messages.rs is the largest file but is not a candidate for splitting: it is a flat list of type definitions followed by tests, one concern. The root AGENTS.md says to split when a file mixes independently workable concerns, and this one does not.
Framing
The framing module is 25 lines of code. A frame is a four-byte little-endian length followed by that many bytes:core/crates/protocol/src/framing.rs:6-18.) The cap check happens before the allocation. That ordering is the whole point of the constant: a corrupt or hostile length prefix must not turn into a multi-gigabyte vec! inside a small static binary. write_message is the mirror image: write the length, write the payload, flush.
The functions take impl Read and impl Write, not a socket type. That is why the same two functions serve a UnixStream on the host, a VsockStream in the guest, a Vec<u8> in a unit test, and a Cursor when the tests replay bytes. The protocol crate never needs to know which.
Messages
messages.rs holds three kinds of type. The first is the request/response pair for AGENT_PORT. The second is the three handshake structs that open the long-lived connection shapes. The third is ExecStreamEvent, the one enum that keeps framing for the whole life of a stream.
The two request/response enums use serde’s internal tagging, with different tag field names:
core/crates/protocol/src/messages.rs:4-19, trimmed.) Request is tagged cmd, Response is tagged status, and ExecStreamEvent is tagged stream. A captured frame therefore reveals its direction and kind at a glance: {"cmd":"read_file",...} can only be host to guest, {"status":"file",...} only guest to host.
The two relationships drawn are the only ones in the type graph. Response::Dir holds a Vec<DirEntry>. An ExecStreamHandshake is followed by a sequence of ExecStreamEvents, but that is a runtime ordering, not a field. Everything else is a standalone type.
Four ports, four connection shapes
The port constants live inlib.rs for one reason: both sides must agree on them, so they cannot be defined on either side. Each constant’s doc comment records why it exists as its own port rather than a new Request variant, and that reasoning is the real design of the protocol.
AGENT_PORTis ordinary request/response. Every frame in both directions is a framed JSON message.PTY_PORTis one long-lived connection per session. After a single framedPtyHandshake { cols, rows }, framing stops and every byte goes straight to a pty. A terminal is a byte stream, and wrapping it in JSON would add latency and base64 overhead to every keystroke.EXEC_STREAM_PORTis long-lived too, but never drops to raw bytes. A caller needs to know which stream a chunk came from and what the exit code was, so each chunk is anExecStreamEventand the sequence ends with exactly oneExit.TUNNEL_PORTis the single exception to “the host connects in and the guest only listens”. The event being carried is “something inside the guest just tried to connect”, which the host cannot have initiated. The constant’s doc comment explains the Firecracker mechanism: a guest connection to this port is forwarded to a Unix socket the host already has listening at<uds_path>_<port>, with noCONNECThandshake line. TheTunnelOpen { tunnel_id, conn_id }frame is sandkiln’s own handshake layered on top, so the host can route the connection to the right tunnel.
content_base64, data_base64), because JSON strings cannot carry arbitrary bytes and process output is not guaranteed to be UTF-8. That costs roughly a third more bytes on the wire, a trade the crate makes explicitly in the ExecStreamEvent doc comment.
Key types and their invariants
A protocol crate has few invariants, but the ones it has are load-bearing. Defaults make additions backward compatible at the field level.Exec::args, Exec::env, Mkdir::parents, ExecStreamHandshake::args and ::env are all #[serde(default)]. A frame that omits them still decodes, and tests pin it (exec_request_args_and_env_default_to_empty_when_omitted, mkdir_request_parents_defaults_to_false_when_omitted). This is not version negotiation; see below.
env arrives already resolved. The doc comment on Exec::env states that the daemon has merged create-time defaults with per-call overrides before the request is built, per-call winning. The guest agent has no notion of “sandbox-level” versus “call-level” environment. It adds the map on top of the process’s own inherited environment rather than replacing it. Keeping the merge out of the protocol means the guest stays a dumb executor.
mode is raw permission bits. Chmod::mode and DirEntry::mode are both plain u32 permission bits such as 0o644, not symbolic strings. The guest masks file-type bits off st_mode for DirEntry::mode, so listing a directory and feeding an entry’s mode straight back to Chmod round-trips.
Response::Ok and Response::Error are shared. Operations with no payload (Chmod, Mkdir, Rename, StartTunnel, and others) all answer Response::Ok. Failure of any operation is Response::Error { message }, carrying the guest’s OS error text.
A PtyHandshake is Copy; the others are not. PtyHandshake is two u16s and derives Clone, Copy. ExecStreamHandshake and TunnelOpen derive Clone only, since they own strings and maps. Neither Request nor Response derives PartialEq; a comment in the test module explains it would be derived only for the tests, so the roundtrip test compares re-serialized bytes instead.
Error model
There are three layers, and they do not mix.- Transport errors are
io::Error.read_messagereturnsUnexpectedEofwhen the stream ends mid-frame or before a length prefix, andInvalidDatawhen the length exceeds the 64 MiB cap. A framing failure means the connection is no longer trustworthy, and callers treat it as fatal for that connection. - Codec errors are
CodecError, an alias forserde_json::Error. A malformed body or an unknowncmdtag is a decode failure, not a panic. Both the host client and the guest agent convert it at their own boundary: the host maps it toio::ErrorKind::InvalidData(to_io_errinvsock_client.rs), and the guest answers a bad request withResponse::Error { message: "bad request: ..." }and keeps the connection open for the next frame. - Operation errors are data, not errors in the Rust sense.
Response::Error { message }is a successful protocol exchange whose content is “the operation failed”. The host’s call returnsOk(Response::Error { .. }), and the daemon decides how to surface it.
AGENTS.md section 7). A guest that rejects a request must not look like a dead guest. Only layers 1 and 2 indicate a broken connection; layer 3 is normal traffic. One consequence for the host: a transport error after a request was fully written does not say whether the guest ran it, which is why sandkiln-vmm tracks that fact separately and never retries such a call (see the vmm tour).
How the pieces compose
The crate’s own composition test exercises the exact pipeline a real request takes: encode, frame, transmit, unframe, decode.full_request_pipeline_host_to_guest and full_response_pipeline_guest_to_host in lib.rs run both directions against a Cursor, so the test needs no socket and no VM. The comment on the first one states the division of labour: each piece has unit tests in its own file, and this test proves they compose.
Test strategy
cargo test -p sandkiln-protocol (re-run 2026-11-24) runs 39 tests, all pure and instant: 7 in framing.rs, 4 in lib.rs, and 28 in messages.rs. There is no I/O beyond in-memory buffers.
Framing tests target the failure modes of a length-prefixed format. A payload containing \n, a NUL and high bytes round-trips (roundtrips_bytes_that_look_like_frame_boundaries), which is the argument for length prefixing over newline delimiting, expressed as a test. An empty payload round-trips. write_message_prefixes_exact_little_endian_length pins the byte layout. read_message_rejects_oversized_length_prefix sends MAX_MESSAGE_LEN + 1 and asserts InvalidData. Truncated input and a missing length prefix both fail with UnexpectedEof rather than hanging or panicking.
Wire-shape tests in messages.rs assert exact JSON strings, one per variant:
core/crates/protocol/src/messages.rs:239-244.) The comment above the group explains why a plain roundtrip is not enough: serde would happily round-trip a renamed tag or a reshaped variant through itself. Comparing against a fixed string is what catches an accidental rename. These tests are also the executable form of the rule in the crate’s AGENTS.md: a new operation gets a variant, a wire-shape test, and an entry in every_request_variant_roundtrips.
Defaulting tests decode JSON with optional fields omitted and assert the defaults, which keeps the #[serde(default)] attributes from being silently dropped.
Decode-rejection tests in lib.rs feed decode_request the bytes not json and a well-formed object with an unknown cmd, and assert both are errors. A host should never be able to make the guest execute something it cannot name.
What is not tested here is behaviour that needs a live guest. The crate’s AGENTS.md is explicit that there is no live-testable behaviour in a types-plus-framing crate; a behavioural change needs the full guest-agent plus daemon verification against a real microVM.
How neighbours consume it
The daemon depends on the protocol crate directly, as well as through
vmm, because its routes build Request values and pattern-match Response variants. On the host, vsock_client owns the request/response and handshake codec calls. The one place the daemon touches framing itself is its streamed-exec log pump (core/crates/daemon/src/http/routes/logs.rs:176), which reads ExecStreamEvent frames off a stream that Vm::open_exec_stream handed back.
Because host and guest link the same definitions, a message change is a lockstep change. There is no version negotiation: the crate’s AGENTS.md says plainly that both guest-agent and vmm/daemon must be rebuilt and redeployed together, and that a new Request variant needs both sides’ match statements updated. Rust’s exhaustive matching helps here only if both sides are actually rebuilt, so the guest agent has to be baked into a fresh rootfs image whenever the protocol changes.
Design alternatives rejected
Newline-delimited JSON. Rejected because output bytes can contain newlines, NULs, or invalid UTF-8, and any delimiter inside a payload would corrupt the stream. Length prefixing is immune by construction, and a test asserts it. A binary schema format. Rejected as unnecessary weight for a protocol with a handful of message shapes: it would add a schema compiler and a build step, and the frames would no longer be readable in a packet capture while debugging. JSON with tagged enums is self-describing and good enough. NewRequest variants for interactive sessions. A pty, a streamed exec, and a tunnel connection are not request/response shaped, so folding them into Request and Response would have forced a second, ad hoc framing rule inside the existing one. Giving each its own port keeps AGENT_PORT a clean one-request-one-response loop and lets each long-lived shape choose its own post-handshake framing.
Defining ports on each side. Rejected because it invites drift. The constants live in the one crate both sides link, so changing a port number is a compile-time change on both ends at once.
Putting environment merging in the protocol. Rejected because it would give the guest a second code path to maintain. The daemon resolves the final map, and the protocol carries exactly one.
Depending on serde_json directly in consumers. Avoided with CodecError and the encode_*/decode_* helpers, so swapping the encoding later would touch one crate.