archidocs 0.0.0-stage → 1.2.0

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.
Files changed (167) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +181 -2
  3. package/dist/cli/commands.d.ts +322 -0
  4. package/dist/cli/commands.js +174 -0
  5. package/dist/cli/dev.d.ts +31 -0
  6. package/dist/cli/dev.js +156 -0
  7. package/dist/cli/diff-format.d.ts +9 -0
  8. package/dist/cli/diff-format.js +50 -0
  9. package/dist/cli/format.d.ts +15 -0
  10. package/dist/cli/format.js +64 -0
  11. package/dist/cli/init.d.ts +17 -0
  12. package/dist/cli/init.js +82 -0
  13. package/dist/cli/main.d.ts +2 -0
  14. package/dist/cli/main.js +330 -0
  15. package/dist/core/adr.d.ts +28 -0
  16. package/dist/core/adr.js +88 -0
  17. package/dist/core/adrs.d.ts +20 -0
  18. package/dist/core/adrs.js +86 -0
  19. package/dist/core/catalogue.d.ts +7 -0
  20. package/dist/core/catalogue.js +35 -0
  21. package/dist/core/compare.d.ts +21 -0
  22. package/dist/core/compare.js +62 -0
  23. package/dist/core/deployment-views.d.ts +28 -0
  24. package/dist/core/deployment-views.js +108 -0
  25. package/dist/core/deployments.d.ts +18 -0
  26. package/dist/core/deployments.js +21 -0
  27. package/dist/core/diff.d.ts +44 -0
  28. package/dist/core/diff.js +138 -0
  29. package/dist/core/dynamic-views.d.ts +73 -0
  30. package/dist/core/dynamic-views.js +162 -0
  31. package/dist/core/files.d.ts +2 -0
  32. package/dist/core/files.js +1 -0
  33. package/dist/core/fs.d.ts +19 -0
  34. package/dist/core/fs.js +21 -0
  35. package/dist/core/history-scope.d.ts +40 -0
  36. package/dist/core/history-scope.js +63 -0
  37. package/dist/core/impact.d.ts +34 -0
  38. package/dist/core/impact.js +62 -0
  39. package/dist/core/index.d.ts +20 -0
  40. package/dist/core/index.js +18 -0
  41. package/dist/core/load.d.ts +32 -0
  42. package/dist/core/load.js +465 -0
  43. package/dist/core/model.d.ts +150 -0
  44. package/dist/core/model.js +1 -0
  45. package/dist/core/ownership.d.ts +34 -0
  46. package/dist/core/ownership.js +65 -0
  47. package/dist/core/paths.d.ts +47 -0
  48. package/dist/core/paths.js +115 -0
  49. package/dist/core/query.d.ts +51 -0
  50. package/dist/core/query.js +77 -0
  51. package/dist/core/refs.d.ts +31 -0
  52. package/dist/core/refs.js +50 -0
  53. package/dist/core/rules.d.ts +41 -0
  54. package/dist/core/rules.js +1033 -0
  55. package/dist/core/save-as-view.d.ts +45 -0
  56. package/dist/core/save-as-view.js +114 -0
  57. package/dist/core/saved-views.d.ts +48 -0
  58. package/dist/core/saved-views.js +243 -0
  59. package/dist/core/schema-check.d.ts +13 -0
  60. package/dist/core/schema-check.js +79 -0
  61. package/dist/core/search.d.ts +27 -0
  62. package/dist/core/search.js +63 -0
  63. package/dist/core/suggest.d.ts +3 -0
  64. package/dist/core/suggest.js +30 -0
  65. package/dist/core/views.d.ts +126 -0
  66. package/dist/core/views.js +217 -0
  67. package/dist/core/yaml.d.ts +16 -0
  68. package/dist/core/yaml.js +52 -0
  69. package/dist/git.d.ts +7 -0
  70. package/dist/git.js +23 -0
  71. package/dist/guide/index.d.ts +53 -0
  72. package/dist/guide/index.js +57 -0
  73. package/dist/history/git.d.ts +39 -0
  74. package/dist/history/git.js +126 -0
  75. package/dist/history/index.d.ts +118 -0
  76. package/dist/history/index.js +223 -0
  77. package/dist/history/migrate.d.ts +16 -0
  78. package/dist/history/migrate.js +30 -0
  79. package/dist/mcp/history.d.ts +36 -0
  80. package/dist/mcp/history.js +175 -0
  81. package/dist/mcp/http.d.ts +23 -0
  82. package/dist/mcp/http.js +85 -0
  83. package/dist/mcp/index.d.ts +3 -0
  84. package/dist/mcp/index.js +3 -0
  85. package/dist/mcp/server.d.ts +54 -0
  86. package/dist/mcp/server.js +824 -0
  87. package/dist/mcp/source.d.ts +50 -0
  88. package/dist/mcp/source.js +132 -0
  89. package/dist/mcp/writes.d.ts +10 -0
  90. package/dist/mcp/writes.js +267 -0
  91. package/dist/render/font.d.ts +14 -0
  92. package/dist/render/font.js +11 -0
  93. package/dist/render/index.d.ts +8 -0
  94. package/dist/render/index.js +8 -0
  95. package/dist/render/layout.d.ts +53 -0
  96. package/dist/render/layout.js +199 -0
  97. package/dist/render/node.d.ts +3 -0
  98. package/dist/render/node.js +8 -0
  99. package/dist/render/png.d.ts +11 -0
  100. package/dist/render/png.js +29 -0
  101. package/dist/render/sequence-svg.d.ts +33 -0
  102. package/dist/render/sequence-svg.js +98 -0
  103. package/dist/render/sequence.d.ts +103 -0
  104. package/dist/render/sequence.js +142 -0
  105. package/dist/render/svg.d.ts +36 -0
  106. package/dist/render/svg.js +222 -0
  107. package/dist/render/teams.d.ts +12 -0
  108. package/dist/render/teams.js +31 -0
  109. package/dist/render/text.d.ts +15 -0
  110. package/dist/render/text.js +29 -0
  111. package/dist/render/tokens.d.ts +53 -0
  112. package/dist/render/tokens.js +71 -0
  113. package/dist/schema/adr.d.ts +36 -0
  114. package/dist/schema/adr.js +27 -0
  115. package/dist/schema/bundle.d.ts +726 -0
  116. package/dist/schema/bundle.js +171 -0
  117. package/dist/schema/index.d.ts +4 -0
  118. package/dist/schema/index.js +4 -0
  119. package/dist/schema/json-schema.d.ts +8 -0
  120. package/dist/schema/json-schema.js +19 -0
  121. package/dist/schema/workspace.d.ts +1066 -0
  122. package/dist/schema/workspace.js +271 -0
  123. package/dist/viewer/assets/index-DvKgCpBr.css +1 -0
  124. package/dist/viewer/assets/index-gucix3xm.js +113 -0
  125. package/dist/viewer/assets/index_bg-Blvrv-U2.wasm +0 -0
  126. package/dist/viewer/assets/index_bg-D3t31u3-.js +1 -0
  127. package/dist/viewer/assets/layout.worker-Bg-OaLVN.js +26 -0
  128. package/dist/viewer/assets/overpass-latin-QyaMbcmi.woff2 +0 -0
  129. package/dist/viewer/assets/overpass-latin-ext-DSRhPfzW.woff2 +0 -0
  130. package/dist/viewer/assets/overpass-mono-latin-BTquVRaO.woff2 +0 -0
  131. package/dist/viewer/assets/overpass-mono-latin-ext-D47EiU4_.woff2 +0 -0
  132. package/dist/viewer/assets/png-BDKIXtm5.js +1 -0
  133. package/dist/viewer/assets/svg-Bulv5YWn.js +2 -0
  134. package/dist/viewer/favicon.svg +1 -0
  135. package/dist/viewer/index.html +53 -0
  136. package/dist/writer/adrs.d.ts +21 -0
  137. package/dist/writer/adrs.js +72 -0
  138. package/dist/writer/deployments.d.ts +51 -0
  139. package/dist/writer/deployments.js +145 -0
  140. package/dist/writer/diff.d.ts +6 -0
  141. package/dist/writer/diff.js +77 -0
  142. package/dist/writer/document.d.ts +25 -0
  143. package/dist/writer/document.js +79 -0
  144. package/dist/writer/edit.d.ts +78 -0
  145. package/dist/writer/edit.js +63 -0
  146. package/dist/writer/index.d.ts +67 -0
  147. package/dist/writer/index.js +310 -0
  148. package/dist/writer/overlay.d.ts +7 -0
  149. package/dist/writer/overlay.js +26 -0
  150. package/dist/writer/views.d.ts +29 -0
  151. package/dist/writer/views.js +78 -0
  152. package/dist/writer/workspace.d.ts +25 -0
  153. package/dist/writer/workspace.js +56 -0
  154. package/guide/SKILL.md +36 -0
  155. package/guide/topics/add-service.md +30 -0
  156. package/guide/topics/bootstrap.md +28 -0
  157. package/guide/topics/change.md +32 -0
  158. package/guide/topics/deployments.md +36 -0
  159. package/guide/topics/dynamic-view.md +39 -0
  160. package/guide/topics/propose-adr.md +12 -0
  161. package/guide/topics/relationships.md +39 -0
  162. package/guide/topics/saved-view.md +36 -0
  163. package/package.json +75 -4
  164. package/schemas/adr.schema.json +82 -0
  165. package/schemas/model.schema.json +1381 -0
  166. package/schemas/project.schema.json +24 -0
  167. package/schemas/workspace.schema.json +72 -0
