Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (claude): word-boundary ingredient-name matching to avoid false positives

[._.]/ Adam Eivyd2cf449ad23d
diff --git a/server/services/catalogExtraction.js b/server/services/catalogExtraction.js
index 3e7c71694a..78dbcdbfb3 100644
--- a/server/services/catalogExtraction.js
+++ b/server/services/catalogExtraction.js
@@ -296,11 +296,18 @@ export async function scanProseForIngredientRefs(text, scope = {}) {
}
if (byId.size === 0) return [];
- const haystack = text.toLowerCase();
const matched = [];
for (const [id, name] of byId) {
- const needle = String(name).trim().toLowerCase();
- if (needle && haystack.includes(needle)) matched.push(id);
+ const needle = String(name).trim();
+ if (!needle) continue;
+ // Word-boundary match (not bare substring) so a short name like "Sun"
+ // doesn't false-positive inside "Sunday" or "Al" inside "always". Names
+ // are user-controlled, so escape regex metacharacters; the \p{L}\p{N}
+ // lookarounds keep multi-word phrases ("The Drowned Harbor") matching as a
+ // unit while still treating the whole name as one token boundary-wise.
+ const esc = needle.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
+ const re = new RegExp(`(?<![\\p{L}\\p{N}])${esc}(?![\\p{L}\\p{N}])`, 'iu');
+ if (re.test(text)) matched.push(id);
}
return matched.sort();
}
diff --git a/server/services/catalogExtraction.test.js b/server/services/catalogExtraction.test.js
index 24bcb25432..ee20517209 100644
--- a/server/services/catalogExtraction.test.js
+++ b/server/services/catalogExtraction.test.js
@@ -272,7 +272,7 @@ describe('catalogExtraction — scanProseForIngredientRefs', () => {
expect(catalogDB.listIngredientsForRef).not.toHaveBeenCalled();
});
- it('matches ingredient names as case-insensitive substrings of the prose', async () => {
+ it('matches ingredient names as case-insensitive word-boundary matches (incl. multi-word phrases)', async () => {
catalogDB.listIngredientsForRef.mockResolvedValue([
row('cat-chr-mira', 'Mira'),
row('cat-chr-tomas', 'Tomas'),
@@ -280,11 +280,29 @@ describe('catalogExtraction — scanProseForIngredientRefs', () => {
]);
const prose = 'mira crossed the drowned harbor at dawn, alone.';
const ids = await scanProseForIngredientRefs(prose, { workId: 'wr-work-1' });
- // Mira (lowercased) + The Drowned Harbor match; Tomas does not. Sorted.
+ // Mira (lowercased) + the multi-word phrase "The Drowned Harbor" match;
+ // Tomas does not. Sorted.
expect(ids).toEqual(['cat-chr-mira', 'cat-plc-harbor'].sort());
expect(catalogDB.listIngredientsForRef).toHaveBeenCalledWith('work', 'wr-work-1');
});
+ it('does not false-positive a short name embedded inside a larger word', async () => {
+ catalogDB.listIngredientsForRef.mockResolvedValue([
+ row('cat-chr-sun', 'Sun'),
+ row('cat-chr-al', 'Al'),
+ ]);
+ // "Sunday" contains "Sun" and "always" contains "Al", but neither is a
+ // real reference — word-boundary matching must reject both.
+ const ids = await scanProseForIngredientRefs('On Sunday she always waited.', { workId: 'wr-work-1' });
+ expect(ids).toEqual([]);
+ });
+
+ it('matches a short name when it appears as a whole word', async () => {
+ catalogDB.listIngredientsForRef.mockResolvedValue([row('cat-chr-sun', 'Sun')]);
+ const ids = await scanProseForIngredientRefs('The Sun rose over the bay.', { workId: 'wr-work-1' });
+ expect(ids).toEqual(['cat-chr-sun']);
+ });
+
it('scopes candidates to the linked refs — never an arbitrary catalog row', async () => {
// listIngredientsForRef is the only candidate source, so a name that isn't
// in the linked cast simply never enters the match set.