burokku docs plan node-ref.md

NodeRef #

Status: design. Lands in a pull request of its own, ahead of feature-slot-events.md, which then only uses it.

Decided marks what the design settled; Proposed marks recommendations that still need a decision; Open marks questions without a decision yet.

Why #

Engine code and features refer to nodes they do not own, and today each reference is its own arrangement:

Reference How it is kept What goes wrong
a NodeId stored in a slot or in engine state nothing: NodeId is Copy the node is freed and the id goes stale with no notice; get returns None one day, and nothing kept the node alive meanwhile
the resize observer's targets UITree::resize_observers.retained_targets, read by name from reclaim_unreachable_detached engine code names a feature; every feature that needs the same would add a call of its own
script's wrappers the wrapper roots reclaim_unreachable_detached takes fine, but it is the only general mechanism, and it is script's

feature-slot-events.md needs one more: a slot or a tree feature that refers to another node, such as the inspector's hovered and selected nodes. Rather than a fourth arrangement, this plan makes one that the others move onto.

Goals:

  • One register of who refers to which node, and one walk that decides what stays alive.
  • A reference that tells its holder when the node goes.
  • A reference that cannot be forgotten: releasing it is dropping it.
  • No cycle leaks.

Decided #

Settled on 2026-10-19:

  1. A strong reference is an edge, not a root. It keeps its target alive only while its holder is reachable. Reclamation walks from the roots over children and strong references, so two detached nodes that retain each other reclaim together.
  2. Release is Drop. Dropping a NodeRef releases it; there is no release(). Its Drop posts a ticket to the tree's box, which the tree drains at its next settle or reclamation.
  3. Its own pull request, ahead of the feature slot series. It serves more than slots: the resize observer's targets and any engine reference to a node become entries in the same register.
  4. NodeRef is Clone, and a clone is a new entry. Cloning takes a new ticket and posts the clone to the tree's box, which copies the entry when it drains: same holder, same target, same strength. So a NodeRef can live in state that is cloned. A clone keeps its holder: a slot that wants a reference of its own calls cx.retain or cx.watch rather than taking another holder's clone, which is a review rule.
  5. Weak is watching, strong is owning, and only weak is built now. A weak reference watches a node its holder does not own: the node lives and dies by the tree's own rules, and the holder hears Released when it goes. A strong reference owns a node outside the tree: the holder keeps it alive, as a portal keeps the content it draws elsewhere. No holder owns a detached node today. The resize observer's targets become weak: a detached component nothing wraps can never be reattached or laid out, so keeping it alive only kept a registration that would never fire. retain, Strength, and the walk of Decided 1 are designed here and built with the first owner. See Strong and weak.
  6. The engine holds its references the same way, and needs no notification. Focus, the hover path, the pressed target, pointer capture, and the pointer grab in Interaction are weak NodeRefs under a fixed holder, EngineHolder::Interaction. A freed node's reference resolves to None, which is all the engine needs: focused() reads None as no focus, the hover path is rebuilt on the next move, and an overwritten field drops its old reference. So Interaction::forget, and PointerState::forget and FocusState::forget under it, are deleted, not replaced, and the engine ignores Released. One rule with no exception: whoever stores a node across events holds a NodeRef.

What it is #

NodeRef is not an Rc. The tree owns every node, in an arena, and a NodeRef is a ticket for one entry in the tree's register of who refers to whom. "Strong" is not a property of the ticket: it is whether the walk follows that entry.

/// One ticket, one entry. `Clone`, not `Copy`: a clone is a second entry
/// under a new ticket.
#[must_use]
pub struct NodeRef {
    target: NodeId,
    ticket: Ticket,
    ops: Rc<OpBox>, // the tree's; see Release
}

impl Clone for NodeRef {
    fn clone(&self) -> Self {
        let ticket = self.ops.next_ticket(); // touches no tree
        self.ops.post(Op::Cloned { from: self.ticket, to: ticket });
        Self { target: self.target, ticket, ops: Rc::clone(&self.ops) }
    }
}

