An open source design system that's fully customizable and agent ready
TypeScript
perf(cli): search computes a typo's distance only within its cap (#10030)
Christopher Chedeauf1f92c3cbc91
diff --git a/.changeset/cli-search-typo-distance.md b/.changeset/cli-search-typo-distance.mdnew file mode 100644index 0000000000..87eb5789fe--- /dev/null+++ b/.changeset/cli-search-typo-distance.md@@ -0,0 +1,7 @@+---+'@astryxdesign/cli': patch+---++[perf] `astryx search` and `astryx build` compute a typo's edit distance only up to the most edits that can count. A fuzzy match now computes far fewer table cells, with unchanged answers.++@vjeuxdiff --git a/packages/cli/api/search/_candidate-score.mjs b/packages/cli/api/search/_candidate-score.mjsindex 1297820143..6acd748000 100644--- a/packages/cli/api/search/_candidate-score.mjs+++ b/packages/cli/api/search/_candidate-score.mjs@@ -35,7 +35,6 @@* "cable" is not "table".*/-import {levenshteinDistance} from '../../foundation/text/string-utils.mjs';import {stem,wordForms,@@ -43,7 +42,7 @@ import {startsAWordOf,wordsOf,joined,- isTypo,+ typoDistance,} from './_words.mjs';import {pluralOf} from './_query.mjs';@@ -224,8 +223,8 @@ export function scoreCandidate(consider(60, `name contains "${term}"`);}if (fuzzy) {- const dist = levenshteinDistance(term, nameLower);- if (isTypo(term, nameLower, dist)) {+ const dist = typoDistance(term, nameLower);+ if (dist != null) {consider(dist === 1 ? 80 : dist === 2 ? 40 : 20,`similar name (distance ${dist})`,@@ -280,8 +279,8 @@ export function scoreCandidate(}if (startsAWordOf(term, kw)) consider(70, `keyword "${kw}"`);if (fuzzy) {- const dist = levenshteinDistance(term, kwLower);- if (isTypo(term, kwLower, dist) && dist <= 2) {+ const dist = typoDistance(term, kwLower);+ if (dist != null && dist <= 2) {consider(dist === 1 ? 70 : 30, `keyword "${kw}" (distance ${dist})`);}}diff --git a/packages/cli/api/search/_words.mjs b/packages/cli/api/search/_words.mjsindex 4b9b7c451d..0d6c163431 100644--- a/packages/cli/api/search/_words.mjs+++ b/packages/cli/api/search/_words.mjs@@ -5,6 +5,8 @@* identifier spelling, joined names, and typo tolerance.*/+import {levenshteinDistance} from '../../foundation/text/levenshtein.mjs';+/*** Light stemmer: strips common English suffixes so "charts"/"charting" and* "chart" share a root. Deliberately crude (no Porter) — good enough to bridge@@ -155,3 +157,27 @@ export const isTypo = (a, b, dist) =>dist <= 3 &&Math.min(a.length, b.length) >=TYPO_MIN_LENGTH[/** @type {1 | 2 | 3} */ (dist)];++/**+ * The edit distance between two words when it counts as a typo (isTypo), or+ * null. The shorter word's length caps the distance that can count. A pair+ * the cap rules out needs no distance table, and the others compute only the+ * cells within the cap.+ * @param {string} a+ * @param {string} b+ * @returns {1 | 2 | 3 | null}+ */+export function typoDistance(a, b) {+ const shorter = Math.min(a.length, b.length);+ const cap =+ shorter >= TYPO_MIN_LENGTH[3]+ ? 3+ : shorter >= TYPO_MIN_LENGTH[2]+ ? 2+ : shorter >= TYPO_MIN_LENGTH[1]+ ? 1+ : 0;+ if (cap === 0) return null;+ const dist = levenshteinDistance(a, b, cap);+ return dist >= 1 && dist <= cap ? /** @type {1 | 2 | 3} */ (dist) : null;+}diff --git a/packages/cli/api/search/_words.typo.test.mjs b/packages/cli/api/search/_words.typo.test.mjsnew file mode 100644index 0000000000..c16e057d63--- /dev/null+++ b/packages/cli/api/search/_words.typo.test.mjs@@ -0,0 +1,118 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file typoDistance (_words.mjs) gives the answer the full edit distance and+ * isTypo give together, for every pair, and search's scoring computes no+ * full edit-distance table.+ */++import * as path from 'node:path';+import {fileURLToPath} from 'node:url';+import {describe, expect, it, vi} from 'vitest';++const fullTables = vi.hoisted(() => ({count: 0}));++vi.mock('../../foundation/text/levenshtein.mjs', async importOriginal => {+ /** @type {typeof import('../../foundation/text/levenshtein.mjs')} */+ const original = await importOriginal();+ return {+ ...original,+ /** @param {string} a @param {string} b @param {number} [max] */+ levenshteinDistance: (a, b, max) => {+ if (max === undefined) fullTables.count += 1;+ return original.levenshteinDistance(a, b, max);+ },+ };+});++const {levenshteinDistance} =+ await import('../../foundation/text/levenshtein.mjs');+const {isTypo, typoDistance} = await import('./_words.mjs');+const {searchCore} = await import('./search.mjs');++const REPO = path.resolve(+ path.dirname(fileURLToPath(import.meta.url)),+ '../../../..',+);++/**+ * What the scoring used before: the full distance, kept when isTypo allows it.+ * @param {string} a+ * @param {string} b+ */+function fullAnswer(a, b) {+ const dist = levenshteinDistance(a, b);+ return isTypo(a, b, dist) ? dist : null;+}++/**+ * Seeded words of five to fourteen letters, each beside copies one to four+ * random edits away, so the pairs cover typos at every distance that counts.+ * @param {number} count+ * @returns {string[]}+ */+function seededWords(count) {+ let seed = 7;+ const next = () => {+ seed = (seed * 1103515245 + 12345) % 2147483648;+ return seed;+ };+ const letter = () => 'abcdefgh'[next() % 8];+ const words = [];+ for (let i = 0; i < count; i++) {+ let word = '';+ for (let j = 0, length = 5 + (next() % 10); j < length; j++) {+ word += letter();+ }+ words.push(word);+ for (let edits = 1; edits <= 4; edits++) {+ let copy = word;+ for (let e = 0; e < edits; e++) {+ const at = next() % copy.length;+ const kind = next() % 3;+ copy =+ kind === 0+ ? copy.slice(0, at) + letter() + copy.slice(at + 1)+ : kind === 1+ ? copy.slice(0, at) + letter() + copy.slice(at)+ : copy.slice(0, at) + copy.slice(at + 1);+ }+ words.push(copy);+ }+ }+ return words;+}++describe('typoDistance', () => {+ it('gives the full distance and isTypo answer for every pair', () => {+ const words = [+ ...seededWords(80),+ 'dashboard',+ 'dashbaord',+ 'dashboards',+ 'datepicker',+ 'datepickr',+ 'notification',+ 'notifcation',+ 'pagination',+ 'paginator',+ ];+ let typos = 0;+ for (const a of words) {+ for (const b of words) {+ const answer = fullAnswer(a, b);+ expect(typoDistance(a, b), `${a} ${b}`).toBe(answer);+ if (answer != null) typos += 1;+ }+ }+ // The pairs include typos at every distance that can count.+ expect(typos).toBeGreaterThan(300);+ });++ it('keeps search finding a typed name, with no full distance table', async () => {+ fullTables.count = 0;+ const response = await searchCore('dashbaord', {cwd: REPO});+ expect(response.data.results.length).toBeGreaterThan(0);+ expect(fullTables.count).toBe(0);+ });+});diff --git a/packages/cli/foundation/text/levenshtein.mjs b/packages/cli/foundation/text/levenshtein.mjsindex 9f373a3058..0a764bc3e8 100644--- a/packages/cli/foundation/text/levenshtein.mjs+++ b/packages/cli/foundation/text/levenshtein.mjs@@ -16,9 +16,13 @@/*** @param {string} a* @param {string} b+ * @param {number} [max] - The largest distance the caller can use. When+ * given, a larger distance answers `max + 1`, and only the cells within+ * `max` of the diagonal are computed.* @returns {number}*/-export function levenshteinDistance(a, b) {+export function levenshteinDistance(a, b, max) {+ if (max !== undefined) return distanceWithin(a, b, max);const m = a.length, n = b.length;// Early exit: the distance is always ≥ |m − n|, so once the length gap// exceeds the loosest fuzzy-match threshold any caller uses (the hook@@ -39,3 +43,42 @@ export function levenshteinDistance(a, b) {}return dp[m][n];}++/**+ * The edit distance between `a` and `b` when it is at most `max`, or+ * `max + 1` when it is more. A path through a cell more than `max` from the+ * diagonal costs more than `max`, so only that band is computed.+ * @param {string} a+ * @param {string} b+ * @param {number} max+ * @returns {number}+ */+function distanceWithin(a, b, max) {+ const over = max + 1;+ if (Math.abs(a.length - b.length) > max) return over;+ let previous = new Array(b.length + 1).fill(over);+ let current = new Array(b.length + 1).fill(over);+ for (let j = 0; j <= Math.min(b.length, max); j++) previous[j] = j;+ for (let i = 1; i <= a.length; i++) {+ current.fill(over);+ const from = Math.max(1, i - max);+ const to = Math.min(b.length, i + max);+ if (i <= max) current[0] = i;+ let rowMin = current[0];+ for (let j = from; j <= to; j++) {+ const cost = a[i - 1] === b[j - 1] ? 0 : 1;+ const value = Math.min(+ previous[j - 1] + cost,+ previous[j] + 1,+ current[j - 1] + 1,+ over,+ );+ current[j] = value;+ if (value < rowMin) rowMin = value;+ }+ // Every later cell costs at least this row's least.+ if (rowMin >= over) return over;+ [previous, current] = [current, previous];+ }+ return Math.min(previous[b.length], over);+}diff --git a/packages/cli/foundation/text/levenshtein.test.mjs b/packages/cli/foundation/text/levenshtein.test.mjsindex 7d4a915fd0..0116f2a0ec 100644--- a/packages/cli/foundation/text/levenshtein.test.mjs+++ b/packages/cli/foundation/text/levenshtein.test.mjs@@ -49,5 +49,18 @@ describe('levenshteinDistance', () => {// so a real 5-away typo isn't dropped by the short-circuit.expect(d('use', 'useMedia')).toBe(5); // 5 pure insertions});++ it('answers max + 1 past an optional bound, and the distance within it', () => {+ const words = ['kitten', 'sitting', 'mitten', 'kitchen', 'knitting', 'kit'];+ for (const a of words) {+ for (const b of words) {+ const full = d(a, b);+ for (let max = 0; max <= 5; max++) {+ const within = full <= max ? full : max + 1;+ expect(d(a, b, max), `${a} ${b} ${max}`).toBe(within);+ }+ }+ }+ });});