Skip to content

Turn any idea, plan, or codebase into a beautiful interactive diagram. An agent skill for Claude Code, Codex, and more.

fix: avoid argument-spread limits in renderers (port #505 to dev)

momoyeyu94b056406648
diff --git a/archify.zip b/archify.zip
index e69bdc82..3cb61a03 100644
Binary files a/archify.zip and b/archify.zip differ
diff --git a/archify/renderers/architecture/render-architecture.mjs b/archify/renderers/architecture/render-architecture.mjs
index d41e4b80..6739c3cb 100644
--- a/archify/renderers/architecture/render-architecture.mjs
+++ b/archify/renderers/architecture/render-architecture.mjs
@@ -104,10 +104,16 @@ for (const [index, c] of asArray(arch.components).entries()) {
function boundaryRect(boundary) {
const members = asArray(boundary.wraps).map((id) => components.get(id)).filter(Boolean);
if (!members.length) return null;
- const minX = Math.min(...members.map((m) => m.x));
- const minY = Math.min(...members.map((m) => m.y));
- const maxX = Math.max(...members.map((m) => m.x + m.width));
- const maxY = Math.max(...members.map((m) => m.y + m.height));
+ let minX = Infinity;
+ let minY = Infinity;
+ let maxX = -Infinity;
+ let maxY = -Infinity;
+ for (const member of members) {
+ minX = Math.min(minX, member.x);
+ minY = Math.min(minY, member.y);
+ maxX = Math.max(maxX, member.x + member.width);
+ maxY = Math.max(maxY, member.y + member.height);
+ }
const pad = boundary.pad ?? layout.boundaryPad;
const topPad = Math.max(
pad,
@@ -163,18 +169,14 @@ function connectionLabelRects() {
}
function autoViewBoxFor(candidateBoundaries, extraRects = []) {
- const maxX = Math.max(
- 0,
- ...[...components.values()].map((component) => component.x + component.width),
- ...candidateBoundaries.map((boundary) => boundary.x + boundary.width),
- ...extraRects.map((rect) => rect.x + rect.width),
- );
- const maxY = Math.max(
- 0,
- ...[...components.values()].map((component) => component.y + component.height),
- ...candidateBoundaries.map((boundary) => boundary.y + boundary.height),
- ...extraRects.map((rect) => rect.y + rect.height),
- );
+ let maxX = 0;
+ let maxY = 0;
+ for (const rects of [components.values(), candidateBoundaries, extraRects]) {
+ for (const rect of rects) {
+ maxX = Math.max(maxX, rect.x + rect.width);
+ maxY = Math.max(maxY, rect.y + rect.height);
+ }
+ }
let width = Math.ceil(maxX + layout.margin);
let footprint = legendFootprint(architectureLegendEntries, {
width: Math.max(1, width - layout.margin * 2),
@@ -266,8 +268,9 @@ function layoutBoundaryTitles(rawBoundaries, minimumFontSize) {
...components.values(),
].filter((candidate) => horizontalOverlap(title, candidate) && rectsOverlap(title, candidate));
if (!blockers.length) break;
- title.y = Math.min(
- ...blockers.map((blocker) => blocker.y - layout.boundaryLabelRailGap - title.height),
+ title.y = blockers.reduce(
+ (min, blocker) => Math.min(min, blocker.y - layout.boundaryLabelRailGap - title.height),
+ Infinity,
);
}
placedTitles.push(title);
@@ -725,11 +728,13 @@ function renderLegend() {
pointsFor: (connection) => pathFor(connection).points,
labelRectFor: connectionLabelBox,
});
- const contentBottom = Math.max(
- 0,
- ...[...components.values()].map((component) => component.y + component.height),
- ...boundaries.map((boundary) => boundary.y + boundary.height),
- );
+ let contentBottom = 0;
+ for (const component of components.values()) {
+ contentBottom = Math.max(contentBottom, component.y + component.height);
+ }
+ for (const boundary of boundaries) {
+ contentBottom = Math.max(contentBottom, boundary.y + boundary.height);
+ }
return renderResolvedLegend({
entries,
locale: arch.meta.locale,
diff --git a/archify/renderers/lifecycle/render-lifecycle.mjs b/archify/renderers/lifecycle/render-lifecycle.mjs
index 12ed5e51..a1b65b6c 100644
--- a/archify/renderers/lifecycle/render-lifecycle.mjs
+++ b/archify/renderers/lifecycle/render-lifecycle.mjs
@@ -534,7 +534,7 @@ function renderLifecycleRail() {
.filter((state) => bandFor(state.lane) === 'phase')
.map((state) => state.col);
if (!mainCols.length) return '';
- const railEnd = layout.phaseXs[Math.max(...mainCols)] + 38;
+ const railEnd = layout.phaseXs[mainCols.reduce((max, col) => Math.max(max, col))] + 38;
return ` <path data-lifecycle-rail="" d="M 154 ${layout.phaseY + 31} L ${railEnd} ${layout.phaseY + 31}" class="a-emphasis" stroke-width="2.2" marker-end="url(#arrowhead-emphasis)"/>`;
}
diff --git a/archify/renderers/shared/geometry.mjs b/archify/renderers/shared/geometry.mjs
index 221390b9..3d09e17c 100644
--- a/archify/renderers/shared/geometry.mjs
+++ b/archify/renderers/shared/geometry.mjs
@@ -785,7 +785,7 @@ export function collectBorderRuns({ routedRelations, frames }) {
frame,
frameIndex,
side: border.side,
- segmentIndex: Math.min(...overlaps.map((overlap) => overlap.segmentIndex)),
+ segmentIndex: overlaps.reduce((min, overlap) => Math.min(min, overlap.segmentIndex), Infinity),
overlapLength: merged.reduce((total, overlap) => total + overlap.length, 0),
overlapStart: longest.start,
overlapEnd: longest.end,
diff --git a/archify/renderers/shared/legend.mjs b/archify/renderers/shared/legend.mjs
index e9e80d6f..5879de54 100644
--- a/archify/renderers/shared/legend.mjs
+++ b/archify/renderers/shared/legend.mjs
@@ -96,7 +96,7 @@ export function legendFootprint(entries, {
measured,
rows,
rowCount: rows.length,
- minWidth: Math.max(...measured.map((entry) => entry.width)),
+ minWidth: measured.reduce((width, entry) => Math.max(width, entry.width), 0),
extraHeight: (rows.length - 1) * lineGap,
};
}
diff --git a/archify/renderers/workflow/workflow-compiler.mjs b/archify/renderers/workflow/workflow-compiler.mjs
index 8b6b9735..aa2f13ac 100644
--- a/archify/renderers/workflow/workflow-compiler.mjs
+++ b/archify/renderers/workflow/workflow-compiler.mjs
@@ -3254,12 +3254,20 @@ function readableCandidateCost(
const directLength = Math.abs(points.at(-1)[0] - points[0][0]) + Math.abs(points.at(-1)[1] - points[0][1]);
const interiorPreferred28Deficit = segmentLengths.slice(1, -1)
.reduce((total, length) => total + Math.max(0, 28 - length), 0);
- const xs = points.map(([x]) => x);
- const ys = points.map(([, y]) => y);
- const canvasGrowthPx = Math.max(0, -Math.min(...xs))
- + Math.max(0, Math.max(...xs) - minimumCanvasWidth)
- + Math.max(0, -Math.min(...ys))
- + Math.max(0, Math.max(...ys) - autoHeight);
+ let minX = Infinity;
+ let maxX = -Infinity;
+ let minY = Infinity;
+ let maxY = -Infinity;
+ for (const [x, y] of points) {
+ minX = Math.min(minX, x);
+ maxX = Math.max(maxX, x);
+ minY = Math.min(minY, y);
+ maxY = Math.max(maxY, y);
+ }
+ const canvasGrowthPx = Math.max(0, -minX)
+ + Math.max(0, maxX - minimumCanvasWidth)
+ + Math.max(0, -minY)
+ + Math.max(0, maxY - autoHeight);
const from = nodes.get(edge.from);
const to = nodes.get(edge.to);
const naturalStart = anchor(from, naturalFromSide);
diff --git a/archify/test/spread-regression.test.mjs b/archify/test/spread-regression.test.mjs
new file mode 100644
index 00000000..d59a7aa1
--- /dev/null
+++ b/archify/test/spread-regression.test.mjs
@@ -0,0 +1,73 @@
+// Regression coverage for the argument-spread RangeError. V8 caps the number of
+// arguments a single call may receive, so `Math.min(...items)` over an
+// input-sized collection used to crash large-but-valid specs. Each case below
+// failed on the pre-fix revision and must now render successfully.
+//
+// The `many-components` case passes `--stack-size=128` to lower the argument
+// ceiling so a 12k-component fixture still exceeds it on the older Node runtimes
+// in the matrix (observed RangeError on Node 20, ~30s render); on newer V8 the
+// same input is a fast completion guard for the legend/layout path.
+//
+// node --test test/spread-regression.test.mjs
+
+import { test } from 'node:test';
+import assert from 'node:assert/strict';
+import { spawnSync } from 'node:child_process';
+import fs from 'node:fs';
+import os from 'node:os';
+import path from 'node:path';
+import { fileURLToPath } from 'node:url';
+
+const __dirname = path.dirname(fileURLToPath(import.meta.url));
+const skillRoot = path.resolve(__dirname, '..');
+const renderer = path.join(skillRoot, 'renderers/architecture/render-architecture.mjs');
+const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'archify-spread-'));
+
+const component = (index) => ({
+ id: `c${index}`,
+ type: 'backend',
+ label: 'N',
+ pos: [40 + index * 140, 80],
+ size: [120, 60],
+});
+
+function render(name, doc, flags = []) {
+ const input = path.join(tmp, `${name}.json`);
+ const output = path.join(tmp, `${name}.html`);
+ fs.writeFileSync(input, JSON.stringify(doc));
+ const result = spawnSync(process.execPath, [...flags, renderer, input, output], {
+ encoding: 'utf8',
+ timeout: 180_000,
+ });
+ return { result, output };
+}
+
+function spec(overrides) {
+ return {
+ schema_version: 1,
+ diagram_type: 'architecture',
+ meta: { title: 'Spread regression' },
+ components: [],
+ boundaries: [],
+ connections: [],
+ ...overrides,
+ };
+}
+
+test('boundary wraps list larger than the call-argument limit still renders', () => {
+ const { result, output } = render('large-wraps', spec({
+ components: [component(0)],
+ boundaries: [{ kind: 'region', label: 'B', wraps: Array(150_000).fill('c0') }],
+ }));
+ assert.equal(result.status, 0, result.stderr);
+ assert.ok(fs.existsSync(output));
+});
+
+test('many components render through legend layout under a shrunken stack', () => {
+ const { result, output } = render('many-components', spec({
+ components: Array.from({ length: 12_000 }, (_, index) => component(index)),
+ }), ['--stack-size=128']);
+ assert.equal(result.status, 0, result.stderr);
+ const html = fs.readFileSync(output, 'utf8');
+ assert.ok(html.includes('<svg'));
+});