@venizia/vulcan 0.4.10-2 → 0.4.10-21

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 (208) hide show
  1. package/README.md +30 -23
  2. package/base/checks/wiki-source-links.ts +17 -1
  3. package/base/claude/events.json +1 -11
  4. package/base/claude/settings.json +5 -76
  5. package/base/extensions/vulcan-guards.ts +81 -18
  6. package/base/hooks/guard-git.ts +95 -19
  7. package/base/hooks/guard-outside.ts +163 -43
  8. package/base/hooks/guard-writes.ts +15 -36
  9. package/base/hooks/session-start.ts +1 -1
  10. package/base/make/vulcan.mk +8 -5
  11. package/base/packs/venizia.md +16 -0
  12. package/base/references/delegation-brief.md +3 -5
  13. package/base/references/testing.md +1 -9
  14. package/base/rules.md +33 -38
  15. package/base/skills/build-a-feature/SKILL.md +4 -4
  16. package/base/skills/docs-update/SKILL.md +19 -8
  17. package/base/skills/fix-a-bug/SKILL.md +4 -2
  18. package/base/skills/plan-a-change/SKILL.md +1 -1
  19. package/base/skills/research/SKILL.md +1 -1
  20. package/base/skills/retro/SKILL.md +2 -2
  21. package/base/skills/review-a-change/SKILL.md +1 -1
  22. package/base/skills/visualize-work/SKILL.md +3 -3
  23. package/base/skills/visualize-work/references/spec.md +6 -3
  24. package/base/templates/AGENTS.md +9 -7
  25. package/base/templates/board.html +10 -110
  26. package/package.json +3 -3
  27. package/src/agents/atlas-block.ts +8 -31
  28. package/src/agents/codex-hooks.ts +1 -1
  29. package/src/agents/generated-block.ts +52 -0
  30. package/src/agents/hooks-json.ts +22 -3
  31. package/src/agents/index.ts +2 -0
  32. package/src/agents/render.ts +15 -2
  33. package/src/agents/resolve.ts +1 -0
  34. package/src/agents/rules-block.ts +92 -0
  35. package/src/board/command.ts +92 -30
  36. package/src/board/common/constants.ts +26 -0
  37. package/src/board/common/types.ts +10 -3
  38. package/src/board/fold.ts +1 -1
  39. package/src/board/github.ts +1 -2
  40. package/src/board/gitlab.ts +436 -0
  41. package/src/board/index.ts +1 -0
  42. package/src/board/logo.ts +49 -0
  43. package/src/board/open-file.ts +110 -0
  44. package/src/board/page.ts +57 -5
  45. package/src/board/schema.ts +4 -1
  46. package/src/board/server.ts +87 -3
  47. package/src/board/settings.ts +21 -24
  48. package/src/commands/changelog.command.ts +40 -0
  49. package/src/commands/clean.command.ts +6 -1
  50. package/src/commands/help.command.ts +2 -63
  51. package/src/commands/index.ts +6 -1
  52. package/src/commands/migrate.command.ts +71 -0
  53. package/src/commands/profile.command.ts +1 -1
  54. package/src/commands/runners.command.ts +43 -0
  55. package/src/commands/sync.command.ts +8 -4
  56. package/src/commands/table.ts +362 -0
  57. package/src/commit/check.ts +1 -0
  58. package/src/commit/common/constants.ts +4 -0
  59. package/src/commit/hook.ts +112 -8
  60. package/src/common/constants.ts +11 -9
  61. package/src/common/env-variables.ts +41 -0
  62. package/src/common/index.ts +1 -0
  63. package/src/common/layout-folder.ts +3 -4
  64. package/src/common/record.ts +5 -0
  65. package/src/config/common/constants.ts +28 -22
  66. package/src/config/loader.ts +22 -0
  67. package/src/config/schema.ts +330 -146
  68. package/src/create/add-app.ts +7 -2
  69. package/src/create/add-lib.ts +2 -4
  70. package/src/create/common/constants.ts +5 -3
  71. package/src/create/preset.ts +1 -0
  72. package/src/create/scratch.ts +2 -1
  73. package/src/doctor/checks.ts +277 -448
  74. package/src/doctor/common/constants.ts +27 -19
  75. package/src/doctor/private-values.ts +4 -1
  76. package/src/doctor/runner.ts +5 -12
  77. package/src/doctor/schema.ts +4 -0
  78. package/src/events/common/actor-names.ts +12 -0
  79. package/src/events/common/constants.ts +65 -38
  80. package/src/events/common/index.ts +1 -0
  81. package/src/events/hook.ts +5 -56
  82. package/src/events/text.ts +41 -6
  83. package/src/hooks/command-view.ts +80 -19
  84. package/src/hooks/common/constants.ts +26 -17
  85. package/src/hooks/common/types.ts +13 -24
  86. package/src/hooks/decision.ts +30 -12
  87. package/src/hooks/git-queries.ts +113 -0
  88. package/src/hooks/guard-git.ts +76 -24
  89. package/src/hooks/guard-outside.ts +334 -311
  90. package/src/hooks/guard-runner.ts +206 -0
  91. package/src/hooks/guard-writes.ts +87 -184
  92. package/src/hooks/hook-input.ts +57 -0
  93. package/src/hooks/index.ts +3 -6
  94. package/src/hooks/link-makers.ts +315 -0
  95. package/src/hooks/package-runners.ts +268 -0
  96. package/src/hooks/project-guards.ts +81 -44
  97. package/src/hooks/project-tmp.ts +39 -4
  98. package/src/hooks/runner-scan.ts +257 -0
  99. package/src/hooks/script-runners.ts +400 -0
  100. package/src/hooks/session-digest.ts +27 -117
  101. package/src/hooks/shell-folders.ts +760 -0
  102. package/src/hooks/shell-writes.ts +646 -0
  103. package/src/hooks/shell.ts +1421 -194
  104. package/src/hooks/subagent-card.ts +2 -2
  105. package/src/hooks/write-commands.ts +898 -0
  106. package/src/init/scaffold.ts +2 -3
  107. package/src/local/auto-clean.ts +2 -1
  108. package/src/local/browser-socket.ts +33 -0
  109. package/src/local/clean.ts +37 -6
  110. package/src/local/constants.ts +15 -1
  111. package/src/local/index.ts +1 -0
  112. package/src/local/scan.ts +41 -10
  113. package/src/manifest/render.ts +84 -30
  114. package/src/owner/constants.ts +4 -5
  115. package/src/owner/index.ts +0 -2
  116. package/src/owner/profile.ts +1 -0
  117. package/src/owner/schema.ts +51 -27
  118. package/src/owner/survey.ts +0 -3
  119. package/src/owner/view.ts +0 -9
  120. package/src/router.ts +8 -132
  121. package/src/rules/base.ts +12 -7
  122. package/src/rules/index.ts +1 -0
  123. package/src/rules/packs.ts +44 -0
  124. package/src/runners/check.ts +130 -0
  125. package/src/skills/frontmatter.ts +1 -0
  126. package/src/sync/agents-block.ts +104 -0
  127. package/src/sync/base-rules.ts +3 -3
  128. package/src/sync/claude-launch.ts +12 -12
  129. package/src/sync/common/constants.ts +0 -2
  130. package/src/sync/index.ts +1 -1
  131. package/src/sync/local-choice.ts +32 -3
  132. package/src/sync/planner.ts +2 -15
  133. package/src/sync/settings.ts +0 -17
  134. package/src/theme/index.ts +1 -0
  135. package/src/theme/tokens.ts +410 -0
  136. package/src/ui/prompt.ts +1 -0
  137. package/src/visualize/architecture.ts +504 -0
  138. package/src/visualize/assets.ts +41 -30
  139. package/src/visualize/compare-render.ts +22 -24
  140. package/src/visualize/constants.ts +27 -0
  141. package/src/visualize/diagrams.ts +13 -121
  142. package/src/visualize/kinds-render.ts +2 -2
  143. package/src/visualize/kinds-schema.ts +49 -0
  144. package/src/visualize/labels-vi.ts +25 -0
  145. package/src/visualize/labels.ts +2 -0
  146. package/src/visualize/layout.ts +415 -66
  147. package/src/visualize/page.ts +82 -15
  148. package/src/visualize/render.ts +2 -1
  149. package/src/visualize/schema.ts +29 -1
  150. package/src/visualize/sections.ts +78 -9
  151. package/src/visualize/theme.ts +49 -18
  152. package/src/wiki/anchors.ts +1 -0
  153. package/src/wiki/command.ts +10 -3
  154. package/src/wiki/source-links.ts +57 -18
  155. package/base/ci-global.json +0 -13
  156. package/base/claude/remote-hosts.json +0 -15
  157. package/base/hooks/guard-manifest.ts +0 -27
  158. package/base/hooks/guard-package.ts +0 -30
  159. package/base/hooks/guard-remote-hosts.ts +0 -70
  160. package/base/hooks/guard-scripting.ts +0 -32
  161. package/base/hooks/post-tool.ts +0 -37
  162. package/base/hooks/prompt-submit.ts +0 -54
  163. package/base/hooks/stop.ts +0 -40
  164. package/base/skills/build-a-feature/evals/evals.json +0 -29
  165. package/base/skills/docs-update/evals/evals.json +0 -23
  166. package/base/skills/docs-writing/evals/evals.json +0 -17
  167. package/base/skills/fix-a-bug/evals/evals.json +0 -17
  168. package/base/skills/knowledge-sync/SKILL.md +0 -81
  169. package/base/skills/knowledge-sync/knowledge-sync.workflow.js +0 -291
  170. package/base/skills/knowledge-sync/references/workflow.md +0 -44
  171. package/base/skills/plan-a-change/evals/evals.json +0 -47
  172. package/base/skills/report-back/evals/evals.json +0 -23
  173. package/base/skills/research/evals/evals.json +0 -17
  174. package/base/skills/retro/evals/evals.json +0 -17
  175. package/base/skills/review-a-change/evals/evals.json +0 -17
  176. package/base/skills/ship/evals/evals.json +0 -23
  177. package/base/skills/visualize-work/evals/evals.json +0 -17
  178. package/base/templates/ci/github-actions.yml +0 -75
  179. package/base/templates/ci/gitlab-ci.yml +0 -52
  180. package/base/templates/trusted-hosts.local.example +0 -11
  181. package/src/ci/affected.ts +0 -176
  182. package/src/ci/command.ts +0 -166
  183. package/src/ci/common/constants.ts +0 -76
  184. package/src/ci/common/index.ts +0 -2
  185. package/src/ci/common/types.ts +0 -37
  186. package/src/ci/git.ts +0 -72
  187. package/src/ci/global.ts +0 -48
  188. package/src/ci/graph.ts +0 -43
  189. package/src/ci/index.ts +0 -7
  190. package/src/ci/workspace.ts +0 -119
  191. package/src/commands/ci.command.ts +0 -4
  192. package/src/hooks/guard-manifest.ts +0 -109
  193. package/src/hooks/guard-package.ts +0 -226
  194. package/src/hooks/guard-scripting.ts +0 -36
  195. package/src/hooks/host-pattern.ts +0 -17
  196. package/src/hooks/owner-reply.ts +0 -162
  197. package/src/hooks/post-tool.ts +0 -138
  198. package/src/hooks/remote-hosts.ts +0 -1013
  199. package/src/hooks/trusted-scripts.ts +0 -212
  200. package/src/owner/shape.ts +0 -496
  201. package/src/owner/violations.ts +0 -332
  202. package/src/signals/claims.ts +0 -73
  203. package/src/signals/classify.ts +0 -137
  204. package/src/signals/constants.ts +0 -88
  205. package/src/signals/index.ts +0 -5
  206. package/src/signals/loop.ts +0 -48
  207. package/src/signals/state.ts +0 -271
  208. package/src/sync/atlas-block.ts +0 -56
@@ -0,0 +1,504 @@
1
+ import { SpecCharts } from './charts.ts';
2
+ import { NodeStates } from './constants.ts';
3
+ import type { TChrome } from './constants.ts';
4
+ import type { IDrawing } from './diagrams.ts';
5
+ import { FigureLayout } from './layout.ts';
6
+ import type { TVisualizeSpec } from './schema.ts';
7
+
8
+ type TArchitecture = NonNullable<TVisualizeSpec['architecture']>;
9
+ type TBox = TArchitecture['layers'][number]['boxes'][number];
10
+ type TLink = TArchitecture['links'][number];
11
+ type TSide = 'top' | 'bottom';
12
+ type TPoint = [number, number];
13
+
14
+ const PAD = 24;
15
+ const GAP = 48;
16
+ const MIN_BOX_WIDTH = 128;
17
+ const MAX_BOX_WIDTH = 192;
18
+ /** The widest the drawing is made: the content column, less its frame, holds it at 92% (see the fit rule of the page). */
19
+ const TARGET_WIDTH = 1270;
20
+ const TOP_ZONE = 32;
21
+ const BOTTOM_PAD = 14;
22
+ /** The least distance between two parallel lines, and so between two lanes, two margin lanes and two ends on one edge. */
23
+ const SPACING = 24;
24
+ const EDGE_INSET = 14;
25
+ const TITLE_SIZE = 14;
26
+ const SUB_SIZE = 12.5;
27
+ const LABEL_SIZE = 12.5;
28
+ const TITLE_LINE = 18;
29
+ const SUB_LINE = 15;
30
+ /** How far from its own line a label may sit, and how much closer it must be to it than to any other line. */
31
+ const LABEL_REACH = 12;
32
+ const LABEL_MARGIN = 6;
33
+
34
+ const escaped = (value: string): string => FigureLayout.escapeText({ value });
35
+
36
+ interface IRect {
37
+ left: number;
38
+ right: number;
39
+ top: number;
40
+ bottom: number;
41
+ }
42
+
43
+ interface IPlacedBox {
44
+ spec: TBox;
45
+ row: number;
46
+ x: number;
47
+ centre: number;
48
+ titleLines: string[];
49
+ subLines: string[];
50
+ }
51
+
52
+ interface IStep {
53
+ channel: number;
54
+ lane: number;
55
+ }
56
+
57
+ interface IPlannedLink {
58
+ link: TLink;
59
+ from: IPlacedBox;
60
+ to: IPlacedBox;
61
+ fromSide: TSide;
62
+ toSide: TSide;
63
+ first: IStep;
64
+ /** A link over more than one layer goes down the right margin: its second horizontal run and the lane of the margin. */
65
+ second?: IStep;
66
+ marginLane?: number;
67
+ }
68
+
69
+ const touches = (one: IRect, other: IRect): boolean =>
70
+ one.left < other.right &&
71
+ one.right > other.left &&
72
+ one.top < other.bottom &&
73
+ one.bottom > other.top;
74
+
75
+ const grow = (rect: IRect, by: number): IRect => ({
76
+ left: rect.left - by,
77
+ right: rect.right + by,
78
+ top: rect.top - by,
79
+ bottom: rect.bottom + by,
80
+ });
81
+
82
+ /** The distance from a rectangle to a straight axis-aligned segment (a rectangle of no thickness). */
83
+ const distanceTo = (opts: { rect: IRect; segment: IRect }): number => {
84
+ const { rect, segment } = opts;
85
+ const across = Math.max(rect.left - segment.right, segment.left - rect.right, 0);
86
+ const down = Math.max(rect.top - segment.bottom, segment.top - rect.bottom, 0);
87
+
88
+ return Math.hypot(across, down);
89
+ };
90
+
91
+ const legOf = (opts: { from: TPoint; to: TPoint }): IRect => ({
92
+ left: Math.min(opts.from[0], opts.to[0]),
93
+ right: Math.max(opts.from[0], opts.to[0]),
94
+ top: Math.min(opts.from[1], opts.to[1]),
95
+ bottom: Math.max(opts.from[1], opts.to[1]),
96
+ });
97
+
98
+ /**
99
+ * The layered view: bands from top to bottom, boxes in each, and an orthogonal line for every link that never crosses
100
+ * a box. Between two bands is a channel; a link runs along its own lane of it, so no two lines are closer than
101
+ * SPACING, and a link over more than one layer goes down the right margin, which no box reaches. The ends of the
102
+ * links on one edge of a box are spread to SPACING and lined up with their other end where they can be, so a line
103
+ * is straight when it can be. Each label sits on its own line, nearer to it than to any other. The boxes are as wide
104
+ * as the widest layer lets the drawing stay under TARGET_WIDTH, and their text wraps inside them.
105
+ */
106
+ export class SpecArchitecture {
107
+ static render(opts: { architecture: TArchitecture; chrome: TChrome }): IDrawing {
108
+ const { architecture } = opts;
109
+ const most = Math.max(...architecture.layers.map((layer) => layer.boxes.length));
110
+ const rowOf = new Map<string, number>();
111
+
112
+ architecture.layers.forEach((layer, row) =>
113
+ layer.boxes.forEach((spec) => rowOf.set(spec.id, row)),
114
+ );
115
+
116
+ const known = architecture.links.filter((link) => rowOf.has(link.from) && rowOf.has(link.to));
117
+ const farCount = known.filter(
118
+ (link) => Math.abs((rowOf.get(link.from) ?? 0) - (rowOf.get(link.to) ?? 0)) > 1,
119
+ ).length;
120
+ const marginRoom = farCount === 0 ? 0 : 20 + SPACING * farCount;
121
+ const boxWidth = Math.min(
122
+ MAX_BOX_WIDTH,
123
+ Math.max(
124
+ MIN_BOX_WIDTH,
125
+ Math.floor((TARGET_WIDTH - 2 * PAD - marginRoom - (most - 1) * GAP) / most),
126
+ ),
127
+ );
128
+ const width = 2 * PAD + marginRoom + most * boxWidth + (most - 1) * GAP;
129
+ const textWidthOf = (spec: TBox): number => boxWidth - (spec.state === undefined ? 24 : 50);
130
+ const placed = new Map<string, IPlacedBox>();
131
+
132
+ architecture.layers.forEach((layer, row) => {
133
+ const total = layer.boxes.length * boxWidth + (layer.boxes.length - 1) * GAP;
134
+ const start = PAD + (width - 2 * PAD - marginRoom - total) / 2;
135
+
136
+ layer.boxes.forEach((spec, col) => {
137
+ const x = start + col * (boxWidth + GAP);
138
+
139
+ placed.set(spec.id, {
140
+ spec,
141
+ row,
142
+ x,
143
+ centre: x + boxWidth / 2,
144
+ titleLines: FigureLayout.wrapText({
145
+ text: spec.label,
146
+ size: TITLE_SIZE,
147
+ isBold: true,
148
+ max: textWidthOf(spec),
149
+ }),
150
+ subLines:
151
+ spec.sub === undefined
152
+ ? []
153
+ : FigureLayout.wrapText({ text: spec.sub, size: SUB_SIZE, max: textWidthOf(spec) }),
154
+ });
155
+ });
156
+ });
157
+
158
+ const textHeight = (box: IPlacedBox): number =>
159
+ box.titleLines.length * TITLE_LINE +
160
+ (box.subLines.length === 0 ? 0 : 4 + box.subLines.length * SUB_LINE);
161
+ const boxHeight = Math.max(60, ...[...placed.values()].map((box) => 20 + textHeight(box)));
162
+
163
+ // Plan each link: the edges it leaves and enters, and a lane of each channel it runs along.
164
+ const lanes = new Map<number, number>();
165
+ const take = (channel: number): IStep => {
166
+ const lane = lanes.get(channel) ?? 0;
167
+
168
+ lanes.set(channel, lane + 1);
169
+
170
+ return { channel, lane };
171
+ };
172
+ let marginLanes = 0;
173
+ const plans: IPlannedLink[] = known.map((link) => {
174
+ const from = placed.get(link.from) as IPlacedBox;
175
+ const to = placed.get(link.to) as IPlacedBox;
176
+ const down = to.row >= from.row;
177
+ const isSame = to.row === from.row;
178
+ // A link between two boxes of one layer leaves and enters by the bottom edges, along the channel below the row.
179
+ const fromSide: TSide = isSame || down ? 'bottom' : 'top';
180
+ const toSide: TSide = isSame ? 'bottom' : down ? 'top' : 'bottom';
181
+
182
+ if (Math.abs(to.row - from.row) <= 1) {
183
+ return {
184
+ link,
185
+ from,
186
+ to,
187
+ fromSide,
188
+ toSide,
189
+ first: take(isSame ? from.row : Math.min(from.row, to.row)),
190
+ };
191
+ }
192
+
193
+ marginLanes += 1;
194
+
195
+ return {
196
+ link,
197
+ from,
198
+ to,
199
+ fromSide,
200
+ toSide,
201
+ first: take(down ? from.row : from.row - 1),
202
+ second: take(down ? to.row - 1 : to.row),
203
+ marginLane: marginLanes,
204
+ };
205
+ });
206
+
207
+ // The ends on one edge of a box: lined up with the other end where the two boxes overlap, spread to SPACING.
208
+ type TEnd = { index: number; end: 'from' | 'to'; wanted: number };
209
+ const ends = new Map<string, TEnd[]>();
210
+ const addEnd = (box: IPlacedBox, side: TSide, entry: TEnd): void => {
211
+ const key = `${box.spec.id}:${side}`;
212
+
213
+ ends.set(key, [...(ends.get(key) ?? []), entry]);
214
+ };
215
+
216
+ plans.forEach((plan, index) => {
217
+ const low = Math.max(plan.from.x, plan.to.x) + EDGE_INSET;
218
+ const high = Math.min(plan.from.x, plan.to.x) + boxWidth - EDGE_INSET;
219
+ const shared = low <= high ? (low + high) / 2 : undefined;
220
+
221
+ addEnd(plan.from, plan.fromSide, { index, end: 'from', wanted: shared ?? plan.to.centre });
222
+ addEnd(plan.to, plan.toSide, { index, end: 'to', wanted: shared ?? plan.from.centre });
223
+ });
224
+
225
+ const xOf = new Map<string, number>();
226
+
227
+ for (const [key, entries] of ends) {
228
+ const box = placed.get(key.split(':')[0] ?? '') as IPlacedBox;
229
+ const left = box.x + EDGE_INSET;
230
+ const right = box.x + boxWidth - EDGE_INSET;
231
+ const sorted = [...entries].sort(
232
+ (one, other) => one.wanted - other.wanted || one.index - other.index,
233
+ );
234
+ const spacing =
235
+ sorted.length < 2 ? 0 : Math.min(SPACING, (right - left) / (sorted.length - 1));
236
+ const xs = sorted.map((entry) => Math.min(right, Math.max(left, entry.wanted)));
237
+
238
+ for (let at = 1; at < xs.length; at += 1) {
239
+ xs[at] = Math.max(xs[at] ?? 0, (xs[at - 1] ?? 0) + spacing);
240
+ }
241
+
242
+ for (let at = xs.length - 2; at >= 0; at -= 1) {
243
+ xs[at] = Math.min(xs[at] ?? 0, (xs[at + 1] ?? 0) - spacing);
244
+ }
245
+
246
+ sorted.forEach((entry, at) => xOf.set(`${entry.index}:${entry.end}`, xs[at] ?? box.centre));
247
+ }
248
+
249
+ // A link whose two ends are less than a lane apart takes one x at both ends, so it has no step.
250
+ plans.forEach((plan, index) => {
251
+ const fromKey = `${index}:from`;
252
+ const toKey = `${index}:to`;
253
+ const fromX = xOf.get(fromKey) ?? 0;
254
+ const toX = xOf.get(toKey) ?? 0;
255
+ const move = (key: string, target: number, box: IPlacedBox, side: TSide): boolean => {
256
+ const clash = (ends.get(`${box.spec.id}:${side}`) ?? []).some(
257
+ (entry) =>
258
+ `${entry.index}:${entry.end}` !== key &&
259
+ Math.abs((xOf.get(`${entry.index}:${entry.end}`) ?? 0) - target) < SPACING,
260
+ );
261
+
262
+ if (clash || target < box.x + EDGE_INSET || target > box.x + boxWidth - EDGE_INSET) {
263
+ return false;
264
+ }
265
+
266
+ xOf.set(key, target);
267
+
268
+ return true;
269
+ };
270
+
271
+ if (
272
+ fromX !== toX &&
273
+ Math.abs(fromX - toX) < SPACING &&
274
+ !move(toKey, fromX, plan.to, plan.toSide)
275
+ ) {
276
+ move(fromKey, toX, plan.from, plan.fromSide);
277
+ }
278
+ });
279
+
280
+ // The geometry of bands and channels, from the lanes the links need.
281
+ const rows = architecture.layers.length;
282
+ const bandHeight = TOP_ZONE + boxHeight + BOTTOM_PAD;
283
+ const channelHeight = (channel: number): number => {
284
+ const count = lanes.get(channel) ?? 0;
285
+
286
+ return count === 0 ? (channel === rows - 1 ? 6 : 24) : 24 + SPACING * count;
287
+ };
288
+ const bandTops: number[] = [];
289
+ const channelTops: number[] = [];
290
+ let y = 4;
291
+
292
+ for (let row = 0; row < rows; row += 1) {
293
+ bandTops.push(y);
294
+ channelTops.push(y + bandHeight);
295
+ y += bandHeight + channelHeight(row);
296
+ }
297
+
298
+ const height = y + 4;
299
+ const boxTop = (row: number): number => (bandTops[row] ?? 0) + TOP_ZONE;
300
+ const boxBottom = (row: number): number => boxTop(row) + boxHeight;
301
+ const laneY = (step: IStep): number =>
302
+ (channelTops[step.channel] ?? 0) + 22 + SPACING * step.lane;
303
+ const marginX = (lane: number): number => width - PAD - marginRoom + 20 + SPACING * (lane - 1);
304
+ const head = (point: { x: number; y: number; toward: 'up' | 'down' }): string => {
305
+ const tip = point.toward === 'down' ? -7 : 7;
306
+
307
+ return `<path d="M${point.x - 5} ${point.y + tip}L${point.x} ${point.y}L${point.x + 5} ${point.y + tip}" fill="none" class="dline"/>`;
308
+ };
309
+
310
+ const bands = architecture.layers
311
+ .map(
312
+ (layer, row) =>
313
+ `<rect x="2" y="${bandTops[row]}" width="${width - 4}" height="${bandHeight}" rx="4" class="dband"/><text class="dlab" x="14" y="${(bandTops[row] ?? 0) + 22}">${escaped(layer.label)}</text>`,
314
+ )
315
+ .join('');
316
+ // Every route first, so a label can be placed clear of the lines of the other links.
317
+ const routes = plans.map((plan, index) => {
318
+ const startX = xOf.get(`${index}:from`) ?? plan.from.centre;
319
+ const endX = xOf.get(`${index}:to`) ?? plan.to.centre;
320
+ const startY = plan.fromSide === 'bottom' ? boxBottom(plan.from.row) : boxTop(plan.from.row);
321
+ const endY = plan.toSide === 'bottom' ? boxBottom(plan.to.row) : boxTop(plan.to.row);
322
+ const first = laneY(plan.first);
323
+ const points: TPoint[] =
324
+ plan.second === undefined || plan.marginLane === undefined
325
+ ? [
326
+ [startX, startY],
327
+ [startX, first],
328
+ [endX, first],
329
+ [endX, endY],
330
+ ]
331
+ : [
332
+ [startX, startY],
333
+ [startX, first],
334
+ [marginX(plan.marginLane), first],
335
+ [marginX(plan.marginLane), laneY(plan.second)],
336
+ [endX, laneY(plan.second)],
337
+ [endX, endY],
338
+ ];
339
+
340
+ return { plan, points, endX, endY };
341
+ });
342
+ const legsOf = (index: number): IRect[] => {
343
+ const points = routes[index]?.points ?? [];
344
+
345
+ return points.slice(1).map((point, at) => legOf({ from: points[at] ?? point, to: point }));
346
+ };
347
+ const boxRects: IRect[] = [...placed.values()].map((box) =>
348
+ grow(
349
+ { left: box.x, right: box.x + boxWidth, top: boxTop(box.row), bottom: boxBottom(box.row) },
350
+ 3,
351
+ ),
352
+ );
353
+ const placedLabels: IRect[] = [];
354
+ const labelOf = (index: number): string => {
355
+ const route = routes[index];
356
+ const label = route?.plan.link.label ?? opts.chrome.uses;
357
+
358
+ if (route === undefined) {
359
+ return '';
360
+ }
361
+
362
+ const textWidth = FigureLayout.textWidth({ text: label, size: LABEL_SIZE });
363
+ const own = legsOf(index);
364
+ const others = routes.flatMap((_, at) => (at === index ? [] : legsOf(at)));
365
+ type TCandidate = { x: number; y: number; anchor: 'middle' | 'start' | 'end' };
366
+ const candidates: TCandidate[] = [];
367
+ const longestFirst = [...own].sort(
368
+ (one, other) =>
369
+ other.right -
370
+ other.left +
371
+ (other.bottom - other.top) -
372
+ (one.right - one.left + (one.bottom - one.top)),
373
+ );
374
+
375
+ for (const leg of longestFirst) {
376
+ if (leg.bottom - leg.top === 0 && leg.right - leg.left >= textWidth + 8) {
377
+ for (const share of [0.5, 0.35, 0.65, 0.2, 0.8, 0.1, 0.9]) {
378
+ const x = leg.left + (leg.right - leg.left) * share;
379
+
380
+ if (x - textWidth / 2 >= leg.left + 2 && x + textWidth / 2 <= leg.right - 2) {
381
+ candidates.push(
382
+ { x, y: leg.top - 5, anchor: 'middle' },
383
+ { x, y: leg.top + 17, anchor: 'middle' },
384
+ );
385
+ }
386
+ }
387
+ } else if (leg.right - leg.left === 0 && leg.bottom - leg.top >= 20) {
388
+ for (const share of [0.5, 0.3, 0.7, 0.15, 0.85]) {
389
+ const at = leg.top + (leg.bottom - leg.top) * share + 4;
390
+
391
+ candidates.push(
392
+ { x: leg.left + 8, y: at, anchor: 'start' },
393
+ { x: leg.left - 8, y: at, anchor: 'end' },
394
+ );
395
+ }
396
+ }
397
+ }
398
+
399
+ const rectOf = (candidate: TCandidate): IRect => {
400
+ const left =
401
+ candidate.anchor === 'middle'
402
+ ? candidate.x - textWidth / 2
403
+ : candidate.anchor === 'start'
404
+ ? candidate.x
405
+ : candidate.x - textWidth;
406
+
407
+ return {
408
+ left: left - 2,
409
+ right: left + textWidth + 2,
410
+ top: candidate.y - 13,
411
+ bottom: candidate.y + 3,
412
+ };
413
+ };
414
+ const usable = (candidate: TCandidate): boolean => {
415
+ const rect = rectOf(candidate);
416
+ const nearest = Math.min(...own.map((segment) => distanceTo({ rect, segment })));
417
+ const rival = Math.min(
418
+ ...others.map((segment) => distanceTo({ rect, segment })),
419
+ Number.POSITIVE_INFINITY,
420
+ );
421
+
422
+ return (
423
+ rect.left > 4 &&
424
+ rect.right < width - 4 &&
425
+ nearest <= LABEL_REACH &&
426
+ rival > nearest + LABEL_MARGIN &&
427
+ !others.some((segment) => touches(rect, grow(segment, 3))) &&
428
+ !boxRects.some((box) => touches(rect, box)) &&
429
+ !placedLabels.some((other) => touches(rect, other))
430
+ );
431
+ };
432
+ const chosen = candidates.find(usable) ?? candidates[0];
433
+
434
+ if (chosen === undefined) {
435
+ return '';
436
+ }
437
+
438
+ placedLabels.push(rectOf(chosen));
439
+
440
+ return `<text class="dl" x="${chosen.x}" y="${chosen.y}" text-anchor="${chosen.anchor}">${escaped(label)}</text>`;
441
+ };
442
+ const links = routes
443
+ .map((route, index) => {
444
+ const linePath = `M${route.points.map((point) => point.join(' ')).join('L')}`;
445
+ const arrow = head({
446
+ x: route.endX,
447
+ y: route.endY,
448
+ toward: route.plan.toSide === 'top' ? 'down' : 'up',
449
+ });
450
+
451
+ return `<g class="dlink"><path d="${linePath}" class="dline"/>${arrow}${labelOf(index)}</g>`;
452
+ })
453
+ .join('');
454
+ const boxes = [...placed.values()]
455
+ .map((box) => {
456
+ const top = boxTop(box.row);
457
+ const tone = box.spec.state === undefined ? undefined : NodeStates.toneOf(box.spec.state);
458
+ const hasIcon = box.spec.state !== undefined;
459
+ const textX = hasIcon ? box.x + 38 : box.centre;
460
+ const anchor = hasIcon ? 'start' : 'middle';
461
+ const first = top + (boxHeight - textHeight(box)) / 2;
462
+ const titles = box.titleLines
463
+ .map(
464
+ (line, index) =>
465
+ `<text class="t" x="${textX}" y="${first + 14 + index * TITLE_LINE}" text-anchor="${anchor}">${escaped(line)}</text>`,
466
+ )
467
+ .join('');
468
+ const subs = box.subLines
469
+ .map(
470
+ (line, index) =>
471
+ `<text class="s" x="${textX}" y="${first + box.titleLines.length * TITLE_LINE + 15 + index * SUB_LINE}" text-anchor="${anchor}">${escaped(line)}</text>`,
472
+ )
473
+ .join('');
474
+ const icon = hasIcon
475
+ ? `<g class="st" transform="translate(${box.x + 10} ${top + boxHeight / 2 - 10})">${SpecCharts.stateIcon({ state: box.spec.state ?? '', size: 20 }).replace('<svg', '<svg x="0" y="0"')}</g>`
476
+ : '';
477
+
478
+ return `<g class="dbox${tone === undefined ? '' : ` c-${tone}`}" data-box="${escaped(box.spec.id)}"><rect x="${box.x}" y="${top}" width="${boxWidth}" height="${boxHeight}" rx="4"/>${icon}${titles}${subs}</g>`;
479
+ })
480
+ .join('');
481
+ const names = new Map(
482
+ architecture.layers.flatMap((layer) =>
483
+ layer.boxes.map((spec) => [spec.id, spec.label] as const),
484
+ ),
485
+ );
486
+ // The state mark and the first word of a name stay together, so a break never leaves the mark behind.
487
+ const nameOf = (spec: TBox): string => {
488
+ if (spec.state === undefined) {
489
+ return escaped(spec.label);
490
+ }
491
+
492
+ const [first = '', ...rest] = spec.label.split(' ');
493
+
494
+ return `<span class="nb"><span class="c-${NodeStates.toneOf(spec.state)} inl">${SpecCharts.stateIcon({ state: spec.state, size: 16 })}</span>${escaped(first)}</span>${rest.length === 0 ? '' : ` ${escaped(rest.join(' '))}`}`;
495
+ };
496
+ const fallback = `<ul class="path">${architecture.layers.map((layer) => `<li><b>${escaped(layer.label)}:</b> <span>${layer.boxes.map(nameOf).join(', ')}</span></li>`).join('')}${architecture.links.map((link) => `<li>${escaped(names.get(link.from) ?? link.from)} <span class="verb">${escaped(link.label ?? opts.chrome.uses)}</span> ${escaped(names.get(link.to) ?? link.to)}</li>`).join('')}</ul>`;
497
+
498
+ return {
499
+ svg: `<svg width="${width}" viewBox="0 0 ${width} ${height}" role="group" aria-label="${escaped(opts.chrome.architecture)}">${bands}${links}${boxes}</svg>`,
500
+ width,
501
+ fallback,
502
+ };
503
+ }
504
+ }