@luhtech/site-template 0.23.0 → 0.23.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -8,7 +8,7 @@ schema: each site repo validates its instances in its own CI against
8
8
  schema-registry (the reusable `content-validate.yml` workflow), so an invalid
9
9
  instance fails that site's CI instead of shipping.
10
10
 
11
- Current release: **0.23.0** on npmjs.com. Every release is a
11
+ Current release: **0.23.1** on npmjs.com. Every release is a
12
12
  `site-template-v*` tag, published by `.github/workflows/publish.yml`; CI
13
13
  (`.github/workflows/ci.yml`) runs `npm install` + `npm test` on every PR and
14
14
  push to `main`.
@@ -425,6 +425,19 @@ Two bins go with it:
425
425
  install chromium`. `SiteLayout` emits the web-app meta for an installable
426
426
  site.
427
427
 
428
+ ## A connection graph with a cycle lays out at its true width (0.23.1)
429
+
430
+ `ConnectionGraph` layered by longest-path relaxation, which on a cycle
431
+ (design -> ... -> operate -> design) pushed the cycle's nodes one column
432
+ right on every pass: desconops.org's five-node three-loops figure came out
433
+ 13000 units wide. `layerGraph` (`src/lib/graphLayout.ts`) now breaks each
434
+ cycle first -- a depth-first search in node order marks the edge that closes
435
+ it -- and layers the rest in one topological pass; both are linear in nodes
436
+ plus edges and give the same layout for the same record. A cycle-closing
437
+ edge draws as a dashed return path under the graph, its label beneath. An
438
+ acyclic graph lays out exactly as before (`test/graphLayout.test.mjs` checks
439
+ 300 random ones against the old layering).
440
+
428
441
  ## A figure renders its label (0.23.0)
429
442
 
430
443
  content/page 2.9.0 and whitepaper 1.4.0 give a figure a `label` (what the
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@luhtech/site-template",
3
- "version": "0.23.0",
3
+ "version": "0.23.1",
4
4
  "description": "Shared Astro component library + content-instance contract for LuhTech venture marketing sites -- one template renders every site from content/page.schema.json + content/site.schema.json instances.",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",
@@ -10,6 +10,9 @@
10
10
  // any real nodes[]/edges[] input -- not hand-tuned to one graph. Self-loops
11
11
  // (a node citing itself, e.g. "a decision supersedes a decision") are
12
12
  // excluded from layering and drawn as a small loop above the node instead.
13
+ // An edge that closes a longer cycle (operate -> design in a three-loop
14
+ // record) is found by src/lib/graphLayout.ts, kept out of layering, and drawn
15
+ // as a dashed return path under the graph -- see layerGraph's own note.
13
16
  export interface Node {
14
17
  id: string;
15
18
  label: string;
@@ -22,6 +25,7 @@ export interface Edge {
22
25
  to: string;
23
26
  label?: string;
24
27
  }
28
+ import { layerGraph } from '../../lib/graphLayout';
25
29
  export interface Props {
26
30
  diagramId: string;
27
31
  nodes: Node[];
@@ -31,27 +35,16 @@ export interface Props {
31
35
  const { diagramId, nodes, edges, class: className = 'w-full h-auto' } = Astro.props;
32
36
 
33
37
  const selfLoops = edges.filter((e) => e.from === e.to);
34
- const crossEdges = edges.filter((e) => e.from !== e.to);
35
38
 
36
- // Longest-path layering: layer(n) = 0 if no incoming cross-edge, else
37
- // 1 + max(layer(source)) over incoming edges. Iterative relaxation with a
38
- // bounded pass count (nodes.length) makes this safe even if the real data
39
- // ever contains a longer cycle than a self-loop -- it converges to a stable
40
- // (if imperfect) layering rather than looping forever.
41
- const layer = new Map<string, number>(nodes.map((n) => [n.id, 0]));
42
- for (let pass = 0; pass < nodes.length + 1; pass++) {
43
- let changed = false;
44
- for (const e of crossEdges) {
45
- const want = (layer.get(e.from) ?? 0) + 1;
46
- if (want > (layer.get(e.to) ?? 0)) {
47
- layer.set(e.to, want);
48
- changed = true;
49
- }
50
- }
51
- if (!changed) break;
52
- }
53
-
54
- const maxLayer = Math.max(...Array.from(layer.values()), 0);
39
+ // Layering and cycle-breaking: src/lib/graphLayout.ts (O(nodes + edges),
40
+ // deterministic). Forward edges are drawn left to right; an edge that closes a
41
+ // cycle is a return edge, drawn under the graph.
42
+ const { layer, maxLayer, backEdges } = layerGraph(
43
+ nodes.map((n) => n.id),
44
+ edges,
45
+ );
46
+ const crossEdges = edges.filter((e, i) => e.from !== e.to && !backEdges.has(i));
47
+ const returnEdges = edges.filter((_, i) => backEdges.has(i));
55
48
  const columns: Node[][] = Array.from({ length: maxLayer + 1 }, () => []);
56
49
  for (const n of nodes) columns[layer.get(n.id) ?? 0].push(n);
57
50
 
@@ -107,8 +100,19 @@ columns.forEach((col, ci) => {
107
100
  });
108
101
  });
109
102
 
110
- const width = PAD_X * 2 + (maxLayer + 1) * NODE_W + maxLayer * COL_GAP;
111
- const height = topPad + PAD + colHeight;
103
+ // A return edge's label runs under the whole graph, so the canvas is at least
104
+ // as wide as the longest one (same ~5.4px/char estimate as COL_GAP).
105
+ const longestReturnLabel = Math.max(0, ...returnEdges.filter((e) => e.label).map((e) => e.label!.length));
106
+ const width = Math.max(
107
+ PAD_X * 2 + (maxLayer + 1) * NODE_W + maxLayer * COL_GAP,
108
+ longestReturnLabel * 5.4 + PAD * 2,
109
+ );
110
+ // Each return edge runs along its own track under the nodes, its label just
111
+ // below the track; reserved only when the graph has one, so every acyclic
112
+ // diagram's canvas stays exactly as tight as before.
113
+ const RETURN_TRACK = 26;
114
+ const RETURN_ROOM = returnEdges.length > 0 ? returnEdges.length * RETURN_TRACK + 14 : 0;
115
+ const height = topPad + PAD + colHeight + RETURN_ROOM;
112
116
 
113
117
  // Cubic bezier from the right edge of `from` to the left edge of `to`
114
118
  // (forward, cross-column edges), or a vertical bow between two nodes in
@@ -141,6 +145,23 @@ const drawnCrossEdges = crossEdges
141
145
  })
142
146
  .filter((e): e is NonNullable<typeof e> => e !== null);
143
147
 
148
+ // A return edge leaves the bottom of its source, runs along its track and
149
+ // rises into the bottom of its target, with rounded corners.
150
+ const drawnReturnEdges = returnEdges
151
+ .map((e, ti) => {
152
+ const from = positions.get(e.from);
153
+ const to = positions.get(e.to);
154
+ if (!from || !to) return null;
155
+ const yRun = topPad + colHeight + 18 + ti * RETURN_TRACK;
156
+ const r = 8;
157
+ const dir = to.cx < from.cx ? -1 : 1;
158
+ const fb = from.y + NODE_H;
159
+ const tb = to.y + NODE_H;
160
+ const path = `M ${from.cx} ${fb} L ${from.cx} ${yRun - r} Q ${from.cx} ${yRun} ${from.cx + dir * r} ${yRun} L ${to.cx - dir * r} ${yRun} Q ${to.cx} ${yRun} ${to.cx} ${yRun - r} L ${to.cx} ${tb + 2}`;
161
+ return { ...e, path, lx: (from.cx + to.cx) / 2, ly: yRun + 12 };
162
+ })
163
+ .filter((e): e is NonNullable<typeof e> => e !== null);
164
+
144
165
  const drawnSelfLoops = selfLoops
145
166
  .map((e) => {
146
167
  const at = positions.get(e.from);
@@ -188,6 +209,15 @@ const ariaLabel = `Connection graph: ${nodes.length} nodes -- ${nodes.map((n) =>
188
209
  </g>
189
210
  ))}
