Skip to content

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

fix: guard unsafe route index coordinates

skyseek5b12bcd446ea
diff --git a/archify/renderers/shared/geometry.mjs b/archify/renderers/shared/geometry.mjs
index a5b2c979..837925ba 100644
--- a/archify/renderers/shared/geometry.mjs
+++ b/archify/renderers/shared/geometry.mjs
@@ -131,6 +131,10 @@ function createRouteCandidateIndex(routes) {
const y1 = Math.floor(bounds.y1 / ROUTE_INDEX_CELL_SIZE);
const x2 = Math.floor(bounds.x2 / ROUTE_INDEX_CELL_SIZE);
const y2 = Math.floor(bounds.y2 / ROUTE_INDEX_CELL_SIZE);
+ if (![x1, y1, x2, y2].every(Number.isSafeInteger)) {
+ globalRouteIndexes.push(routeIndex);
+ return;
+ }
const cellCount = (x2 - x1 + 1) * (y2 - y1 + 1);
// Very long routes are cheaper to test once per label than to duplicate
@@ -157,7 +161,9 @@ function createRouteCandidateIndex(routes) {
const cellCount = (x2 - x1 + 1) * (y2 - y1 + 1);
// A huge threshold offers no selectivity and can overflow cell iteration.
- if (!Number.isSafeInteger(cellCount) || cellCount > Math.max(64, cells.size * 4)) return routes;
+ if (![x1, y1, x2, y2].every(Number.isSafeInteger)
+ || !Number.isSafeInteger(cellCount)
+ || cellCount > Math.max(64, cells.size * 4)) return routes;
const routeIndexes = new Set(globalRouteIndexes);
for (let y = y1; y <= y2; y += 1) {
diff --git a/archify/test/geometry.test.mjs b/archify/test/geometry.test.mjs
index ecc1b67a..4687b336 100644
--- a/archify/test/geometry.test.mjs
+++ b/archify/test/geometry.test.mjs
@@ -182,6 +182,32 @@ test('collectLabelRouteClearance keeps very long routes in the global candidate
assert.equal(hits[0].clearance, 0);
});
+test('collectLabelRouteClearance falls back for unsafe finite grid coordinates', () => {
+ const owner = { id: 'owner', from: 'a', to: 'b' };
+ const unsafe = { id: 'unsafe', from: 'c', to: 'd' };
+ const hits = collectLabelRouteClearance({
+ labels: [{ relation: owner, relationIndex: 0, label: 'unsafe coordinate', ...rect(1e20, 1e20, 1e7, 1e7) }],
+ routedRelations: [{ relation: unsafe, relationIndex: 1, points: [[1e20 - 1e8, 1e20 + 5e6], [1e20 + 1e8, 1e20 + 5e6]] }],
+ threshold: 4,
+ });
+ assert.equal(hits.length, 1);
+ assert.equal(hits[0].otherRelation, unsafe);
+ assert.equal(hits[0].clearance, 0);
+});
+
+test('collectLabelRouteClearance falls back when threshold query is unselective', () => {
+ const owner = { id: 'owner', from: 'a', to: 'b' };
+ const other = { id: 'other', from: 'c', to: 'd' };
+ const hits = collectLabelRouteClearance({
+ labels: [{ relation: owner, relationIndex: 0, label: 'wide threshold', ...rect(100, 100, 10, 10) }],
+ routedRelations: [{ relation: other, relationIndex: 1, points: [[0, 105], [200, 105]] }],
+ threshold: 1e9,
+ });
+ assert.equal(hits.length, 1);
+ assert.equal(hits[0].otherRelation, other);
+ assert.equal(hits[0].clearance, 0);
+});
+
test('collectLabelRouteClearance stays bounded for thousands of sparse routes', () => {
const count = 3000;
const routedRelations = Array.from({ length: count }, (_, index) => ({