impl Drop for NodeRef {
    fn drop(&mut self) {
        self.ops.post(Op::Dropped(self.ticket)); // touches no tree
    }
}
Rc / Weak NodeRef strong / weak
Owns the memory the Rc the tree
Released by drop drop, posted to the tree
Copied by Rc::clone: the count goes up clone: a new ticket, posted to the tree, with the same holder and strength
Strong means count up; freed at zero the walk follows the edge: the target lives while the holder does
Weak means no count; upgrade may fail the walk skips the edge; get may be None, and the holder hears Released first
Cycles leak reclaim, since it is a walk, not a count
Told when the target goes no yes, Released

Why not a lifetime: a NodeRef<'tree> would end with the borrow, and a reference lives across events in state the tree owns. Why not an Rc: nodes are arena entries, not allocations of their own, and counts leak on cycles where a walk does not.

Strong and weak #

Decided (1 and 5). The question a holder answers is whether the node should stay alive because of it:

Weak Strong
The holder watches the node owns the node
Keeps the target alive no: the tree's own rules decide while the holder is reachable
When the target is freed the holder hears Released, and get is None from then on not while the holder lives; the holder's drop, or forget_holder, lets it go
Made with cx.watch(node) cx.retain(node)
For a node the feature points at: the inspector's hovered and selected nodes, a drag's anchor, a reveal target, and focus and hover if they move here a subtree the feature keeps outside the tree: a portal's content, a template's content, a recycled list row
Today the resize observer's targets, the inspector nobody; built with the first owner

The rule for choosing: if removing the node from the tree should still free it, the reference is weak. Only a holder that would reattach the node, or draw it itself, owns it. A devtool never owns an app's node, and an observer that only reports on a node does not either.

The register #

Proposed. Strength::Strong and retain land with the first owner (Decided 5); the register keeps room for them.

/// Who refers to a node: a node's slot of one type, a tree feature, or the
/// engine.
enum Holder { Slot(NodeId, SlotTypeIndex), Feature(FeatureIndex), Engine(EngineHolder) }

/// The engine's holders. `Interaction` stays; `ResizeObservers` goes once
/// the resize observer is a tree feature (phase 3, step 6).
enum EngineHolder { Interaction, ResizeObservers }

enum Strength { Strong, Weak }

struct References {
    /// Who refers to this node, and how.
    by_target: SecondaryMap<NodeId, Vec<(Ticket, Holder, Strength)>>,
    /// What this holder registered, so it can be cleared when the holder
    /// goes. A set, so dropping one of many references finds it at once.
    by_holder: HashMap<Holder, HashSet<Ticket>>,
    /// What `NodeRef`s did since the last drain, drops and clones in
    /// order, and the ticket counter, so that `clone` needs no tree. A
    /// `Cell<Vec<Op>>`, taken and put back, as the no-`RefCell` rule asks
    /// at `&self` edges.
    ops: Rc<OpBox>,
}

impl UITree {
    pub(crate) fn watch(&mut self, holder: Holder, target: NodeId) -> NodeRef;
    pub(crate) fn retain(&mut self, holder: Holder, target: NodeId) -> NodeRef;
    /// The target, while it exists. Reads only the generation-checked id,
    /// never the register, so a clone resolves before the drain.
    pub(crate) fn get(&self, node_ref: &NodeRef) -> Option<NodeId>;
    /// Removes every entry of `holder`; a ticket dropped later is a no-op.
    pub(crate) fn forget_holder(&mut self, holder: Holder);
    /// Applies the box in order: copies the entry of each clone to its new
    /// ticket, and removes the entries whose tickets were dropped. Called
    /// at the start of settling and of reclamation.
    pub(crate) fn drain_ref_ops(&mut self);
}

by_target is a SecondaryMap, so a node that reuses a freed node's arena slot never sees the freed node's entries, as with slots.

Liveness #

Decided (1 and 5). The roots are:

  1. the connected tree;
  2. the detached nodes script still wraps, as reclaim_unreachable_detached takes them today;
  3. the tree features, which live as long as the tree.

