@beehexa/hexasync-template-model 2610.12.1 → 2610.12.3

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.
@@ -1,44 +1,34 @@
1
1
  /**
2
- * Mermaid and Markdown emission — the primitives both flow packages render through (Story 4.3).
2
+ * Flow drawing: a COMPACT graph, and the minimal Mermaid text emitted from it.
3
3
  *
4
- * ### Why this is in `model` rather than a package of its own
4
+ * ### Two steps, and why the first one exists
5
5
  *
6
- * `PACKAGE-CONTRACT.md` §5 says the renderers are *"exported from both flow packages over the shared model"*, and
7
- * AD-21 forbids those two packages importing each other — so the shared half cannot live in either. A package of its
8
- * own would make both flow packages depend on it, contradicting Story 4.1 Task 1's *"zero runtime dependencies beyond
9
- * `hexasync-template-model`"*.
6
+ * A flow is drawn from `compactFlowGraph` — per node only a short id (`n0`…), the step key, the display name, the
7
+ * step type, its icon family, its shape and its run state; per edge only its ends, an optional label and a kind. No
8
+ * step payload (expressions, data, filters, conditions) ever enters it, so nothing the author typed inside a step can
9
+ * reach the drawing, its accessibility text or a host that caches it. The graph is small enough to embed, hash and
10
+ * ship to a webview; `compactFlowMermaid` turns it into Mermaid text for ONE direction on demand.
10
11
  *
11
- * It belongs here on its own merits: `renderedNodeId` — the `:` → `__` projection every diagram id goes through — is
12
- * already in this package and is already half a renderer. The address algebra and the emission algebra are one
13
- * subject.
12
+ * ### What the definition carries, and what it deliberately does not
14
13
  *
15
- * ### What each rule below cost to learn
14
+ * - short node ids and subgraph ids (`n12`, `g0`), never projected addresses;
15
+ * - a label of `<b>name</b><br>KEY · TYPE` (escaped), plus `×N` for a counted repeat;
16
+ * - the shape (stadium, hexagon, subroutine, delay, rounded box);
17
+ * - `class` statements grouping ids per icon family (`ic-*`) and per run state (`rs-*`); a `classDef` only for the
18
+ * run-state classes, whose line-work (width, dash, opacity) is part of the reading, each emitted once;
19
+ * - one `linkStyle default`, plus per-index `linkStyle` statements only for edges a run reading set apart;
20
+ * - a short `accTitle`/`accDescr` stating counts, not names.
16
21
  *
17
- * **1. Every subgraph emits its OWN direction.** Mermaid ignores the parent chart's direction inside a subgraph, so a
18
- * `TD` chart with `LR` stages needs the statement repeated per subgraph. (`PACKAGE-CONTRACT.md` §5 rule 1.)
22
+ * Colours, the card layout, icons and the theme live in the host stylesheet (`flowStylesheet` in `flowStyles.ts`), so
23
+ * the definition stays a few KB for a large puller and a theme change never regenerates it.
19
24
  *
20
- * **2. Styling is PER ELEMENT — `style` and `linkStyle`, not `class`/`classDef`.** The Mermaid 12 definition
21
- * baseline emits overlays as exact element-addressed statements; traversal classes are absent from those bytes.
25
+ * ### Determinism
22
26
  *
23
- * **3. …with ONE exception, and it is not a contradiction.** The VS Code extension uses `classDef` successfully — for
24
- * `stroke-width` only, never a fill — precisely so the **webview's stylesheet** can colour that optional shape hint
25
- * from theme variables. Run-state colors remain caller-supplied per-element `style` values; approved kind borders and
26
- * polygon fill use deterministic role defaults which callers may override.
27
- *
28
- * **4. Shape-hint `classDef` declarations are chart-level output.** They are hoisted after every subgraph and emitted
29
- * once, rather than repeated beside the nodes they name.
30
- *
31
- * **5. A colour is VALIDATED, and an unusable one drops the highlight rather than the chart.** The Mermaid 12
32
- * characterization parses and renders the representative hex form and parses a bare named colour. Other CSS colour
33
- * forms remain outside this emitter's declared literal grammar; no claim about Mermaid 12 accepting them is needed.
34
- * A fill tint still needs exactly `#rrggbb`, because the tint has no alpha suffix to append otherwise.
35
- *
36
- * **6. The same `(model, opts)` always produces the same string.** No clock, no randomness, no ambient state — the
37
- * golden-diagram suite depends on it (AD-29), which is also why the overlay is an ARGUMENT and never state.
27
+ * The same `(groups, edges, opts)` always produces the same graph and the same text: lists are emitted in the order
28
+ * given, duplicates collapse by first appearance, and nothing consults the clock or the environment.
38
29
  */
39
- import { renderedNodeId } from './nodeAddress.js';
40
30
  import { expandOccurrences, mergeNodeOverlays } from './occurrenceExpansion.js';
41
- import { RUN_STATE_ERROR_MARKER, isCountableRepeat, repeatBadgeText, runStateAlternativeText, stepIconClassName, } from './stepGlyph.js';
31
+ import { isCountableRepeat, repeatBadgeText, stepIconClassName, } from './stepGlyph.js';
42
32
  /** The complete, ordered shape vocabulary. No renderer path may invent another treatment. */
43
33
  export const APPROVED_NODE_TREATMENTS = [
44
34
  'flow-start',
@@ -50,31 +40,6 @@ export const APPROVED_NODE_TREATMENTS = [
50
40
  'exception',
51
41
  'unimplemented',
52
42
  ];
53
- /**
54
- * The hard boundary between deterministic definition generation and host DOM decoration.
55
- *
56
- * Exported as data so adapters and contract tests can share one inventory instead of describing the boundary in
57
- * prose that drifts. Stage 3 may make generated nodes operable; every visible or structural byte belongs here.
58
- */
59
- export const FLOW_RENDER_STAGE_OWNERSHIP = {
60
- generate: [
61
- 'shape',
62
- 'nodeId',
63
- 'labelText',
64
- 'accTitle',
65
- 'accDescr',
66
- 'iconClass',
67
- 'nodeStyle',
68
- 'edge',
69
- 'linkStyle',
70
- 'edgeColor',
71
- 'edgeDash',
72
- 'direction',
73
- 'repeatBadge',
74
- 'errorMarker',
75
- ],
76
- decorate: ['interaction'],
77
- };
78
43
  /**
79
44
  * The nine runtime executors whose authoritative Epic 7 evidence records `NotImplementedException`.
80
45
  *
@@ -92,74 +57,28 @@ export const UNIMPLEMENTED_WORKER_STEP_TYPES = [
92
57
  'QueryDSL',
93
58
  'Restful',
94
59
  ];
95
- const UNIMPLEMENTED_WORKER_STEP_TYPE = {
96
- Begin: true,
97
- Composite: true,
98
- cURL: true,
99
- End: true,
100
- GraphQL: true,
101
- Liquid: true,
102
- OData: true,
103
- QueryDSL: true,
104
- Restful: true,
105
- };
106
- const SUBROUTINE_WORKER_STEP_TYPE = {
107
- PULLER: true,
108
- PUSHER: true,
109
- ENQUEUER: true,
110
- };
60
+ const UNIMPLEMENTED_WORKER_STEP_TYPE = Object.fromEntries(UNIMPLEMENTED_WORKER_STEP_TYPES.map((type) => [type, true]));
61
+ const SUBROUTINE_WORKER_STEP_TYPE = { PULLER: true, PUSHER: true, ENQUEUER: true };
111
62
  /** Classify treatment from model facts only; labels and icon families are deliberately irrelevant. */
112
63
  export function classifyNodeTreatment(node) {
113
64
  if (node.type === 'Begin')
114
65
  return 'flow-start';
115
66
  if (node.type === 'End')
116
67
  return 'flow-end';
117
- if (node.decides === true ||
118
- node.type === 'IF' ||
119
- node.type === 'SWITCH') {
68
+ if (node.decides === true || node.type === 'IF' || node.type === 'SWITCH')
120
69
  return 'branch';
121
- }
122
- if (node.type !== undefined &&
123
- SUBROUTINE_WORKER_STEP_TYPE[node.type] === true) {
70
+ if (node.type !== undefined && Object.hasOwn(SUBROUTINE_WORKER_STEP_TYPE, node.type))
124
71
  return 'subroutine';
125
- }
126
72
  if (node.type === 'SLEEP')
127
73
  return 'delay';
128
74
  if (node.type === 'EXCEPTION' || node.type === 'Error')
129
75
  return 'exception';
130
- if (node.type !== undefined &&
131
- UNIMPLEMENTED_WORKER_STEP_TYPE[node.type] === true) {
76
+ if (node.type !== undefined && Object.hasOwn(UNIMPLEMENTED_WORKER_STEP_TYPE, node.type))
132
77
  return 'unimplemented';
133
- }
134
78
  if (node.terminal === true)
135
79
  return 'flow-end';
136
80
  return 'process';
137
81
  }
138
- /** Deterministic approved light palette; valid host-resolved values may override these roles. */
139
- export const APPROVED_NODE_COLORS = {
140
- fill: '#ffffff',
141
- nodeBorder: '#64748b',
142
- failStroke: '#b91c1c',
143
- edge: '#64748b',
144
- notTakenStroke: '#475569',
145
- edgeLabelText: '#334155',
146
- edgeLabelBg: '#ffffff',
147
- };
148
- /**
149
- * The declared line-work roles (DESIGN.md `strokes:`). Blueprint carries state in the LINE, so weight and pattern
150
- * are roles, not values a render path picks.
151
- *
152
- * They live here as data because FR59 forbids a width or dash-array literal inside an emitter function: a width literal
153
- * typed into one of three emit sites is exactly how the shipped emitter ended up with three copies of one decision.
154
- */
155
- export const STROKE_ROLES = {
156
- /** Every node outline and every edge. */
157
- hairline: '1px',
158
- /** The failed node's outline, and nothing else on the canvas. */
159
- emphasis: '2px',
160
- /** Dash array for the branch-not-taken stroke. RUN STATE ONLY. */
161
- dash: '6 3',
162
- };
163
82
  /** The four states DESIGN.md's state table draws as rows; `indeterminate` is the fifth reading, not a fifth state. */
164
83
  export const RUN_STATES = [
165
84
  'ranOk',
@@ -170,974 +89,443 @@ export const RUN_STATES = [
170
89
  /** The drawn reading for a node the producer could not place. Counted apart from `RUN_STATES` on purpose. */
171
90
  export const INDETERMINATE_RUN_STATE = 'indeterminate';
172
91
  /**
173
- * The five drawn readings, resolved from DESIGN.md's closed light palette (`colors:` 9-88, `node-*` components
174
- * 171-195, the state table 389-400).
175
- *
176
- * Data, not code, for the same reason as `STROKE_ROLES`: FR59 forbids a colour, width, dash or opacity literal
177
- * inside an emitter function. `opacity: '0.7'` is the ONLY numeric opacity in DESIGN.md (`node-unreached` line 187)
178
- * and it exists once, here.
92
+ * The parse guards a host passes to `mermaid.initialize` for a generated flow.
179
93
  *
180
- * Read the columns against the matrix they came from:
181
- * - **failed** is the only `emphasis`, the only `marker`, and the only non-white fill. All three unconditional.
182
- * - **never reached** is SOLID with a fully visible border and recedes by muted text + `opacity` on the card.
183
- * - **branch not taken** is DASHED at full neutral contrast and is NEVER dimmed.
184
- * - **indeterminate** is the neutral pair and carries nothing else, so it stays clickable and unremarkable.
94
+ * Guards only: a compact definition of a 300-node flow is a small fraction of `maxTextSize`, and nothing here refuses
95
+ * to draw a normal flow. Mermaid's own defaults (50 000 characters, 500 edges) are what a host would otherwise trip
96
+ * over silently — over `maxTextSize` Mermaid draws a single error node.
185
97
  */
186
- export const STATE_ROLES = {
187
- ranOk: {
188
- stroke: '#c2410c',
189
- fill: APPROVED_NODE_COLORS.fill,
190
- title: '#0f172a',
191
- description: '#475569',
192
- width: 'hairline',
193
- dash: false,
194
- opacity: undefined,
195
- marker: false,
196
- },
197
- ranFailed: {
198
- stroke: APPROVED_NODE_COLORS.failStroke,
199
- fill: '#fee2e2',
200
- title: '#991b1b',
201
- description: '#7f1d1d',
202
- width: 'emphasis',
203
- dash: false,
204
- opacity: undefined,
205
- marker: true,
206
- },
207
- neverReached: {
208
- stroke: '#7c8899',
209
- fill: '#f8fafc',
210
- title: '#64748b',
211
- description: '#64748b',
212
- width: 'hairline',
213
- dash: false,
214
- opacity: '0.7',
215
- marker: false,
216
- },
217
- branchNotTaken: {
218
- stroke: APPROVED_NODE_COLORS.notTakenStroke,
219
- fill: APPROVED_NODE_COLORS.fill,
220
- title: '#334155',
221
- description: '#475569',
222
- width: 'hairline',
223
- dash: true,
224
- opacity: undefined,
225
- marker: false,
226
- },
227
- indeterminate: {
228
- stroke: APPROVED_NODE_COLORS.nodeBorder,
229
- fill: APPROVED_NODE_COLORS.fill,
230
- title: '#0f172a',
231
- description: '#475569',
232
- width: 'hairline',
233
- dash: false,
234
- opacity: undefined,
235
- marker: false,
236
- },
98
+ export const FLOW_MERMAID_LIMITS = {
99
+ maxTextSize: 1_000_000,
100
+ maxEdges: 5_000,
237
101
  };
102
+ /** Collapse whitespace so a name can never break a line of the definition or of an accessibility directive. */
103
+ const oneLine = (value) => value.replace(/[\u0000-\u001f\u007f-\u009f\u2028\u2029\s]+/g, ' ').trim();
238
104
  /**
239
- * The count badge's declared roles (DESIGN.md `count-badge` 218-222, `rounded.full`).
105
+ * Escape authored text for a quoted Mermaid label.
240
106
  *
241
- * Exported as DATA and not emitted as a `style` property, because Mermaid 12 has no way to paint a plate behind one
242
- * span of a node label: the badge reaches the definition as label TEXT (`×997`) in the node's single label colour.
243
- * A host that draws its own legend or inspector chip reads the plate roles from here, so the two surfaces cannot
244
- * disagree about what a badge looks like. Emitting it as label text is what AD-23 requires anyway — run-state
245
- * content is stage 1's, and `securityLevel: 'strict'` routes a label through DOMPurify.
107
+ * Every authored byte is text: `<`, `>`, `&`, `"` become entities and a backtick cannot open Mermaid markdown, so the
108
+ * renderer-owned `<b>`/`<br>` are the only markup in a label.
246
109
  */
247
- export const REPEAT_BADGE_ROLES = {
248
- background: '#ffffff',
249
- foreground: '#9a3412',
250
- border: '#c2410c',
251
- borderWidth: STROKE_ROLES.hairline,
252
- radius: '9999px',
253
- };
254
- /** An opacity Mermaid will accept in a `style` statement: a decimal in `[0,1]`, nothing else. */
255
- function usableOpacity(value) {
256
- if (value === undefined)
257
- return undefined;
258
- const trimmed = value.trim();
259
- if (!/^(?:0?\.\d+|0|1(?:\.0+)?)$/.test(trimmed))
260
- return undefined;
261
- /**
262
- * `0` is rejected on purpose, and it is not a formatting quibble: Mermaid honours node opacity as
263
- * `opacity:0 !important`, so `opacity:0` draws a node that is present in the DOM, present in the layout, and
264
- * invisible — label included. A run reading that erases the step it describes is worse than no reading.
265
- */
266
- const numeric = Number(trimmed);
267
- return numeric > 0 && numeric <= 1 ? trimmed : undefined;
268
- }
269
- /** Does this entry carry a `state` — the only thing that can give the typed reading chart-wide authority? */
270
- function carriesState(entry) {
271
- // `?? undefined` folds a JSON `null` into "no state" without widening the declared type.
272
- return (entry?.state ?? undefined) !== undefined;
273
- }
274
- /**
275
- * Resolve one node's entry, given the resolved node fill (a roles-only entry's fill default) and whether the typed
276
- * reading is authoritative for the chart. See `RenderOverlay.nodes` for the rules this implements.
277
- */
278
- function resolveNodeState(entry, nodeFill, authoritative) {
279
- const roles = entry?.roles;
280
- const requestedRepeat = entry?.repeat;
281
- const repeat = isCountableRepeat(requestedRepeat) ? requestedRepeat : undefined;
282
- const declined = roles?.dash === true;
283
- if (!carriesState(entry)) {
284
- const stroke = overlayColor(roles?.stroke);
285
- if (stroke !== undefined) {
286
- /**
287
- * ROLES-ONLY: exactly the roles given, and nothing the entry did not ask for.
288
- *
289
- * This is what lets a producer that can prove no state — the log-derived reading, where "started and never
290
- * ended" is not a proven `ranFailed` — draw its caller colours byte-identically: no `color:`, no marker and no
291
- * chart-wide reading unless it asks for them.
292
- */
293
- return {
294
- state: undefined,
295
- pass: 'roles',
296
- style: {
297
- stroke,
298
- // `emphasis` or the default `hairline`; any other value — `dash` included, a pattern not a weight — is hairline.
299
- width: roles?.width === 'emphasis' ? STROKE_ROLES.emphasis : STROKE_ROLES.hairline,
300
- fill: overlayColor(roles?.fill) ?? nodeFill,
301
- color: overlayColor(roles?.title),
302
- opacity: usableOpacity(roles?.opacity),
303
- dash: declined ? STROKE_ROLES.dash : undefined,
304
- },
305
- declined,
306
- reached: true,
307
- marker: roles?.marker === true,
308
- repeat,
309
- };
310
- }
311
- // No state and no drawable stroke: no style of its own, unless the chart-wide reading draws it indeterminate.
312
- if (!authoritative) {
313
- return { state: undefined, pass: 'none', style: undefined, declined, reached: false, marker: false, repeat };
314
- }
315
- }
316
- /**
317
- * An unrecognised `state` READS AS `indeterminate`; it never throws.
318
- *
319
- * The union is a compile-time promise and an overlay is runtime data: it arrives as parsed JSON from an evidence
320
- * producer or as a webview `postMessage` payload, where `state: 'running'` costs one keystroke. Indexed blind,
321
- * `STATE_ROLES['running']` is `undefined` and the next property read takes down the WHOLE diagram — every node,
322
- * over one node's bad field.
323
- */
324
- const requested = entry?.state;
325
- const state = requested !== undefined && Object.hasOwn(STATE_ROLES, requested)
326
- ? requested
327
- : INDETERMINATE_RUN_STATE;
328
- const declared = STATE_ROLES[state];
329
- /**
330
- * **The three non-chromatic channels follow the STATE, never the override.**
331
- *
332
- * `emphasis` width, the error marker and the dimming are the failure signals a reader without colour vision has,
333
- * and UX-DR52 makes all three unconditional. Honouring an override on them let a caller put the marker and 2px on a
334
- * `ranOk` node, take both off a `ranFailed` one, or dim a `branchNotTaken` node DESIGN.md says is never dimmed —
335
- * each of which breaks "exactly one node on this canvas is 2px / marked / dashed / dimmed", the property the whole
336
- * accessibility argument rests on.
337
- *
338
- * So beside a state `roles.width`, `roles.dash` and `roles.marker` are ignored, and `roles.opacity` may only retune
339
- * a state that is already dimmed. COLOUR overrides stay fully honoured: a host resolving its own theme is exactly
340
- * what AD-23 stage 2 asks for, and a colour cannot move a node into a channel it does not own.
341
- */
342
- const opacity = declared.opacity === undefined
343
- ? undefined
344
- : (usableOpacity(roles?.opacity) ?? declared.opacity);
345
- /**
346
- * A STATED entry outside the chart-wide reading can only be a DERIVED one (a caller state takes the chart): it keeps
347
- * its label content, as it always has, and draws no style — only a caller statement restyles the drawing.
348
- */
349
- const drawn = authoritative;
350
- return {
351
- state,
352
- pass: drawn ? 'state' : 'none',
353
- style: drawn
354
- ? {
355
- stroke: overlayColor(roles?.stroke) ?? declared.stroke,
356
- fill: overlayColor(roles?.fill) ?? declared.fill,
357
- color: overlayColor(roles?.title) ?? declared.title,
358
- width: STROKE_ROLES[declared.width],
359
- dash: declared.dash ? STROKE_ROLES.dash : undefined,
360
- opacity,
361
- }
362
- : undefined,
363
- // A stateless entry drawn indeterminate keeps its declined dash; a stated one declines only as `branchNotTaken`.
364
- declined: drawn && (carriesState(entry) ? state === 'branchNotTaken' : declined),
365
- reached: drawn && (state === 'ranOk' || state === 'ranFailed'),
366
- marker: declared.marker,
367
- repeat,
368
- };
369
- }
370
- /**
371
- * Mermaid's own `maxTextSize` default, measured against 12.0.0 rather than read from a changelog.
372
- *
373
- * Exported as data because the number is the whole finding: 305 nodes (49 620 chars) of the measurement fixture draw
374
- * fully and 310 (50 445 chars) draw one node. The ceiling is therefore a CHARACTER budget and scales with label
375
- * length — a workflow with long step names hits it sooner, and Story 8.7's `accDescr`, which lists every node, spends
376
- * part of the same budget.
377
- */
378
- export const MERMAID_DEFAULT_MAX_TEXT_SIZE = 50_000;
379
- /**
380
- * What a definition costs, for a caller that wants to REPORT rather than to be refused.
381
- *
382
- * The same arithmetic `renderMermaid` applies to `maxTextSize`, exported so a host can warn, offer to raise its own
383
- * `maxTextSize`, or split a chart — all of which are better answers than an exception, and none of which the generator
384
- * can choose on a caller's behalf.
385
- */
386
- export function measureDefinitionBudget(definition, budget = MERMAID_DEFAULT_MAX_TEXT_SIZE) {
387
- return {
388
- characters: definition.length,
389
- budget,
390
- overBudget: definition.length > budget,
391
- };
392
- }
393
- /**
394
- * The refusal. Carries the numbers, because "too big" without them tells a host nothing it can act on.
395
- *
396
- * A throw and not a truncated string: the alternative the measurement documents — hand the host the oversized
397
- * definition and let Mermaid quietly draw one node — is the exact failure this exists to prevent, and a caller that
398
- * would rather warn than fail has `measureDefinitionBudget` and simply does not pass `maxTextSize`.
399
- */
400
- export class DefinitionOverBudgetError extends Error {
401
- budgetReport;
402
- constructor(budgetReport) {
403
- super(`Mermaid definition is ${budgetReport.characters} characters, over the host's maxTextSize of ${budgetReport.budget}. ` +
404
- 'Mermaid renders an oversized definition as a single node WITHOUT reporting an error, and mermaid.parse() ' +
405
- 'resolves on it, so this is refused here rather than drawn wrongly. Raise the host maxTextSize explicitly, ' +
406
- 'shorten labels, or split the chart.');
407
- this.budgetReport = budgetReport;
408
- this.name = 'DefinitionOverBudgetError';
409
- }
410
- }
411
- /**
412
- * Words reserved by the generator's established flowchart contract.
413
- *
414
- * These words remain generator-owned grammar tokens rather than accepted identifiers in this established contract.
415
- */
416
- const MERMAID_RESERVED = new Set([
417
- 'end',
418
- 'graph',
419
- 'subgraph',
420
- 'style',
421
- 'class',
422
- 'classdef',
423
- 'linkstyle',
424
- 'click',
425
- 'flowchart',
426
- ]);
427
- /** Mermaid's own spelling: a chart is `TD`, a subgraph is `TB`. */
428
- const subgraphDirection = (direction) => direction === 'TD' ? 'TB' : 'LR';
429
- /** Escape authored text for an ordinary quoted Mermaid label. */
430
- function escapeLabelText(value) {
431
- return value
432
- .replace(/[\r\n]+/g, ' ')
110
+ export function escapeLabel(value) {
111
+ return oneLine(value)
433
112
  .replace(/&/g, '&amp;')
434
113
  .replace(/"/g, '&quot;')
435
114
  .replace(/</g, '&lt;')
436
115
  .replace(/>/g, '&gt;')
437
- // A public label beginning with a backtick must remain plain text, not opt into Mermaid markdown.
438
116
  .replace(/`/g, '&#96;');
439
117
  }
440
- /** Avoid `#` inside Mermaid's HTML-label style attributes; its Markdown pass rewrites a leading hash. */
441
- function htmlStyleColor(color) {
442
- if (!color.startsWith('#'))
443
- return color;
444
- const hex = color.slice(1);
445
- const expanded = hex.length === 3 || hex.length === 4
446
- ? [...hex].map((digit) => `${digit}${digit}`).join('')
447
- : hex;
448
- if (expanded.length !== 6 && expanded.length !== 8)
449
- return color;
450
- const red = Number.parseInt(expanded.slice(0, 2), 16);
451
- const green = Number.parseInt(expanded.slice(2, 4), 16);
452
- const blue = Number.parseInt(expanded.slice(4, 6), 16);
453
- if (expanded.length === 6)
454
- return `rgb(${red},${green},${blue})`;
455
- const alpha = Number.parseInt(expanded.slice(6, 8), 16) / 255;
456
- return `rgba(${red},${green},${blue},${alpha})`;
457
- }
458
- function renderLabelAnatomy(anatomy, accent) {
459
- /**
460
- * Renderer-owned HTML is the only way to give Mermaid a measurable two-column card. Authored bytes remain text:
461
- * every heading and description is escaped before entering the markup, and callers cannot opt into this path by
462
- * forging public `label` bytes because `labelAnatomy` is the trust boundary.
463
- */
464
- const icon = anatomy.icon ?? '';
465
- // Geometric diamonds are optically centered by their own ink box. Arrow/operator glyphs sit low on the
466
- // alphabetic baseline in VS Code's UI font, so loose hosts may optically correct those without moving the tile.
467
- const iconClass = icon === '◆'
468
- ? 'hx-node-card__icon'
469
- : 'hx-node-card__icon hx-node-card__icon--baseline';
470
- const marker = anatomy.marker === undefined
471
- ? ''
472
- : `<span class='hx-node-card__marker' style='margin-right:4px'>${anatomy.marker}</span>`;
473
- const badge = anatomy.badge === undefined
474
- ? ''
475
- : `<span class='hx-node-card__badge' style='margin-left:4px'>${anatomy.badge}</span>`;
476
- const description = anatomy.description === undefined
477
- ? ''
478
- : `<span class='hx-node-card__subtitle' style='display:block;min-width:0;overflow-wrap:anywhere;font-size:11px;line-height:1.35'>${escapeLabelText(anatomy.description)}</span>`;
479
- const iconColor = htmlStyleColor(accent);
480
- const tint = `color-mix(in srgb,${iconColor} 16%,transparent)`;
481
- const rim = `color-mix(in srgb,${iconColor} 38%,transparent)`;
482
- return `<span class='hx-node-card' style='display:grid;width:240px;min-width:240px;box-sizing:border-box;grid-template-columns:44px minmax(0,1fr);align-items:stretch;column-gap:10px;font-family:inherit;font-size:11px;line-height:1.35;text-align:left'>` +
483
- `<span class='${iconClass}' style='display:inline-flex;width:44px;min-height:44px;align-items:center;justify-content:center;align-self:stretch;box-sizing:border-box;border-radius:9px;background:${tint};box-shadow:inset 0 0 0 1px ${rim};color:${iconColor};font-size:32px;line-height:1'>${icon}</span>` +
484
- `<span class='hx-node-card__copy' style='display:grid;min-width:0;grid-template-rows:auto auto;row-gap:2px'>` +
485
- `<span class='hx-node-card__title' style='display:block;min-width:0;font-size:12px;font-weight:650;line-height:1.3'>${marker}<strong>${escapeLabelText(anatomy.heading)}</strong>${badge}</span>` +
486
- description +
487
- `</span>` +
488
- `</span>`;
489
- }
490
- /** A public label escaped as plain text; only structural anatomy can enable Mermaid markdown. */
491
- export function escapeLabel(value) {
492
- return escapeLabelText(value).trim();
493
- }
494
- /**
495
- * A single-line Mermaid accessibility value.
496
- *
497
- * Accessibility directives are grammar, not quoted labels: every line/control separator must be collapsed before
498
- * authored text reaches them. Ordinary punctuation and Unicode remain visible instead of being stripped.
499
- */
500
- function alternativeText(value) {
501
- return value
502
- .replace(/[\u0000-\u001f\u007f-\u009f\u2028\u2029]+/g, ' ')
503
- .replace(/\s+/g, ' ')
504
- .trim()
505
- .replace(/&/g, '&amp;')
506
- .replace(/</g, '&lt;')
507
- .replace(/>/g, '&gt;');
508
- }
509
- /** Meaningful node text, independent of strict/loose presentation markup and icon glyphs. */
510
- function semanticNodeText(node, entry) {
511
- /**
512
- * The run reaches the text alternative or it reaches this reader not at all (NFR13).
513
- *
514
- * A screen reader sees no stroke, no weight, no dash, no opacity and no marker — every channel the four states are
515
- * drawn in. Appended only for a node the overlay actually NAMES, so the no-overlay bytes cannot move, and
516
- * `indeterminate` contributes nothing because "we could not tell" is not a fact about the run.
517
- */
518
- const run = runStateAlternativeText(entry);
519
- const withRun = (text) => run === '' ? text : `${text} — ${run}`;
520
- const anatomy = node.labelAnatomy;
521
- if (anatomy === undefined) {
522
- return withRun(alternativeText(node.label) || alternativeText(node.address) || 'Unnamed step');
523
- }
524
- const heading = alternativeText(anatomy.heading);
525
- const description = anatomy.description === undefined ? '' : alternativeText(anatomy.description);
526
- return withRun([heading || alternativeText(node.address) || 'Unnamed step', description]
527
- .filter((part) => part !== '')
528
- .join(' — '));
529
- }
530
- /** Exactly one title and description for every chart, in authored group/node order. */
531
- function accessibilityAlternative(groups, emptyLabel, overlayNodes,
532
- /** Present exactly when the typed reading is chart-wide; the SAME resolution the style passes read. */
533
- readingOf) {
534
- const populated = groups.filter((group) => group.nodes.length > 0);
535
- if (populated.length === 0) {
536
- const absent = alternativeText(emptyLabel) || 'No steps';
537
- return ['Empty workflow', `${absent}.`];
538
- }
539
- const titled = populated
540
- .map((group) => group.title === undefined ? '' : alternativeText(group.title))
541
- .filter((title) => title !== '');
542
- const title = titled.length === 0
543
- ? 'Workflow'
544
- : titled.length === 1
545
- ? `${titled[0]} workflow`
546
- : `${titled.join(', ')} workflows`;
547
- const seenNodes = new Set();
548
- const description = populated
549
- .map((group) => {
550
- const groupTitle = group.title === undefined ? '' : alternativeText(group.title);
551
- const nodes = group.nodes
552
- .filter((node) => {
553
- if (seenNodes.has(node.address))
554
- return false;
555
- seenNodes.add(node.address);
556
- return true;
557
- })
558
- /**
559
- * The text channel is resolved through `resolveNodeState`, the SAME resolution the `style` pass uses.
560
- *
561
- * Not a tidiness point: while the typed overlay is active the drawing gives every node a reading, so the text
562
- * has to as well, and an unrecognised state has to degrade to `indeterminate` identically in both. Sharing the
563
- * resolver is the only way the two channels cannot drift apart. A roles-only entry claims no state, so under
564
- * the chart-wide reading it is stated as the indeterminate one.
565
- */
566
- .map((node) => {
567
- if (readingOf === undefined)
568
- return semanticNodeText(node, overlayNodes[node.address]);
569
- const resolved = readingOf(node.address);
570
- return semanticNodeText(node, {
571
- state: resolved.state ?? INDETERMINATE_RUN_STATE,
572
- repeat: resolved.repeat,
573
- });
574
- })
575
- .join('; ');
576
- return groupTitle === '' ? nodes : `${groupTitle}: ${nodes}`;
577
- })
578
- .join('. ');
579
- return [title, description];
580
- }
118
+ /** The compact id of the node drawn at `index`: `n` and the index in base 36 (`n9`, `na`, `n10`), as short as stays readable. */
119
+ export const compactNodeId = (index) => `n${index.toString(36)}`;
581
120
  /**
582
- * Validate the many-to-one Mermaid-id projection once, before any node, group or edge bytes are emitted.
121
+ * Build the compact graph.
583
122
  *
584
- * Duplicate appearances of one node address remain harmless and are still de-duplicated during emission. A group id
585
- * occupies Mermaid's same identifier namespace, so it is checked even when its canonical text equals a node address.
123
+ * The occurrence expansion runs first, so a run's repeated step is already a chain of drawn nodes; then every node
124
+ * gets a short id in first-appearance order and every edge whose ends were both drawn is kept once.
586
125
  */
587
- function assertUniqueRenderedIds(groups, multiStage) {
588
- const claimed = new Map();
589
- const seenNodes = new Set();
590
- const claim = (kind, address) => {
591
- const rendered = renderedNodeId(address);
592
- const existing = claimed.get(rendered);
593
- if (existing !== undefined) {
594
- if (kind === 'node' && existing.kind === 'node' && existing.address === address)
595
- return;
596
- throw new Error(`Mermaid rendered-id collision for "${rendered}": ${existing.kind} "${existing.address}" and ${kind} "${address}"`);
597
- }
598
- claimed.set(rendered, { kind, address });
599
- };
126
+ export function compactFlowGraph(authoredGroups, authoredEdges, opts = {}) {
127
+ const expansion = expandOccurrences(authoredGroups, authoredEdges, opts.occurrences);
128
+ const groups = expansion.groups;
129
+ const multiStage = opts.multiStage ?? groups.length > 1;
130
+ const callerOverlay = opts.overlay?.nodes ?? {};
131
+ const overlay = mergeNodeOverlays(mergeNodeOverlays(opts.occurrenceOverlay, expansion.nodes), callerOverlay);
132
+ const idOf = new Map();
133
+ const nodes = [];
134
+ const compactGroups = [];
600
135
  for (const group of groups) {
601
- if (multiStage && group.title !== undefined)
602
- claim('group', group.key);
136
+ const members = [];
603
137
  for (const node of group.nodes) {
604
- if (seenNodes.has(node.address))
138
+ if (idOf.has(node.address))
605
139
  continue;
606
- seenNodes.add(node.address);
607
- claim('node', node.address);
140
+ const id = compactNodeId(nodes.length);
141
+ idOf.set(node.address, id);
142
+ members.push(id);
143
+ const shape = classifyNodeTreatment(node);
144
+ // The shape decides the icon where the type alone cannot: a failure, a stub and a sleep read by their kind.
145
+ const family = shape === 'exception'
146
+ ? 'terminal.error'
147
+ : shape === 'unimplemented'
148
+ ? 'unmapped'
149
+ : shape === 'delay'
150
+ ? 'wait.sleep'
151
+ : node.iconFamily;
152
+ const key = node.key === undefined ? undefined : oneLine(node.key) || undefined;
153
+ const type = node.type === undefined ? undefined : oneLine(node.type) || undefined;
154
+ const repeat = overlay[node.address]?.repeat;
155
+ nodes.push({
156
+ id,
157
+ ...(key === undefined ? {} : { key }),
158
+ name: oneLine(node.name) || key || '?',
159
+ ...(type === undefined ? {} : { type }),
160
+ family: stepIconClassName(family),
161
+ shape,
162
+ ...(isCountableRepeat(repeat) ? { repeat } : {}),
163
+ });
164
+ }
165
+ if (members.length > 0) {
166
+ compactGroups.push({
167
+ id: `g${compactGroups.length}`,
168
+ ...(multiStage && group.title !== undefined ? { title: oneLine(group.title) } : {}),
169
+ nodes: members,
170
+ });
608
171
  }
609
172
  }
610
- }
611
- /**
612
- * A label guaranteed non-empty, so every emitted node has visible text.
613
- *
614
- * `escapeLabel` ends in `.trim()`, so a label that is non-empty in the model can become `''`; the fallback prevents
615
- * the emitter from producing an empty node label.
616
- *
617
- * Four reachable inputs, each verified end to end through the real builders: `if: ""` on an IF step (the route graph
618
- * maps it to the label `''`, since only `null`/`undefined` become `'?'`), `next: ' '`, `next: '\n'`, and a SWITCH
619
- * whose case key is `' '`. One keystroke of YAML.
620
- *
621
- * A regression relative to the authorities rather than a ported flaw: neither dashboard's nor the CLI's `escapeLabel`
622
- * trims, so neither can manufacture an empty label.
623
- */
624
- function safeLabel(value, fallback) {
625
- const escaped = escapeLabel(value);
626
- if (escaped !== '')
627
- return escaped;
628
- // The fallback is escaped too, and if THAT is empty the node still needs something a reader can see.
629
- const escapedFallback = escapeLabel(fallback);
630
- return escapedFallback === '' ? '?' : escapedFallback;
631
- }
632
- /**
633
- * The same choice `safeLabel` makes — label, else fallback, else `'?'` — but on the RAW text.
634
- *
635
- * `safeLabel` returns an ESCAPED string. Handing that to `renderLabelAnatomy`, which escapes what it is given,
636
- * escaped it twice: a node with no `stepKey`/`type` labelled `Load & save` drew the visible text `Load &amp; save`
637
- * the moment an overlay gave it a marker or a badge. Anatomy carries raw text by contract; only the emitter escapes.
638
- */
639
- function rawHeading(value, fallback) {
640
- const trimmed = value.trim();
641
- if (trimmed !== '')
642
- return trimmed;
643
- const trimmedFallback = fallback.trim();
644
- return trimmedFallback === '' ? '?' : trimmedFallback;
645
- }
646
- /**
647
- * A colour mermaid's grammar accepts in a `style` / `linkStyle` declaration.
648
- *
649
- * The Mermaid 12 characterization proves a bare identifier like `red` parses and is emitted stroke-only.
650
- * Function-valued colours and CSS variables are outside the emitter's literal grammar and are withheld.
651
- *
652
- * It matters more here than in the Vue caller it was ported from: this is a published library whose contract invites
653
- * the caller to supply a colour, and a theme token (`var(--vscode-focusBorder)`, an `rgba` from a palette) is the most
654
- * likely thing a webview or dashboard reaches for.
655
- *
656
- * So an unusable colour drops the whole highlight rather than emitting a declaration that cannot parse.
657
- */
658
- function usableColor(color) {
659
- if (color === undefined)
660
- return undefined;
661
- const trimmed = color.trim();
662
- // Mermaid accepts only CSS's legal hex lengths: RGB, RGBA, RRGGBB and RRGGBBAA.
663
- return /^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/.test(trimmed) ||
664
- /^[A-Za-z][A-Za-z0-9-]*$/.test(trimmed)
665
- ? trimmed
666
- : undefined;
667
- }
668
- /**
669
- * The OVERLAY colour grammar — narrower than `usableColor`, and deliberately so (Story 8.7, FR57/FR58).
670
- *
671
- * Every `fill:` the emitter writes must be opaque six-digit hex, and a run-state colour reaches both a stroke and a
672
- * polygon fill, so six-digit hex is the only form the emitter can honour completely. `#f00`, `#f00a`, `#ff000080`
673
- * and a bare identifier like `orange` parse but cannot be honoured, and the shipped behaviour — keep the stroke,
674
- * silently drop the fill — is a half-applied highlight that reads as a different run state.
675
- *
676
- * So the whole highlight for that dimension is dropped: no node `style`, no `linkStyle`. AD-23 stage 2 and Story
677
- * 10.3 both require a host to resolve its theme token to literal hex before calling, so nothing loses a capability
678
- * it was meant to have.
679
- */
680
- function overlayColor(color) {
681
- if (color === undefined)
682
- return undefined;
683
- const trimmed = color.trim();
684
- return /^#[0-9a-fA-F]{6}$/.test(trimmed) ? trimmed : undefined;
685
- }
686
- /** One node line, shaped by its approved treatment and labelled for the requested host fidelity. */
687
- function nodeLine(node, fidelity, accent, content) {
688
- const id = renderedNodeId(node.address);
689
- const sourceLabel = fidelity === 'loose' ? (node.looseLabel ?? node.label) : node.label;
690
- const baseAnatomy = fidelity === 'loose'
691
- ? (node.looseLabelAnatomy ?? (node.looseLabel === undefined ? node.labelAnatomy : undefined))
692
- : node.labelAnatomy;
693
- const marker = content?.marker === true ? RUN_STATE_ERROR_MARKER.glyph : undefined;
694
- const badge = repeatBadgeText(content?.repeat);
695
173
  /**
696
- * Run-state content is grafted onto the anatomy HERE rather than in the composer, because the composer runs in the
697
- * host that builds the model and the overlay is an ARGUMENT to this render (AD-29). `composeNodeLabelModel` takes
698
- * the same content for a caller that composes its own labels; this path covers every model already built.
699
- *
700
- * A node with no anatomy — `composeNodeLabelModel`'s early return, a node with neither `stepKey` nor `type` — gets
701
- * one synthesised from its plain label the moment it carries a marker or a badge. Dropping a run fact because a
702
- * step had no key would hide a failure.
174
+ * The run reading is chart-wide once a CALLER entry with a `state` names a drawn node. An unknown state value reads
175
+ * as `indeterminate`: a payload that arrived as JSON can say anything, and guessing is worse than saying unknown.
703
176
  */
704
- const anatomy = marker === undefined && badge === undefined
705
- ? baseAnatomy
706
- : {
707
- ...(baseAnatomy ?? { heading: rawHeading(sourceLabel, node.address) }),
708
- ...(marker ? { marker } : {}),
709
- ...(badge ? { badge } : {}),
710
- };
711
- const treatment = classifyNodeTreatment(node);
712
- const label = anatomy
713
- ? renderLabelAnatomy(anatomy, accent)
714
- : safeLabel(sourceLabel, node.address);
715
- const iconFamily = treatment === 'exception'
716
- ? 'terminal.error'
717
- : treatment === 'unimplemented'
718
- ? 'unmapped'
719
- : treatment === 'delay'
720
- ? 'wait.sleep'
721
- : node.iconFamily;
722
- const iconClass = `:::${stepIconClassName(iconFamily)}`;
723
- switch (treatment) {
724
- case 'flow-start':
725
- case 'flow-end':
726
- return ` ${id}(["${label}"])${iconClass}`;
727
- case 'branch':
728
- return ` ${id}{{"${label}"}}${iconClass}`;
729
- case 'subroutine':
730
- return ` ${id}[["${label}"]]${iconClass}`;
731
- case 'delay':
732
- return ` ${id}@{ shape: delay, label: "${label}" }`;
733
- case 'process':
734
- case 'exception':
735
- case 'unimplemented':
736
- return ` ${id}("${label}")${iconClass}`;
177
+ const stated = Object.entries(callerOverlay).some(([address, entry]) => entry?.state !== undefined && idOf.has(address));
178
+ if (stated) {
179
+ const addressOf = new Map([...idOf].map(([address, id]) => [id, address]));
180
+ for (const [index, node] of nodes.entries()) {
181
+ const requested = overlay[addressOf.get(node.id)]?.state;
182
+ nodes[index] = {
183
+ ...node,
184
+ state: requested !== undefined && Object.hasOwn(RUN_STATE_CLASS_DEF, requested) ? requested : INDETERMINATE_RUN_STATE,
185
+ };
186
+ }
737
187
  }
738
- }
739
- function edgeLine(edge) {
740
- const from = renderedNodeId(edge.from);
741
- const to = renderedNodeId(edge.to);
742
- /**
743
- * Emptiness is tested on the ESCAPED value, not the raw one (CRITICAL-1).
744
- *
745
- * The guard used to read the raw label and then emit the escaped one, so `label: ' '` took the labelled branch and
746
- * emitted `-->|""|` — a parse error. An unlabelled arrow is the right answer for a label that escapes to nothing.
747
- */
748
- const label = edge.label === undefined ? '' : escapeLabel(edge.label);
749
- return label === ''
750
- ? ` ${from} --> ${to}`
751
- : ` ${from} -->|"${label}"| ${to}`;
752
- }
753
- /**
754
- * A per-element node run-state style.
755
- *
756
- * Width comes from the declared role table, never a literal, and the fill is the opaque node fill. The
757
- * alpha-suffixed `fill:#rrggbb24` tint the shipped emitter wrote is gone: Mermaid does not apply an 8-digit fill
758
- * reliably to a polygon, and FR58 caps a `fill:` value at seven characters.
759
- */
760
- function nodeStyle(node, color, fill, width, extra) {
761
- /**
762
- * The three Story 8.8 properties are appended CONDITIONALLY, each measured against Mermaid 12.0.0 on 2026-09-28 at
763
- * `securityLevel: 'strict'`:
764
- *
765
- * 1. `opacity:0.7` is honoured on a plain rect AND on a hexagon polygon — it reaches the SVG as
766
- * `opacity:0.7 !important`.
767
- * 2. `color:#hex` is honoured and reaches the label, but paints the WHOLE label one colour (see `resolveNodeState`).
768
- * 3. `stroke-dasharray:6 3` is honoured on a NODE, not only on a `linkStyle`.
769
- *
770
- * Conditional and not unconditional because an always-emitted property would write `opacity:1` onto every node and
771
- * move every overlay-free golden — the byte-identity AD-23 requires.
772
- */
773
- const properties = [
774
- `stroke:${color}`,
775
- `stroke-width:${width}`,
776
- `fill:${fill}`,
777
- ...(extra?.color === undefined ? [] : [`color:${extra.color}`]),
778
- ...(extra?.opacity === undefined ? [] : [`opacity:${extra.opacity}`]),
779
- ...(extra?.dash === undefined ? [] : [`stroke-dasharray:${extra.dash}`]),
780
- ];
781
- return ` style ${renderedNodeId(node.address)} ${properties.join(',')}`;
782
- }
783
- /** A node `style` statement from one resolved reading — the state pass and the roles-only pass both write this. */
784
- function resolvedNodeStyle(node, style) {
785
- return nodeStyle(node, style.stroke, style.fill, style.width, {
786
- color: style.color,
787
- opacity: style.opacity,
788
- dash: style.dash,
789
- });
790
- }
791
- /** The base and kind roles, each host override checked and every unusable one falling back to the approved role. */
792
- function resolveNodeColors(requested) {
188
+ const edges = [];
189
+ const seen = new Set();
190
+ for (const edge of expansion.edges) {
191
+ const from = idOf.get(edge.from);
192
+ const to = idOf.get(edge.to);
193
+ // An edge naming a node no group drew would make Mermaid invent a placeholder box.
194
+ if (from === undefined || to === undefined)
195
+ continue;
196
+ const label = edge.label === undefined ? undefined : oneLine(edge.label) || undefined;
197
+ const identity = `${from} ${to} ${label ?? ''}`;
198
+ if (seen.has(identity))
199
+ continue;
200
+ seen.add(identity);
201
+ edges.push({
202
+ from,
203
+ to,
204
+ ...(label === undefined ? {} : { label }),
205
+ kind: edge.kind ?? (label === undefined ? 'next' : 'branch'),
206
+ });
207
+ }
208
+ const titles = groups
209
+ .filter((group) => group.nodes.length > 0 && group.title !== undefined)
210
+ .map((group) => oneLine(group.title))
211
+ .filter((title) => title !== '');
212
+ const title = nodes.length === 0
213
+ ? 'Empty workflow'
214
+ : titles.length === 0
215
+ ? 'Workflow'
216
+ : titles.length === 1
217
+ ? `${titles[0]} workflow`
218
+ : `${titles.join(', ')} workflows`;
793
219
  return {
794
- // A fill reaches a polygon, so it obeys FR58's six-digit form rather than the looser stroke grammar.
795
- fill: overlayColor(requested?.fill) ?? APPROVED_NODE_COLORS.fill,
796
- nodeBorder: usableColor(requested?.nodeBorder) ?? APPROVED_NODE_COLORS.nodeBorder,
797
- failStroke: usableColor(requested?.failStroke) ?? APPROVED_NODE_COLORS.failStroke,
798
- edge: usableColor(requested?.edge) ?? APPROVED_NODE_COLORS.edge,
799
- notTakenStroke: usableColor(requested?.notTakenStroke) ?? APPROVED_NODE_COLORS.notTakenStroke,
800
- edgeLabelText: usableColor(requested?.edgeLabelText) ?? APPROVED_NODE_COLORS.edgeLabelText,
801
- // The plate is a FILL, so it obeys FR58 rather than the looser stroke grammar.
802
- edgeLabelBg: overlayColor(requested?.edgeLabelBg) ?? APPROVED_NODE_COLORS.edgeLabelBg,
220
+ graph: {
221
+ title,
222
+ groups: compactGroups,
223
+ nodes,
224
+ edges,
225
+ ...(nodes.length === 0 ? { empty: oneLine(opts.emptyLabel ?? '') || 'No steps' } : {}),
226
+ },
227
+ idOf,
228
+ collapsedRanges: expansion.collapsedRanges,
803
229
  };
804
230
  }
805
- /** Kind/polygon styling. Run state suppresses this complete statement when both dimensions collide. */
806
- function kindStyle(node, colors) {
807
- const fill = colors.fill;
808
- const treatment = classifyNodeTreatment(node);
809
- if (treatment === 'branch') {
810
- return ` style ${renderedNodeId(node.address)} fill:${fill}`;
811
- }
812
- const stroke = treatment === 'exception'
813
- ? usableColor(colors.failStroke)
814
- : treatment === 'unimplemented'
815
- ? usableColor(colors.nodeBorder)
816
- : undefined;
817
- if (stroke === undefined)
818
- return undefined;
819
- return ` style ${renderedNodeId(node.address)} stroke:${stroke},fill:${fill}`;
820
- }
231
+ /** The `classDef` a run-state class carries: line-work only, because colour is the host stylesheet's. */
232
+ const RUN_STATE_CLASS_DEF = {
233
+ ranOk: 'stroke-width:1px',
234
+ ranFailed: 'stroke-width:2px',
235
+ neverReached: 'opacity:0.7',
236
+ branchNotTaken: 'stroke-dasharray:6 3',
237
+ indeterminate: 'stroke-width:1px',
238
+ };
239
+ const RUN_STATE_WORDS = {
240
+ ranOk: 'ran OK',
241
+ ranFailed: 'failed',
242
+ neverReached: 'never ran',
243
+ branchNotTaken: 'branch not taken',
244
+ indeterminate: 'run state unknown',
245
+ };
246
+ /** The class a run state draws with. */
247
+ export const runStateClassName = (state) => `rs-${state}`;
248
+ /** One edge label, unquoted when it is plain enough to need no quotes. */
249
+ const edgeLabel = (label) => {
250
+ const escaped = escapeLabel(label);
251
+ return /^[\w .-]+$/.test(escaped) ? escaped : `"${escaped}"`;
252
+ };
821
253
  /**
822
- * The diagram.
823
- *
824
- * Deterministic for a given `(groups, edges, opts)`: every list is emitted in the order given, duplicates are
825
- * collapsed by first appearance, and nothing consults the clock or the environment.
254
+ * Mermaid text for ONE direction of a compact graph.
826
255
  *
827
- * The font each host must pin (AD-32(a)): this generator emits no font. A host picks one font string and passes
828
- * it to mermaid as both `fontFamily` and `themeVariables.fontFamily` (mermaid copies the former into the latter
829
- * only when the latter is unset), and makes the canvas container's CSS `font-family` resolve to that same string,
830
- * so the boxes mermaid measures match the text the host paints. Write the string in CSS-canonical form (unquoted
831
- * family names) so the resolved CSS value is byte-equal to what mermaid received. Known hosts:
832
- * - dashboard-v2 flow canvas: `Inter, -apple-system, system-ui, sans-serif` (`typography.fontFamily.sans`), set
833
- * on both mermaid keys and on the render container.
834
- * - VS Code webview (`src/webview/flowDiagram.ts`): `var(--vscode-font-family)`, which is also the preview body
835
- * CSS (`previewHtml.ts`).
836
- * - Report page (`hexasync-template-report-render` `renderHtml.ts`): not pinned yet — it passes no font, so mermaid
837
- * uses its default while the body paints `-apple-system, Segoe UI, …`.
256
+ * Pure: a host that toggles direction calls this again with the same graph and the other direction.
838
257
  */
839
- export function renderMermaid(authoredGroups, authoredEdges, opts = {}) {
840
- return renderMermaidWithOccurrences(authoredGroups, authoredEdges, opts).definition;
841
- }
842
- /** `renderMermaid`, for a caller that also needs the occurrence facts the drawing carries. */
843
- export function renderMermaidWithOccurrences(authoredGroups, authoredEdges, opts = {}) {
844
- /**
845
- * The occurrence expansion runs FIRST — before the collision check, before a single byte (FR64).
846
- *
847
- * Not merely early: there is no later point at which it could run. `assertUniqueRenderedIds` is what proves two
848
- * occurrence addresses do not project onto one mermaid id, and it can only see nodes that already exist. A host
849
- * doing this afterwards would be rewriting a validated definition, which AD-32(b) forbids outright.
850
- *
851
- * With no plan `expandOccurrences` returns the very arrays passed in, so the whole feature costs a
852
- * `plan === undefined` test on every no-run render.
853
- */
854
- const expansion = expandOccurrences(authoredGroups, authoredEdges, opts.occurrences);
855
- const groups = expansion.groups;
856
- const edges = expansion.edges;
857
- // Empty when the caller expanded first (the two flow adapters do); each of those returns its OWN expansion's set.
858
- const collapsedRanges = expansion.collapsedRanges;
859
- const direction = opts.direction ?? 'TD';
860
- const fidelity = opts.fidelity ?? 'strict';
861
- const multiStage = opts.multiStage ?? groups.length > 1;
862
- assertUniqueRenderedIds(groups, multiStage);
863
- const emptyLabel = opts.emptyLabel ?? 'No steps';
864
- /**
865
- * TWO maps, and the distinction is the whole of Story 8.9's regression fix.
866
- *
867
- * - `callerOverlayNodes` is what the CALLER stated, and it alone decides whether the typed overlay is in charge.
868
- * - `overlayNodes` is what each node's content and style are read from: the caller's statement over the derived
869
- * occurrence badge, merged FIELD BY FIELD so a failed range keeps both its state and its `×N`.
870
- *
871
- * Deciding activation from the merged map made a plan on one step restyle the whole chart — see
872
- * `RenderOptions.occurrenceOverlay`.
873
- */
874
- const callerOverlayNodes = opts.overlay?.nodes;
875
- const derivedOverlayNodes = mergeNodeOverlays(opts.occurrenceOverlay, expansion.nodes);
876
- const overlayNodes = mergeNodeOverlays(derivedOverlayNodes, callerOverlayNodes);
877
- const nodeColors = resolveNodeColors(opts.nodeColors);
878
- /**
879
- * Is the TYPED reading chart-wide? Decided ONCE, here, and read by every channel that depends on it.
880
- *
881
- * Only a CALLER entry that carries a `state` and names an address a group actually holds decides it — an entry
882
- * naming nothing on the canvas is ignored in silence, and a roles-only or badge-only entry makes no state claim to
883
- * be authoritative about. Once it is, EVERY drawn node draws a state and EVERY drawn node is described as one: a
884
- * chart where some nodes carry a run reading and others carry none would leave a reader unable to tell "not
885
- * reached" from "not described", which is the distinction CAP-7 exists for. The unnamed ones read `indeterminate`.
886
- *
887
- * Computed before the accessibility text because `accTitle`/`accDescr` are the first bytes emitted; every node in
888
- * `groups` is drawn, so this is the same answer the style pass would reach from `drawn`.
889
- */
890
- const groupAddresses = new Set(groups.flatMap((group) => group.nodes.map((node) => node.address)));
891
- const typedOverlayActive = Object.entries(callerOverlayNodes ?? {}).some(([address, entry]) => carriesState(entry) && groupAddresses.has(address));
892
- const readings = new Map();
893
- /** Each node's ONE resolution, shared by the label, `accDescr`, every style pass and `linkStyle`. */
894
- const readingOf = (address) => {
895
- let reading = readings.get(address);
896
- if (reading === undefined) {
897
- reading = resolveNodeState(overlayNodes[address], nodeColors.fill, typedOverlayActive);
898
- readings.set(address, reading);
899
- }
900
- return reading;
901
- };
902
- const [accTitle, accDescr] = accessibilityAlternative(groups, emptyLabel, overlayNodes, typedOverlayActive ? readingOf : undefined);
903
- const lines = [
904
- `flowchart ${direction}`,
905
- ` accTitle: ${accTitle}`,
906
- ` accDescr: ${accDescr}`,
907
- ];
908
- /**
909
- * Nothing to draw ⇒ SAY SO (Story 4.3 review, MED-1 / MEDIUM-3).
910
- *
911
- * A bare `flowchart TD` parses and renders a blank box, and a titled Markdown section wrapping one is
912
- * indistinguishable from a failure. Measured against `epic-4-ground-truth.md`: the shipped renderer had invented a
913
- * fourth answer where the note says one of three must be chosen.
914
- */
915
- if (groups.every((group) => group.nodes.length === 0)) {
916
- return {
917
- definition: withinBudget([
918
- ...lines,
919
- ` empty("${safeLabel(emptyLabel, 'No steps')}"):::${stepIconClassName(undefined)}`,
920
- ].join('\n'), opts.maxTextSize),
921
- collapsedRanges,
922
- };
258
+ export function compactFlowMermaid(graph, direction = 'TD') {
259
+ const lines = [`flowchart ${direction}`, `accTitle: ${escapeLabel(graph.title)}`];
260
+ if (graph.nodes.length === 0) {
261
+ const empty = graph.empty ?? 'No steps';
262
+ lines.push(`accDescr: ${escapeLabel(empty)}.`, `n0("${escapeLabel(empty)}")`, `class n0 ${stepIconClassName(undefined)}`);
263
+ return lines.join('\n');
923
264
  }
924
- const emitted = new Set();
925
- const push = (line) => {
926
- if (emitted.has(line))
927
- return;
928
- emitted.add(line);
929
- lines.push(line);
930
- };
931
- const drawn = new Set();
932
- const drawnNodes = new Map();
933
- for (const group of groups) {
934
- if (multiStage && group.title !== undefined) {
935
- lines.push(` subgraph ${renderedNodeId(group.key)}["${escapeLabel(group.title)}"]`);
936
- // Rule 1: its OWN direction, because the parent's is ignored inside a subgraph.
937
- lines.push(` direction ${subgraphDirection(direction)}`);
938
- }
939
- for (const node of group.nodes) {
940
- if (drawn.has(node.address))
941
- continue;
942
- const entry = overlayNodes[node.address];
943
- // Both slots come from the shared resolution, so the label can never claim a marker the style pass did not draw.
944
- const resolved = entry === undefined ? undefined : readingOf(node.address);
945
- const content = resolved === undefined
946
- ? undefined
947
- : { marker: resolved.marker, ...(resolved.repeat === undefined ? {} : { repeat: resolved.repeat }) };
948
- const accent = resolved?.style?.stroke ??
949
- (classifyNodeTreatment(node) === 'exception' ? nodeColors.failStroke : nodeColors.nodeBorder);
950
- push(nodeLine(node, fidelity, accent, content));
951
- drawn.add(node.address);
952
- drawnNodes.set(node.address, node);
953
- }
954
- if (multiStage && group.title !== undefined)
955
- lines.push(' end');
265
+ const runCounts = new Map();
266
+ for (const node of graph.nodes) {
267
+ if (node.state !== undefined)
268
+ runCounts.set(node.state, (runCounts.get(node.state) ?? 0) + 1);
956
269
  }
957
- // Mermaid 12 rejects the `:::class` suffix on general-shape syntax, so delay hooks use its class statement form.
958
- for (const node of drawnNodes.values()) {
959
- if (classifyNodeTreatment(node) !== 'delay')
960
- continue;
961
- push(` class ${renderedNodeId(node.address)} ${stepIconClassName('wait.sleep')}`);
962
- }
963
- let labelledEdgeDrawn = false;
964
- for (const edge of edges) {
965
- // An edge naming a node no group drew would render mermaid's own placeholder box, silently inventing a node.
966
- if (!drawn.has(edge.from) || !drawn.has(edge.to))
967
- continue;
968
- const line = edgeLine(edge);
969
- // Tested on the EMITTED line, so a label that escapes to nothing does not buy a plate it never shows.
970
- if (line.includes('-->|'))
971
- labelledEdgeDrawn = true;
972
- push(line);
973
- }
974
- /**
975
- * The overlay — per element, and AFTER the nodes so a style statement always names something already drawn.
976
- *
977
- * Class declarations precede base/kind styles and the run-state overlay (Story 4.3 review, MED-4). They remain
978
- * hoisted out of every subgraph and declared once per class name.
979
- */
980
- for (const shapeClass of opts.shapeClasses ?? []) {
981
- const members = shapeClass.addresses.filter((a) => drawn.has(a));
982
- if (members.length === 0)
983
- continue;
984
- /**
985
- * The class NAME reaches Mermaid verbatim, so a space or a reserved word is a parse error. The `ic-` prefix is
986
- * reserved for the asset-backed icon hook; allowing a caller-defined `classDef` there would make one class mean
987
- * both shape and icon styling. Invalid or reserved names are refused rather than costing the whole chart.
988
- */
989
- if (!/^[A-Za-z][A-Za-z0-9_-]*$/.test(shapeClass.name) ||
990
- MERMAID_RESERVED.has(shapeClass.name.toLowerCase()) ||
991
- shapeClass.name.toLowerCase().startsWith('ic-')) {
992
- continue;
270
+ const decisions = graph.nodes.filter((node) => node.shape === 'branch').length;
271
+ const run = [...runCounts].map(([state, count]) => `${count} ${RUN_STATE_WORDS[state]}`).join(', ');
272
+ lines.push(`accDescr: ${graph.nodes.length} nodes, ${decisions} decisions, ${graph.edges.length} edges` +
273
+ (run === '' ? '' : `; ${run}`));
274
+ const byId = new Map(graph.nodes.map((node) => [node.id, node]));
275
+ const subgraphDirection = direction === 'TD' ? 'TB' : 'LR';
276
+ for (const group of graph.groups) {
277
+ // Rule 1: a subgraph ignores the chart's direction, so it states its own.
278
+ if (group.title !== undefined)
279
+ lines.push(`subgraph ${group.id}["${escapeLabel(group.title)}"]`, `direction ${subgraphDirection}`);
280
+ for (const id of group.nodes) {
281
+ const node = byId.get(id);
282
+ // The key is shown unless the name already spells it (`Read Ledger` ↔ `READ_LEDGER`): that is lossless, and the
283
+ // single largest saving in a long definition.
284
+ const key = node.key === undefined || node.key === keyOfName(node.name) ? undefined : node.key;
285
+ const subtitle = [key, node.type].filter((part) => part !== undefined).join(' · ');
286
+ // The title is the label's first line; the stylesheet styles that line, so no markup is spent on it.
287
+ const title = escapeLabel(node.name) +
288
+ (node.repeat === undefined ? '' : ` ${repeatBadgeText(node.repeat)}`);
289
+ const label = subtitle === '' ? title : `${title}<br>${escapeLabel(subtitle)}`;
290
+ // Quotes only where Mermaid needs them: plain words (and our own `<br>`) parse bare; `end` is grammar.
291
+ const text = /^[A-Za-z0-9 _.·<>]+$/.test(label) && !/\bend\b/i.test(label) ? label : `"${label}"`;
292
+ switch (node.shape) {
293
+ case 'flow-start':
294
+ case 'flow-end':
295
+ lines.push(`${id}([${text}])`);
296
+ break;
297
+ case 'branch':
298
+ lines.push(`${id}{{${text}}}`);
299
+ break;
300
+ case 'subroutine':
301
+ lines.push(`${id}[[${text}]]`);
302
+ break;
303
+ case 'delay':
304
+ lines.push(`${id}@{ shape: delay, label: "${label}" }`);
305
+ break;
306
+ default:
307
+ lines.push(`${id}(${text})`);
308
+ }
993
309
  }
994
- push(` classDef ${shapeClass.name} ${shapeClass.declaration}`);
995
- push(` class ${members.map((a) => renderedNodeId(a)).join(',')} ${shapeClass.name}`);
310
+ if (group.title !== undefined)
311
+ lines.push('end');
996
312
  }
997
313
  /**
998
- * Kind styling is suppressed for every node once the typed overlay is in charge, not only for the named ones, and
999
- * for a node a roles-only entry draws.
1000
- *
1001
- * DESIGN.md `node-exception-kind` / `node-stub-kind`: *"Where the node also has a run state, the run state stroke
1002
- * wins and only the icon carries the kind."* Under the typed overlay every node has a run state, so every node's
1003
- * kind stroke yields and the icon family alone carries the kind — which is also the only way a node never ends up
1004
- * with two competing `style` statements.
314
+ * An edge that leaves the node the previous edge arrived at continues that line (`n1-->n2-->|YES|n3`). Mermaid
315
+ * numbers chained edges in the order written, so `linkStyle` indices still follow `graph.edges`.
1005
316
  */
1006
- for (const node of drawnNodes.values()) {
1007
- if (typedOverlayActive || readingOf(node.address).pass === 'roles')
1008
- continue;
1009
- const style = kindStyle(node, nodeColors);
1010
- if (style !== undefined)
1011
- push(style);
317
+ let chainEnd;
318
+ for (const edge of graph.edges) {
319
+ const arrow = edge.label === undefined ? '-->' : `-->|${edgeLabel(edge.label)}|`;
320
+ if (edge.from === chainEnd)
321
+ lines[lines.length - 1] += `${arrow}${edge.to}`;
322
+ else
323
+ lines.push(`${edge.from}${arrow}${edge.to}`);
324
+ chainEnd = edge.to;
1012
325
  }
1013
- // One `style` statement per stated (or indeterminate) node, in DRAWING order.
1014
- for (const node of drawnNodes.values()) {
1015
- const reading = readingOf(node.address);
1016
- if (reading.pass === 'state' && reading.style !== undefined)
1017
- push(resolvedNodeStyle(node, reading.style));
326
+ // One `class` statement per class, listing its members — never one per node.
327
+ const members = new Map();
328
+ const member = (className, id) => {
329
+ const list = members.get(className);
330
+ if (list === undefined)
331
+ members.set(className, [id]);
332
+ else
333
+ list.push(id);
334
+ };
335
+ for (const node of graph.nodes) {
336
+ member(node.family, node.id);
337
+ if (node.state !== undefined)
338
+ member(runStateClassName(node.state), node.id);
1018
339
  }
1019
- /**
1020
- * Roles-only statements, in the OVERLAY's own entry order — the caller's entries first, then any derived ones.
1021
- *
1022
- * Not drawing order: the retired lists wrote every traversed style and then every errored one, so a producer that
1023
- * lists its entries the same way keeps those bytes even where a failing step is drawn before a reached one. The
1024
- * overlay is part of AD-22's tuple, so its entry order is a deterministic input.
1025
- *
1026
- * The failed node is the ONE element the GENERATOR draws at `emphasis` in the log reading. A caller-supplied
1027
- * `shapeClasses` declaration reaches Mermaid verbatim and is NOT role-checked, so a `classDef` body may carry its
1028
- * own `stroke-width` — that is the caller's statement about its own class, not a second run-state emphasis.
1029
- */
1030
- for (const address of new Set([...Object.keys(callerOverlayNodes ?? {}), ...Object.keys(overlayNodes)])) {
1031
- const node = drawnNodes.get(address);
1032
- if (node === undefined)
1033
- continue;
1034
- const reading = readingOf(address);
1035
- if (reading.pass === 'roles' && reading.style !== undefined)
1036
- push(resolvedNodeStyle(node, reading.style));
340
+ for (const state of [...RUN_STATES, INDETERMINATE_RUN_STATE]) {
341
+ if (runCounts.has(state))
342
+ lines.push(`classDef ${runStateClassName(state)} ${RUN_STATE_CLASS_DEF[state]}`);
1037
343
  }
1038
- /**
1039
- * A `linkStyle` for EVERY drawn edge, not only the highlighted ones — DERIVED from the per-node readings.
1040
- *
1041
- * AD-23 makes stage 1 the sole owner of edge colour, so a neutral edge that carried no declaration was leaving its
1042
- * colour to whatever theme the host happened to load — three surfaces, three answers. Edges are styled by INDEX
1043
- * (`linkStyle 3 …`), which is why the index has to be the one mermaid will assign: the position of the edge among
1044
- * those actually EMITTED and deduped, not among those the model holds.
1045
- *
1046
- * The three-way rule (UX-DR16):
1047
- * 1. an edge entering a DECLINED node — `notTakenStroke`, dashed; the only dash on the canvas. Every inbound edge
1048
- * of it, because a node the run never entered was entered by no route, and it outranks a walk because reading
1049
- * a contradiction as declined cannot invent a traversal;
1050
- * 2. an edge whose both ends were REACHED — walked, in the stroke of the end drawn at `emphasis` (target first),
1051
- * else the target's stroke, so an edge touching the failed node reads as failed;
1052
- * 3. everything else — the neutral `edge` role, solid.
1053
- *
1054
- * A roles-only entry with an unusable stroke is not reached, so its edges fall to rule 3 — exactly the bytes the
1055
- * no-overlay render produces.
1056
- */
1057
- const drawnEdges = edges.filter((e) => drawn.has(e.from) && drawn.has(e.to));
1058
- const seenEdge = new Set();
1059
- let index = -1;
1060
- for (const edge of drawnEdges) {
1061
- const key = edgeLine(edge);
1062
- if (seenEdge.has(key))
1063
- continue;
1064
- seenEdge.add(key);
1065
- index += 1;
1066
- const target = readingOf(edge.to);
1067
- if (target.declined) {
1068
- push(` linkStyle ${index} stroke:${nodeColors.notTakenStroke},stroke-width:${STROKE_ROLES.hairline},stroke-dasharray:${STROKE_ROLES.dash}`);
1069
- continue;
344
+ for (const [className, ids] of members)
345
+ lines.push(`class ${ids.join(',')} ${className}`);
346
+ if (graph.edges.length > 0) {
347
+ lines.push('linkStyle default stroke-width:1px');
348
+ const declined = [];
349
+ const walked = [];
350
+ for (const [index, edge] of graph.edges.entries()) {
351
+ const from = byId.get(edge.from).state;
352
+ const to = byId.get(edge.to).state;
353
+ if (to === 'branchNotTaken')
354
+ declined.push(index);
355
+ else if ((from === 'ranOk' || from === 'ranFailed') && (to === 'ranOk' || to === 'ranFailed'))
356
+ walked.push(index);
1070
357
  }
1071
- const source = readingOf(edge.from);
1072
- const walked = target.reached && source.reached
1073
- ? ([target, source].find((end) => end.style?.width === STROKE_ROLES.emphasis) ?? target).style?.stroke
1074
- : undefined;
1075
- push(` linkStyle ${index} stroke:${walked ?? nodeColors.edge},stroke-width:${STROKE_ROLES.hairline}`);
1076
- }
1077
- /**
1078
- * The edge-label plate — a definition byte, not a host stylesheet (AD-23).
1079
- *
1080
- * MEASURED against Mermaid 12.0.0 in `story87RoleDrivenStyles.atdd.spec.ts`: `themeVariables.edgeLabelBackground`
1081
- * alone is NOT enough, because the shipped theme emits `.edgeLabel rect{opacity:0.5}` and `.labelBkg` as an
1082
- * `rgba(…, 0.5)` — the line still shows through the text. `themeCSS` is appended AFTER the theme's own rules, so
1083
- * it is what actually makes the plate opaque. `pointer-events:none` keeps the label non-interactive (stage 3 owns
1084
- * interaction, and a label is not a target).
1085
- *
1086
- * It rides an `%%{init}%%` directive placed after the `flowchart` line rather than YAML frontmatter, measured the
1087
- * same way: both apply, and only the directive keeps `^flowchart <dir>` as the definition's first bytes, which
1088
- * three host surfaces and every golden already depend on.
1089
- *
1090
- * Emitted only when a labelled edge was actually drawn: a chart with no label must not carry configuration for
1091
- * one.
1092
- */
1093
- if (labelledEdgeDrawn) {
1094
- const css = `.edgeLabel rect { opacity: 1; } ` +
1095
- `.labelBkg { background-color: ${nodeColors.edgeLabelBg}; } ` +
1096
- `.edgeLabel, .edgeLabel p, .edgeLabel span { background-color: ${nodeColors.edgeLabelBg}; color: ${nodeColors.edgeLabelText}; pointer-events: none; }`;
1097
- lines.splice(1, 0, `%%{init: {"themeVariables": {"edgeLabelBackground": "${nodeColors.edgeLabelBg}"}, "themeCSS": "${css}"} }%%`);
358
+ if (walked.length > 0)
359
+ lines.push(`linkStyle ${walked.join(',')} stroke-width:2px`);
360
+ if (declined.length > 0)
361
+ lines.push(`linkStyle ${declined.join(',')} stroke-dasharray:6 3`);
1098
362
  }
1099
- return { definition: withinBudget(lines.join('\n'), opts.maxTextSize), collapsedRanges };
363
+ return lines.join('\n');
1100
364
  }
1101
365
  /**
1102
- * The last gate before the definition leaves the generator.
366
+ * A stable fingerprint of a compact graph (53-bit, base 36).
1103
367
  *
1104
- * Applied to BOTH return paths, including the empty-flow one — not because a one-node chart can exceed a budget, but
1105
- * because a budget that some return paths honour is a budget a caller cannot rely on.
368
+ * A host compares it to skip re-posting an unchanged drawing, and a webview keys its rendered-SVG cache with it.
1106
369
  */
1107
- function withinBudget(definition, maxTextSize) {
1108
- if (maxTextSize === undefined)
1109
- return definition;
1110
- /**
1111
- * A budget that is not a positive finite number is REFUSED, not ignored.
1112
- *
1113
- * `NaN > n` is false, so a non-finite budget silently disabled the check — and `maxTextSize: NaN` is exactly what a
1114
- * host reaches by passing `Number(config.get('maxTextSize'))` on an unset setting. A budget that cannot be compared
1115
- * is a caller error, and treating it as "no budget" is how the silent truncation this guards comes back.
1116
- */
1117
- if (!Number.isFinite(maxTextSize) || maxTextSize <= 0) {
1118
- throw new RangeError(`maxTextSize must be a positive finite number of characters; received ${String(maxTextSize)}.`);
370
+ export function compactFlowHash(graph) {
371
+ const text = JSON.stringify(graph);
372
+ let h1 = 0xdeadbeef;
373
+ let h2 = 0x41c6ce57;
374
+ for (let index = 0; index < text.length; index += 1) {
375
+ const code = text.charCodeAt(index);
376
+ h1 = Math.imul(h1 ^ code, 2654435761);
377
+ h2 = Math.imul(h2 ^ code, 1597334677);
378
+ }
379
+ h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909);
380
+ h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909);
381
+ return (4294967296 * (2097151 & h2) + (h1 >>> 0)).toString(36);
382
+ }
383
+ /** The key a name spells: `Read Ledger` → `READ_LEDGER`. */
384
+ const keyOfName = (name) => name.toUpperCase().replace(/ /g, '_');
385
+ /** The ways a node's name can be spelled from its key, by wire code: as is, Title Case, Sentence case. */
386
+ const NAME_FROM_KEY = [
387
+ (key) => key,
388
+ (key) => key.toLowerCase().split('_').map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join(' '),
389
+ (key) => {
390
+ const words = key.toLowerCase().replace(/_/g, ' ');
391
+ return words.charAt(0).toUpperCase() + words.slice(1);
392
+ },
393
+ ];
394
+ /** A table that stores each distinct value once and answers its index. */
395
+ function table(identity) {
396
+ const rows = [];
397
+ const seen = new Map();
398
+ return {
399
+ rows,
400
+ index: (value) => {
401
+ const id = identity(value);
402
+ let at = seen.get(id);
403
+ if (at === undefined) {
404
+ at = rows.length;
405
+ rows.push(value);
406
+ seen.set(id, at);
407
+ }
408
+ return at;
409
+ },
410
+ };
411
+ }
412
+ export function encodeCompactFlow(graph) {
413
+ const strings = table((value) => value);
414
+ const kinds = table((value) => value.join());
415
+ const links = table((value) => value.join());
416
+ const position = new Map(graph.nodes.map((node, index) => [node.id, index]));
417
+ // Edges first: their few labels and kinds then hold the smallest indices.
418
+ const edges = graph.edges.flatMap((edge) => [
419
+ position.get(edge.from),
420
+ position.get(edge.to),
421
+ edge.kind === 'next' && edge.label === undefined
422
+ ? 0
423
+ : 1 + links.index([edge.label === undefined ? -1 : strings.index(edge.label), strings.index(edge.kind)]),
424
+ ]);
425
+ const groups = graph.groups.map((group) => [group.title === undefined ? -1 : strings.index(group.title), group.nodes.length]);
426
+ const runs = [];
427
+ const nodes = graph.nodes.flatMap((node, index) => {
428
+ const spelled = node.key === undefined ? -1 : NAME_FROM_KEY.findIndex((spell) => spell(node.key) === node.name);
429
+ const kind = kinds.index([
430
+ node.type === undefined ? -1 : strings.index(node.type),
431
+ strings.index(node.family),
432
+ strings.index(node.shape),
433
+ ]);
434
+ if (node.state !== undefined || node.repeat !== undefined) {
435
+ runs.push([index, node.state === undefined ? -1 : strings.index(node.state), ...(node.repeat === undefined ? [] : [node.repeat])]);
436
+ }
437
+ return [node.key ?? 0, spelled >= 0 ? spelled : NAME_FROM_KEY.length + strings.index(node.name), kind];
438
+ });
439
+ return {
440
+ t: graph.title,
441
+ s: strings.rows,
442
+ k: kinds.rows,
443
+ n: nodes,
444
+ l: links.rows,
445
+ e: edges,
446
+ g: groups,
447
+ ...(runs.length === 0 ? {} : { r: runs }),
448
+ ...(graph.empty === undefined ? {} : { x: graph.empty }),
449
+ };
450
+ }
451
+ /** The inverse of `encodeCompactFlow`: `decodeCompactFlow(encodeCompactFlow(g))` deep-equals `g`. */
452
+ export function decodeCompactFlow(wire) {
453
+ const text = (index) => index !== undefined && index >= 0 ? wire.s[index] : undefined;
454
+ const runOf = new Map((wire.r ?? []).map(([node, state, repeat]) => [node, { state, repeat }]));
455
+ const nodes = [];
456
+ for (let at = 0; at < wire.n.length; at += 3) {
457
+ const index = at / 3;
458
+ const key = typeof wire.n[at] === 'string' ? wire.n[at] : undefined;
459
+ const name = wire.n[at + 1];
460
+ const [type, family, shape] = wire.k[wire.n[at + 2]];
461
+ const typeText = text(type);
462
+ const run = runOf.get(index);
463
+ const state = text(run?.state);
464
+ nodes.push({
465
+ id: compactNodeId(index),
466
+ ...(key === undefined ? {} : { key }),
467
+ name: name < NAME_FROM_KEY.length ? NAME_FROM_KEY[name](key ?? '') : text(name - NAME_FROM_KEY.length),
468
+ ...(typeText === undefined ? {} : { type: typeText }),
469
+ family: text(family),
470
+ shape: text(shape),
471
+ ...(state === undefined ? {} : { state }),
472
+ ...(run?.repeat === undefined ? {} : { repeat: run.repeat }),
473
+ });
474
+ }
475
+ let next = 0;
476
+ const groups = wire.g.map(([title, count], index) => {
477
+ const members = Array.from({ length: count }, (_unused, offset) => compactNodeId(next + offset));
478
+ next += count;
479
+ const titleText = text(title);
480
+ return { id: `g${index}`, ...(titleText === undefined ? {} : { title: titleText }), nodes: members };
481
+ });
482
+ const edges = [];
483
+ for (let at = 0; at < wire.e.length; at += 3) {
484
+ const code = wire.e[at + 2];
485
+ const [label, kind] = code === 0 ? [-1, -1] : wire.l[code - 1];
486
+ const labelText = text(label);
487
+ edges.push({
488
+ from: compactNodeId(wire.e[at]),
489
+ to: compactNodeId(wire.e[at + 1]),
490
+ ...(labelText === undefined ? {} : { label: labelText }),
491
+ kind: text(kind) ?? 'next',
492
+ });
493
+ }
494
+ return { title: wire.t, groups, nodes, edges, ...(wire.x === undefined ? {} : { empty: wire.x }) };
495
+ }
496
+ /** Parse the page's serialized wire form; `undefined` when it is not a compact flow. */
497
+ export function parseCompactFlow(text) {
498
+ let wire;
499
+ try {
500
+ wire = JSON.parse(text);
501
+ }
502
+ catch {
503
+ return undefined;
1119
504
  }
1120
- const report = measureDefinitionBudget(definition, maxTextSize);
1121
- if (report.overBudget)
1122
- throw new DefinitionOverBudgetError(report);
1123
- return definition;
505
+ if (!Array.isArray(wire?.s) || !Array.isArray(wire.n) || !Array.isArray(wire.e) || !Array.isArray(wire.g))
506
+ return undefined;
507
+ return decodeCompactFlow(wire);
508
+ }
509
+ /** The diagram, in one direction. */
510
+ export function renderMermaid(groups, edges, opts = {}) {
511
+ return renderMermaidWithOccurrences(groups, edges, opts).definition;
512
+ }
513
+ /** `renderMermaid`, for a caller that also needs the occurrence facts the drawing carries. */
514
+ export function renderMermaidWithOccurrences(groups, edges, opts = {}) {
515
+ const compact = compactFlowGraph(groups, edges, opts);
516
+ return {
517
+ definition: compactFlowMermaid(compact.graph, opts.direction),
518
+ collapsedRanges: compact.collapsedRanges,
519
+ };
1124
520
  }
1125
521
  /**
1126
- * A titled section with a fenced diagram, and nothing else (rule 3).
522
+ * A titled section with a fenced diagram, and nothing else.
1127
523
  *
1128
- * What the composition report embeds and what an MCP resource serves — the same string, because two surfaces
1129
- * describing one flow disagreeing is the defect this epic exists to remove.
524
+ * What the composition report embeds and what an MCP resource serves — the same string.
1130
525
  */
1131
526
  export function renderMarkdown(title, groups, edges, opts = {}) {
527
+ // Collapsed, not HTML-escaped: a newline would end the heading, and an entity would read as noise in plain text.
1132
528
  return [
1133
- /**
1134
- * The title is COLLAPSED, not HTML-escaped (Story 4.3 review, MEDIUM-4).
1135
- *
1136
- * A newline would end the heading and turn the diagram into body text, so collapsing is required. Entity-escaping
1137
- * is not: §5 says this string is what an MCP resource and the composition report both serve, and served as text
1138
- * `Orders &amp; Returns` reads as visible noise. Only an HTML consumer would decode it, and a heading is not a
1139
- * mermaid label.
1140
- */
1141
529
  `### ${title.replace(/[\r\n]+/g, ' ').trim()}`,
1142
530
  '',
1143
531
  '```mermaid',