190
211
 
212
+ {drawnReturnEdges.map((e) => (
213
+ <g class="text-ink/50">
214
+ <path d={e.path} fill="none" stroke="currentColor" stroke-width="1.25" stroke-opacity="0.4" stroke-dasharray="4 3" marker-end={`url(#${markerId})`} />
215
+ {e.label && (
216
+ <text x={e.lx} y={e.ly} text-anchor="middle" font-family="JetBrains Mono, monospace" font-size="9" fill="currentColor" fill-opacity="0.65">{e.label}</text>
217
+ )}
218
+ </g>
219
+ ))}
220
+
191
221
  {drawnSelfLoops.map((e) => (
192
222
  <g class="text-ink/50">
193
223
  <path d={e.path} fill="none" stroke="currentColor" stroke-width="1.25" stroke-opacity="0.4" marker-end={`url(#${markerId})`} />
@@ -0,0 +1,96 @@
1
+ // Layering for ConnectionGraph: which column each node sits in, and which
2
+ // edges close a cycle.
3
+ //
4
+ // A layered (Sugiyama-style) layout needs an acyclic graph. Real records are
5
+ // not always one: a loop such as design -> release -> construct -> operate ->
6
+ // design closes back on its start. Layering such a graph by longest-path
7
+ // relaxation pushes every node in the cycle one column further right on each
8
+ // pass, so the canvas grows with the pass count (found live: desconops.org's
9
+ // three-loops figure, 5 nodes, laid out 13000 units wide).
10
+ //
11
+ // So the cycle is broken first, the standard way: a depth-first search in node
12
+ // order marks each edge that reaches a node still on the search stack as a back
13
+ // edge. The remaining forward edges form a DAG, layered by longest path in one
14
+ // topological pass. Back edges are not lost: the component draws each one as a
15
+ // return path under the graph. Both passes are O(nodes + edges), and the result
16
+ // depends only on node and edge order, so the same record always draws the same.
17
+ // A graph with no cycle has no back edges and lays out exactly as before.
18
+
19
+ export interface LayoutEdge {
20
+ from: string;
21
+ to: string;
22
+ }
23
+
24
+ export interface GraphLayering {
25
+ /** column index per node id, 0 for a node with no forward incoming edge */
26
+ layer: Map<string, number>;
27
+ /** the highest column index */
28
+ maxLayer: number;
29
+ /** indexes into the edges array of the edges that close a cycle */
30
+ backEdges: Set<number>;
31
+ }
32
+
33
+ export function layerGraph(nodeIds: readonly string[], edges: readonly LayoutEdge[]): GraphLayering {
34
+ const known = new Set(nodeIds);
35
+ const out = new Map<string, number[]>(nodeIds.map((id) => [id, []]));
36
+ edges.forEach((e, i) => {
37
+ if (e.from !== e.to && known.has(e.from) && known.has(e.to)) out.get(e.from)!.push(i);
38
+ });
39
+
40
+ // Depth-first search, iterative so a long chain cannot overflow the stack.
41
+ // Roots are taken in node order: nodes with no incoming edge first, so a
42
+ // graph's natural start stays in column 0, then any node not yet reached.
43
+ const hasIncoming = new Set<string>();
44
+ for (const e of edges) if (e.from !== e.to && known.has(e.from)) hasIncoming.add(e.to);
45
+ const roots = [...nodeIds.filter((id) => !hasIncoming.has(id)), ...nodeIds];
46
+
47
+ const state = new Map<string, 0 | 1 | 2>(); // 0 unseen, 1 on stack, 2 done
48
+ const backEdges = new Set<number>();
49
+ for (const root of roots) {
50
+ if (state.get(root)) continue;
51
+ const stack: Array<{ id: string; next: number }> = [{ id: root, next: 0 }];
52
+ state.set(root, 1);
53
+ while (stack.length) {
54
+ const top = stack[stack.length - 1];
55
+ const outs = out.get(top.id)!;
56
+ if (top.next < outs.length) {
57
+ const ei = outs[top.next++];
58
+ const to = edges[ei].to;
59
+ const s = state.get(to) ?? 0;
60
+ if (s === 1) backEdges.add(ei);
61
+ else if (s === 0) {
62
+ state.set(to, 1);
63
+ stack.push({ id: to, next: 0 });
64
+ }
65
+ } else {
66
+ state.set(top.id, 2);
67
+ stack.pop();
68
+ }
69
+ }
70
+ }
71
+
72
+ // Longest-path layering over the forward edges, in topological order (Kahn).
73
+ const indeg = new Map<string, number>(nodeIds.map((id) => [id, 0]));
74
+ const fwd = new Map<string, string[]>(nodeIds.map((id) => [id, []]));
75
+ out.forEach((list, from) => {
76
+ for (const ei of list) {
77
+ if (backEdges.has(ei)) continue;
78
+ fwd.get(from)!.push(edges[ei].to);
79
+ indeg.set(edges[ei].to, indeg.get(edges[ei].to)! + 1);
80
+ }
81
+ });
82
+ const layer = new Map<string, number>(nodeIds.map((id) => [id, 0]));
83
+ const queue = nodeIds.filter((id) => indeg.get(id) === 0);
84
+ for (let qi = 0; qi < queue.length; qi++) {
85
+ const id = queue[qi];
86
+ for (const to of fwd.get(id)!) {
87
+ layer.set(to, Math.max(layer.get(to)!, layer.get(id)! + 1));
88
+ indeg.set(to, indeg.get(to)! - 1);
89
+ if (indeg.get(to) === 0) queue.push(to);
90
+ }
91
+ }
92
+
93
+ let maxLayer = 0;
94
+ for (const l of layer.values()) if (l > maxLayer) maxLayer = l;
95
+ return { layer, maxLayer, backEdges };
96
+ }