Expand description
The Phi eXtension Binary wire protocol — a Rust port of ext/go/pxb.
§Frame
Every message is one length-prefixed binary frame on a duplex byte stream (typically a child process stdin/stdout). No JSON, no newlines.
┌──────── header (16 bytes, little-endian) ────────┐
│ magic[4]="PXB\x01" │ typ u16 │ flags u16 │ id u32│
│ payload_len u32 │
└──────────────────────────────────────────────────┘
│ payload: tagged fields │§Evolution rules
- New message fields: allocate a new tag (≥1). Never reuse a tag.
- New lifecycle events: allocate a new
Ev*code. Never reuse a code. - New frame types: allocate a
Type*in the Ext→Host (1–99) or Host→Ext (100–199) range; peers skip unknown types by length. - Incompatible renames / semantic breaks: bump
PROTOCOL_VERSIONand refuse old peers. - Experimental tags use 128+ and must remain skippable.
These rules keep host and extension binaries independently upgradable without a shared JSON schema or lockstep release.
Structs§
- Command
Invoked - Host→ext when the user runs a slash command.
- Command
Response - Ext→host slash command outcome.
- Event
Notify - Fire-and-forget host→ext lifecycle event.
- Field
Reader - Walks a tagged-field payload.
- Field
Writer - Builds a tagged-field payload.
- Frame
- One complete message: header plus payload bytes.
- Header
- The 16-byte frame prefix.
- Hello
- The first frame from an extension.
- Hello
Ack - The host reply to
Hello. - Host
Request - Ext→host capability RPC.
- Host
Result - Host→ext reply to a
HostRequest. - Intercept
Req - Host→ext for a blocking decision point.
- Intercept
Resp - Ext→host intercept reply.
- Notify
Msg - Ext→host UI toast / footer status.
- Register
Command - Registers a slash command.
- Register
Tool - Registers an LLM tool;
schema_jsonis opaque JSON Schema bytes. - Session
Meta - Host→ext session identity push.
- Subscribe
- Declares event / intercept interests.
- Tool
Invoke - Host→ext for a registered tool.
- Tool
Result Msg - Ext→host tool outcome.
Enums§
- Error
- PXB protocol errors.
- Event
- Lifecycle event codes (compact on the wire; strings only at SDK edges). Append-only: never reuse a code. Unknown codes are ignored by peers that did not Subscribe to them.
- Frame
Type - Frame types with an explicit
Unknownarm so peers that do not understand a type still skip the frame by length.
Constants§
- CAP_
COMMANDS - Capability bits advertised in Hello.
- CAP_
EVENTS - CAP_
INTERCEPT - CAP_
TOOLS - FLAG_
HAS_ ID - Flag bits in the header.
- HEADER_
SIZE - Fixed frame header length.
- MAGIC
- Frame magic:
P X B+ version byte. - MAX_
PAYLOAD - Maximum payload accepted from a peer (16 MiB).
- PROTOCOL_
VERSION - Negotiated in Hello / HelloAck; bump only for incompatible renames.
- TYPE_
COMMAND_ INVOKED - TYPE_
COMMAND_ RESPONSE - TYPE_
EVENT - TYPE_
HELLO - TYPE_
HELLO_ ACK - TYPE_
HOST_ REQUEST - TYPE_
HOST_ RESULT - TYPE_
INTERCEPT - TYPE_
INTERCEPT_ RESPONSE - TYPE_
NOTIFY - TYPE_
READY - TYPE_
REGISTER_ COMMAND - TYPE_
REGISTER_ TOOL - TYPE_
SESSION_ META - TYPE_
SHUTDOWN - TYPE_
SHUTDOWN_ ACK - TYPE_
SUBSCRIBE - TYPE_
TOOL_ INVOKE - TYPE_
TOOL_ RESULT - WIRE_
BYTES - u32 length + bytes.
- WIRE_
U64 - 8-byte little-endian (u16/u32/bool/event codes share this kind).
Functions§
- decode_
command_ invoked - decode_
command_ response - decode_
event_ notify - decode_
header - Parses a 16-byte header.
- decode_
hello - decode_
hello_ ack - decode_
host_ request - decode_
host_ result - decode_
intercept_ req - decode_
intercept_ resp - decode_
notify - decode_
register_ command - decode_
register_ tool - decode_
session_ meta - decode_
subscribe - decode_
tool_ invoke - decode_
tool_ result - encode_
command_ invoked - encode_
command_ response - encode_
event_ notify - encode_
header - Encodes a header into a fixed-size array.
- encode_
hello - encode_
hello_ ack - encode_
host_ request - encode_
host_ result - encode_
intercept_ req - encode_
intercept_ resp - encode_
notify - encode_
register_ command - encode_
register_ tool - encode_
session_ meta - encode_
subscribe - encode_
tool_ invoke - encode_
tool_ result - read_
frame - Reads one frame from
rinto an owned body. - walk_
fields - Calls
ffor each field; unknown tags should be skipped via the reader.fmay callu64/bytesexactly once for the current field, orskip. - write_
frame - Writes header + body to
w, then flushes. Stdout is line-buffered, and PXB frames contain no newlines, so skipping the flush would leave small frames stuck in the buffer.