@principal-ai/subsystems-react 0.56.27 → 0.56.29

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.
@@ -0,0 +1,162 @@
1
+ /**
2
+ * Lines authored between components must survive a collapsed container.
3
+ *
4
+ * A sketch is allowed to draw an edge at the component level. When the
5
+ * container is closed those components are not on screen, and the old
6
+ * `drawnIds.has(source) && drawnIds.has(target)` filter dropped the line
7
+ * outright. The relationship did not go away — it just moved up a level — so
8
+ * the endpoints roll up to their containers instead.
9
+ */
10
+ import { describe, expect, test } from 'bun:test';
11
+ import { resolveC4EdgeEndpoints } from './C4Graph';
12
+ import { MAINTENANCE_BRIDGE_SKETCH } from './__fixtures__/c4SketchGallery';
13
+ import type { C4Edge, C4Element } from './c4';
14
+
15
+ const SYSTEM = 'system:studio';
16
+
17
+ const NODES: C4Element[] = [
18
+ {
19
+ id: 'container:host',
20
+ kind: 'container',
21
+ label: 'Host',
22
+ containerKind: 'application',
23
+ technology: 'Bun',
24
+ parentId: SYSTEM,
25
+ state: 'accepted',
26
+ },
27
+ {
28
+ id: 'container:web',
29
+ kind: 'container',
30
+ label: 'Web',
31
+ containerKind: 'application',
32
+ technology: 'React',
33
+ parentId: SYSTEM,
34
+ state: 'accepted',
35
+ },
36
+ {
37
+ id: 'component:host-store',
38
+ kind: 'component',
39
+ label: 'Store',
40
+ container: 'container:host',
41
+ technology: 'Bun',
42
+ state: 'accepted',
43
+ },
44
+ {
45
+ id: 'component:host-tabs',
46
+ kind: 'component',
47
+ label: 'Tabs',
48
+ container: 'container:host',
49
+ technology: 'Bun',
50
+ state: 'accepted',
51
+ },
52
+ {
53
+ id: 'component:web-view',
54
+ kind: 'component',
55
+ label: 'View',
56
+ container: 'container:web',
57
+ technology: 'React',
58
+ state: 'accepted',
59
+ },
60
+ {
61
+ id: 'external:github',
62
+ kind: 'external-system',
63
+ label: 'GitHub',
64
+ technology: 'REST',
65
+ state: 'accepted',
66
+ },
67
+ ];
68
+
69
+ function edge(id: string, source: string, target: string, label = 'calls'): C4Edge {
70
+ return {
71
+ id,
72
+ source,
73
+ target,
74
+ label,
75
+ protocol: label,
76
+ mechanisms: ['calls'],
77
+ count: 1,
78
+ };
79
+ }
80
+
81
+ const collapsed = new Set(['container:host', 'container:web', 'external:github']);
82
+
83
+ const endpoints = (
84
+ edges: readonly C4Edge[],
85
+ drawnIds: ReadonlySet<string>,
86
+ ) =>
87
+ resolveC4EdgeEndpoints(edges, NODES, drawnIds).map(({ edge: e, source, target }) => ({
88
+ id: e.id,
89
+ source,
90
+ target,
91
+ }));
92
+
93
+ describe('resolveC4EdgeEndpoints — collapsed containers', () => {
94
+ test('rolls a cross-container component line up to its containers', () => {
95
+ // The bug: both components are hidden, so this line used to vanish and the
96
+ // two containers read as unrelated.
97
+ expect(endpoints([edge('e:view-store', 'component:web-view', 'component:host-store')], collapsed))
98
+ .toEqual([{ id: 'e:view-store', source: 'container:web', target: 'container:host' }]);
99
+ });
100
+
101
+ test('drops a line wholly inside one closed container', () => {
102
+ // Both endpoints fold to the same container; there is no boundary to cross,
103
+ // so drawing it would be a self-loop on the closed card.
104
+ expect(endpoints([edge('e:tabs-store', 'component:host-tabs', 'component:host-store')], collapsed))
105
+ .toEqual([]);
106
+ });
107
+
108
+ test('rolls a component endpoint up to its container against an external', () => {
109
+ expect(endpoints([edge('e:store-github', 'component:host-store', 'external:github')], collapsed))
110
+ .toEqual([{ id: 'e:store-github', source: 'container:host', target: 'external:github' }]);
111
+ });
112
+
113
+ test('keeps the real maintenance-bridge cross-container line visible', () => {
114
+ // The sketch authors `binding → progress card` at component level. Both
115
+ // containers start closed, so this line is the only evidence that OpenBot's
116
+ // server feeds its app — and the bug erased it.
117
+ const model = MAINTENANCE_BRIDGE_SKETCH.model;
118
+ const drawn = new Set(
119
+ model.nodes.filter((node) => node.kind !== 'component').map((node) => node.id),
120
+ );
121
+ expect(
122
+ resolveC4EdgeEndpoints(model.edges, model.nodes, drawn).find(
123
+ ({ edge }) => edge.id === 'e:binding-card',
124
+ ),
125
+ ).toEqual(expect.objectContaining({
126
+ source: 'container:openbot-server',
127
+ target: 'container:openbot-app',
128
+ }));
129
+ });
130
+
131
+ test('leaves an authored container line unchanged', () => {
132
+ expect(endpoints([edge('e:host-web', 'container:host', 'container:web')], collapsed))
133
+ .toEqual([{ id: 'e:host-web', source: 'container:host', target: 'container:web' }]);
134
+ });
135
+
136
+ test('drops an endpoint whose container is not drawn either', () => {
137
+ expect(endpoints([edge('e:missing', 'component:missing', 'container:web')], collapsed))
138
+ .toEqual([]);
139
+ });
140
+ });
141
+
142
+ describe('resolveC4EdgeEndpoints — opened containers', () => {
143
+ const opened = new Set([...collapsed, 'component:web-view']);
144
+
145
+ test('keeps a drawn component as the endpoint, on either side', () => {
146
+ // Opening the web container draws its component, so the line stays at its
147
+ // authored level while the still-closed host end rolls up.
148
+ expect(endpoints([edge('e:view-store', 'component:web-view', 'component:host-store')], opened))
149
+ .toEqual([{ id: 'e:view-store', source: 'component:web-view', target: 'container:host' }]);
150
+ });
151
+
152
+ test('keeps both component endpoints when both are drawn', () => {
153
+ const bothOpen = new Set([...collapsed, 'component:web-view', 'component:host-store']);
154
+ expect(endpoints([edge('e:view-store', 'component:web-view', 'component:host-store')], bothOpen))
155
+ .toEqual([{ id: 'e:view-store', source: 'component:web-view', target: 'component:host-store' }]);
156
+ });
157
+
158
+ test('does not roll a component endpoint up when the component itself is drawn', () => {
159
+ expect(endpoints([edge('e:github-view', 'external:github', 'component:web-view')], opened))
160
+ .toEqual([{ id: 'e:github-view', source: 'external:github', target: 'component:web-view' }]);
161
+ });
162
+ });
@@ -844,6 +844,46 @@ export function c4CameraNodeIds(model: C4Model, targetIds?: string | readonly st
844
844
  ];
