Status: accepted 2026-10-12 (Daniel; Claude’s review folded in — per-instance naming and the ext-api tag). Related: ADR-0020 Undirected pins in the flow model, ADR-0018 One frontend graph engine, Flow Editor, Julia and Lenticulum.
Context
ModelingToolkit systems nest (compose), and the MTK library (ADR-0020’s first consumer, in moonkale-julia) proved the pins model end to end — but every circuit is still one flat sheet. The ModelingToolkit editor needs hierarchy, and so does Lenticulum (a sub-graph as one factor). Claude’s review order put the MTK library first precisely so this design knows what nesting really needs.
What building the library taught: the panel already owns every shape decision (ADR-0018 §2) — the engine never learned what a “pin” or a “block kind” is; it draws nodes, ports and edges. That is the lever for hierarchy: the engine needs nothing at all.
Decision
- One document, nested. A subflow lives in the same
.flow.json—Flowgainssubflows: Vec<Subflow>(#[serde(default)], non-breaking like every field since the pins review), whereSubflow { id, name, blocks, wires, inputs, outputs, connections }reuses the ordinaryBlock/Wire/Portshapes. (Amended 2026-10-13, in stage A’s implementation: the sketch had oneportsfield, but §2 already says a boundary port is a directed input, an output or a domain-typed pin — the declaration mirrors aBlockKind’s three port lists, so the shape carries which.) A parent block references one byBlock.subflow: Option<String>(also defaulted). Not a referenced file: the document stays one unit for git, diff, agents and undo — the founding rule (“a diagram stays a diagram”; the file is the serialisation, the editor is the interface). - Boundary ports are declared, and they are the parent block’s ports.
Subflow’s boundary declaration —inputs/outputs/connections, the samePorttype aBlockKinduses, so a boundary port is a directed input, an output or a domain-typed pin exactly as before. The panel’sto_scenedraws the subsystem block like any other, with its ports resolved from the subflow’s declaration instead of a library kind;wire()resolves a subflow block’s ports the same way. Wires to the subsystem are ordinary wires; nothing about validation’s shape changes (the resolution source grows by one case). - Inside, a boundary port is a pseudo-block. Within the subflow’s own
blocks, a port appears as a block with a reserved kind (boundary/…, one per port, placed at the canvas edge and locked there). Inner wires attach to it like any block. The panel owns the illusion; the engine draws a node. - Editing is a scope stack in the panel. Double-click a subsystem block enters its subflow (the scene swaps — a plain
set_graph, the same as any document change); a breadcrumb (parent / sub_1) and Escape or an empty-canvas click pop out. Undo needs nothing new: a snapshot is the whole document, so an edit inside a subflow restores with the parent intact. The palette, toolbar and status bar serve whichever scope is open; the engine never knows a “level” exists. - No engine change; no group machinery. The parent view draws the subsystem as one block — not a hull, not a group. The engine’s groups and hulls stay what they are: graph-exploration devices (spec 031 §4). A subsystem that is closed stays closed; “entering” is a scene swap, not a zoom into a region.
- Codegen flattens, with per-instance names. An MTK subsystem inlines into the parent circuit: its components and nets join the parent’s, and each boundary pin’s inner net merges with whatever the parent wires to it (a boundary pin is just a net continuation —
connectnever cared where a component came from). Two blocks may reference the same subflow, so every inner component carries a per-instance prefix in MTK’s own₊style (rc_1₊resistor_1) — the second instance must not collide with the first. (Amended 2026-10-13, in stage C’s implementation: the separator is_—rc_filter_1_resistor_1— not₊. ModelingToolkit reads₊in a component’s name as its own namespace separator at runtime and looks for a systemrc_filter_1that does not exist; the prefix is still per instance, which is the point.) A Lux subflow is a sub-Chain: the inner layer path splices between the boundary input and output. Nothing generates a nested Julia structure;structural_simplifyflattens anyway.
Stages
- A. Model + parent view:
Subflow, theBlock.subflowreference, boundary ports on the parent block, validation of the boundary wiring. Native tests; a subflow block visible with its declared pins in the browser. - B. Editing: the scope stack, boundary pseudo-blocks inside, undo across levels, the breadcrumb. The palette can create a subflow (a “Subsystem” block whose empty subflow you enter and fill).
- C. Codegen + e2e: MTK flattening and the Lux splice in moonkale-julia (its v0.3.0, the same tag ladder);
mtk.mjsgrows an RC whose RC-filter is one subsystem — golden generation and the same 99.3 % simulation, proving the flatten merges nets correctly.
Consequences
- The document schema grows two defaulted fields — old files parse; source compatibility is a separate question:
Block.subflowis a new field onBlock, andBlock { … }literals that list every field break. Stage A therefore ships with a newext-api-YYYY-MM-DDtag and its CHANGELOG entry, like the pins did (Block: Defaultcovers only literals finished with..Default::default()). - Subflows compose: a subflow may reference a subflow (the scope stack is a stack; flattening recurses). Depth is bounded by sense, not by the model.
- What we do not decide here: cross-subflow references between documents (Lenticulum’s “a sub-graph as one factor” may want a referenced subflow later — the model leaves room:
Block.subflownames an id, today always in the same document), and the palette/UX of editing boundary declarations (a small editor on the subsystem block, like the parameter editor).