I now have a complete picture. Here is the detailed report.
Architecture Investigation: Pathland Opcode + Transport + Host
Summary of the current transport mechanism
There are two concrete transport paths, both producing/consuming the same pathland_opcode::Frame view type:
- Shared linear-memory ring (local/desktop, zero-copy):
Guest (producer) writes 16-byte opcodes + arena data into a fixed region of linear memory; Host (consumer) drains completed frames. Both live in pathland-opcode.
- Network batch (SSR → browser):
BatchEncoder/Batcher serialize opcodes + an arena delta into a binary batch; BatchDecoder deserializes and mirrors the arena, yielding a Frame.
There is NO transport abstraction trait. The polymorphism is achieved through a concrete type, pathland_opcode::Frame — explicitly documented as "the transport seam" (lib.rs:227-229, transport/src/lib.rs:13-16). The only traits in the workspace are pathland-view's View/ViewModifier/ViewExt, pathland-wit-host's AppHandler, and generated WIT Guest world traits. RenderTree::apply_frame accepts a &Frame, so ring-sourced frames and network-batch frames flow through the identical decode path.
1. pathland-opcode (crates/pathland-opcode/src/)
no_std + alloc, no runtime dependencies (Cargo.toml:11).
opcode.rs — the 16-byte Opcode
Opcode struct (opcode.rs:23-38): #[repr(C, packed)], #[derive(Clone, Copy, Debug, PartialEq, Eq)]. Fields in wire order:
pub category: u8 (line 27)
pub command: u8 (line 29)
pub flags: u16 (line 31)
pub a: u32 (line 33), pub b: u32 (line 35), pub c: u32 (line 37)
OPCODE_SIZE: usize = 16 (line 15), Opcode::SIZE const (line 42).
new(category, command, flags, a, b, c) -> Self (const fn, lines 46-55).
- Accessors use
ptr::addr_of!().read_unaligned() because the struct is packed: category() (59-61), command() (65-67), flags() (71-74), a() (78-81), b() (85-88), c() (92-95). Float reinterpretations a_f32/b_f32/c_f32 (99-113).
to_bytes(&self) -> [u8; OPCODE_SIZE] (lines 116-130): field 0 = category, 1 = command, then little-endian flags at [2..4], a at [4..8], b at [8..12], c at [12..16].
from_bytes(bytes: &[u8; OPCODE_SIZE]) -> Self (lines 133-148): byte-slices with u16/u32::from_le_bytes.
constants.rs — wire protocol constants
- Categories (
category mod, lines 7-18): TREE = 0x01, STYLE = 0x02, EVENT = 0x03, META = 0x04.
- TREE commands (
tree mod, lines 21-32): CREATE_NODE=0x01, DELETE_NODE=0x02, INSERT_CHILD=0x03, REMOVE_CHILD=0x04, MOVE_CHILD=0x05, each with payload-field docs.
- STYLE commands (
style mod, lines 34-42): SET_PROPERTY=0x01 (B=(valueType<<16)|propertyId), SET_DESIGN_TOKEN=0x02, SET_TEXT=0x03.
- EVENT commands (
event mod, lines 44-60): POINTER_DOWN=0x01, POINTER_MOVE=0x02, POINTER_UP=0x03, KEY_DOWN=0x04, KEY_UP=0x05.
- META commands (
meta mod, lines 62-68): RESET=0x01, ENVIRONMENT=0x02.
- Flags (
flag mod, lines 70-82): INSERT_APPEND=0x0001, POINTER_SECONDARY=0x0001, HOVER_ENTER=0x0001, HOVER_LEAVE=0x0002, KEY_REPEAT=0x0002.
- Value types (
value_type mod, lines 84-94): U8=0x01, U32=0x02, I32=0x03, F32=0x04, STRING=0x05, ENUM=0x06, COLOR=0x07, DESIGN_TOKEN=0x08.
- Component types (
component_type mod, lines 96-112): HSTACK=0x0001, VSTACK=0x0002, TEXT=0x0003, BUTTON=0x0004, IMAGE=0x0005, SWITCH=0x0006, TEXT_FIELD=0x0007, SPACER=0x0008, SCROLLVIEW=0x0009, LIST=0x000A, GRID=0x000B, COMMENT=0x000C.
- Property IDs (
property_id mod, lines 114-154): stack (SPACING=0x0001…CONTENT_MARGINS=0x0005), text (TEXT=0x000A, LINE_LIMIT=0x000B, TEXT_ALIGNMENT=0x000C, TRUNCATION_MODE=0x000D), style ( … ), semantic (, , , ).
ring.rs — producer-side ring
RingError (lines 10-15): single variant Full.
push(slots: &mut [u8], header: &mut [u8], mask: usize, op: &Opcode) -> Result<(), RingError> (lines 22-33):
- reads
writeCursor (masked, line 23), computes next = (write + 1) & mask (line 24);
- returns
Err(RingError::Full) when next == readCursor (lines 25-27) → effective capacity is slot_count - 1;
- writes 16 bytes at
write * 16 (lines 28-30) and advances writeCursor (line 31).
cursor(header, off, mask) -> usize (lines 37-39): masked u32 read.
push_* helpers, all pub(crate), all delegating to push:
push_create_node (42-62, TREE/CREATE_NODE, B=componentType),
push_delete_node (64-76),
push_insert_child (78-99),
push_remove_child (101-121),
push_move_child (123-144),
push_set_property (146-168, packs (valueType<<16)|propertyId into B),
push_set_text (170-190, B=arenaRef),
push_reset (192-203).
memory.rs — linear memory layout
- Constants:
MAGIC = 0x504C_504C "PLPL" (line 9), PROTOCOL_VERSION: u16 = 1 (line 11), DEFAULT_SLOT_COUNT = 4096 (line 14), DEFAULT_ARENA_BYTES = 1 << 20 (1 MiB, line 16).
- Header offsets (lines 19-42), a 64-byte header:
OFF_MAGIC=0x00, OFF_VERSION=0x04, OFF_RING_OFFSET=0x06, OFF_RING_BYTES=0x0A, OFF_SLOT_COUNT=0x0E, OFF_ARENA_OFFSET=0x12, OFF_ARENA_BYTES=0x14, OFF_ARENA_CURSOR=0x18 (guest-owned), OFF_READ_CURSOR=0x1C (host-owned), OFF_WRITE_CURSOR=0x20 (guest-owned), OFF_FRAME_COUNT=0x24 (guest-owned), HEADER_SIZE=0x40 (64 bytes).
MemoryLayout (lines 45-51): { slot_count: usize, arena_bytes: usize }; Default = 4096 slots / 1 MiB arena (53-60). Methods: ring_bytes() (65-67), total_bytes() (70-72 = HEADER + ring + arena), ring_offset() (76-78 = HEADER_SIZE), arena_offset() (82-84 = after ring), ring_mask() (88-91, power-of-two wrap).
pub(crate) mod header (98-150): LE get_u32/set_u32/get_u16 (102-114), init(buf, layout) (117-130) writes magic/version/region offsets/zero cursors, validate(buf, layout) (135-149).
arena.rs — bump arena (variable-length data)
ArenaError (lines 10-16): OutOfSpace, InvalidOffset.
alloc(data, header, bytes) -> Result<u32, ArenaError> (lines 22-33): entry format [u32 byteLength][bytes...]; cursor lives at OFF_ARENA_CURSOR; checks bounds (lines 26-28); returns the byte offset as arenaRef.
alloc_str (37-43), get(data, offset) -> Result<&[u8], ArenaError> (47-57), get_str (61-64).
lib.rs — Guest, Frame, Host, frameCount framing
init_memory(mem, layout) (lines 77-79) — call once before Guest/Host.
Guest<'a> producer (struct lines 99-105): { header, slots, arena: &'a mut [u8], frame_start: u32 }.
new(mem, layout) (110-120) — splits memory into header/ring/arena disjoint slices.
begin_frame() (127-129) — snapshots writeCursor into frame_start.
pending() (132-136) — opcodes since begin_frame ((write - frame_start) & mask).
end_frame() (139-142) — increments frameCount in the header (this is the frame boundary publication).
frame_count() (145-147).
- TREE:
create_node (151-153), delete_node (155-157), insert_child (159-161), remove_child (163-165), move_child (167-174).
- STYLE:
set_property(node_id, property_id, value_type, value) (178-194), set_text(node_id, text) -> Result<u32, RingError> (197-201, allocates arena then pushes; maps ArenaError→RingError::Full), raw alloc/alloc_str (204-211).
- META:
reset() (215-217).
Frame<'a> (struct lines 230-235): { slots: &[u8], arena: &[u8], start: usize, end: usize } — the transport seam (doc comment lines 227-229). Constructed by transport backends via Frame::from_parts(slots, arena, start, end) (lines 243-250). Methods: opcodes() (253-259), len() (262-264), is_empty() (266-268), arena_get(offset) (271-273), arena_str(offset) (276-278), arena() (281-283, full arena slice for delta serialization).
FrameIter (lines 287-304): yields Opcode by decoding each contiguous 16-byte slot.
Host<'a> consumer (struct lines 312-318): { header: &mut [u8], slots: &[u8], arena: &[u8], consumed: u32 }.
new(mem, layout) (322-331).
frame_count() (334-336).
- (342-368): returns empty if ; otherwise reads →, spanning all new opcodes (multiple guest frames coalesce), advances (line 365) and (line 366). If the ring wrapped () it drains nothing (line 354-356) — the POC producer flushes before the ring fills.
events.rs — Event enum (host → guest raw inputs)
Event (lines 16-53): PointerDown { target, x, y, secondary }, PointerMove { target, x, y, hovering, leaving }, PointerUp { target, x, y, secondary }, KeyDown { target, key_code: u16, modifiers: u8, repeat }, KeyUp { target, key_code, modifiers }.
Event::encode(&self) -> Opcode (57-125) — maps to EVENT category opcodes with the flag_for helper (130-142).
EventError (146-151): NotAnEvent, UnknownCommand.
impl TryFrom<Opcode> for Event (153-195).
2. pathland-transport (crates/pathland-transport/src/)
std crate; sole runtime dep is pathland-opcode (Cargo.toml:12). Dev-deps: pathland-engine, pathland-host, pathland-view.
lib.rs — module surface
- Re-exports:
encode_batch, Batch, BatchDecoder, BatchEncoder (line 52), Batcher, BatchPolicy (line 53).
BATCH_MAGIC = 0x504C_504C (line 56), BATCH_VERSION: u16 = 1 (line 58).
direction mod (61-65): GUEST_TO_HOST = 0x0000, HOST_TO_GUEST = 0x0001 (reserved, events-over-network not implemented).
- Batch wire format documented at lines 18-28.
batch.rs — batch encode/decode
BATCH_HEADER: usize = 16 (line 9) = magic(4) + version(2) + flags(2) + frameCount(4) + opcodeCount(4).
BatchError (lines 12-24): Truncated, BadMagic, BadVersion, BadOpcodeCount, BadArenaLen.
Batch<'a> (lines 29-33): wraps frame: Frame<'a>, frame_count: u32, flags: u16. Delegates opcodes() (47-49), len() (52-54), is_empty() (56-58), plus frame_count() (61-63), flags() (66-68), as_frame() -> &Frame<'a> (71-73, the transport-seam conversion), arena_get() (76-78), arena_str() (81-83).
encode_batch(frame_count: u32, flags: u16, opcodes: &[Opcode], arena_delta: &[u8]) -> Vec<u8> (free function, lines 88-106): header fields LE, then 16×opcodeCount opcode bytes, then arenaDeltaLen (u32 LE), then arena bytes.
ParsedBatch<'a> (109-114) and parse(bytes) -> Result<ParsedBatch, BatchError> (117-163): validates magic/version, checked arithmetic for opcode payload and arena length.
BatchEncoder (struct lines 171-174): { last_arena_len: usize }, new() (178-180).
encode(&mut self, frame_count, opcodes, arena_used: &[u8]) -> Vec<u8> (187-204): scans for META::RESET and resets last_arena_len = 0 (188-194); computes delta = arena_used[last_arena_len..] (195-200, falling back to the full arena if the arena shrank); calls encode_batch with direction::GUEST_TO_HOST; updates last_arena_len (202).
arena_sent() (207-209).
BatchDecoder (struct lines 217-220): { arena: Vec<u8> } — a mirrored arena.
decode<'a>(&'a mut self, bytes: &'a [u8]) -> Result<Batch<'a>, BatchError> (232-254): parses; scans opcodes for META::RESET and clears the mirror (237-245); appends the arena delta to the mirror (line 246) so absolute arenaRef offsets stay valid; builds (line 248).
batching.rs — time/size batching policy
- Defaults:
DEFAULT_MAX_INTERVAL = 4 ms (line 17), DEFAULT_MAX_BATCH_BYTES = 1400 (line 19, "conservative network frame / MTU").
BatchPolicy (lines 22-28): { max_interval: Duration, max_batch_bytes: usize }. new (32-37), default_policy() (40-42), Default (45-49).
Batcher (struct lines 56-71): { policy, frame_count, opcodes: Vec<Opcode>, pending_arena: Vec<u8>, last_arena_len: usize, started: Option<Instant>, size_triggered: bool }.
new(policy) (75-85), default_policy() (88-90), is_empty() (93-95), pending_opcodes() (98-100), pending_bytes() (103-105, estimate incl. header + arenaLen field).
push_frame(&mut self, frame_count, opcodes, arena_used) -> bool (109-125): latches the first frame's frame_count and started timestamp (110-113); if the arena shrank (arena_used.len() < last_arena_len) it clears pending buffer + resets delta tracking (114-118); appends the arena delta (119-120); appends opcodes (121); sets size_triggered |= pending_bytes() >= max_batch_bytes (123) and returns it.
interval_elapsed() (128-133).
flush(&mut self, force: bool) -> Option<Vec<u8>> (139-159): returns None if empty (140-142); flushes when force || size_triggered || interval_elapsed() (143-146); encodes via encode_batch with GUEST_TO_HOST (147-152); resets all buffers/triggers (153-158).
How the arena delta works
The arena is an append-only bump buffer. Each batch carries only bytes appended since the previous batch:
- Producer side: the encoder/batcher records the arena's used length (
[0..cursor)) at the last emission; the delta is arena_used[last_arena_len..] (batch.rs:195-200, batching.rs:119-120). A META::RESET opcode (which zeroes the arena cursor) causes tracking to restart and the full used arena to be sent (batch.rs:188-194, batching.rs:114-118).
- Consumer side: the decoder maintains a mirror
Vec<u8> and appends each batch's delta (batch.rs:246), clearing it on RESET (batch.rs:243-245). Because the mirror grows identically to the producer's arena, absolute arenaRef offsets (referenced by STYLE::SET_TEXT's B field) resolve to the same bytes as in shared memory.
3. pathland-host (crates/pathland-host/src/lib.rs)
std crate; deps: pathland-opcode, pathland-engine (Cargo.toml:12-13). Re-exports both (lib.rs:13-14).
HostNode — decoded native-element node
HostNode (lines 23-32): { id: u32, component_type: u16, text: Option<String>, parent: Option<u32>, children: Vec<u32>, properties: HashMap<u16, u32> } (propertyId → raw u32 value).
Default (34-45): id=0, component_type=HSTACK.
RenderTree — the native-element description
RenderTree (lines 48-52): { nodes: HashMap<u32, HostNode> }, #[derive(Debug, Default)].
apply_frame(&mut self, frame: &Frame<'_>) (lines 56-121) — the heart of the consumer, a single match on (category, command):
TREE::CREATE_NODE (59-65): inserts node, sets id + component_type.
TREE::DELETE_NODE (66-68): removes by id.
TREE::INSERT_CHILD (69-79): appends to parent's children (dedup), sets child's parent.
TREE::REMOVE_CHILD (80-88): removes from parent's children, clears child's parent.
TREE::MOVE_CHILD (89-102): repositions child; u32::MAX/usize::MAX index means append.
STYLE::SET_PROPERTY (103-108): properties.insert(prop_id, op.c()) — values kept as raw u32 bit patterns (float values are reinterpreted by the renderer).
STYLE::SET_TEXT (109-113): resolves text via frame.arena_str(op.b()) (works for both ring and network frames).
STYLE::SET_DESIGN_TOKEN (114-117): no-op — token overrides are the renderer's theme layer's job.
node(id) (124-126), root() (129-131, id 0).
render_tree_from_frame(frame: &Frame<'_>) -> RenderTree (135-139): convenience wrapper.
describe(op: &Opcode) -> String (142-174): debug formatting of each opcode kind.
Note the consumer never sees a Batch directly — the batch decoder's Batch::as_frame() (batch.rs:71-73) is what makes the network path drop into apply_frame unchanged; the cross-crate test decoded_batch_feeds_render_tree (batch.rs:275-326) proves ring-sourced and network-sourced frames are interchangeable.
Direct answers to your investigation questions
-
Is there a transport abstraction trait? No. There is no trait Transport, trait Encoder, etc. anywhere in lib/rust. The abstraction seam is the concrete pathland_opcode::Frame type (lib.rs:227-235), constructed by both Host::frames() (lib.rs:342-368) and BatchDecoder::decode() (batch.rs:248), and consumed by RenderTree::apply_frame (pathland-host/src/lib.rs:56).
-
Shared-memory ring vs network batch today: Both exist and are live.
- Shared-memory:
Guest (producer) / Host (consumer) in pathland-opcode; used by the GTK demo and WIT host via Host::frames().
- Network batch:
BatchEncoder/Batcher (producer) + BatchDecoder (consumer) in pathland-transport; the WASM guest component (pathland-guest/src/lib.rs:225-239) drains the ring in take_batch() and feeds it through a BatchEncoder. Host-side, BatchDecoder is used in pathland-gtk-demo/src/main.rs:122 and pathland-wit-host/src/lib.rs:244/263.
-
Host → guest events over the network batch are not implemented: the direction::HOST_TO_GUEST bit is reserved (transport/src/lib.rs:65), and Event encode/decode in pathland-opcode is only exercised through the shared-memory opcode ring so far.