Skip to main content
Low-level typed and raw transport for communicating with agentd through a sandbox relay.

Module functions

agent::connect_sandbox()

Resolve a sandbox name to its agent relay socket path and connect, using the default ten-second handshake timeout. The socket lives under the SDK’s configured runtime directory at a short, name-derived path. Sandbox names are limited to 128 UTF-8 bytes.

Parameters

name&str
Sandbox name, up to 128 UTF-8 bytes.

Returns

Connected client.

agent::connect_sandbox_with_timeout()

Like connect_sandbox, but with an explicit handshake timeout instead of the ten-second default.

Parameters

name&str
Sandbox name, up to 128 UTF-8 bytes.
timeoutDuration
Maximum time to wait for the relay handshake.

Returns

Connected client.

AgentClient

Returned by agent::connect_sandbox(), AgentClient::connect() · wrapped by AgentBridge

Low-level client for raw agent frames.

AgentClient::connect()

Connect to an arbitrary agent relay socket by path, using the default ten-second handshake timeout. The connection performs the relay handshake, validates the cached core.ready frame, and starts one background reader task.

Parameters

sock_pathimpl AsRef<Path>
Path to the agent relay socket.

Returns

Connected client.

AgentClient::connect_with_timeout()

Connect to an arbitrary agent relay socket by path with an explicit handshake timeout.

Parameters

sock_pathimpl AsRef<Path>
Path to the agent relay socket.
timeoutDuration
Maximum time to wait for the relay handshake.

Returns

Connected client.

AgentClient::connect_with_deadline()

Connect to an arbitrary agent relay socket by path with an explicit handshake deadline. The deadline bounds both handshake reads, so an accepted connection that stalls before writing the handshake bytes cannot block this call indefinitely.

Parameters

sock_pathimpl AsRef<Path>
Path to the agent relay socket.
deadlineInstant
Tokio instant by which the handshake must complete.

Returns

Connected client.

AgentClient::connect_sandbox()

Resolve a sandbox name to its agent socket path and connect. Equivalent to the module-level agent::connect_sandbox.

Parameters

name&str
Sandbox name, up to 128 UTF-8 bytes.

Returns

Connected client.

AgentClient::connect_sandbox_with_timeout()

Resolve a sandbox name to its agent socket path and connect with an explicit handshake timeout. Equivalent to the module-level agent::connect_sandbox_with_timeout.

Parameters

name&str
Sandbox name, up to 128 UTF-8 bytes.
timeoutDuration
Maximum time to wait for the relay handshake.

Returns

Connected client.

AgentClient::socket_path()

Resolve a sandbox’s relay socket path without connecting. Returns the same path connect_sandbox would dial: the hashed path under the runtime directory when it fits the platform’s Unix-socket length limit, and the legacy name-derived path otherwise. Useful for talking to agentd over a raw byte transport (for example a transparent relay that splices bytes to and from the socket) instead of this frame client. The sandbox need not be running.

Parameters

name&str
Sandbox name, up to 128 UTF-8 bytes.

Returns

PathBuf
Relay socket path.

AgentClient::ensure_version_compat_for()

Check a message type against an explicit negotiated generation. The single place the rule lives, exposed for callers that hold the negotiated generation but not a live client. Returns AgentClientError::UnsupportedOperation if the type was introduced after the given generation.

Parameters

tMessageType
Protocol message type to gate.
negotiatedu8
Protocol generation to check against.

Instance methods

client.request()

Send one typed protocol message and wait for one response frame with the same correlation id. Flags are derived from the message type. Use this for one-shot RPCs such as filesystem stat or list requests. Fails fast with AgentClientError::UnsupportedOperation if the connected sandbox is too old for the message type.

Parameters

tMessageType
Protocol message type.
payload&T: Serialize
Message payload, serialized with CBOR.

Returns

Message
Decoded response message.

client.stream()

Open a typed streaming session. The returned id is the protocol correlation id. Use it with send() for follow-up messages such as stdin, resize, signal, or file data chunks. The receiver yields messages until a terminal frame is delivered or the connection closes.

Parameters

tMessageType
Protocol message type for the opening frame.
payload&T: Serialize
Opening message payload, serialized with CBOR.

Returns

(u32, Receiver<Message>)
Correlation id and a typed message receiver.

client.send()

Send a typed follow-up message on an existing correlation id (the id returned by stream()).

Parameters

idu32
Correlation id from the open stream.
tMessageType
Protocol message type.
payload&T: Serialize
Message payload, serialized with CBOR.

client.request_raw()

Allocate a correlation id, send one raw frame with (flags, body), and wait for one raw response frame with the matching id. CBOR encoding and decoding are left to the caller.

Parameters

flagsu8
Frame flag byte.
bodyVec<u8>
CBOR-encoded protocol message body.

Returns

Raw response frame.

client.stream_raw()

Open a raw streaming session. The receiver yields raw frames for the returned correlation id until a frame with the terminal flag arrives or the receiver is dropped. Use send_raw() with the returned id to send follow-up frames.

Parameters

flagsu8
Frame flag byte for the opening frame.
bodyVec<u8>
CBOR-encoded protocol message body.

Returns

(u32, Receiver<RawFrame>)
Correlation id and a raw frame receiver.

client.send_raw()

Send a raw follow-up frame on an existing correlation id (the id returned by stream_raw()).

Parameters

idu32
Correlation id from the open raw stream.
flagsu8
Frame flag byte.
body&[u8]
CBOR-encoded protocol message body.

client.ready()

