@atolis-hq/corum 0.1.40 → 0.1.42

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,103 @@
1
+ /**
2
+ * ADR-009b rule 1 support: index of existing standalone schemas (`{component}.Schema.{name}`)
3
+ * and their field names, used by adapters to reuse-before-inline and to detect shape drift.
4
+ * Schemas marked `removed` are excluded — they are no longer available for reuse.
5
+ */
6
+ export function buildExistingSchemaIndex(graph) {
7
+ const index = new Map();
8
+ for (const node of graph.nodesById.values()) {
9
+ const parts = node.id.split('.');
10
+ if (parts.length !== 3 || parts[1] !== 'Schema')
11
+ continue;
12
+ if (node.state === 'removed')
13
+ continue;
14
+ const fields = new Set();
15
+ const prefix = `${node.id}.fields.`;
16
+ for (const other of graph.nodesById.values()) {
17
+ if (!other.id.startsWith(prefix))
18
+ continue;
19
+ if (other.id.slice(prefix.length).includes('.'))
20
+ continue;
21
+ fields.add(other.id.slice(prefix.length));
22
+ }
23
+ index.set(node.id, fields);
24
+ }
25
+ return index;
26
+ }
27
+ /**
28
+ * ADR-009b rule 4: detect inline→standalone promotions produced by this run —
29
+ * a newly created standalone schema (`{C}.Schema.{N}`) that did not exist before
30
+ * this run, matched against any prior inline copy (`{rootId}.schemas.{N}`) under
31
+ * the same component.
32
+ */
33
+ export function detectSchemaPromotions(priorNodeIds, newlyCreatedSchemaIds) {
34
+ const promotions = [];
35
+ for (const newId of newlyCreatedSchemaIds) {
36
+ if (priorNodeIds.has(newId))
37
+ continue;
38
+ const [component, , name] = newId.split('.');
39
+ for (const oldId of priorNodeIds) {
40
+ if (oldId.split('.')[0] !== component)
41
+ continue;
42
+ if (oldId.endsWith(`.schemas.${name}`)) {
43
+ promotions.push({ oldPrefix: oldId, newPrefix: newId });
44
+ }
45
+ }
46
+ }
47
+ return promotions;
48
+ }
49
+ /**
50
+ * ADR-009b rule 4: mechanically rewrite every edge endpoint that falls under a
51
+ * promoted schema's old subtree to the new subtree — import-derived and
52
+ * design-derived edges alike. Warns (and leaves the edge untouched) when the
53
+ * rewritten target does not exist in the new subtree.
54
+ */
55
+ export function rewritePromotedSchemaEdges(graph, promotions, diagnostics) {
56
+ if (promotions.length === 0)
57
+ return;
58
+ const allEdges = new Map();
59
+ for (const edges of graph.edgesByFrom.values()) {
60
+ for (const edge of edges)
61
+ allEdges.set(edge.id, edge);
62
+ }
63
+ function rewriteEndpoint(id) {
64
+ for (const { oldPrefix, newPrefix } of promotions) {
65
+ if (id !== oldPrefix && !id.startsWith(`${oldPrefix}.`))
66
+ continue;
67
+ const rewritten = newPrefix + id.slice(oldPrefix.length);
68
+ if (!graph.nodesById.has(rewritten))
69
+ return { id, changed: false, unresolved: true };
70
+ return { id: rewritten, changed: true, unresolved: false };
71
+ }
72
+ return { id, changed: false, unresolved: false };
73
+ }
74
+ for (const edge of allEdges.values()) {
75
+ const from = rewriteEndpoint(edge.from);
76
+ const to = rewriteEndpoint(edge.to);
77
+ if (from.unresolved || to.unresolved) {
78
+ diagnostics.push({
79
+ severity: 'warning',
80
+ file: '',
81
+ message: `Cannot rewrite edge ${edge.id} for schema promotion — rewritten target does not exist (unresolvable)`,
82
+ });
83
+ continue;
84
+ }
85
+ if (!from.changed && !to.changed)
86
+ continue;
87
+ removeEdge(graph, edge);
88
+ const rewritten = { ...edge, from: from.id, to: to.id, id: `${from.id}__${edge.type}__${to.id}` };
89
+ addEdge(graph, rewritten);
90
+ }
91
+ }
92
+ function removeEdge(graph, edge) {
93
+ const fromList = graph.edgesByFrom.get(edge.from);
94
+ if (fromList)
95
+ graph.edgesByFrom.set(edge.from, fromList.filter(e => e.id !== edge.id));
96
+ const toList = graph.edgesByTo.get(edge.to);
97
+ if (toList)
98
+ graph.edgesByTo.set(edge.to, toList.filter(e => e.id !== edge.id));
99
+ }
100
+ function addEdge(graph, edge) {
101
+ graph.edgesByFrom.set(edge.from, [...(graph.edgesByFrom.get(edge.from) ?? []), edge]);
102
+ graph.edgesByTo.set(edge.to, [...(graph.edgesByTo.get(edge.to) ?? []), edge]);
103
+ }
@@ -18,14 +18,40 @@ import { getTemplateSchema } from '../loader/template-props.js';
18
18
  * - Edge property validation: edge.properties validated against the
