Skip to content

A port order the diagram draws is kept with its layout - #236

Merged
endrix merged 2 commits into
mainfrom
feat/kept-port-order
Nov 24, 2026
Merged

endrix merged 2 commits into
mainfrom
feat/kept-port-order

Conversation

@endrix

@endrix endrix commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

First of the two PRs that let every Dialogram product reorder ports (plan approved 2026-11-24). This one makes a port order survive: until now nothing kept one, so a Free Ports layout was lost on reload, its routes jogging back to the declared rows.

What changes

  • Kept in the layout file. LayoutFileContent.portOrders: one record for the file, { [definitionKey]: { west?, east? } }, keyed by definitionKeyOf (name, defining file, port names), so adding or renaming a port lets the source's order stand on its own. Every writer carries it over (writeLayout, savePortBuses, saveUnwrittenStates, saveViewOptions); loadPortOrders reads it.
  • Said at every layout save. persistLayout and saveLayoutGeometry pass keptPortOrderChanges(root): each definition whose instances are drawn in one order is kept when that is not the source's and let go when it is; definitions not drawn here, or drawn in two orders, are left as the file has them.
  • Drawn when the model is built. finishLoadFromDoc applies the kept orders right after convertToGModelRoot (applyKeptPortOrders): the instances' ports take the rows their side already has, in the kept order. A view drawn from its fallback's layout takes the fallback's orders; a rebuild "as drawn now" (a bus or node toggle) keeps the order drawn now, since an undo is not saved.
  • The source's order is the children's order. A reorder moves a port to another row, never among the node's children, so sidesOf(node, 'source') reads it there. The wf:declaredRowY stamp and restoreDeclaredRows are gone. Layout Diagram, and every layout but Untangled and Free Ports, now keeps the ports in the order they are drawn in, so the order outlives the layout run after an edit. Untangled puts them back in the source's order (drawInSourceOrder), as before: the way back after a reload, and its save lets the kept orders go.
  • Buses. An open bus's members go with it: one place in an order, keyed and ordered alike open or closed, and regridPortRows moves the bus with its members under it. This also stops Free Ports from pulling a member out of its bus.
  • Symbols. A cell drawn as its symbol (wf:symbol) is not reordered, by Free Ports or a kept order: its pins are where the symbol has them (a mux's select, a register's clock).
  • A view leaving its fallback's layout (an in-place instance drawn on its workflow's layout, getting its own at the first placement) saves the port orders it was drawn in with the routes.
  • Untangle (sidecar-toolkit, for now). Apply saves no kept order for the definitions it writes to source (withOrdersInSource), so a later hand edit of the source's order is drawn in this diagram; undo saves the drawing before with its orders. Each try starts from the drawing the untangle began on (the probe's result depends on the order it starts from); a try that fails shows that drawing. declared reads the source's order.

Tests

  • kept-port-order.test.ts: applied on every instance on the same rows; a stale or partial order ignored; changes derived (kept, let go, left alone); open and closed bus share a key and an order, members move with their bus; symbols untouched; file record parsing.
  • layout-port-orders.test.ts: set and let go by saves, survives a move (immediate and debounced), view options, buses and unwritten states; none with persistence off.
  • kept-port-order-load.test.ts: the storage draws the kept order at load; a rebuild keeps the order drawn now.
  • elk-layout-port-order.test.ts: Free Ports, then the orders saved, then the model built again from source: the same rows; a plain layout after keeps them; Untangled after Free Ports puts every port back on its declared row.
  • untangle.test.ts: Apply keeps the order it does not write and lets go of the one it writes; undo saves the orders before. untangle-mcp-tools.test.ts: each try starts from the first drawing.
  • Typecheck 5/5, neutrality 5/5, diagram-server 1033/1033, extension-core 681/681, sidecar-toolkit 606/611: the 5 are variant-node-create-wizard timeouts on this machine (20 s a test here, 79 ms on CI for main 5d2e73a; untouched by this change).

Review (/code-review medium)

  1. A file-level order can disagree with another view's saved routes. Kept: one order per definition for the file is the approved reach. Saved routes never detach, since their ends re-snap to the current port anchors with the neighbouring bend (persisted-edge-routes.ts), so the other view shows routes shaped for other rows until its next layout, as any port move does today.
  2. No way back to the source's order once kept. Fixed: Untangled puts the source's order back and its save lets the orders go (test: Untangled after Free Ports gives the declared rows, set empty).
  3. A fallback-drawn view's first own save drops the fallback's orders. Fixed: placeNewNodes passes keptPortOrderChanges(root) with the routes when it leaves the fallback (test in growth-on-submit).
  4. Stale keys (a port added since) stay in the file. Not changed: a key is a definition's name, file and ports, and another view of the file may still draw the older port set; pruning by name could drop a live order. Entries are small and only made by a reorder.
  5. Source order judged on nodes[0] only. Fixed: a definition whose instances declare their ports in two orders (two definitions taken for one: one name, the same ports, no file) is left as the file has it. Children are in declaration order by construction (graph-gmodel-source.ts: rows are the children's indices).
  6. Another diagram's layout keeps its order after Apply writes source. By design (per file); CHANGELOG now says "in this diagram".
  7. A failed try leaves the server reset under the client's last try. Fixed: the failure shows the drawing it was reset to (test).
  8. JSDoc of loadViewOptions landed on loadPortOrders. Fixed.

Next

PR 2: the untangle tools and Apply move into diagram-server, the source write becomes a hook the sidecar binds, and /untangle is offered by every product with the diagram chat; orders that cannot go to source are kept here.

https://claude.ai/code/session_011UYzzUKxNqKPdmLsRmJ6Xu

The order a Free Ports layout or an applied untangle gives a definition's
ports is saved in the layout file (file-level `portOrders`, by definition
key) at every layout save, derived from the drawing, and the model is
built in it. Every writer of the file carries the record over. A rebuild
keeps the order drawn now (an undo is not saved). Layout Diagram and
Untangled keep the drawn order instead of restoring the source's, so the
declared-row stamp and its restore are gone: the source's order is the
order of a node's children.

An open bus is one place in an order and moves with its members; a cell
drawn as its symbol is not reordered. An untangle applied to source keeps
no layout order for what it wrote, and each try starts from the drawing
the untangle began on.

Claude-Session: https://claude.ai/code/session_011UYzzUKxNqKPdmLsRmJ6Xu
Untangled (`dialogram.portOrder: declared`) draws every reorderable node in
its source order again, the way back from a Free Ports layout after a
reload, and the save lets the kept orders go; every other layout keeps
the drawn order. A view that leaves its fallback's layout saves the port
orders it was drawn in with the routes. A definition whose instances
declare their ports in two orders is not kept. A failed untangle try
shows the drawing it was reset to. The view-options doc is back on its
method.

Claude-Session: https://claude.ai/code/session_011UYzzUKxNqKPdmLsRmJ6Xu
@endrix
endrix merged commit 2d0ad5c into main Nov 24, 2026
1 check passed
@endrix
endrix deleted the feat/kept-port-order branch November 24, 2026 14:27
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