@@ -0,0 +1,65 @@
1
+ import { cmp, within } from "./query.js";
2
+ const kinds = ["person", "system", "service", "component", "target"];
3
+ /**
4
+ * The Elements and Deployment Targets `team` owns, grouped by kind in the
5
+ * order Person, System, Service, Component, Target, each sorted by id. An
6
+ * Element is listed under its own Owner, whoever owns its parent.
7
+ *
8
+ * For an Element, Upstream Teams own the consumers of Relationships into it
9
+ * or its descendants, and Downstream Teams own their providers; a `via`
10
+ * broker is a provider of both the consumer and the provider. For a Target,
11
+ * Upstream Teams own the Deployments on it or the Targets beneath it, and
12
+ * Downstream Teams own the Targets above it. The Team itself is never
13
+ * counted.
14
+ */
15
+ export function listOwned(bundle, team, kind) {
16
+ const ownerOf = new Map([...bundle.elements, ...bundle.targets].map((e) => [e.id, e.owner]));
17
+ const inside = within(bundle);
18
+ const others = (ids) => new Set([...ids].map((id) => ownerOf.get(id)).filter((o) => o && o !== team)).size;
19
+ const elementItem = (e) => {
20
+ const up = new Set();
21
+ const down = new Set();
22
+ const ins = (x) => x !== null && inside(x, e.id);
23
+ for (const r of bundle.relationships) {
24
+ if (ins(r.from) && !ins(r.to))
25
+ down.add(r.to);
26
+ if (ins(r.to) && !ins(r.from))
27
+ up.add(r.from);
28
+ if (r.via && !ins(r.via) && (ins(r.from) || ins(r.to)))
29
+ down.add(r.via);
30
+ if (ins(r.via))
31
+ for (const x of [r.from, r.to])
32
+ if (!ins(x))
33
+ up.add(x);
34
+ }
35
+ return { id: e.id, name: e.name, parent: e.parent, upstreamTeams: others(up), downstreamTeams: others(down) };
36
+ };
37
+ const targetParent = new Map(bundle.targets.map((t) => [t.id, t.parent]));
38
+ const underTarget = (id, root) => {
39
+ for (let at = id; at; at = targetParent.get(at))
40
+ if (at === root)
41
+ return true;
42
+ return false;
43
+ };
44
+ const targetItem = (t) => {
45
+ const deployers = new Set(bundle.deployments.filter((d) => underTarget(d.target, t.id)).map((d) => d.owner));
46
+ const beneath = bundle.targets.filter((x) => x.id !== t.id && underTarget(x.id, t.id)).map((x) => x.owner);
47
+ const above = new Set();
48
+ for (let at = t.parent; at; at = targetParent.get(at) ?? null)
49
+ above.add(ownerOf.get(at) ?? "");
50
+ const count = (owners) => new Set([...owners].filter((o) => o && o !== team)).size;
51
+ return { id: t.id, name: t.name, parent: t.parent, upstreamTeams: count([...deployers, ...beneath]), downstreamTeams: count(above) };
52
+ };
53
+ const groups = [];
54
+ for (const k of kinds) {
55
+ if (kind && k !== kind)
56
+ continue;
57
+ const items = k === "target"
58
+ ? bundle.targets.filter((t) => t.owner === team).map(targetItem)
59
+ : bundle.elements.filter((e) => e.kind === k && e.owner === team).map(elementItem);
60
+ items.sort((a, b) => cmp(a.id, b.id));
61
+ if (items.length)
62
+ groups.push({ kind: k, total: items.length, items });
63
+ }
64
+ return { team, kind: kind ?? null, total: groups.reduce((n, g) => n + g.total, 0), groups };
65
+ }
@@ -0,0 +1,47 @@
1
+ import type { Bundle, BundleRelationship } from "../schema/bundle.ts";
2
+ /** One step along a path, with the Relationships that carry it. */
3
+ export interface Hop {
4
+ from: string;
5
+ to: string;
6
+ relationships: BundleRelationship[];
7
+ }
8
+ /** A path between the two Elements, in the direction its Relationships point. */
9
+ export interface Path {
10
+ hops: Hop[];
11
+ }
12
+ /**
13
+ * The Relationships straight between the two Elements in one direction:
14
+ * declared on exactly those two (`derived: false`), or declared beneath
15
+ * either and lifted to them (`derived: true`).
16
+ */
17
+ export interface DirectEdge {
18
+ from: string;
19
+ to: string;
20
+ derived: boolean;
21
+ relationships: BundleRelationship[];
22
+ }
23
+ export interface PathsResult {
24
+ from: string;
25
+ to: string;
26
+ maxHops: number;
27
+ direct: DirectEdge[];
28
+ /** The shortest paths each way, shortest first. */
29
+ paths: Path[];
30
+ }
31
+ /**
32
+ * How two Elements are connected, compared at their own levels. Each
33
+ * Relationship's end inside either Element is lifted to that Element; every
34
+ * other end is lifted to the finer of the two Elements' levels. A Relationship
35
+ * through a `via` broker runs consumer → broker → provider, so the broker is
36
+ * a hop. Paths follow Relationships from consumer to provider, and are
37
+ * searched both ways: `from` to `to`, then `to` to `from`.
38
+ */
39
+ export declare function findPaths(bundle: Bundle, from: string, to: string, options?: {
40
+ maxHops?: number;
41
+ }): PathsResult;
42
+ /**
43
+ * The paths a path selector or Lens takes between two Elements, both ways:
44
+ * `shortest` gives the shortest paths each way, however long; n gives every
45
+ * path without a repeated node of at most n hops. Unknown ids give none.
46
+ */
47
+ export declare function pathsBetween(bundle: Bundle, from: string, to: string, paths: "shortest" | number): Path[];
@@ -0,0 +1,115 @@
1
+ import { cmp, finer, Graph, levelOf, lifter, within } from "./query.js";
2
+ /**
3
+ * How two Elements are connected, compared at their own levels. Each
4
+ * Relationship's end inside either Element is lifted to that Element; every
5
+ * other end is lifted to the finer of the two Elements' levels. A Relationship
6
+ * through a `via` broker runs consumer → broker → provider, so the broker is
7
+ * a hop. Paths follow Relationships from consumer to provider, and are
8
+ * searched both ways: `from` to `to`, then `to` to `from`.
9
+ */
10
+ export function findPaths(bundle, from, to, options = {}) {
11
+ const maxHops = options.maxHops ?? 4;
12
+ const byId = new Map(bundle.elements.map((e) => [e.id, e]));
13
+ const a = byId.get(from);
14
+ const b = byId.get(to);
15
+ if (!a || !b)
16
+ throw new Error(`Unknown Element "${!a ? from : to}"`);
17
+ const inside = within(bundle);
18
+ const graph = connections(bundle, from, to);
19
+ const direct = [];
20
+ for (const [x, y] of [[from, to], [to, from]]) {
21
+ const rels = bundle.relationships.filter((r) => inside(r.from, x) && inside(r.to, y) && !(inside(r.from, y) && inside(r.to, x)));
22
+ const declared = rels.filter((r) => r.from === x && r.to === y);
23
+ const derived = rels.filter((r) => !declared.includes(r));
24
+ if (declared.length)
25
+ direct.push({ from: x, to: y, derived: false, relationships: declared });
26
+ if (derived.length)
27
+ direct.push({ from: x, to: y, derived: true, relationships: derived });
28
+ }
29
+ const paths = [...shortestPaths(graph, from, to, maxHops), ...shortestPaths(graph, to, from, maxHops)].sort((p, q) => p.hops.length - q.hops.length || cmp(ids(p), ids(q)));
30
+ return { from, to, maxHops, direct, paths };
31
+ }
32
+ /**
33
+ * The paths a path selector or Lens takes between two Elements, both ways:
34
+ * `shortest` gives the shortest paths each way, however long; n gives every
35
+ * path without a repeated node of at most n hops. Unknown ids give none.
36
+ */
37
+ export function pathsBetween(bundle, from, to, paths) {
38
+ const known = new Set(bundle.elements.map((e) => e.id));
39
+ if (!known.has(from) || !known.has(to) || from === to)
40
+ return [];
41
+ const graph = connections(bundle, from, to);
42
+ const found = paths === "shortest"
43
+ ? [...shortestPaths(graph, from, to, known.size), ...shortestPaths(graph, to, from, known.size)]
44
+ : [...allPaths(graph, from, to, paths), ...allPaths(graph, to, from, paths)];
45
+ return found.sort((p, q) => p.hops.length - q.hops.length || cmp(ids(p), ids(q)));
46
+ }
47
+ /**
48
+ * The graph `findPaths` searches between two Elements: each Relationship's
49
+ * end inside either Element is lifted to it, every other end to the finer of
50
+ * their two levels, and a `via` broker is a hop of its own.
51
+ */
52
+ function connections(bundle, from, to) {
53
+ const byId = new Map(bundle.elements.map((e) => [e.id, e]));
54
+ const inside = within(bundle);
55
+ const lift = lifter(bundle, finer(levelOf(byId.get(from)), levelOf(byId.get(to))));
56
+ // The more specific of the two wins when one is inside the other.
57
+ const [inner, outer] = inside(to, from) ? [to, from] : [from, to];
58
+ const place = (id) => (inside(id, inner) ? inner : inside(id, outer) ? outer : lift(id));
59
+ const graph = new Graph();
60
+ for (const r of bundle.relationships) {
61
+ if (r.via) {
62
+ graph.add(place(r.from), place(r.via), r);
63
+ graph.add(place(r.via), place(r.to), r);
64
+ }
65
+ else
66
+ graph.add(place(r.from), place(r.to), r);
67
+ }
68
+ return graph;
69
+ }
70
+ /** Every path from `source` to `target` of at most `maxHops` hops that repeats no node. */
71
+ function allPaths(graph, source, target, maxHops) {
72
+ const found = [];
73
+ const walk = (nodes) => {
74
+ const at = nodes.at(-1);
75
+ if (at === target) {
76
+ found.push({ hops: nodes.slice(1).map((n, i) => ({ from: nodes[i], to: n, relationships: graph.out.get(nodes[i]).get(n) })) });
77
+ return;
78
+ }
79
+ if (nodes.length > maxHops)
80
+ return;
81
+ for (const [next] of graph.next(at))
82
+ if (!nodes.includes(next))
83
+ walk([...nodes, next]);
84
+ };
85
+ walk([source]);
86
+ return found;
87
+ }
88
+ const ids = (p) => [p.hops[0]?.from, ...p.hops.map((h) => h.to)].join("\0");
89
+ /** Every shortest path from `source` to `target` of at most `maxHops` hops. */
90
+ function shortestPaths(graph, source, target, maxHops) {
91
+ const dist = new Map([[source, 0]]);
92
+ const preds = new Map();
93
+ let frontier = [source];
94
+ for (let d = 1; d <= maxHops && frontier.length && !dist.has(target); d++) {
95
+ const next = [];
96
+ for (const node of frontier) {
97
+ for (const [n] of graph.next(node)) {
98
+ if (dist.has(n) && dist.get(n) !== d)
99
+ continue;
100
+ if (!dist.has(n)) {
101
+ dist.set(n, d);
102
+ next.push(n);
103
+ }
104
+ preds.set(n, [...(preds.get(n) ?? []), node]);
105
+ }
106
+ }
107
+ frontier = next;
108
+ }
109
+ if (!dist.has(target) || source === target)
110
+ return [];
111
+ const walk = (node) => (node === source ? [[source]] : (preds.get(node) ?? []).flatMap((p) => walk(p).map((w) => [...w, node])));
112
+ return walk(target).map((nodes) => ({
113
+ hops: nodes.slice(1).map((n, i) => ({ from: nodes[i], to: n, relationships: graph.out.get(nodes[i]).get(n) })),
114
+ }));
115
+ }
@@ -0,0 +1,51 @@
1
+ import type { Bundle, BundleElement, BundleRelationship } from "../schema/bundle.ts";
2
+ /** The levels edges can be derived at, from System (C1) down to Component (C3). */
3
+ export type Level = "system" | "service" | "component";
4
+ /** An edge between two Elements at one level, with the declared Relationships beneath it. */
5
+ export interface DerivedEdge {
6
+ from: string;
7
+ to: string;
8
+ relationships: BundleRelationship[];
9
+ }
10
+ /** The level an Element sits at: a Person sits at System level. */
11
+ export declare const levelOf: (e: BundleElement) => Level;
12
+ /** The finer of two levels. */
13
+ export declare const finer: (a: Level, b: Level) => Level;
14
+ /**
15
+ * Lifts an Element id to its ancestor at `level`; an Element with no such
16
+ * ancestor, such as a Person or a Service outside any System, stays itself.
17
+ */
18
+ export declare function lifter(bundle: Bundle, level: Level): (id: string) => string;
19
+ /** Whether `id` is `root` or one of its descendants. */
20
+ export declare function within(bundle: Bundle): (id: string, root: string) => boolean;
21
+ /**
22
+ * Derives the edges between Elements at `level`: each Relationship's ends are
23
+ * lifted to their ancestor at that level, and the Relationships landing on
24
+ * one pair form one edge. An edge that collapses onto a single node is
25
+ * dropped. Edges are sorted by `from`, then `to`.
26
+ */
27
+ export declare function deriveEdges(bundle: Bundle, level: Level): DerivedEdge[];
28
+ /** A directed graph whose edges carry the Relationships beneath them. */
29
+ export declare class Graph {
30
+ readonly out: Map<string, Map<string, {
31
+ id: string | null;
32
+ from: string;
33
+ to: string;
34
+ description: string | null;
35
+ technology: string | null;
36
+ interaction: "sync" | "async";
37
+ via: string | null;
38
+ channel: string | null;
39
+ tags: string[];
40
+ source: {
41
+ file: string;
42
+ line: number;
43
+ };
44
+ }[]>>;
45
+ add(from: string, to: string, r: BundleRelationship): void;
46
+ /** The graph with every edge turned round. */
47
+ reversed(): Graph;
48
+ /** Neighbours of a node, sorted by id. */
49
+ next(id: string): [string, BundleRelationship[]][];
50
+ }
51
+ export declare const cmp: (a: string, b: string) => 0 | 1 | -1;
@@ -0,0 +1,77 @@
1
+ const depth = { person: 1, system: 1, service: 2, component: 3 };
2
+ /** The level an Element sits at: a Person sits at System level. */
3
+ export const levelOf = (e) => (e.kind === "person" ? "system" : e.kind);
4
+ /** The finer of two levels. */
5
+ export const finer = (a, b) => (depth[a] >= depth[b] ? a : b);
6
+ /**
7
+ * Lifts an Element id to its ancestor at `level`; an Element with no such
8
+ * ancestor, such as a Person or a Service outside any System, stays itself.
9
+ */
10
+ export function lifter(bundle, level) {
11
+ const byId = new Map(bundle.elements.map((e) => [e.id, e]));
12
+ return (id) => {
13
+ let e = byId.get(id);
14
+ while (e && depth[e.kind] > depth[level] && e.parent !== null && byId.has(e.parent))
15
+ e = byId.get(e.parent);
16
+ return e?.id ?? id;
17
+ };
18
+ }
19
+ /** Whether `id` is `root` or one of its descendants. */
20
+ export function within(bundle) {
21
+ const parentOf = new Map(bundle.elements.map((e) => [e.id, e.parent]));
22
+ return (id, root) => {
23
+ for (let at = id; at; at = parentOf.get(at))
24
+ if (at === root)
25
+ return true;
26
+ return false;
27
+ };
28
+ }
29
+ /**
30
+ * Derives the edges between Elements at `level`: each Relationship's ends are
31
+ * lifted to their ancestor at that level, and the Relationships landing on
32
+ * one pair form one edge. An edge that collapses onto a single node is
33
+ * dropped. Edges are sorted by `from`, then `to`.
34
+ */
35
+ export function deriveEdges(bundle, level) {
36
+ const lift = lifter(bundle, level);
37
+ const edges = new Map();
38
+ for (const r of bundle.relationships) {
39
+ const from = lift(r.from);
40
+ const to = lift(r.to);
41
+ if (from === to)
42
+ continue;
43
+ const key = `${from}\0${to}`;
44
+ const edge = edges.get(key) ?? { from, to, relationships: [] };
45
+ edge.relationships.push(r);
46
+ edges.set(key, edge);
47
+ }
48
+ return [...edges.values()].sort((a, b) => cmp(a.from, b.from) || cmp(a.to, b.to));
49
+ }
50
+ /** A directed graph whose edges carry the Relationships beneath them. */
51
+ export class Graph {
52
+ out = new Map();
53
+ add(from, to, r) {
54
+ if (from === to)
55
+ return;
56
+ const next = this.out.get(from) ?? new Map();
57
+ const rels = next.get(to) ?? [];
58
+ if (!rels.includes(r))
59
+ rels.push(r);
60
+ next.set(to, rels);
61
+ this.out.set(from, next);
62
+ }
63
+ /** The graph with every edge turned round. */
64
+ reversed() {
65
+ const g = new Graph();
66
+ for (const [from, next] of this.out)
67
+ for (const [to, rels] of next)
68
+ for (const r of rels)
69
+ g.add(to, from, r);
70
+ return g;
71
+ }
72
+ /** Neighbours of a node, sorted by id. */
73
+ next(id) {
74
+ return [...(this.out.get(id) ?? new Map())].sort(([a], [b]) => cmp(a, b));
75
+ }
76
+ }
77
+ export const cmp = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
@@ -0,0 +1,31 @@
1
+ import type { ElementDecl, LoadedWorkspace, Project } from "./model.ts";
2
+ /** A well-formed reference written in `project`, as a qualified id; undefined when malformed or the Project has no id. */
3
+ export declare function qualify(ref: unknown, project: Project): string | undefined;
4
+ /** Indexes declarations by qualified id, keeping the first of each and skipping Projects without a usable id. */
5
+ export declare function indexByQualifiedId<T extends {
6
+ id: string;
7
+ project: Project;
8
+ }>(decls: readonly T[]): Map<string, T>;
9
+ /** What an Element reference (`id` or `project:id`) written in `from` resolves to. */
10
+ export type Resolved = {
11
+ ok: true;
12
+ element: ElementDecl; /** The reference qualifies an id of its own Project. */
13
+ ownPrefix: boolean;
14
+ } | {
15
+ ok: false;
16
+ reason: "malformed";
17
+ } | {
18
+ ok: false;
19
+ reason: "unknown-project";
20
+ project: string;
21
+ id: string;
22
+ } | {
23
+ ok: false;
24
+ reason: "unknown-element";
25
+ project: Project;
26
+ id: string;
27
+ };
28
+ /** Resolves Element references against a loaded Workspace. */
29
+ export declare function referenceResolver(ws: LoadedWorkspace): (ref: unknown, from: Project) => Resolved;
30
+ /** An Element's qualified id, `project:id`. */
31
+ export declare const qualified: (e: ElementDecl) => string;
@@ -0,0 +1,50 @@
1
+ import { Ref } from "../schema/workspace.js";
2
+ /** A well-formed reference written in `project`, as a qualified id; undefined when malformed or the Project has no id. */
3
+ export function qualify(ref, project) {
4
+ if (!Ref.safeParse(ref).success)
5
+ return undefined;
6
+ const text = ref;
7
+ if (text.includes(":"))
8
+ return text;
9
+ return project.id === undefined ? undefined : `${project.id}:${text}`;
10
+ }
11
+ /** Indexes declarations by qualified id, keeping the first of each and skipping Projects without a usable id. */
12
+ export function indexByQualifiedId(decls) {
13
+ const index = new Map();
14
+ for (const d of decls) {
15
+ if (d.project.id === undefined || d.project.duplicateOf)
16
+ continue;
17
+ const key = `${d.project.id}:${d.id}`;
18
+ if (!index.has(key))
19
+ index.set(key, d);
20
+ }
21
+ return index;
22
+ }
23
+ /** Resolves Element references against a loaded Workspace. */
24
+ export function referenceResolver(ws) {
25
+ const projects = new Map();
26
+ for (const p of ws.projects)
27
+ if (p.id !== undefined && !p.duplicateOf && !projects.has(p.id))
28
+ projects.set(p.id, p);
29
+ const elements = new Map();
30
+ for (const e of ws.elements) {
31
+ const inProject = elements.get(e.project) ?? new Map();
32
+ if (!inProject.has(e.id))
33
+ inProject.set(e.id, e);
34
+ elements.set(e.project, inProject);
35
+ }
36
+ return (ref, from) => {
37
+ if (!Ref.safeParse(ref).success)
38
+ return { ok: false, reason: "malformed" };
39
+ const [prefix, id] = ref.includes(":") ? ref.split(":") : [undefined, ref];
40
+ const project = prefix === undefined ? from : projects.get(prefix);
41
+ if (!project)
42
+ return { ok: false, reason: "unknown-project", project: prefix, id };
43
+ const element = elements.get(project)?.get(id);
44
+ if (!element)
45
+ return { ok: false, reason: "unknown-element", project, id };
46
+ return { ok: true, element, ownPrefix: prefix !== undefined && project === from };
47
+ };
48
+ }
49
+ /** An Element's qualified id, `project:id`. */
50
+ export const qualified = (e) => `${e.project.id}:${e.id}`;
@@ -0,0 +1,41 @@
1
+ import type { Bundle } from "../schema/bundle.ts";
2
+ import type { Files } from "./files.ts";
3
+ import type { IssueDraft, LoadedWorkspace, Severity } from "./model.ts";
4
+ /**
5
+ * A check rule. Its examples are small Workspaces (paths relative to the
6
+ * Workspace root): `failing` triggers the rule, `passing` is the same
7
+ * Workspace with the fix applied and has no issues at all.
8
+ */
9
+ export interface RuleDefinition {
10
+ id: string;
11
+ severity: Severity;
12
+ summary: string;
13
+ fix: string;
14
+ example: {
15
+ failing: Files;
16
+ passing: Files;
17
+ };
18
+ /** `bundle` is the Bundle built from the same Workspace, for rules about what it resolves to. */
19
+ check(workspace: LoadedWorkspace, bundle: Bundle): IssueDraft[];
20
+ }
21
+ export declare const rules: RuleDefinition[];
22
+ /** Base URL of the per-rule pages on the docs site; a rule's page is `<base><rule-id>/`. */
23
+ export declare const RULE_DOCS_BASE_URL = "https://archidocs.dev/docs/reference/rules/";
24
+ /** The docs page for a check rule. The site keeps a page for every rule id, removed ones included. */
25
+ export declare const ruleDocsUrl: (ruleId: string) => string;
26
+ /** An issue with a link to its rule's docs page, as `check --format json` and MCP report it. */
27
+ export declare const withDocs: <T extends {
28
+ rule: string;
29
+ }>(issue: T) => T & {
30
+ docs: string;
31
+ };
32
+ /** A check rule taken out of the registry. The docs site keeps a stub page for it, so links older CLIs print still work. */
33
+ export interface RemovedRule {
34
+ id: string;
35
+ /** The release that removed it, such as `"1.4.0"`. */
36
+ removedIn: string;
37
+ /** What replaced it: another rule id, or a sentence. */
38
+ replacement: string;
39
+ }
40
+ /** Rules removed from the registry, oldest first. An id here never returns to `rules`. */
41
+ export declare const removed: RemovedRule[];