Return the decoded core.ready payload captured during the handshake (boot timings and the runtime’s self-reported version).

Returns

Ready
Decoded handshake payload.

client.ready_bytes()

Return the cached handshake core.ready frame body as CBOR bytes. Useful for bindings that want to deserialize the ready payload with their own CBOR tooling. For typed access, use ready().

Returns

&[u8]
CBOR-encoded ready frame body.

client.protocol()

The agent protocol generation (wire codec) negotiated for this connection.

Returns

Codec generation: Current or LegacyV1.

client.is_legacy_protocol()

Whether this connection is using the legacy pre-0.5 protocol.

Returns

bool
true if the relay speaks the pre-0.5 protocol.

client.negotiated_version()

The negotiated protocol generation for this connection: the lower of what this client speaks and what the sandbox advertised at handshake. This is the capability gate that drives supports() and the typed send path, and it is distinct from protocol(), which selects the wire codec.

Returns

u8
Negotiated capability generation.

client.agent_version()

The runtime’s self-reported package version, taken from its core.ready frame. Empty when the runtime predates this field (an older agent), in which case fall back to the generation for diagnostics.

Returns

&str
Runtime package version, or empty if unknown.

client.supports()

Whether the connected sandbox is new enough to handle the given message type. The single source of truth for feature gating: callers that cannot gate by sending (for example the SSH/SFTP layer) consult this instead of inspecting the protocol generation directly.

Parameters

tMessageType
Protocol message type to check.

Returns

bool
true if the runtime can handle the type.

client.ensure_version_compat()

Reject a message type the connected sandbox is too old to handle, against this connection’s negotiated generation. Fails before any bytes are sent, so only that one operation fails and the session continues. The typed request(), stream(), and send() methods call this internally.

Parameters

tMessageType
Protocol message type to gate.

client.close()

Close the client by consuming it. Drops the writer and aborts the reader task; any in-flight requests resolve with AgentClientError::Closed. Dropping the client has the same effect.

AgentBridge

FFI-friendly bytes-in/bytes-out wrapper around AgentClient.

AgentBridge::connect_sandbox()

Connect to a sandbox by name, resolving the socket path from SDK config. Sandbox names are limited to 128 UTF-8 bytes.

Parameters

name&str
Sandbox name, up to 128 UTF-8 bytes.

Returns

Connected bridge.

AgentBridge::connect_sandbox_with_timeout()

Connect to a sandbox by name with an explicit handshake timeout.

Parameters

name&str
Sandbox name, up to 128 UTF-8 bytes.
timeoutDuration
Maximum time to wait for the relay handshake.

Returns

Connected bridge.

AgentBridge::connect_path()

Connect to an arbitrary agentd relay socket by path.

Parameters

path&str
Path to the agent relay socket.

Returns

Connected bridge.

AgentBridge::connect_path_with_timeout()

Connect to an arbitrary agentd relay socket by path with an explicit handshake timeout.

Parameters

path&str
Path to the agent relay socket.
timeoutDuration
Maximum time to wait for the relay handshake.

Returns

Connected bridge.

bridge.request()

One-shot request: send (flags, body) and wait for one response frame.

Parameters

flagsu8
Frame flag byte.
bodyVec<u8>
CBOR-encoded protocol message body.

Returns

Response frame.

bridge.send()

Send a follow-up frame on an existing correlation id.

Parameters

idu32
Correlation id from an open stream.
flagsu8
Frame flag byte.
bodyVec<u8>
CBOR-encoded protocol message body.

bridge.stream_open()

Open a streaming session. Returns the protocol correlation id (for follow-up sends via send()) and an opaque stream handle (for stream_next() and stream_close()).

Parameters

flagsu8
Frame flag byte for the opening frame.
bodyVec<u8>
CBOR-encoded protocol message body.

Returns

Correlation id and an opaque stream handle.

bridge.stream_next()

Pull the next frame from a stream. Returns None when the stream has ended (the terminal frame was already delivered, or the stream was closed or dropped).

Parameters

Handle returned by stream_open().

Returns

Option<BridgeFrame>
Next frame, or None at end of stream.

bridge.stream_close()

Close a stream and drop its handle. Idempotent.

Parameters

Handle returned by stream_open().

bridge.ready_bytes()

The cached handshake core.ready frame body bytes (CBOR). Errors with AgentClientError::Closed if the bridge has been closed.

Returns

Vec<u8>
CBOR-encoded ready frame body.

bridge.close()

Close the connection. Idempotent. After close, every operation except another close returns AgentClientError::Closed.

Types

BridgeFrame

Returned by bridge.request(), bridge.stream_next()

FFI-friendly view of a RawFrame: id, flags, body bytes.

StreamHandle

Returned by bridge.stream_open() · used by bridge.stream_next(), bridge.stream_close()

Opaque handle identifying an open stream on an AgentBridge. Foreign-language wrappers reference streams by this u64 instead of owning a tokio receiver.

AgentProtocol

Returned by client.protocol()

Agent protocol generation (wire codec) spoken by a connected sandbox relay.

RawFrame

Returned by client.request_raw(), client.stream_raw()

A framed protocol message at the byte level. id is the protocol correlation id, flags is the frame flag byte, and body is the CBOR-encoded protocol message body. Re-exported from microsandbox::protocol::codec.

AgentClientError

Error variant of AgentClientResult

Errors raised by AgentClient and AgentBridge.

AgentClientResult

Returned by most AgentClient and AgentBridge methods

Result alias for agent client operations. The error is AgentClientError.