Skip to content

refactor: one layout snapshot for every handler - #121

Open
endrix wants to merge 2 commits into
mainfrom
refactor/layout-snapshot-helpers
Open

endrix wants to merge 2 commits into
mainfrom
refactor/layout-snapshot-helpers

Conversation

@endrix

@endrix endrix commented Nov 8, 2026 •

Copy link
Copy Markdown
Owner

Phase 4a of the declutter plan (de-duplication, server side). Pure refactor inside diagram-server, plus two small fixes the consolidation forced into the open.

What was duplicated

Every handler that lays nodes out or routes edges ended the same way: walk the GModel, key nodes by AST path / entity name / port name, key edges, save, update the layout meta. Each had its own private copy:

helper copies
collectNodePositions, collectEdgeRoutes 8 each (boundary flow, serpentine, reset routes, change routing points, reroute, and three classes in diagram-action-handlers.ts)
edgeSignatureFromArgs, buildEdgeSignatureCounts, edgeRouteKeyFor 5 each (the one in persisted-edge-routes.ts is the loader's)
snapshotNodePositionsById, snapshotEdgeRoutesById, restoreUnselectedLayout, getSelectedMovableNodeIds, isMovableNode, getMovableNodeIdForElementId 3–4 each
absolutePosition, portAnchorAbsolute change-routing-points re-implemented the loader's

A diff of all copies (done before writing) showed differences of formatting and parameter types, plus two of meaning, below.

What this PR does

  • src/server/layout-snapshot.ts: isMovableNode, movableNodeIdFor, movableNodeIdsIn, snapshotNodePositionsById, snapshotEdgeRoutesById, restoreUnselectedLayout, collectNodePositions, collectEdgeRoutes, LayoutPersistenceMeta, and persistLayout(modelState, layoutPersistence, { layoutPerformed?, keepPersistedPositions? }), which replaces the seven near-identical save blocks (the reset and bend handlers keep the positions already on disk; the layout handlers mark the layout persisted).
  • src/routing/persisted-edge-routes.ts exports absolutePosition, portAnchorAbsolute, edgeSignatureFromArgs, buildEdgeSignatureCounts, edgeRouteKeyFor (they were module-private); the change-routing-points handler uses the loader's portAnchorAbsolute (same geometry via shared portAnchor) instead of its own.
  • The six handlers import these and lose their copies. runBoundaryFlowAndPersist keeps its own meta object (it is handed one) and calls the two collectors.
  • Kept as is: the serpentine handler's own absolutePosition (it sums GNode ancestors only, a different rule for its own port geometry) and simplifyRoutingPoints (still called).

The two meaning drifts, and what the review made of them

  1. Reset Routes and the reroute set the layout meta under the literal 'workflow.layoutPersistence' while everything else uses WORKFLOW_LAYOUT_PERSISTENCE_KEY, so their note never landed. The first version of this PR routed them through the shared key; the review showed that lowering hasPersistedEdgeRoutes after a reset arms the ModelSubmissionHandler's bootstrap reroute, which would redraw what was just cleared. They now call persistLayout(..., { noteInMeta: false }): the save happens, the meta is left alone, and the comment says why.
  2. Four handlers keyed saved routes by AST path alone; the loader, the reroute and the stock layout key by endpoint signature (AST path appended where the signature repeats). One collectEdgeRoutes now, keyed as the loader reads. A lone bend is still saved (the loader rebuilds it into a full polyline); the first version dropped it, the review caught it.

Also from the review: the bend handler keeps its own portAnchorAbsolute (it accepts any end with a node above it, where the loader's requires a GPort); runBoundaryFlowAndPersist goes through persistLayout too; placeNewNodes uses the shared nodeLayoutKeyOf; the four inline layout-meta types are the one LayoutPersistenceMeta; Point comes from the routes module; the reroute's persistRoutes drops the root parameter it no longer read.

Tests

layout-snapshot.test.ts: node keys and absolute positions through a nested group, route keys by signature, the selection's movable nodes (element, edge ends, nested node), restore of the unselected part, and persistLayout in its three modes. Existing coverage: reroute-keeps-the-layouts-trunk, persisted-edge-routes-snap-to-ports, glsp-auto-layout-clears-routes, per-instance-layout.

Diff: 7 files, +69/−1470 in the handlers, +1 module (~230 lines), +1 test. Typecheck 5/5, neutrality 5/5, server 449 tests.

Note: this branch is cut from main before #119 and #120 merged; only CHANGELOG.md can conflict, and I will rebase onto main on a fresh branch if it does.

https://claude.ai/code/session_01EToj6RJRMSSHQiqGNMU9wv

Phase 4a of the declutter plan. The six handlers that lay nodes out or
route edges each carried a copy of "walk the model, key the nodes and
the routes, save": eight collectNodePositions, eight collectEdgeRoutes,
five signature helpers, four snapshot/restore sets, about 1,400 lines,
drifted in formatting, in types and in two places in meaning.

server/layout-snapshot.ts holds them once, and persisted-edge-routes.ts
exports the route-key and anchor helpers it always had. persistLayout
replaces the seven save blocks; the one in runBoundaryFlowAndPersist
keeps its own meta and calls the collectors.

Two drifts end here: Reset Routes and the reroute noted their save under
a model-state key nothing read ('workflow.layoutPersistence' instead of
the shared constant), and four handlers keyed routes by AST path alone
where the loader and the other writers key by endpoint signature.

Claude-Session: https://claude.ai/code/session_01EToj6RJRMSSHQiqGNMU9wv
Reset Routes and the reroute leave the layout meta alone on purpose:
their old note went to a key nothing read, and writing it where the
rest of the server reads it would have lowered "has persisted edge
routes" and armed the bootstrap reroute, which redraws what was just
cleared. A lone bend is saved as before: the loader rebuilds it into a
full polyline. The bend handler keeps its own port-anchor helper, which
accepts any end with a node above it where the loader's wants a port.

One save path: runBoundaryFlowAndPersist goes through persistLayout,
placeNewNodes keys nodes with the shared nodeLayoutKeyOf, the inline
layout-meta types are the one LayoutPersistenceMeta, Point comes from
the routes module, and the reroute's persist drops a parameter it no
longer read.

Claude-Session: https://claude.ai/code/session_01EToj6RJRMSSHQiqGNMU9wv
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