I have all the information needed. Both crates are single-file lib.rs modules, and I've cross-checked the opcode constants, the Guest API, the guest component state, and the WIT world to confirm what exists and what does not.
Architecture Investigation Report
1. pathland-view — crates/pathland-view/src/lib.rs
Node struct (lines 126–166)
pub struct Node { // line 127
pub id: u32, // line 129 — stable node id (0 until assign_ids)
pub component: Component, // line 130
pub children: Vec<Node>, // line 131 — owned recursive children
pub width: Option<f32>, // line 133 — fixed width, None = native size
pub height: Option<f32>, // line 135 — fixed height, None = native size
pub properties: BTreeMap<u16, u32>, // line 138 — propertyId → value (STYLE:SET_PROPERTY)
}
- Derives
Clone, PartialEq (line 126); manual Debug at lines 141–152.
- Retained-tree representation: a plain owned recursive
Vec<Node>. There is no arena, no parent pointers, no weak handles — it is a value tree.
Node::from_component(component) (lines 154–166) builds an empty node (id 0, no children, no properties).
Component enum (lines 170–182)
Exactly five variants, all structural (no layout math):
pub enum Component { // line 171
VStack, // line 173
HStack, // line 175
Text { text: String }, // line 177
Button { label: String }, // line 179
Spacer, // line 181
}
Derives Clone, PartialEq; manual Debug at 184–194. Note: Component only covers 5 of the 12 protocol component types (the protocol also defines IMAGE, SWITCH, TEXT_FIELD, SCROLLVIEW, LIST, GRID, COMMENT — see constants.rs lines 104–111, none of which have DSL wrappers here).
component_type_id(&Component) -> u16 (lines 197–205) maps variants to protocol IDs: VStack→VSTACK(0x0002), HStack→HSTACK(0x0001), Text→TEXT(0x0003), Button→BUTTON(0x0004), Spacer→SPACER(0x0008).
value_type_for(prop) -> u8 (lines 211–218): COLOR/BACKGROUND_COLOR/BORDER_COLOR → value_type::COLOR(0x07), everything else → F32.
Traits
pub trait View { // line 226
fn build(&self) -> Node; // line 228
}
pub trait ViewModifier { // line 235
fn apply(&self, node: &mut Node); // line 237
}
Modified<V, M> wrapper (lines 241–253)
pub struct Modified<V, M> { // line 242
view: V,
modifier: M,
}
impl<V: View, M: ViewModifier> View for Modified<V, M> { // line 247
fn build(&self) -> Node {
let mut node = self.view.build(); // line 249 — inner view first
self.modifier.apply(&mut node); // line 250 — then modifier mutates it
node
}
}
Critical semantics: modifiers do not create wrapper tree nodes. Modified::build builds the inner node and then mutates the same node's properties BTreeMap in place. Tree depth in Node corresponds only to VStack/HStack nesting — modifier chains are flattened into the property map. Ordering is innermost-first (test at lines 655–663 proves padding(4).padding(8) → 8 wins).
ViewExt sugar (lines 261–293)
Blanket trait on View + Sized; blanket impl for all T: View at line 293. Methods:
modifier<M: ViewModifier>(self, m: M) -> Modified<Self, M> (line 263)
spacing(f32) (line 268) → Spacing
padding(f32) (line 273) → Padding
font_size(f32) (line 278) → FontSize
color(u32) (line 283) → Color
background(u32) (line 288) → Background
There is no .width()/.height() sugar — those Node fields exist but can only be set by direct Node construction (they are never set via the DSL path or the WIT builder).
Core modifiers (lines 300–348)
All are tuple structs implementing ViewModifier::apply by inserting into node.properties:
Spacing(pub f32) → property_id::SPACING (0x0001), stored as f32::to_bits() (lines 319–323)
Padding(pub f32) → PADDING (0x0004), to_bits() (325–329)
FontSize(pub f32) → FONT_SIZE (0x1007), to_bits() (331–336)
Color(pub u32) → COLOR (0x100A), raw 0xAARRGGBB (338–342)
Background(pub u32) → BACKGROUND_COLOR (0x1001), raw (344–348)
Core components and build() collapse
VStack (lines 355–395): field children: Vec<Box<dyn View>> (line 356); View::build (384–395) produces Node { id: 0, component: Component::VStack, children: self.children.iter().map(|c| c.build()).collect(), ... } — each child Box<dyn View> is collapsed to a Node recursively. This is where Box<dyn View> (compile-time view graph) becomes Vec<Node> (retained value tree).
HStack (398–438): identical pattern.
Text (441–466): text: String; build → Component::Text { text: clone }.
Spacer (469–496): unit struct; build → Component::Spacer.
Button (499–524): label: String; build → Component::Button { label: clone }.
- Free functions:
text(&str) (112–114), spacer() (117–119), button(&str) (527–529).
vstack!/hstack! macros (lines 94–109)
macro_rules! vstack { // line 95
($($child:expr),* $(,)?) => {
$crate::VStack::new().children($crate::vec![$($crate::Box::new($child)),*])
};
}
hstack! is identical (lines 104–109). They box children automatically so children can be heterogeneous (Text, HStack, Modified, custom Views). Box and vec! are re-exported at crate root for hygiene (lines 71, 73).
assign_ids (lines 539–545)
pub fn assign_ids(root: &mut Node, next: &mut u32) { // line 539
root.id = *next;
*next += 1;
for child in &mut root.children {
assign_ids(child, next); // line 543
}
}
Pre-order, root first, sequential (test at 666–673: root=1, children=2,3, grandchild=4). The doc comment (lines 536–538) states the contract: the engine keys its diff snapshot by node id, so call this after building the (possibly re-built) tree and before Engine::emit. Stable ids across rebuilds are the entire basis for diffing.
2. pathland-engine — crates/pathland-engine/src/lib.rs
SnapshotNode (lines 60–71)
struct SnapshotNode { // line 61
component_type: u16, // line 62
parent: Option<u32>, // line 63
index: usize, // line 64
text: Option<String>, // line 65
width: Option<f32>, // line 66
height: Option<f32>, // line 67
properties: BTreeMap<u16, u32>, // line 68
gen: u64, // line 69 — generation stamp for deletion detection
}
This is the only state the engine remembers — it is a snapshot of the last emitted tree, not the application state.
Engine struct (lines 89–94)
pub struct Engine { // line 89
snapshot: Vec<Option<SnapshotNode>>, // line 91 — nodeId → last emitted state (preallocated slab)
gen: u64, // line 93 — monotonic generation stamp for current pass
}
new() at 103–108; Default at 96–100. tracked_nodes() diagnostic at 135–137. slot_mut(id) (139–145) resizes the slab on demand (allocation only when the tree grows).
emit() — diff-based reactive emission (lines 111–132)
pub fn emit(&mut self, root: &Node, guest: &mut Guest<'_>)
-> Result<EmitResult, pathland_opcode::RingError> { // line 111
self.gen = self.gen.wrapping_add(1); // line 116
let mut out = 0usize;
self.reconcile_node(root, None, 0, guest, &mut out)?; // line 118
// Delete sweep: any snapshot slot not visited this pass has stale gen. // 121–130
for (id, slot) in self.snapshot.iter_mut().enumerate() {
if let Some(s) = slot {
if s.gen != self.gen {
guest.delete_node(id as u32)?; // line 125 DELETE_NODE
*out += 1;
*slot = None; // line 127
}
}
}
Ok(EmitResult { opcodes: out }) // line 131
}
Algorithm: bump generation → reconcile the passed tree recursively → sweep the snapshot slab for any entry whose generation stamp was not refreshed (i.e., not visited this pass) and emit DELETE_NODE. EmitResult (lines 78–82) just counts opcodes.
reconcile_node (lines 147–296)
Signature (lines 148–155): (&mut self, node: &Node, parent: Option<u32>, index: usize, guest: &mut Guest<'_>, out: &mut usize).
Preliminaries:
component_type = component_type_id(&node.component) (line 156).
text_borrowed: text of Text or label of Button, else None (lines 158–162).
let prev = self.slot_mut(node.id).take() (line 164) — pops the previous snapshot entry; prev == None means "new node", prev == Some(p) means "existing node". This is the entire new-vs-existing detection mechanism.
props_changed (165–168) and text_changed (170–173) computed without cloning.
New-node arm (None, lines 177–193) emits, in order:
guest.create_node(node.id, component_type) → TREE:CREATE_NODE (line 178)
guest.insert_child(p, node.id, index) → TREE:INSERT_CHILD (only if parent exists, line 181)
- Every property:
guest.set_property(node.id, prop, value_type_for(prop), value) → STYLE:SET_PROPERTY (lines 184–187)
emit_size_props → WIDTH/HEIGHT properties (line 188; helper at 299–324)
guest.set_text(node.id, t) → STYLE:SET_TEXT for Text/Button (lines 189–192)
Existing-node arm (Some(p), lines 194–255), diffed against the snapshot:
- Parent moved (
p.parent != parent, lines 196–204): emits REMOVE_CHILD(old, node.id) if there was an old parent, then INSERT_CHILD(new, node.id, index) if there is a new one.
- Reordered (
p.index != index within same parent, lines 205–210): emits MOVE_CHILD(par, node.id, index) → TREE:MOVE_CHILD.
- Component type changed (lines 212–215): re-emits
CREATE_NODE (protocol has no "change type" opcode — the host recreates the element).
- Text changed (lines 217–222):
SET_TEXT.
- Width/height diffs (lines 224–245):
SET_PROPERTY with WIDTH/HEIGHT. Note: only the Some → Some change direction emits; clearing a size (Some → None) emits nothing.
- Property map diff (lines 246–254): iterates
node.properties and emits SET_PROPERTY only for keys where p.properties.get(prop) != Some(value). Removed properties emit no opcode (no "unset property" exists in the protocol).
After the match, the new snapshot entry is stored (lines 261–290):
- Zero-alloc optimization: if
props_changed/text_changed were false, the previous BTreeMap/String allocations are moved back into the new SnapshotNode (lines 264–271); only changed fields are freshly allocated (273–280). This is what makes steady-state emission allocation-free.
- Snapshot written with
gen: self.gen (lines 281–290).
- Recursion into children:
reconcile_node(child, Some(node.id), i, ...) (lines 292–294).
Does the engine hold a retained tree?
No. Engine holds only the diff snapshot (snapshot slab + gen). The caller passes a fresh &Node to emit on every frame (line 112). The retained tree lives elsewhere:
- In the native Rust path: in the application (e.g., a struct holding a
Node the app mutates and re-passes).
- In the WASM guest path (
crates/pathland-guest/src/lib.rs): the retained tree is a flat id-indexed map State.nodes: HashMap<u32, FlatNode> (lines 42–48, 56). Every emit() call re-materializes the entire Node tree from scratch via materialize(&s.nodes, root_id) (lines 109–120, called at line 214), then passes it to engine.emit(&root, &mut ring). So the full tree is re-authored from the flat builder state on every emit — the DSL build() in the pure-Rust path similarly rebuilds a fresh Node from the view structs each time.
Dependency tracking: Does not exist
I searched all crates for signal|dirty|depend|invalidate|memo|reactive|subscribe. Findings:
- The only hits in
pathland-engine are doc comments:
- Lines 15–16: "This is what signals in the application drive — a signal marks the tree dirty;
emit reconciles."
- Line 84:
/// The diff-based reactive emitter.
- These are aspirational documentation only — there is no signal type, no
dirty flag, no dependency graph, no memoization, no invalidation mechanism, no subscription primitive anywhere in the crate (or in pathland-view, pathland-guest, or the WIT world wit/pathland.wit).
- The guest's
builder interface (create-node, insert-child, remove-child, set-text, set-property) mutates the flat map with no change tracking — create_node even unconditionally clears all properties (guest lib.rs lines 135–144).
AGENTS.md confirms this is a known gap: "Planned / deferred: signals (Goal 2/#13)".
Conclusion: Reconciliation is pure tree-to-snapshot structural diffing of two full trees. There is no link from application state to view nodes. The application (or WIT host) is responsible for (a) detecting that something changed, (b) mutating the retained tree (Node properties / flat map), (c) re-authoring/re-passing the full &Node, and (d) calling emit. The engine only diff-compares the freshly passed tree against what it last emitted, keyed by stable node ids assigned via assign_ids / host-supplied ids. An unchanged tree emits exactly zero opcodes (test, lines 502–512), and steady-state emission provably performs zero heap allocations (test, lines 515–563).
Opcode coverage summary
The engine emits exactly these protocol commands (constants in pathland-opcode/src/constants.rs):
TREE:CREATE_NODE (0x01, line 23), TREE:DELETE_NODE (0x02, line 25), TREE:INSERT_CHILD (0x03, line 27), TREE:REMOVE_CHILD (0x04, line 29), TREE:MOVE_CHILD (0x05, line 31)
STYLE:SET_PROPERTY (0x01, line 37), STYLE:SET_TEXT (0x03, line 41)
- All funneled through the
Guest API in pathland-opcode/src/lib.rs (lines 151–201).