With only weak references, the walk is today's: the roots' components stay and every other detached component goes. reclaim_unreachable_detached loses its observer_targets input, since the resize observer's targets are weak entries that keep nothing alive.

With the first owner, the walk also follows strong references from the roots over children. What it does not reach is freed. So:

  • Two detached nodes whose slots retain each other are reached by nothing, and both go.
  • A detached node script still wraps keeps alive what its slots retain, and once script lets the wrapper go, so does what it retained.
  • A tree feature's strong reference acts as a root in effect, since the feature is one, which is what the inspector and the resize observer want.

reclaim_unreachable_detached becomes this walk then.

Release #

Decided (2 and 4). Drop posts the ticket to the box, and clone posts the new ticket with the one it was cloned from; the tree drains the box in order at the start of settling and of reclamation, copying each clone's entry and removing each dropped ticket's. A clone dropped before the drain is copied and removed in the same drain; a clone of a ticket whose entry is already gone, because its holder was forgotten or its target freed, is a dead ticket, as the original is. Until the drain, a strong target released this way stays, which is reclamation's own cadence and changes nothing observable. Overwriting a field holding a NodeRef releases the old one, so a feature that tracks a changing node, such as the inspector's hovered node, cannot forget to.

When a holder goes, a slot detached or freed, or an engine holder shut down, the tree calls forget_holder, so a ticket dropped afterwards finds no entry and is a no-op.

Notification #

When a node is freed, release_freed looks up by_target and tells each holder once per entry, with the NodeRef that now resolves to nothing:

Holder Hears
a slot the released hook now, SlotEvent::Released(NodeRef) once feature-slot-events.md lands
a tree feature TreeEvent::Released(NodeRef), once tree features exist
the resize observer registry Released for the target: it drops the target's registrations and wakes their observers, as Observations' Drop does today
Interaction nothing: a dead reference reads as None, and that is the right answer for focus, hover, capture, and the grab (Decided 6)

The holder's own entries for the freed node are removed at the same time, so the NodeRef it still holds is a dead ticket, dropped whenever the holder lets it go.

Open: whether the notification runs inside release_freed, with the tree borrowed, or queues for the next step. A slot's released hook runs with the tree borrowed today, as its other hooks do, so inside is the simpler first answer.

Who moves onto it #

Today After
resize_observers.retained_targets, read by name in reclaim_unreachable_detached weak entries held by the registry; reclamation takes no observer input, and Released drops the registrations (Decided 5)
Observations keeping targets alive through the registry the registry's entries; Observations keeps its registrations only
a slot or engine state storing a NodeId across events a NodeRef; storing a NodeId across events becomes a review rule to refuse, since NodeId is Copy and silent
Interaction::forget, with PointerState::forget and FocusState::forget, called from reclamation deleted: Interaction's fields are weak NodeRefs that read as None once freed (Decided 6)
feature-slot-events.md's cx.watch, cx.retain, and cx.release cx.watch on the slot and feature contexts, returning a NodeRef; cx.retain with the first owner; no release

Order #

Proposed. One pull request, in commits:

  1. The register, watch, get, forget_holder, and tests on a tree with no script.
  2. The box: Drop, Clone, drain_ref_ops at settling and reclamation, a test that overwriting a NodeRef releases the old one, and one that a clone keeps resolving after the original is dropped.
  3. Notification: release_freed tells holders, through a slot's released hook for now.
  4. The resize observer onto the register: retained_targets goes, reclaim_unreachable_detached takes no observer input, and Released drops a freed target's registrations.
  5. Interaction onto the register: focus, the hover path, the pressed target, pointer capture, and the grab as weak NodeRefs under EngineHolder::Interaction; forget and its callers go; a test that a freed focused node reads as no focus with nothing told.
  6. Later, with the first owner: retain, Strength::Strong, and the walk that follows strong entries from the roots; tests for the cycle, for a wrapped node keeping what it retains, and for release freeing what nothing else reaches.

Open questions #

  1. Whether Released runs inside release_freed or queues; see Notification.