845
845
  }
846
846
 
847
+ /**
848
+ * Fold authored lines onto the boxes that are actually drawn.
849
+ *
850
+ * A sketch can author a line between components. When their container is
851
+ * closed, those components are hidden — but the relationship they express did
852
+ * not disappear, it changed level. Roll each hidden component endpoint up to
853
+ * its container, so a component-to-component line across containers reads as a
854
+ * container-to-container line instead of being dropped along with the box.
855
+ *
856
+ * A line whose endpoints both fold to the same container is internal to a
857
+ * closed box, so it has nowhere to land and is omitted. Endpoints that are
858
+ * neither drawn nor a component of a drawn container are dropped too.
859
+ *
860
+ * Returns the original edge alongside its resolved endpoints: the caller still
861
+ * needs the authored id/label/provenance for selection and the inspector.
862
+ */
863
+ export function resolveC4EdgeEndpoints(
864
+ edges: readonly C4Edge[],
865
+ nodes: readonly C4Element[],
866
+ drawnIds: ReadonlySet<string>,
867
+ ): Array<{ edge: C4Edge; source: string; target: string }> {
868
+ const byId = new Map(nodes.map((node) => [node.id, node]));
869
+ const anchorOf = (id: string): string | null => {
870
+ if (drawnIds.has(id)) return id;
871
+ const node = byId.get(id);
872
+ if (node?.kind !== 'component') return null;
873
+ const owner = node.container ?? node.parentId;
874
+ return owner && drawnIds.has(owner) ? owner : null;
875
+ };
876
+
877
+ const resolved: Array<{ edge: C4Edge; source: string; target: string }> = [];
878
+ for (const edge of edges) {
879
+ const source = anchorOf(edge.source);
880
+ const target = anchorOf(edge.target);
881
+ if (!source || !target || source === target) continue;
882
+ resolved.push({ edge, source, target });
883
+ }
884
+ return resolved;
885
+ }
886
+
847
887
  /** A fully laid-out open state: the RF nodes/edges and label anchors. */
