Skip to content

Lay out the nested diagrams Dagre's constraint cycles break (v0.4.1) - #11

Merged
alixander merged 3 commits into
mainfrom
fix/nested-layer-gaps
Nov 27, 2026
Merged

alixander merged 3 commits into
mainfrom
fix/nested-layer-gaps

Conversation

@alixander

Copy link
Copy Markdown
Contributor

Makes dagro lay out the nested diagrams on which Dagre's ordering breaks, for v0.4.1. Every input that v0.4.0 lays out keeps its exact output.

The bug (also in Dagre: dagrejs/dagre#234, #255, unfixed). In an ordering sweep, resolveConflicts merges an entry into its successor, but the merged entry does not take over the absorbed entry's incoming constraints. The barycenter sort can then put it before a sibling cluster it must follow, addSubgraphConstraints closes a cycle in the sweep's constraint graph, and the clusters on that cycle drop out of the sweep's later layers with stale orders. D2 diagrams with three or more sibling containers and connections between their contents hit it, and fail with dagre fails on this input: it sets "order" on a node missing from a layer (or "rank"), or later with not possible to find intersection inside of the rectangle. dagre.js 3.x loses the same nodes and fails at the intersection.

The fix. The ordering runs exactly as before. Only where its result has an empty position or a missing node, which is exactly where v0.4.0 fails, does it run again with the takeover: a merged entry takes over the incoming constraints of the entries it absorbs. Where a node is merely lost (some such layouts succeed), the layout goes on as before, and only if it then fails is it repeated with the takeover, drawing dummy names from a sequence of its own. About 50 lines of the change are the takeover itself, which could also be proposed upstream.

Checked

  • Every input v0.4.0 lays out gives the same bits and the same name-counter draws: TestGolden (14,111 inputs) is unchanged, and so are differentials of more than 1.6 million generated inputs (with NaN, infinite, zero and negative values, dummy-looking ids and every ranker), natively, under js/wasm and under TinyGo 0.44.0-d2.3 with D2's flags, on reused pooled state and from 12 concurrent goroutines.
  • All 1,357 known failing D2 inputs lay out with finite coordinates (the 30 of D2's earlier review, min.d2, and about 1,300 generated); 2,295 of 2,296 failing D2-shaped inputs in a fresh differential lay out.
  • Allocations are unchanged (TestOversizedStateReusedUnderGC 77 per layout, TestFreshStateAllocation 29.6 MB / 20.0 MB); passing layouts run at 1.007× v0.4.0's time.
  • The tests pin each repaired layout by hash and counter draws; seven deliberately broken versions of the repair each fail them.
  • go test ./..., -race, go vet, gofmt and the js/wasm build pass at every commit. Not run here: linux/amd64.

What still fails, or fails differently (also in the README and UPSTREAM.md)

  • A D2-shaped input whose repaired layering is still inconsistent fails as before (1 of 2,296; testdata/knownsites/takeover_inconsistent.json).
  • Inputs with unusual numbers that failed with a layer gap can now fail later instead, with the intersection error, with a runtime error at an edge whose zero, negative or fractional minlen leaves it no route (an old site), or, with NaN or infinite sizes, without an error and with non-finite coordinates. D2 sends finite sizes, minlen 1 and weight 1.
  • An input that now lays out draws the dummy names its layout needs; a later layout in the same process whose node ids look like dummy names can depend on that, as it already depends on earlier layouts. D2's ids are integers.

After this merges, tag v0.4.1; D2 then moves from the pseudo-version in its bump PR to the tag.

🤖 Generated with Claude Code

alixander and others added 3 commits November 26, 2026 21:09
On some compound graphs Dagre's ordering ends with an inconsistent
layering (dagrejs/dagre#234, #255). In a sweep, resolveConflicts merges
an entry into its successor when their barycenters conflict, but the
merged entry does not take over the absorbed entry's incoming
constraints. The sort can then put the merged entry before a sibling
cluster it has to follow, addSubgraphConstraints records the reverse
constraint and closes a cycle in the sweep's constraint graph, and
resolveConflicts leaves the clusters on that cycle out of the sweep's
later layers. Their nodes keep stale orders: the best layering has an
empty position, a node missing from it, or two nodes on one position.
LayoutTyped then failed with "dagre fails on this input: it sets
"order" (or "rank") on a node missing from a layer", or later with "not
possible to find intersection inside of the rectangle". Nested D2
diagrams with three or more sibling containers and connections between
their contents do this. dagre.js 3.x loses the same nodes and fails
later, at the intersection.

order runs the sweeps exactly as before and checks the layering they
keep (orderGap):

- An empty position, in o.best (gapEmpty) or in the orders after it
  (gapMissing), where v0.4.0 fails in assignOrder or insertSelfEdges:
  order runs the sweeps again from the initial order with the
  takeover, in which a merged entry takes over the incoming constraints
  of the entries it absorbs and checks each against its new barycenter
  (takeIn, an iterative walk over lists of absorbed entries). It keeps
  that layering when it is consistent, and otherwise runs the sweeps
  once more as before, which fails as before.
- Two nodes on one position and none empty (gapLost): the layout goes
  on as before, since some such layouts succeed (when the lost node is
  an inner border node). Only if it fails does LayoutTyped lay the
  input out again with the takeover from the first sweep on (relayout),
  returning the first error if that fails too. The repeat draws dummy
  names from a sequence of its own, so the process-wide counter moves
  by the first layout's draws only.

Every input that v0.4.0 lays out gets the same layout bit for bit and
the same counter draws (TestGolden, 14,111 inputs, is unchanged; so are
differentials of more than 1.6 million generated inputs, natively,
under js/wasm and under TinyGo). The new buffers (absorbedHead,
absorbedTail, takeStack) live in the pooled state, retainedBytes counts
them, and begin resets the recorded gap.

What still fails, or fails differently:
- A D2-shaped input whose repaired layering is still inconsistent fails
  as before (1 of 2,296 in a differential;
  testdata/knownsites/takeover_inconsistent.json).
- Inputs with unusual numbers that failed with a layer gap can now get
  past the ordering and fail later: with the intersection error, with a
  runtime error at an edge whose zero, negative or fractional minlen
  leaves it no route (a site v0.4.0 also reaches on other inputs), or,
  with NaN or infinite sizes, without an error and with non-finite
  coordinates. D2 sends finite sizes, minlen 1 and weight 1.
- An input that now lays out draws the dummy names its layout needs,
  more than v0.4.0's failed attempt drew; a later layout in the same
  process whose node ids look like dummy names (_d17) can depend on
  that, as it already depends on every earlier layout.

The comments on PanicError, failNilMapWrite and the package no longer
say that these inputs fail "where dagre itself fails".
TestLayoutTypedPanicBecomesError checks a remaining empty-position
failure instead of the knownsites fixtures, which now lay out.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Regression tests for the takeover: the tiny reproduction, the min.d2
input, the "rank" fixture, an input that needs the absorbed-list walk,
one that needs the nested takeover, the intersection input that takes
the repeated layout, and one whose repair is still inconsistent and
fails as before. Each repaired layout is pinned by hash and counter
draws, every child must lie inside its container, and the input that
loses an inner border node but lays out today is pinned to v0.4.0.
TestOrderGap checks the four results on hand-built layerings;
TestRepairAllocations and TestRetainedBytesCountsEveryBuffer cover the
new buffers, and TestConcurrentLayouts the repeated layout.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
README has a "Changes in v0.4.1" section and lists what still fails;
UPSTREAM.md describes the ordering repair, a departure from Dagre
3.1.1, and its limits.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alixander
alixander merged commit da9ba3e into main Nov 27, 2026
2 checks passed
alixander added a commit to d2lang/d2 that referenced this pull request Nov 27, 2026
The tag of the commit this branch pinned by pseudo-version
(d2lang/dagro#11). The changelog entry moves to
the top of the bugfixes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
opensource-fork-sync Bot pushed a commit to cla7aye15I4nd/opensource-d2lang__d2 that referenced this pull request Nov 27, 2026
Dagre loses sibling containers in some nested diagrams, which then fail
with "dagre fails on this input: it sets \"order\" on a node missing
from a layer" or "not possible to find intersection inside of the
rectangle". The dagro commit repairs those orderings and leaves every
layout that already worked bit for bit as it was
(d2lang/dagro#11). This pins its pseudo-version;
switch to v0.4.1 once that is tagged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant