dsh-knowhub 0.9.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (195) hide show
  1. package/README.md +11 -2
  2. package/cordis.patch.yml +10 -0
  3. package/lib/host/model.js +1 -1
  4. package/lib/host/pipeline/incremental.js +3 -3
  5. package/lib/host/pipeline/initialize.js +144 -27
  6. package/lib/host/pipeline/model-output.js +2 -2
  7. package/package.json +3 -2
  8. package/skills/archify/LICENSE +202 -0
  9. package/skills/archify/LICENSE-MIT +22 -0
  10. package/skills/archify/NOTICE +26 -0
  11. package/skills/archify/SKILL.md +137 -0
  12. package/skills/archify/THIRD_PARTY_NOTICES.md +78 -0
  13. package/skills/archify/assets/JetBrainsMono-OFL.txt +93 -0
  14. package/skills/archify/assets/template.html +14768 -0
  15. package/skills/archify/bin/archify.mjs +7289 -0
  16. package/skills/archify/bin/delivery-update.mjs +92 -0
  17. package/skills/archify/bin/finalize.mjs +1055 -0
  18. package/skills/archify/bin/open-artifact.mjs +146 -0
  19. package/skills/archify/bin/preview.mjs +970 -0
  20. package/skills/archify/bin/recover-output.mjs +41 -0
  21. package/skills/archify/bin/visual-check.mjs +2780 -0
  22. package/skills/archify/brand-marks/README.md +31 -0
  23. package/skills/archify/brand-marks/catalog.json +131 -0
  24. package/skills/archify/delta/architecture-delta.mjs +1318 -0
  25. package/skills/archify/examples/agent-run.lifecycle.json +55 -0
  26. package/skills/archify/examples/agent-tool-call.workflow.json +74 -0
  27. package/skills/archify/examples/archify-dev-activity.timeline.json +176 -0
  28. package/skills/archify/examples/archify-repository.tree.json +197 -0
  29. package/skills/archify/examples/async-job-roundtrip.sequence.json +56 -0
  30. package/skills/archify/examples/brand-aware-delivery.architecture.json +34 -0
  31. package/skills/archify/examples/cache-miss-request.sequence.json +77 -0
  32. package/skills/archify/examples/checkout-platform.base.architecture.json +32 -0
  33. package/skills/archify/examples/checkout-platform.head.architecture.json +32 -0
  34. package/skills/archify/examples/checkout-request.waterfall.json +20 -0
  35. package/skills/archify/examples/delivery.explainer.json +155 -0
  36. package/skills/archify/examples/deployment-release.lifecycle.json +72 -0
  37. package/skills/archify/examples/event-stream.dataflow.json +52 -0
  38. package/skills/archify/examples/example-rebuild.waterfall.json +212 -0
  39. package/skills/archify/examples/holiday-planning.workflow.json +143 -0
  40. package/skills/archify/examples/incident-response.workflow.json +59 -0
  41. package/skills/archify/examples/locales/de.json +463 -0
  42. package/skills/archify/examples/locales/fr.json +463 -0
  43. package/skills/archify/examples/locales/fr.partial.json +22 -0
  44. package/skills/archify/examples/locales/it.json +463 -0
  45. package/skills/archify/examples/locales/ja.json +463 -0
  46. package/skills/archify/examples/locales/pt.json +463 -0
  47. package/skills/archify/examples/locales/ru.json +463 -0
  48. package/skills/archify/examples/orders.erd.json +337 -0
  49. package/skills/archify/examples/payment-incident.timeline.json +24 -0
  50. package/skills/archify/examples/payment-platform.tree.json +22 -0
  51. package/skills/archify/examples/payment-processors.class.json +177 -0
  52. package/skills/archify/examples/payments.class.json +85 -0
  53. package/skills/archify/examples/product-analytics.dataflow.json +71 -0
  54. package/skills/archify/examples/production-deployment.architecture.json +51 -0
  55. package/skills/archify/examples/release-delivery.workflow.json +57 -0
  56. package/skills/archify/examples/subscription-billing.erd.json +548 -0
  57. package/skills/archify/examples/web-app.architecture.json +41 -0
  58. package/skills/archify/explainer/browser-check.mjs +208 -0
  59. package/skills/archify/explainer/charts.mjs +70 -0
  60. package/skills/archify/explainer/check.mjs +167 -0
  61. package/skills/archify/explainer/diagrams.mjs +517 -0
  62. package/skills/archify/explainer/diff.mjs +124 -0
  63. package/skills/archify/explainer/inline.mjs +155 -0
  64. package/skills/archify/explainer/layout.mjs +35 -0
  65. package/skills/archify/explainer/page-bundle.mjs +16 -0
  66. package/skills/archify/explainer/parse.mjs +300 -0
  67. package/skills/archify/explainer/patch.mjs +117 -0
  68. package/skills/archify/explainer/prepare.mjs +131 -0
  69. package/skills/archify/explainer/reader.css +472 -0
  70. package/skills/archify/explainer/reader.js +666 -0
  71. package/skills/archify/explainer/render.mjs +224 -0
  72. package/skills/archify/explainer/revision.mjs +20 -0
  73. package/skills/archify/explainer/sources.mjs +318 -0
  74. package/skills/archify/explainer/validate.mjs +138 -0
  75. package/skills/archify/explainer/vendor/pele/LICENSE +40 -0
  76. package/skills/archify/explainer/vendor/pele/README.md +5 -0
  77. package/skills/archify/explainer/vendor/pele/chunks/3DKXF73K.js +4 -0
  78. package/skills/archify/explainer/vendor/pele/chunks/42UTT654.js +3 -0
  79. package/skills/archify/explainer/vendor/pele/chunks/DKTLEL5E.js +1 -0
  80. package/skills/archify/explainer/vendor/pele/chunks/EEIX5ZYH.js +2 -0
  81. package/skills/archify/explainer/vendor/pele/chunks/EJ5KOXYD.js +25 -0
  82. package/skills/archify/explainer/vendor/pele/chunks/GUJTEFZK.js +1 -0
  83. package/skills/archify/explainer/vendor/pele/chunks/IZOKQ2GT.js +4 -0
  84. package/skills/archify/explainer/vendor/pele/chunks/L4LY553T.js +3 -0
  85. package/skills/archify/explainer/vendor/pele/chunks/MSKIJHB2.js +2 -0
  86. package/skills/archify/explainer/vendor/pele/chunks/RVARI57O.js +1 -0
  87. package/skills/archify/explainer/vendor/pele/chunks/UKSIWDDN.js +2 -0
  88. package/skills/archify/explainer/vendor/pele/chunks/UPTXNAXN.js +2 -0
  89. package/skills/archify/explainer/vendor/pele/chunks/XTDUIGUI.js +9 -0
  90. package/skills/archify/explainer/vendor/pele/chunks/YAYTP6L5.js +9 -0
  91. package/skills/archify/explainer/vendor/pele/core.js +1 -0
  92. package/skills/archify/explainer/vendor/pele/diagrams/pie.js +1 -0
  93. package/skills/archify/explainer/vendor/pele/diagrams/quadrantChart.js +1 -0
  94. package/skills/archify/explainer/vendor/pele/diagrams/radar.js +1 -0
  95. package/skills/archify/explainer/vendor/pele/diagrams/sankey.js +1 -0
  96. package/skills/archify/explainer/vendor/pele/diagrams/venn.js +1 -0
  97. package/skills/archify/explainer/vendor/pele/diagrams/xychart.js +1 -0
  98. package/skills/archify/explainer/writing.mjs +139 -0
  99. package/skills/archify/locales/README.md +40 -0
  100. package/skills/archify/locales/ar.json +488 -0
  101. package/skills/archify/locales/en.json +488 -0
  102. package/skills/archify/locales/es.json +488 -0
  103. package/skills/archify/locales/ko.json +488 -0
  104. package/skills/archify/locales/manifest.json +10 -0
  105. package/skills/archify/locales/zh-CN.json +488 -0
  106. package/skills/archify/locales/zh-TW.json +488 -0
  107. package/skills/archify/migrations/workflow-v2.mjs +280 -0
  108. package/skills/archify/package.json +17 -0
  109. package/skills/archify/recipes/scenarios.mjs +455 -0
  110. package/skills/archify/references/architecture-layout-repair.md +18 -0
  111. package/skills/archify/references/authoring-contract.md +645 -0
  112. package/skills/archify/references/authoring-defaults.md +57 -0
  113. package/skills/archify/references/brand-marks.md +119 -0
  114. package/skills/archify/references/delivery-contract.md +665 -0
  115. package/skills/archify/references/explainer-authoring.md +274 -0
  116. package/skills/archify/references/repository-authoring.md +126 -0
  117. package/skills/archify/references/update-awareness.md +13 -0
  118. package/skills/archify/references/viewer-runtime.md +52 -0
  119. package/skills/archify/renderers/architecture/grid.mjs +62 -0
  120. package/skills/archify/renderers/architecture/labels.mjs +2 -0
  121. package/skills/archify/renderers/architecture/render-architecture.mjs +1204 -0
  122. package/skills/archify/renderers/architecture/routing.mjs +1469 -0
  123. package/skills/archify/renderers/class/README.md +130 -0
  124. package/skills/archify/renderers/class/render-class.mjs +1095 -0
  125. package/skills/archify/renderers/dataflow/README.md +176 -0
  126. package/skills/archify/renderers/dataflow/dataflow-compiler.mjs +1605 -0
  127. package/skills/archify/renderers/dataflow/joint-label-routing.mjs +165 -0
  128. package/skills/archify/renderers/dataflow/label-capacity.mjs +81 -0
  129. package/skills/archify/renderers/dataflow/render-dataflow.mjs +23 -0
  130. package/skills/archify/renderers/erd/README.md +246 -0
  131. package/skills/archify/renderers/erd/grid.mjs +150 -0
  132. package/skills/archify/renderers/erd/render-erd.mjs +1588 -0
  133. package/skills/archify/renderers/explainer/render-explainer.mjs +42 -0
  134. package/skills/archify/renderers/lifecycle/README.md +104 -0
  135. package/skills/archify/renderers/lifecycle/render-lifecycle.mjs +1134 -0
  136. package/skills/archify/renderers/sequence/README.md +146 -0
  137. package/skills/archify/renderers/sequence/render-sequence.mjs +1105 -0
  138. package/skills/archify/renderers/shared/atomic-output.mjs +1982 -0
  139. package/skills/archify/renderers/shared/automatic-labels.mjs +290 -0
  140. package/skills/archify/renderers/shared/brand-marks.mjs +706 -0
  141. package/skills/archify/renderers/shared/cli.mjs +430 -0
  142. package/skills/archify/renderers/shared/desktop-readability.mjs +132 -0
  143. package/skills/archify/renderers/shared/diagnostics.mjs +0 -0
  144. package/skills/archify/renderers/shared/engineering-profiles.mjs +157 -0
  145. package/skills/archify/renderers/shared/generated-brand-marks.mjs +2003 -0
  146. package/skills/archify/renderers/shared/generated-validators.mjs +279 -0
  147. package/skills/archify/renderers/shared/geometry.mjs +2073 -0
  148. package/skills/archify/renderers/shared/i18n.mjs +328 -0
  149. package/skills/archify/renderers/shared/layout-report.mjs +57 -0
  150. package/skills/archify/renderers/shared/legend.mjs +227 -0
  151. package/skills/archify/renderers/shared/node-card-style.mjs +32 -0
  152. package/skills/archify/renderers/shared/output-path.mjs +440 -0
  153. package/skills/archify/renderers/shared/owned-temp-directory.mjs +82 -0
  154. package/skills/archify/renderers/shared/path-semantics.mjs +1017 -0
  155. package/skills/archify/renderers/shared/portable-path.mjs +408 -0
  156. package/skills/archify/renderers/shared/repository-evidence.mjs +394 -0
  157. package/skills/archify/renderers/shared/repository-location.mjs +71 -0
  158. package/skills/archify/renderers/shared/route-quality.mjs +670 -0
  159. package/skills/archify/renderers/shared/sidecar-path.mjs +38 -0
  160. package/skills/archify/renderers/shared/software-notices.mjs +6 -0
  161. package/skills/archify/renderers/shared/spatial-grid.mjs +66 -0
  162. package/skills/archify/renderers/shared/svg-path-analysis.mjs +135 -0
  163. package/skills/archify/renderers/shared/text-fit.mjs +105 -0
  164. package/skills/archify/renderers/shared/utils.mjs +260 -0
  165. package/skills/archify/renderers/shared/validator.mjs +83 -0
  166. package/skills/archify/renderers/timeline/README.md +76 -0
  167. package/skills/archify/renderers/timeline/render-timeline.mjs +576 -0
  168. package/skills/archify/renderers/tree/README.md +96 -0
  169. package/skills/archify/renderers/tree/render-tree.mjs +573 -0
  170. package/skills/archify/renderers/waterfall/README.md +73 -0
  171. package/skills/archify/renderers/waterfall/render-waterfall.mjs +413 -0
  172. package/skills/archify/renderers/workflow/README.md +337 -0
  173. package/skills/archify/renderers/workflow/render-workflow.mjs +47 -0
  174. package/skills/archify/renderers/workflow/workflow-compiler.mjs +5920 -0
  175. package/skills/archify/renderers/workflow/workflow-migration-geometry.mjs +161 -0
  176. package/skills/archify/renderers/workflow/workflow-placement-repair.mjs +94 -0
  177. package/skills/archify/schemas/README.md +258 -0
  178. package/skills/archify/schemas/architecture.schema.json +152 -0
  179. package/skills/archify/schemas/class.schema.json +345 -0
  180. package/skills/archify/schemas/common.schema.json +181 -0
  181. package/skills/archify/schemas/dataflow.schema.json +254 -0
  182. package/skills/archify/schemas/erd.schema.json +381 -0
  183. package/skills/archify/schemas/explainer.schema.json +927 -0
  184. package/skills/archify/schemas/lifecycle.schema.json +179 -0
  185. package/skills/archify/schemas/sequence.schema.json +234 -0
  186. package/skills/archify/schemas/timeline.schema.json +160 -0
  187. package/skills/archify/schemas/tree.schema.json +141 -0
  188. package/skills/archify/schemas/waterfall.schema.json +147 -0
  189. package/skills/archify/schemas/workflow.schema.json +439 -0
  190. package/skills/archify/scripts/check-render-output.mjs +1469 -0
  191. package/skills/archify/scripts/check-update.mjs +1717 -0
  192. package/skills/archify/scripts/delivery-update-child.mjs +26 -0
  193. package/skills/archify/scripts/render-examples.mjs +36 -0
  194. package/skills/archify/scripts/update-contract.mjs +187 -0
  195. package/skills/archify/skill-release.json +10 -0
@@ -0,0 +1,290 @@
1
+ import { normalizeRoutePoints, rectsOverlap, segmentRectClearanceWithin } from './geometry.mjs';
2
+ import { createSpatialGrid } from './spatial-grid.mjs';
3
+
4
+ // A bounded fallback for an unpinned label whose usual position collides.
5
+ // It never routes an edge, moves a node, expands the canvas, or rewrites input.
6
+ export function placeAutomaticLabels({
7
+ labels, routes, components, titles, viewBox, placementBottom = viewBox[1], fallbackRing = true, keepFallbackNearRoute = false,
8
+ gridSweep = false, maxOwnRouteDistance = Infinity,
9
+ }) {
10
+ const placed = [...labels];
11
+ const obstacles = [...components, ...titles];
12
+ const segments = routes.flatMap(({ relationIndex, points }) => {
13
+ const normalized = normalizeRoutePoints(points);
14
+ return normalized.slice(1).map((end, index) => ({ relationIndex, start: normalized[index], end }));
15
+ });
16
+ const inside = rect => (
17
+ rect.x >= 0 && rect.y >= 0
18
+ && rect.x + rect.width <= viewBox[0] && rect.y + rect.height <= viewBox[1]
19
+ );
20
+ // The mask test asked every segment about every candidate position. Segments
21
+ // go into a uniform grid once, and a candidate only asks the cells it covers.
22
+ const SEGMENT_CELL = 120;
23
+ const segmentGrid = createSpatialGrid(SEGMENT_CELL);
24
+ for (const segment of segments) {
25
+ const [sx, sy] = segment.start;
26
+ const [ex, ey] = segment.end;
27
+ segmentGrid.insert({
28
+ minX: Math.min(sx, ex), maxX: Math.max(sx, ex),
29
+ minY: Math.min(sy, ey), maxY: Math.max(sy, ey),
30
+ }, segment);
31
+ }
32
+ const segmentsNear = (rect, margin) => segmentGrid.query({
33
+ minX: rect.x - margin, maxX: rect.x + rect.width + margin,
34
+ minY: rect.y - margin, maxY: rect.y + rect.height + margin,
35
+ });
36
+ const masksRoute = rect => {
37
+ for (const segment of segmentsNear(rect, 4)) {
38
+ if (segment.relationIndex === rect.relationIndex) continue;
39
+ if (segmentRectClearanceWithin(segment, rect, 4) + 0.0001 < 4) return true;
40
+ }
41
+ return false;
42
+ };
43
+ const overlapsLabel = (rect, index, gap = 0) => placed.some((other, otherIndex) => (
44
+ otherIndex !== index && rectsOverlap(rect, other, gap)
45
+ ));
46
+ const clear = (rect, index) => (
47
+ inside(rect) && rect.y + rect.height <= placementBottom
48
+ && !obstacles.some(obstacle => rectsOverlap(rect, obstacle, 2))
49
+ && !overlapsLabel(rect, index, 2) && !masksRoute(rect)
50
+ && (!Number.isFinite(maxOwnRouteDistance) || segments.some(segment =>
51
+ segment.relationIndex === rect.relationIndex
52
+ && segmentRectClearanceWithin(segment, rect, maxOwnRouteDistance) <= maxOwnRouteDistance))
53
+ );
54
+ const rectAt = (label, lx, ly) => ({
55
+ ...label, lx, ly, x: lx - label.width / 2, y: ly - 10,
56
+ });
57
+ // Fixed fractions can miss a narrow free slot. Try the ends of the free
58
+ // intervals beside an orthogonal segment, using obstacle edges rather
59
+ // than a finer sampling grid. The ordinary clearance test still decides
60
+ // whether a candidate is usable; this search never changes geometry.
61
+ const slidingCandidates = (label, index, { start: a, end: b }) => {
62
+ const horizontal = Math.abs(a[1] - b[1]) < 0.0001;
63
+ if (!horizontal && Math.abs(a[0] - b[0]) >= 0.0001) return [];
64
+ const axis = horizontal ? 'x' : 'y';
65
+ const size = horizontal ? 'width' : 'height';
66
+ const across = horizontal ? 'y' : 'x';
67
+ const crossSize = horizontal ? 'height' : 'width';
68
+ const low = Math.min(a[horizontal ? 0 : 1], b[horizontal ? 0 : 1]) + 8;
69
+ const high = Math.max(a[horizontal ? 0 : 1], b[horizontal ? 0 : 1]) - label[size] - 8;
70
+ if (high < low) return [];
71
+ const offsets = horizontal
72
+ ? [a[1] - 20, a[1] + 10, a[1] - 28, a[1] + 18]
73
+ : [a[0] - label.width - 6, a[0] + 6, a[0] - label.width - 14, a[0] + 14];
74
+ return offsets.flatMap(offset => {
75
+ const band = { ...label, [axis]: low, [size]: high - low + label[size], [across]: offset };
76
+ const blockers = [
77
+ ...obstacles.map(rect => ({ rect, gap: 2 })),
78
+ ...placed.filter((_, otherIndex) => otherIndex !== index).map(rect => ({ rect, gap: 2 })),
79
+ ...segmentsNear(band, 4).filter(segment => segment.relationIndex !== label.relationIndex).map(segment => ({
80
+ rect: {
81
+ x: Math.min(segment.start[0], segment.end[0]), y: Math.min(segment.start[1], segment.end[1]),
82
+ width: Math.abs(segment.end[0] - segment.start[0]), height: Math.abs(segment.end[1] - segment.start[1]),
83
+ }, gap: 4,
84
+ })),
85
+ ];
86
+ const positions = [Math.max(low, Math.min(high, label[axis])), low, high];
87
+ for (const { rect, gap } of blockers) {
88
+ if (offset + label[crossSize] + gap <= rect[across]
89
+ || rect[across] + rect[crossSize] + gap <= offset) continue;
90
+ positions.push(rect[axis] - gap - label[size], rect[axis] + rect[size] + gap);
91
+ }
92
+ return [...new Set(positions)].filter(position => position >= low && position <= high)
93
+ .sort((left, right) => Math.abs(left - label[axis]) - Math.abs(right - label[axis]))
94
+ .map(position => {
95
+ const rect = { ...label, [axis]: position, [across]: offset };
96
+ return rectAt(label, rect.x + label.width / 2, rect.y + 10);
97
+ });
98
+ });
99
+ };
100
+ // An opted-in grid layout runs parallel lines through shared gaps. Rank
101
+ // every position along all of the label's own segments: beside the line
102
+ // first, then centred on its own line (the plate interrupts only that
103
+ // line), then stepping outward past neighbouring parallels.
104
+ const gridCandidates = (label) => {
105
+ const fractions = [0.5, 0.25, 0.75, 0.375, 0.625, 0.125, 0.875];
106
+ const ranked = [];
107
+ // A close parallel of another relationship on one side (a reciprocal
108
+ // pair) makes a label on that side read as the neighbour's: prefer the
109
+ // far side.
110
+ const parallelOnLowSide = (a, b, axis) => {
111
+ const across = 1 - axis;
112
+ const [low, high] = [Math.min(a[axis], b[axis]), Math.max(a[axis], b[axis])];
113
+ let nearest = null;
114
+ for (const other of segments) {
115
+ if (other.relationIndex === label.relationIndex) continue;
116
+ if (Math.abs(other.start[across] - other.end[across]) > 0.0001) continue;
117
+ const distance = other.start[across] - a[across];
118
+ if (Math.abs(distance) < 0.0001 || Math.abs(distance) > 36) continue;
119
+ const overlap = Math.min(high, Math.max(other.start[axis], other.end[axis]))
120
+ - Math.max(low, Math.min(other.start[axis], other.end[axis]));
121
+ if (overlap <= 0) continue;
122
+ if (nearest === null || Math.abs(distance) < Math.abs(nearest)) nearest = distance;
123
+ }
124
+ return nearest !== null && nearest < 0;
125
+ };
126
+ for (const { start: a, end: b } of segments.filter(segment => segment.relationIndex === label.relationIndex)) {
127
+ if (Math.abs(a[1] - b[1]) < 0.0001 && Math.abs(a[0] - b[0]) >= label.width + 16) {
128
+ const [above, below] = parallelOnLowSide(a, b, 0) ? [1, 0] : [0, 1];
129
+ for (const fraction of fractions) {
130
+ const x = a[0] + (b[0] - a[0]) * fraction;
131
+ if (Math.min(Math.abs(x - a[0]), Math.abs(x - b[0])) < label.width / 2 + 6) continue;
132
+ ranked.push([above, x, a[1] - 10], [below, x, a[1] + 20], [2, x, a[1] + 3], [3, x, a[1] - 18], [3, x, a[1] + 28]);
133
+ }
134
+ } else if (Math.abs(a[0] - b[0]) < 0.0001 && Math.abs(a[1] - b[1]) >= label.height + 16) {
135
+ const leftFirst = !parallelOnLowSide(a, b, 1);
136
+ for (const fraction of fractions) {
137
+ const y = a[1] + (b[1] - a[1]) * fraction;
138
+ if (Math.min(Math.abs(y - a[1]), Math.abs(y - b[1])) < label.height / 2 + 6) continue;
139
+ ranked.push([2, a[0], y + 3]);
140
+ [6, 14, 22, 30, 38, 46, 54].forEach((offset, step) => {
141
+ const tier = step === 0 ? 0 : step === 1 ? 1 : 2 + step;
142
+ const left = [tier + (leftFirst ? 0 : 0.5), a[0] - label.width / 2 - offset, y + 3];
143
+ const right = [tier + (leftFirst ? 0.5 : 0), a[0] + label.width / 2 + offset, y + 3];
144
+ ranked.push(left, right);
145
+ });
146
+ }
147
+ }
148
+ }
149
+ return ranked.map((entry, order) => [...entry, order])
150
+ .sort((left, right) => left[0] - right[0] || left[3] - right[3])
151
+ .map(([, lx, ly]) => [lx, ly]);
152
+ };
153
+
154
+ const pinned = label => ['labelAt', 'labelDx', 'labelDy', 'labelSegment']
155
+ .some(key => label.relation[key] !== undefined);
156
+ const needsPlacement = (label, index) => !inside(label)
157
+ || components.some(component => rectsOverlap(label, component, -2))
158
+ || titles.some(title => rectsOverlap(label, title))
159
+ || overlapsLabel(label, index) || masksRoute(label);
160
+ const placeLabel = (index, rerank) => {
161
+ const label = placed[index];
162
+ // Match actual defect thresholds before searching; a valid placement is
163
+ // not a reason to restyle the diagram. New placements leave extra space.
164
+ // The grid ranking already starts from the preferred position, so it
165
+ // also re-ranks labels whose default spot is merely valid.
166
+ if (!rerank && !needsPlacement(label, index)) return;
167
+ if (rerank) {
168
+ // Callers measure the plate one pixel higher than rectAt; test with a
169
+ // pixel of slack so the chosen position also passes their clearance.
170
+ const replacement = gridCandidates(label).map(([lx, ly]) => rectAt(label, lx, ly))
171
+ .find(rect => clear({ ...rect, x: rect.x - 1, y: rect.y - 1, width: rect.width + 2, height: rect.height + 2 }, index));
172
+ if (replacement) {
173
+ placed[index] = replacement;
174
+ return;
175
+ }
176
+ }
177
+ for (const segment of segments.filter(segment => segment.relationIndex === label.relationIndex)) {
178
+ const [a, b] = [segment.start, segment.end];
179
+ let candidates = [];
180
+ // These candidates sit beside the line. They need the same 2px node
181
+ // clearance checked by clear(), not 8px reserved at each endpoint.
182
+ // Extra endpoint padding can hide a valid outer slot for a reciprocal
183
+ // pair and make the planner send its other arrow around both nodes.
184
+ if (Math.abs(a[1] - b[1]) < 0.0001 && Math.abs(a[0] - b[0]) >= label.width + 4) {
185
+ candidates = [0.5, 0.25, 0.75, 0.125, 0.875].flatMap(fraction => {
186
+ const x = a[0] + (b[0] - a[0]) * fraction;
187
+ if (Math.min(Math.abs(x - a[0]), Math.abs(x - b[0])) < 8) return [];
188
+ return [
189
+ [x, a[1] - 10],
190
+ [x, a[1] + 20],
191
+ [x, a[1] - 18],
192
+ [x, a[1] + 28],
193
+ ];
194
+ });
195
+ } else if (Math.abs(a[0] - b[0]) < 0.0001 && Math.abs(a[1] - b[1]) >= label.height + 16) {
196
+ candidates = [0.5, 0.25, 0.75, 0.125, 0.875].flatMap(fraction => {
197
+ const y = a[1] + (b[1] - a[1]) * fraction;
198
+ if (Math.min(Math.abs(y - a[1]), Math.abs(y - b[1])) < 8) return [];
199
+ return [
200
+ [a[0] - label.width / 2 - 6, y + 3],
201
+ [a[0] + label.width / 2 + 6, y + 3],
202
+ [a[0] - label.width / 2 - 14, y + 3],
203
+ [a[0] + label.width / 2 + 14, y + 3],
204
+ ];
205
+ });
206
+ }
207
+ const replacement = candidates.map(([lx, ly]) => rectAt(label, lx, ly))
208
+ .find(rect => clear(rect, index));
209
+ if (replacement) {
210
+ placed[index] = replacement;
211
+ break;
212
+ }
213
+ }
214
+ if (placed[index] !== label) return;
215
+ const sliding = segments.filter(segment => segment.relationIndex === label.relationIndex)
216
+ .flatMap(segment => slidingCandidates(label, index, segment)).find(rect => clear(rect, index));
217
+ if (sliding) {
218
+ placed[index] = sliding;
219
+ return;
220
+ }
221
+ if (!fallbackRing) return;
222
+
223
+ // Dense but valid topologies can leave every point directly beside the
224
+ // relationship occupied by another route. Search a small deterministic
225
+ // ring around the current anchor and the relationship's segment centres.
226
+ // A collision-free island above a node is not a readable edge label.
227
+ // Architecture opts into keeping the mask within two label heights of
228
+ // its own route; shared callers retain their existing policy. If no nearby
229
+ // slot fits, retain the collision so validation can request more space.
230
+ const ownSegments = segments.filter(segment => segment.relationIndex === label.relationIndex);
231
+ const baseAnchors = [
232
+ [label.lx, label.ly],
233
+ ...ownSegments.map(segment => [
234
+ (segment.start[0] + segment.end[0]) / 2,
235
+ (segment.start[1] + segment.end[1]) / 2,
236
+ ]),
237
+ ];
238
+ const horizontalStep = label.width / 2 + 12;
239
+ const ringOffsets = [
240
+ [0, -28], [0, 38],
241
+ [-horizontalStep, -28], [horizontalStep, -28],
242
+ [-horizontalStep, 38], [horizontalStep, 38],
243
+ [-(label.width + 20), -52], [label.width + 20, -52],
244
+ [-(label.width + 20), 62], [label.width + 20, 62],
245
+ [-(label.width + 20), -76], [label.width + 20, -76],
246
+ [-(label.width + 20), 86], [label.width + 20, 86],
247
+ ];
248
+ const fallback = baseAnchors.flatMap(([baseX, baseY]) => (
249
+ ringOffsets.map(([dx, dy]) => rectAt(label, baseX + dx, baseY + dy))
250
+ )).find(rect => clear(rect, index) && (!keepFallbackNearRoute || ownSegments.some(segment => (
251
+ segmentRectClearanceWithin(segment, rect, label.height * 2) <= label.height * 2
252
+ ))));
253
+ if (fallback) placed[index] = fallback;
254
+ };
255
+ let pending = placed.map((_, index) => index).filter(index => !pinned(placed[index]));
256
+ // A later label can vacate the only slot available to an earlier one.
257
+ // Retry only unresolved labels after a move; accepted placements clear all
258
+ // current labels and fixed geometry, so each success retires one label.
259
+ // A stalled pass ends the search, with at most one pass per input label.
260
+ for (let pass = 0; pass < labels.length && pending.length; pass++) {
261
+ let moved = false;
262
+ for (const index of pending) {
263
+ const before = placed[index];
264
+ placeLabel(index, gridSweep && pass === 0);
265
+ moved ||= placed[index] !== before;
266
+ }
267
+ if (!moved) break;
268
+ pending = pending.filter(index => needsPlacement(placed[index], index));
269
+ }
270
+ return placed;
271
+ }
272
+
273
+ // The rect a single unpinned label would occupy given only its own route and
274
+ // the nodes: what the planner reserves before the remaining routes are laid.
275
+ // Only a placement beside the route itself is worth reserving; a label that
276
+ // would already need the fallback ring is left to the final placement pass.
277
+ export function reservedLabelRect({
278
+ label, points, routes = [], labels = [], components, viewBox = [Infinity, Infinity], placementBottom = Infinity,
279
+ }) {
280
+ const [rect] = placeAutomaticLabels({
281
+ labels: [{ ...label, relationIndex: -1 }, ...labels.map(other => ({ ...other, relationIndex: -2 }))],
282
+ routes: [{ relationIndex: -1, points }, ...routes],
283
+ components,
284
+ titles: [],
285
+ viewBox,
286
+ placementBottom,
287
+ fallbackRing: false,
288
+ });
289
+ return components.some(component => rectsOverlap(rect, component, -2)) ? null : rect;
290
+ }