Skip to content

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

fix(architecture): infer endpoint side from the dominant axis, not the bare dx sign

dajiaohuang05c8ebf749d1
diff --git a/archify/renderers/shared/geometry.mjs b/archify/renderers/shared/geometry.mjs
index 733129c2..8af8c19e 100644
--- a/archify/renderers/shared/geometry.mjs
+++ b/archify/renderers/shared/geometry.mjs
@@ -1271,18 +1271,23 @@ export function automaticPortSpread(relations, boxes, { gutter = 16, maxSpacing
return spread;
}
+// A centre delta on the horizontal axis does not by itself mean the route
+// leaves sideways: a hub above an offset spoke has both a horizontal and a
+// larger vertical delta, and the router draws the vertical dogleg. Compare the
+// deltas so the inferred side matches the axis the route actually uses. Equal
+// deltas, and a zero horizontal delta, keep the previous vertical result.
export function defaultFromSide(from, to) {
- if (to.cx < from.cx) return 'left';
- if (to.cx > from.cx) return 'right';
- if (to.cy > from.cy) return 'bottom';
- return 'top';
+ const dx = to.cx - from.cx;
+ const dy = to.cy - from.cy;
+ if (dx !== 0 && Math.abs(dx) >= Math.abs(dy)) return dx < 0 ? 'left' : 'right';
+ return dy > 0 ? 'bottom' : 'top';
}
export function defaultToSide(from, to) {
- if (to.cx < from.cx) return 'right';
- if (to.cx > from.cx) return 'left';
- if (to.cy > from.cy) return 'top';
- return 'bottom';
+ const dx = to.cx - from.cx;
+ const dy = to.cy - from.cy;
+ if (dx !== 0 && Math.abs(dx) >= Math.abs(dy)) return dx < 0 ? 'right' : 'left';
+ return dy > 0 ? 'top' : 'bottom';
}
export function chosenSide(side, fallback) {
diff --git a/archify/test/geometry.test.mjs b/archify/test/geometry.test.mjs
index 2b93f8c4..62902074 100644
--- a/archify/test/geometry.test.mjs
+++ b/archify/test/geometry.test.mjs
@@ -530,6 +530,37 @@ test('defaultFromSide / defaultToSide are mirror pairs', () => {
assert.equal(defaultToSide(a, below), 'top');
});
+test('defaultFromSide / defaultToSide follow the dominant axis, not any horizontal delta', () => {
+ // A hub above an offset spoke: the centres differ horizontally, but the
+ // vertical delta dominates, so the router draws the vertical dogleg and the
+ // inferred sides must be bottom/top (issue #376). Every mirror pair is
+ // asserted together so a regression cannot silently flip one endpoint.
+ const hub = { cx: 288, cy: 66 };
+ const spoke = { cx: 124, cy: 266 }; // dx = -164, dy = +200
+ assert.equal(defaultFromSide(hub, spoke), 'bottom');
+ assert.equal(defaultToSide(hub, spoke), 'top');
+
+ const spokeToHub = { from: spoke, to: hub };
+ assert.equal(defaultFromSide(spokeToHub.from, spokeToHub.to), 'top');
+ assert.equal(defaultToSide(spokeToHub.from, spokeToHub.to), 'bottom');
+
+ // Equal deltas keep the horizontal side (previous behaviour preserved).
+ const diagonal = { cx: 100, cy: 100 };
+ assert.equal(defaultFromSide({ cx: 0, cy: 0 }, diagonal), 'right');
+ assert.equal(defaultToSide({ cx: 0, cy: 0 }, diagonal), 'left');
+
+ // A pure vertical delta must still resolve vertically, never to a side.
+ assert.equal(defaultFromSide({ cx: 50, cy: 0 }, { cx: 50, cy: 100 }), 'bottom');
+ assert.equal(defaultToSide({ cx: 50, cy: 0 }, { cx: 50, cy: 100 }), 'top');
+ assert.equal(defaultFromSide({ cx: 50, cy: 0 }, { cx: 50, cy: -100 }), 'top');
+ assert.equal(defaultToSide({ cx: 50, cy: 0 }, { cx: 50, cy: -100 }), 'bottom');
+
+ // Coincident centres stay on the vertical fallback (top/bottom), matching
+ // the pre-existing dx === 0 / dy === 0 behaviour.
+ assert.equal(defaultFromSide({ cx: 7, cy: 7 }, { cx: 7, cy: 7 }), 'top');
+ assert.equal(defaultToSide({ cx: 7, cy: 7 }, { cx: 7, cy: 7 }), 'bottom');
+});
+
test('chosenSide treats explicit "auto" as "use the geometric fallback"', () => {
assert.equal(chosenSide('left', 'right'), 'left');
assert.equal(chosenSide('auto', 'right'), 'right');
diff --git a/archify/test/layout-rules.test.mjs b/archify/test/layout-rules.test.mjs
index 433f3975..0ff3da6c 100644
--- a/archify/test/layout-rules.test.mjs
+++ b/archify/test/layout-rules.test.mjs
@@ -1339,6 +1339,50 @@ test('architecture: container border run is blocking in standard and showcase',
}
});
+test('architecture: an inferred side follows the dominant axis for a hub above an offset spoke', () => {
+ // Regression for issue #376. The hub sits above the spoke with a horizontal
+ // offset (dx = -164, dy = +200). The router draws a vertical dogleg out of
+ // the hub's bottom into the spoke's top, so the INFERRED sides must be
+ // bottom/top. Inferring left/right from the bare sign of dx made the
+ // Clean Flow Gate reject the very route the router had just drawn.
+ const doc = {
+ schema_version: 1,
+ diagram_type: 'architecture',
+ meta: {
+ title: 'Hub above an offset spoke',
+ output: 'hub-spoke.html',
+ quality_profile: 'standard',
+ viewBox: [640, 360],
+ },
+ components: [
+ { id: 'hub', type: 'cloud', label: 'Hub', pos: [200, 40], size: [176, 52] },
+ { id: 'spoke', type: 'cloud', label: 'Spoke', pos: [40, 240], size: [168, 52] },
+ ],
+ connections: [{ id: 'hub-spoke', from: 'hub', to: 'spoke' }],
+ };
+
+ const { code, stderr, outPath } = render('architecture', doc);
+ assert.equal(code, 0, `inferred vertical route must render cleanly: ${stderr}`);
+ assert.doesNotMatch(stderr, /clean-flow\/endpoint-side-direction/);
+
+ // The route leaves the hub's bottom edge and enters the spoke's top edge,
+ // so every composition point stays inside that column band.
+ const html = fs.readFileSync(outPath, 'utf8');
+ const points = html
+ .match(/data-edge-id="hub-spoke" data-composition-points="([^"]+)"/)?.[1];
+ assert.ok(points, 'expected rendered composition points for hub-spoke');
+ const route = points.split(';').map((point) => point.split(',').map(Number));
+ const hubCx = 200 + 176 / 2; // 288
+ assert.equal(route[0][0], hubCx, 'route must leave the hub centre column');
+ assert.equal(route.at(-1)[0], 40 + 168 / 2, 'route must enter the spoke centre column');
+ assert.equal(route[0][1], 40 + 52, 'route must leave the hub bottom edge');
+ assert.equal(route.at(-1)[1], 240, 'route must enter the spoke top edge');
+
+ // The CLI validation surface agrees with the renderer.
+ const cli = validateCli('architecture', doc, 'standard');
+ assert.equal(cli.code, 0, `validate must accept the inferred vertical route: ${JSON.stringify(cli.result)}`);
+});
+
test('dataflow: stage border run is blocking and the inter-stage gutter passes', () => {
const bad = load('dataflow');
bad.flows.find((flow) => flow.id === 'web-clickstream').via = [[184, 157], [184, 271]];