19
19
  * matching EdgeTypeDef's `properties` schema, using the same shallow
20
20
  * validator as node properties.
21
+ * - ADR-009b rule 5: flags an inline schema (`{rootId}.schemas.{name}`)
22
+ * whose (component, name) matches an existing standalone schema
23
+ * (`{component}.Schema.{name}`) — the order-dependent interim state
24
+ * between an importer creating the inline copy and a later re-import
25
+ * converging it via reuse-before-inline.
21
26
  */
22
27
  export function lintGraph(graph) {
23
28
  const diagnostics = [];
24
29
  lintNodeProperties(graph, diagnostics);
25
30
  lintEdgeTypeConstraints(graph, diagnostics);
26
31
  lintEdgeProperties(graph, diagnostics);
32
+ lintInlineStandaloneSchemaCollisions(graph, diagnostics);
27
33
  return diagnostics;
28
34
  }
35
+ function lintInlineStandaloneSchemaCollisions(graph, diagnostics) {
36
+ for (const node of graph.nodesById.values()) {
37
+ const parts = node.id.split('.');
38
+ // Only inline schema nodes themselves (`{rootId}.schemas.{name}`) — not their
39
+ // fields/descendants, and not standalone schemas (`{component}.Schema.{name}`).
40
+ if (parts.length < 3 || parts[parts.length - 2] !== 'schemas')
41
+ continue;
42
+ const name = parts[parts.length - 1];
43
+ const standaloneId = `${node.component}.Schema.${name}`;
44
+ const standalone = graph.nodesById.get(standaloneId);
45
+ if (!standalone || standalone.state === 'removed')
46
+ continue;
47
+ diagnostics.push({
48
+ severity: 'warning',
49
+ file: approxFilePath(graph, node),
50
+ nodeId: node.id,
51
+ message: `inline schema '${node.id}' collides with existing standalone schema '${standaloneId}' — re-import to converge via reuse-before-inline`,
52
+ });
53
+ }
54
+ }
29
55
  function lintNodeProperties(graph, diagnostics) {
30
56
  for (const node of graph.nodesById.values()) {
31
57
  const template = graph.templates.get(node.template);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atolis-hq/corum",
3
- "version": "0.1.40",
3
+ "version": "0.1.42",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
package/web/app.jsx CHANGED
@@ -15,13 +15,17 @@ const { buildNavTree, buildOverlayIndicatorIds } = window.CorumNav;
15
15
  const { parseRoute, buildRoute } = window.CorumRouter;
16
16
  const { SearchModal } = window.CorumSearch;
17
17
 
18
- const PANEL_EDGE_TYPES = new Set(['triggers', 'produces', 'calls', 'implements']);
18
+ // uses-type is excluded here: it's the auto-generated type-reference edge
19
+ // (schema/field-level), surfaced via SchemaCard node-ref links and the
20
+ // includedSchemaNodes panel instead of the root-to-root Connections list.
21
+ const PANEL_EDGE_TYPES = new Set(['triggers', 'produces', 'calls', 'implements', 'reads']);
19
22
 
20
23
  const EDGE_TYPE_STYLES = {
21
24
  triggers: { background: '#fef3c7', color: '#b45309' },
22
25
  produces: { background: '#ccfbf1', color: '#0f766e' },
23
26
  calls: { background: '#ede9fe', color: '#6d28d9' },
24
27
  implements: { background: '#f1f5f9', color: '#475569' },
28
+ reads: { background: '#dbeafe', color: '#1d4ed8' },
25
29
  };
26
30
 
27
31
  function classifyEdges(edges, clusterIds) {
@@ -808,7 +812,7 @@ function NodePage({ nodeId, templates, onNavigate, refreshToken, viewingRef, ove
808
812
  const overlayParam = overlayRefs && overlayRefs.length > 0
809
813
  ? '&' + overlayRefs.map(ref => `overlayRefs=${encodeURIComponent(ref)}`).join('&')
810
814
  : '';
811
- fetch(`/api/cluster?nodeId=${encodeURIComponent(nodeId)}&includeEdges=maps-to,reads,triggers,produces,calls,implements${refParam}${overlayParam}`)
815
+ fetch(`/api/cluster?nodeId=${encodeURIComponent(nodeId)}&includeEdges=maps-to,reads,uses-type,triggers,produces,calls,implements${refParam}${overlayParam}`)
812
816
  .then(response => response.ok ? response.json() : Promise.reject(response.status))
813
817
  .then(setCluster)
814
818
  .catch(err => setError(String(err)));
@@ -42,9 +42,74 @@ function applyEdgeTypeFilter(edges, visibleTypes) {
42
42
  return edges.filter(e => visibleTypes.has(e.type));
43
43
  }
44
44
 
45
+ // Weakly connected components. Edges use {from, to}; extraLinks are additional
46
+ // affinity pairs (e.g. group co-membership) that merge components without
47
+ // being real edges. Returns arrays of node ids.
48
+ function computeConnectedComponents(nodes, edges, extraLinks = []) {
49
+ const adjacency = new Map(nodes.map(n => [n.id, []]));
50
+ for (const link of [...edges, ...extraLinks]) {
51
+ if (!adjacency.has(link.from) || !adjacency.has(link.to)) continue;
52
+ adjacency.get(link.from).push(link.to);
53
+ adjacency.get(link.to).push(link.from);
54
+ }
55
+ const visited = new Set();
56
+ const components = [];
57
+ for (const node of nodes) {
58
+ if (visited.has(node.id)) continue;
59
+ const component = [];
60
+ const queue = [node.id];
61
+ visited.add(node.id);
62
+ while (queue.length) {
63
+ const id = queue.pop();
64
+ component.push(id);
65
+ for (const next of adjacency.get(id)) {
66
+ if (visited.has(next)) continue;
67
+ visited.add(next);
68
+ queue.push(next);
69
+ }
70
+ }
71
+ components.push(component);
72
+ }
73
+ return components;
74
+ }
75
+
76
+ // Scope threads (connected components) to those containing an interaction:
77
+ // an edge of scope.edgeType whose endpoints match the optional
78
+ // fromTemplate/toTemplate. Returns the node ids of matching threads.
79
+ function filterThreadScope(nodes, edges, scope) {
80
+ const components = computeConnectedComponents(nodes, edges);
81
+ const templateById = new Map(nodes.map(n => [n.id, n.template]));
82
+ const matchingEdges = edges.filter(e =>
83
+ e.type === scope.edgeType &&
84
+ (!scope.fromTemplate || templateById.get(e.from) === scope.fromTemplate) &&
85
+ (!scope.toTemplate || templateById.get(e.to) === scope.toTemplate)
86
+ );
87
+ const matchEndpoints = new Set(matchingEdges.flatMap(e => [e.from, e.to]));
88
+ const nodeIds = new Set();
89
+ let matchCount = 0;
90
+ for (const component of components) {
91
+ if (!component.some(id => matchEndpoints.has(id))) continue;
92
+ matchCount += 1;
93
+ for (const id of component) nodeIds.add(id);
94
+ }
95
+ return { nodeIds, threadCount: components.length, matchCount };
96
+ }
97
+
98
+ // Scope to one owner: the owner node itself, nodes owned by it, plus their
99
+ // direct neighbours via the given edges.
100
+ function filterOwnerScope(nodes, edges, parentId) {
101
+ const memberIds = new Set(nodes.filter(n => n.id === parentId || n.parentId === parentId).map(n => n.id));
102
+ const nodeIds = new Set(memberIds);
103
+ for (const e of edges) {
104
+ if (memberIds.has(e.from)) nodeIds.add(e.to);
105
+ if (memberIds.has(e.to)) nodeIds.add(e.from);
106
+ }
107
+ return nodeIds;
108
+ }
109
+
45
110
  if (typeof module !== 'undefined' && module.exports) {
46
- module.exports = { buildComponentMap, applyEdgeTypeFilter, getDisplayName };
111
+ module.exports = { buildComponentMap, applyEdgeTypeFilter, getDisplayName, computeConnectedComponents, filterThreadScope, filterOwnerScope };
47
112
  }
48
113
  if (typeof window !== 'undefined') {
49
- window.CorumGraphUtils = { buildComponentMap, applyEdgeTypeFilter, getDisplayName };
114
+ window.CorumGraphUtils = { buildComponentMap, applyEdgeTypeFilter, getDisplayName, computeConnectedComponents, filterThreadScope, filterOwnerScope };
50
115
  }
@@ -43,6 +43,44 @@ const { buildComponentMap, applyEdgeTypeFilter, getDisplayName } = graphUtils;
43
43
  const none = applyEdgeTypeFilter(edges, new Set([]));
44
44
  assert(none.length === 0, 'applyEdgeTypeFilter: empty set returns nothing');
45
45
 
46
+ const { computeConnectedComponents, filterThreadScope, filterOwnerScope } = graphUtils;
47
+
48
+ const ccAll = computeConnectedComponents(nodes, edges);
49
+ assert(ccAll.length === 1, 'computeConnectedComponents: fully linked graph is one component');
50
+ const ccSplit = computeConnectedComponents(nodes, [edges[0], edges[2]]);
51
+ assert(ccSplit.length === 2, 'computeConnectedComponents: removing bridge edge splits components');
52
+ const ccIsolated = computeConnectedComponents([...nodes, { id: 'lone.Node.x', template: 'X', component: 'lone' }], edges);
53
+ assert(ccIsolated.some(c => c.length === 1 && c[0] === 'lone.Node.x'), 'computeConnectedComponents: isolated node is own component');
54
+ const ccMerged = computeConnectedComponents(nodes, [edges[0], edges[2]], [{ from: 'orders.DomainModel.order', to: 'payments.DomainModel.payment' }]);
55
+ assert(ccMerged.length === 1, 'computeConnectedComponents: extraLinks merge components');
56
+
57
+ const splitEdges = [edges[0], edges[2]];
58
+ const scoped = filterThreadScope(nodes, splitEdges, { edgeType: 'reads', fromTemplate: 'RestAPI', toTemplate: null });
59
+ assert(scoped.threadCount === 2 && scoped.matchCount === 2, 'filterThreadScope: both threads have RestAPI reads');
60
+ const scopedNone = filterThreadScope(nodes, splitEdges, { edgeType: 'calls', fromTemplate: null, toTemplate: null });
61
+ assert(scopedNone.matchCount === 0 && scopedNone.nodeIds.size === 0, 'filterThreadScope: no matching threads yields empty set');
62
+ const scopedTo = filterThreadScope(nodes, splitEdges, { edgeType: 'reads', fromTemplate: null, toTemplate: 'DomainModel' });
63
+ assert(scopedTo.nodeIds.has('orders.RestAPI.ordersApi'), 'filterThreadScope: matching thread includes all its nodes');
64
+
65
+ const aggNodes = [
66
+ { id: 'a.T.agg', template: 'T', parentId: null },
67
+ { id: 'a.T.agg.ops.one', template: 'Op', parentId: 'a.T.agg' },
68
+ { id: 'a.T.agg.ops.two', template: 'Op', parentId: 'a.T.agg' },
69
+ { id: 'a.T.other', template: 'T', parentId: null },
70
+ { id: 'a.T.unrelated', template: 'T', parentId: null },
71
+ ];
72
+ const aggEdges = [
73
+ { id: 'ae1', from: 'a.T.agg.ops.one', to: 'a.T.other', type: 'reads' },
74
+ { id: 'ae2', from: 'a.T.unrelated', to: 'a.T.other', type: 'reads' },
75
+ { id: 'ae3', from: 'a.T.reader', to: 'a.T.agg', type: 'reads' },
76
+ ];
77
+ const aggScope = filterOwnerScope([...aggNodes, { id: 'a.T.reader', template: 'T', parentId: null }], aggEdges, 'a.T.agg');
78
+ assert(aggScope.has('a.T.agg.ops.one') && aggScope.has('a.T.agg.ops.two'), 'filterOwnerScope: keeps members');
79
+ assert(aggScope.has('a.T.agg'), 'filterOwnerScope: keeps the owner node itself');
80
+ assert(aggScope.has('a.T.other'), 'filterOwnerScope: keeps direct neighbours');
81
+ assert(aggScope.has('a.T.reader'), 'filterOwnerScope: keeps neighbours of the owner node');
82
+ assert(!aggScope.has('a.T.unrelated'), 'filterOwnerScope: drops unrelated nodes');
83
+
46
84
  console.log(`\n${passed} passed, ${failed} failed`);
47
85
  if (failed > 0) process.exit(1);
48
86
  })();