Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

search: a test guards the entry index cache against stale reuse (#7885)

Joeyf8fad28c24e6
diff --git a/.changeset/search-entry-cache-guard.md b/.changeset/search-entry-cache-guard.md
new file mode 100644
index 0000000000..bd84f65e6a
--- /dev/null
+++ b/.changeset/search-entry-cache-guard.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[fix] `astryx search` ties its entry-index reuse to one list of the fields the index reads, so the index is never reused after any of them changes; answers are unchanged.
+
+@josephfarina
diff --git a/packages/cli/api/search/_entries.mjs b/packages/cli/api/search/_entries.mjs
index 7e63f3da5e..8097105a01 100644
--- a/packages/cli/api/search/_entries.mjs
+++ b/packages/cli/api/search/_entries.mjs
@@ -77,6 +77,29 @@ let last = null;
const cardIds = new WeakMap();
let nextCardId = 0;
+/**
+ * The candidate fields buildTable reads, all of them hashed by fingerprint;
+ * `_entries.test.mjs` fails if buildTable starts reading another.
+ */
+/** How many guidance lines and prose paragraphs of a candidate the index reads. */
+const GUIDANCE_LINES = 10;
+const PROSE_PARAGRAPHS = 4;
+
+export const HASHED_FIELDS = Object.freeze([
+ 'domain',
+ 'name',
+ '_topic',
+ '_section',
+ '_resultName',
+ '_package',
+ 'description',
+ 'keywords',
+ 'aliases',
+ 'titles',
+ 'guidance',
+ 'prose',
+]);
+
/**
* Everything the table reads from the catalog, hashed.
* @param {any[]} all
@@ -89,20 +112,15 @@ function fingerprint(all, cards) {
h.update(String(cardIds.get(cards)));
for (const c of all) {
h.update(
- JSON.stringify([
- c.domain,
- c.name,
- c._topic,
- c._section,
- c._resultName,
- c._package,
- c.description,
- c.keywords,
- c.aliases,
- c.titles,
- (c.guidance ?? []).slice(0, 10),
- (c.prose ?? []).slice(0, 4),
- ]),
+ JSON.stringify(
+ HASHED_FIELDS.map(f =>
+ f === 'guidance'
+ ? (c.guidance ?? []).slice(0, GUIDANCE_LINES)
+ : f === 'prose'
+ ? (c.prose ?? []).slice(0, PROSE_PARAGRAPHS)
+ : c[f],
+ ),
+ ),
);
}
return h.digest('hex');
@@ -113,7 +131,7 @@ function fingerprint(all, cards) {
* @param {Record<string, {what?: string, synonyms?: string[], props?: string[]}>} cards
* @returns {EntryTable}
*/
-function buildTable(all, cards) {
+export function buildTable(all, cards) {
/** @type {Map<string, any[]>} */
const by = new Map();
/** @type {Map<string, string>} each id's first package */
@@ -164,8 +182,8 @@ function buildTable(all, cards) {
...(c.titles ?? []),
);
text.push(
- ...(c.guidance ?? []).slice(0, 10),
- ...(c.prose ?? []).slice(0, 4),
+ ...(c.guidance ?? []).slice(0, GUIDANCE_LINES),
+ ...(c.prose ?? []).slice(0, PROSE_PARAGRAPHS),
);
}
const card =
diff --git a/packages/cli/api/search/_entries.test.mjs b/packages/cli/api/search/_entries.test.mjs
new file mode 100644
index 0000000000..907f1aeb22
--- /dev/null
+++ b/packages/cli/api/search/_entries.test.mjs
@@ -0,0 +1,48 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file The entry index is reused while the catalog's text is unchanged, so
+ * every field it reads must be part of the hash that decides reuse.
+ */
+
+import {describe, it, expect} from 'vitest';
+import * as path from 'node:path';
+import {fileURLToPath} from 'node:url';
+import {gatherCandidates, loadSearchData} from './_adapter.mjs';
+import {HASHED_FIELDS, buildTable, entryTable} from './_entries.mjs';
+
+const cwd = path.resolve(
+ path.dirname(fileURLToPath(import.meta.url)),
+ '../../../..',
+);
+
+describe('entry index cache', () => {
+ it('hashes every candidate field the index reads', async () => {
+ const {all} = await gatherCandidates({cwd});
+ /** @type {Set<string>} */
+ const read = new Set();
+ const watched = all.map(
+ c =>
+ new Proxy(c, {
+ get(target, key, receiver) {
+ if (typeof key === 'string') read.add(key);
+ return Reflect.get(target, key, receiver);
+ },
+ }),
+ );
+ buildTable(watched, loadSearchData().cards);
+ const unhashed = [...read].filter(k => !HASHED_FIELDS.includes(k));
+ expect(unhashed).toEqual([]);
+ }, 60_000);
+
+ it('rebuilds when a candidate text changes and reuses it otherwise', async () => {
+ const {all} = await gatherCandidates({cwd});
+ const {cards} = loadSearchData();
+ const first = entryTable([...all], cards);
+ expect(entryTable([...all], cards)).toBe(first);
+ const changed = all.map((c, i) =>
+ i === 0 ? {...c, description: `${c.description ?? ''} changed`} : c,
+ );
+ expect(entryTable(changed, cards)).not.toBe(first);
+ }, 60_000);
+});