Lay flows out so edges stop crossing where they need not
Docs / docs (push) Successful in 23s
Playwright Tests / test-playwright (1, 2) (push) Successful in 4m17s
Playwright Tests / test-playwright (2, 2) (push) Successful in 1m43s
Playwright Tests / merge-reports (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 18m52s
Test Backend / test-backend (push) Canceled after 0s
Compose Smoke Test / test-compose (push) Canceled after 0s

The canvas placed nodes with dagre, which orders nodes but knows nothing
about ports: an edge leaves a particular handle and lands on a particular
one, so two producers feeding a consumer in the other order crossed just
before they landed however well the nodes were arranged. The graph was not
a multigraph either, so two messages between one pair of nodes collapsed
into one before crossings were counted, and the endpoint labels were laid
out as 220-wide cards with every handle stacked on a single pixel.

ELK's layered algorithm replaces it. Every declared port is handed to it as
a fixed point on the node's rim, at the fraction `portFraction` puts the
handle at — which the node components now render from the same function —
so what the crossing count is minimised over is what ends up on the screen.
Measured on this installation's flows, with the labels included: `home`
goes from 51 crossings to 6 across and 70 to 29 down, `demo_training` 27 to
5 and 59 to 32, and every graph is the same size or smaller.

The engine answers asynchronously and is a chunk of its own, so positions
became state: the canvas draws nothing until the first layout lands, and an
edit keeps the arrangement it had rather than flashing through the corner.
The entry chunk is untouched and the flow route's own chunk came down 167
to 121 kB, since dagre used to be in it.

Two things the crossings made worse come with it. A hovered edge resolves
to full strength so one line can be followed through a busy rank, and a
feedback edge — one whose target the layout put behind its source — swings
out into a lane beside the graph instead of being drawn through everything
between its ends, which is what "House history" was reported for.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01U6vyQvfX249hsv9YPmsYYg
This commit is contained in:
2026-08-26 15:37:36 +02:00
co-authored by Claude Opus 5
parent b80b92aad7
commit a71e0b3375
9 changed files with 648 additions and 163 deletions
+86 -33
View File
@@ -2,6 +2,7 @@ import {
Background,
BackgroundVariant,
type Connection,
type Edge,
type EdgeChange,
type Node as FlowCanvasNode,
type NodeChange,
@@ -57,12 +58,17 @@ import {
} from "./deriveEdges"
import { EdgeInspector, type InspectedEdge } from "./EdgeInspector"
import { EndpointNode } from "./EndpointNode"
import { deriveEndpoints, ENDPOINT_TYPE, isEndpointNode } from "./endpoints"
import {
deriveEndpoints,
ENDPOINT_TYPE,
type EndpointNodeData,
isEndpointNode,
} from "./endpoints"
import { FIT_VIEW, FIT_VIEW_PANEL, FlowDock } from "./FlowDock"
import { FlowNode, type FlowNodeData } from "./FlowNode"
import { FlowPanel } from "./FlowPanel"
import { LiveEdge } from "./LiveEdge"
import { type Direction, layoutGraph, nodeHeight } from "./layout"
import { type Direction, layoutGraph, type NodeShape } from "./layout"
import { NodePanel } from "./NodePanel"
import { RunDialog } from "./RunDialog"
import "./flow.css"
@@ -80,6 +86,41 @@ import { useFlowSocket } from "./useFlowSocket"
const nodeTypes = { flow: FlowNode, [ENDPOINT_TYPE]: EndpointNode }
/**
* Where every node sits, once the engine has said so.
*
* The layout is a chunk of its own and answers asynchronously, so a canvas has
* a moment before it knows where anything goes. It draws nothing at all until
* then — a route already shows a skeleton for that — and an *edit* keeps the
* arrangement it had, so only the node that changed lands a frame late rather
* than the whole graph flickering through the corner.
*/
function useLayout(
ids: string[],
edges: Edge[],
direction: Direction,
shapes: Map<string, NodeShape>,
shapeKey: string,
): Map<string, { x: number; y: number }> | null {
const [placed, setPlaced] = useState<Map<
string,
{ x: number; y: number }
> | null>(null)
// biome-ignore lint/correctness/useExhaustiveDependencies: the shape key is the dependency; the arrays are rebuilt every render.
useEffect(() => {
let stale = false
layoutGraph(ids, edges, direction, shapes).then((positions) => {
// A graph edited while the last one was still being laid out has already
// asked for another; that one is the answer, not this.
if (!stale) setPlaced(positions)
})
return () => {
stale = true
}
}, [shapeKey, edges])
return placed
}
/** Is this canvas node actually part of the flow document? */
const isDocumentNode = (node: { id: string }) => !isEndpointNode(node)
const edgeTypes = { live: LiveEdge }
@@ -517,29 +558,30 @@ function FlowEditorInner({
...external.nodes.map((node) => node.id),
]
const shapeKey = `${direction}|${key}|${ids.join(",")}`
// How tall each node's ports make it, which `FlowNode` draws to the same
// number. A function of the document — the bindings key above already covers
// every port, so this changes exactly when the layout has to run again, and
// nothing measured is ever fed back into it.
const heights =
direction === "LR"
? new Map(
definitions.map((node) => [
node.id,
nodeHeight(
Math.max(
(node.requires ?? []).length,
(node.provides ?? []).length,
),
),
]),
)
: new Map<string, number>()
// biome-ignore lint/correctness/useExhaustiveDependencies: the shape key is the dependency; the arrays are rebuilt every render.
const positions = useMemo(
() => layoutGraph(ids, edges, direction, heights),
[shapeKey, edges],
)
// What each node is, as far as the layout is concerned: the ports it draws,
// in the order it draws them. A function of the document — the bindings key
// above already covers every port, so this changes exactly when the layout
// has to run again, and nothing measured is ever fed back into it.
const shapes = new Map<string, NodeShape>([
...definitions.map(
(node) =>
[
node.id,
{
requires: (node.requires ?? []).map(portOf),
provides: (node.provides ?? []).map(portOf),
},
] as const,
),
...external.nodes.map((node) => {
const data = node.data as EndpointNodeData
return [
node.id,
{ requires: data.requires, provides: data.provides, endpoint: true },
] as const
}),
])
const positions = useLayout(ids, edges, direction, shapes, shapeKey)
/**
* The endpoints, placed.
@@ -551,10 +593,15 @@ function FlowEditorInner({
*/
const externalNodes = useMemo(
() =>
external.nodes.map((node) => ({
...node,
position: positions.get(node.id) ?? node.position,
})),
external.nodes
// A node the layout has not placed yet — one just added, or the whole
// graph on the first render — is left out rather than drawn at the
// origin, which is where every unplaced node would pile up.
.filter((node) => positions?.has(node.id))
.map((node) => ({
...node,
position: positions?.get(node.id) ?? node.position,
})),
[external, positions],
)
@@ -568,10 +615,12 @@ function FlowEditorInner({
const shownNodes = useMemo(
() => [
...renderedNodes.map((node) => ({
...node,
position: positions.get(node.id) ?? node.position,
})),
...renderedNodes
.filter((node) => positions?.has(node.id))
.map((node) => ({
...node,
position: positions?.get(node.id) ?? node.position,
})),
...externalNodes,
],
[renderedNodes, externalNodes, positions],
@@ -621,6 +670,9 @@ function FlowEditorInner({
// A phone's sheet covers the canvas outright, and so does the expanded
// editor: there is no viewport to aim.
if (editorExpanded) return
// Nothing is on the canvas until the layout has answered, and fitting an
// empty one would spend the single instant fit on nothing.
if (!positions) return
const frame = requestAnimationFrame(() => {
const view = panelOpen && !isMobile ? FIT_VIEW_PANEL : FIT_VIEW
const duration = fitted.current ? 300 : 0
@@ -637,6 +689,7 @@ function FlowEditorInner({
definitions.length,
external.nodes.length,
edges.length,
positions,
selectedId,
panelOpen,
editorExpanded,