848
888
  interface Rendered {
849
889
  nodes: Node[];
@@ -1381,20 +1421,23 @@ function Inner({ model, containerFocus, selectedNodeId, selectedEdgeId: controll
1381
1421
  );
1382
1422
 
1383
1423
  const drawnIds = new Set(rfNodes.map((n) => n.id));
1384
- const rfEdges: Edge[] = model.edges
1385
- .filter((e) => drawnIds.has(e.source) && drawnIds.has(e.target))
1386
- .map((e) => {
1424
+ // Authored component lines survive a collapse by rolling up to the
1425
+ // container that now stands in for their endpoint. Without this, closing a
1426
+ // container silently removes every relationship drawn from inside it.
1427
+ const rfEdges: Edge[] = resolveC4EdgeEndpoints(model.edges, model.nodes, drawnIds).map(
1428
+ ({ edge: e, source, target }) => {
1387
1429
  const color = protocolColor(e.protocol);
1388
1430
  return {
1389
1431
  id: e.id,
1390
1432
  type: 'c4-edge',
1391
- source: e.source,
1392
- target: e.target,
1433
+ source,
1434
+ target,
1393
1435
  label: e.protocol ?? e.label,
1394
1436
  markerEnd: { type: MarkerType.ArrowClosed, color, width: 18, height: 18 },
1395
1437
  data: { edge: e, color, path: '' },
1396
1438
  };
1397
- });
1439
+ },
1440
+ );
1398
1441
  // Construct edges attach only when both endpoints are drawn — a step to an
1399
1442
  // external, or to a construct in another container, is dropped here and left
1400
1443
  // to the C4 container-level line.
@@ -36,6 +36,7 @@ import { SUBSYSTEM_MODEL_DESCRIPTION_MAX } from '@principal-ai/subsystems-core';
36
36
  import { ArrowDown, ArrowRight, Boxes, Files, Route, Unlink, X } from 'lucide-react';
37
37
  import {
38
38
  buildSubsystemGraph,
39
+ componentPackageKey,
39
40
  deriveGraphEdges,
40
41
  buildBoundaryLayoutGroups,
41
42
  cardFramesForAliases,
@@ -215,6 +216,38 @@ function edgeMatches(set: ReadonlySet<string>, e: Edge): boolean {
215
216
  return ids?.some((id) => set.has(id)) === true;
216
217
  }
217
218
 
219
+ /**
220
+ * Whether a boundary region holds any of `aliases` — i.e. whether hovering a
221
+ * trail should keep its container bright. Reads component membership directly
222
+ * (process / module / package / directory path) so nesting is transitive: a
223
+ * package lights when a leaf in its nested process is in the trail, even
224
+ * though the package's layout member list only holds the nested group id.
225
+ * This also covers trails folded into nothing (a step whose endpoints share
226
+ * one collapsed card leaves no edge for `endpointsOf` to find).
227
+ */
228
+ function regionTouchesTrailAliases(
229
+ region: SubsystemProcessRegion | undefined,
230
+ aliases: ReadonlySet<string> | null,
231
+ byAlias: ReadonlyMap<string, SubsystemComponent>,
232
+ ): boolean {
233
+ if (!region || !aliases || aliases.size === 0) return false;
234
+ for (const alias of aliases) {
235
+ const comp = byAlias.get(alias);
236
+ if (!comp) continue;
237
+ if (region.kind === 'process') {
238
+ if (comp.process === region.key) return true;
239
+ } else if (region.kind === 'module') {
240
+ if (comp.module?.trim() === region.key) return true;
241
+ } else if (region.kind === 'directory') {
242
+ const m = comp.module?.trim().replace(/\\/g, '/');
243
+ if (m === region.key || (m != null && m.startsWith(`${region.key}/`))) return true;
244
+ } else if (region.kind === 'package') {
245
+ if (componentPackageKey(comp) === region.key) return true;
246
+ }
247
+ }
248
+ return false;
249
+ }
250
+
218
251
  /** Screen-space rectangle of a component node, relative to the graph canvas box. */
219
252
  export interface SubsystemNodeRect {
220
253
  alias: string;
@@ -1827,6 +1860,33 @@ function Inner({ components, trails, graphifyRelations, orderByLine, layoutDirec
1827
1860
  // eslint-disable-next-line react-hooks/exhaustive-deps
1828
1861
  [baseEdges, openedEdgeIds],
1829
1862
  );
1863
+ // Component aliases touched by the hovered trail — the whole flow when a
1864
+ // collapsed row is hovered, one step's endpoints otherwise. Drives container
1865
+ // dimming directly (rather than via rolled-up edge endpoints) so a parent
1866
+ // frame lights when any transitive member is in the trail, and a trail
1867
+ // folded entirely inside one collapsed card still dims the other cards.
1868
+ const hoverTrailAliases = useMemo(() => {
1869
+ if (!hoveredTrailStep || !trails) return null;
1870
+ const tl = trails.find((t) => t.id === hoveredTrailStep.trailId);
1871
+ if (!tl) return null;
1872
+ const ids = new Set<string>();
1873
+ if (hoveredTrailStep.stepIndex == null) {
1874
+ for (const s of tl.steps) {
1875
+ ids.add(s.from);
1876
+ ids.add(s.to);
1877
+ }
1878
+ } else {
1879
+ const step = tl.steps[hoveredTrailStep.stepIndex];
1880
+ if (!step) return null;
1881
+ ids.add(step.from);
1882
+ ids.add(step.to);
1883
+ }
1884
+ return ids.size > 0 ? ids : null;
1885
+ }, [trails, hoveredTrailStep]);
1886
+ const aliasToComponent = useMemo(
1887
+ () => new Map(components.map((c) => [c.alias, c])),
1888
+ [components],
1889
+ );
1830
1890
  // Endpoints of the bright set: the selected step's edge, or the whole flow
1831
1891
  // when no step is focused.
1832
1892
  const brightNodeIds = useMemo(
@@ -1872,13 +1932,20 @@ function Inner({ components, trails, graphifyRelations, orderByLine, layoutDirec
1872
1932
  // there is no opened set, so dimming has to be driven from the
1873
1933
  // participants or nothing outside the focused step would dim.
1874
1934
  const dimSource = previewNodeIds ?? brightNodeIds ?? issueFocusNodeIds;
1935
+ // Hover keeps a container bright when any of its contents is in the
1936
+ // hovered trail — read off component membership so nesting is
1937
+ // transitive and internally-folded trails still count.
1938
+ const touchesHover = regionTouchesTrailAliases(region, hoverTrailAliases, aliasToComponent);
1939
+ const inSpotlight =
1940
+ memberAliases.some((alias) => dimSource?.has(alias) === true) || touchesHover;
1941
+ const anySpotlight = dimSource != null || hoverTrailAliases != null;
1875
1942
  const { hidden, dimmed } = flowNodeVisibility({
1876
1943
  inOpened: memberAliases.some((alias) => openedNodeIds?.has(alias) === true),
1877
1944
  inSelected: memberAliases.some((alias) => brightNodeIds?.has(alias) === true),
1878
- inSpotlight: memberAliases.some((alias) => dimSource?.has(alias) === true),
1945
+ inSpotlight,
1879
1946
  anyOpened: openedNodeIds != null,
1880
1947
  anySelected: brightNodeIds != null,
1881
- anySpotlight: dimSource != null,
1948
+ anySpotlight,
1882
1949
  });
1883
1950
  // Host override wins over the library's derived frame color.
1884
1951
  const color = region?.key != null ? boundaryColors?.[region.key] : undefined;
@@ -1910,10 +1977,10 @@ function Inner({ components, trails, graphifyRelations, orderByLine, layoutDirec
1910
1977
  const { hidden, dimmed } = flowNodeVisibility({
1911
1978
  inOpened: openedNodeIds?.has(n.id) === true,
1912
1979
  inSelected: brightNodeIds?.has(n.id) === true,
1913
- inSpotlight: dimSource?.has(n.id) === true,
1980
+ inSpotlight: dimSource?.has(n.id) === true || (hoverTrailAliases?.has(n.id) === true),
1914
1981
  anyOpened: openedNodeIds != null,
1915
1982
  anySelected: brightNodeIds != null,
1916
- anySpotlight: dimSource != null,
1983
+ anySpotlight: dimSource != null || hoverTrailAliases != null,
1917
1984
  });
1918
1985
  // Component findings key by alias, boundary findings by region node id.
1919
1986
  // The two id spaces are disjoint (`module:` / `process:` are prefixed), so
@@ -1936,7 +2003,7 @@ function Inner({ components, trails, graphifyRelations, orderByLine, layoutDirec
1936
2003
  },
1937
2004
  };
1938
2005
  });
1939
- }, [xyflowNodesBase, openFile, selected, focusNodeIds, openedNodeIds, brightNodeIds, previewNodeIds, issueFocusNodeIds, issueBadgeByAlias, regionIssueByNodeId, boundaryColors, toggleCollapsed]);
2006
+ }, [xyflowNodesBase, openFile, selected, focusNodeIds, openedNodeIds, brightNodeIds, previewNodeIds, hoverTrailAliases, aliasToComponent, issueFocusNodeIds, issueBadgeByAlias, regionIssueByNodeId, boundaryColors, toggleCollapsed]);
1940
2007
 
1941
2008
  const baseNodesKey = useMemo(() => nodes.map((n) => n.id).sort().join(','), [nodes]);
1942
2009
  const baseEdgesKey = useMemo(