Skip to content

Commit bea3d29

Browse files
ly-wang19claude
andauthored
fix(memory-core): keep short protected-glossary terms past the min-length gate (#96304)
PROTECTED_GLOSSARY exists to preserve short technical terms that generic filtering would discard, but every glossary match still flowed through normalizeConceptToken's per-script minimum-length gate. The 2-char latin entries "kv" and "s3" were therefore never emitted as concept tags despite being on the protect-list. Thread a fromGlossary flag so glossary matches bypass only that length check; all other gates still apply. Because that bypass lets short entries through, a bare substring match would also surface them from inside longer words ("kv" in "mkv", "s3" in "css3"). Match ONLY the short entries (those below their script's min length) as delimiter-bounded whole tokens; longer entries keep substring containment, so the shipped behavior of "backup" tagging inside "backups" is preserved. CJK entries (no word delimiters) always use substring matching. Positive (standalone kv/s3) and negative (mkv/css3 substrings) regression tests cover both directions, and the short-term-promotion stable-tags assertion gains "s3". Co-authored-by: ly-wang19 <[email protected]> Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
1 parent 17066f2 commit bea3d29

3 files changed

Lines changed: 82 additions & 17 deletions

File tree

‎extensions/memory-core/src/concept-vocabulary.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,30 @@ describe("concept vocabulary", () => {
2929
expect(tags).not.toContain("2026-04-04.md");
3030
});
3131

32+
it("preserves short protected-glossary terms past the latin minimum-length gate", () => {
33+
const tags = deriveConceptTags({
34+
path: "memory/2026-04-04.md",
35+
snippet: "Store the session in kv and back up to s3 nightly.",
36+
});
37+
38+
// "kv" and "s3" are 2-char latin glossary entries that the generic min-length-3 gate would drop.
39+
expect(tags).toContain("kv");
40+
expect(tags).toContain("s3");
41+
});
42+
43+
it("does not surface short glossary terms that only appear inside longer words", () => {
44+
const tags = deriveConceptTags({
45+
path: "memory/2026-04-04.md",
46+
snippet: "Played the mkv recording and tuned the css3 layout.",
47+
});
48+
49+
// "kv"/"s3" are substrings of "mkv"/"css3"; whole-word matching must not emit them as tags.
50+
expect(tags).not.toContain("kv");
51+
expect(tags).not.toContain("s3");
52+
expect(tags).toContain("mkv");
53+
expect(tags).toContain("css3");
54+
});
55+
3256
it("extracts protected and segmented CJK concept tags", () => {
3357
const tags = deriveConceptTags({
3458
path: "memory/2026-04-04.md",

‎extensions/memory-core/src/concept-vocabulary.ts‎

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ function isKanaOnlyToken(value: string): boolean {
330330
);
331331
}
332332

333-
function normalizeConceptToken(rawToken: string): string | null {
333+
function normalizeConceptToken(rawToken: string, fromGlossary = false): string | null {
334334
const normalized = normalizeLowercaseStringOrEmpty(
335335
rawToken
336336
.normalize("NFKC")
@@ -348,7 +348,9 @@ function normalizeConceptToken(rawToken: string): string | null {
348348
return null;
349349
}
350350
const script = classifyConceptTagScript(normalized);
351-
if (normalized.length < minimumTokenLengthForScript(script)) {
351+
// Glossary entries are an explicit allowlist of short technical terms (e.g. "kv", "s3"); they
352+
// bypass the per-script minimum length that would otherwise discard them.
353+
if (!fromGlossary && normalized.length < minimumTokenLengthForScript(script)) {
352354
return null;
353355
}
354356
if (isKanaOnlyToken(normalized) && normalized.length < 3) {
@@ -360,14 +362,43 @@ function normalizeConceptToken(rawToken: string): string | null {
360362
return normalized;
361363
}
362364

365+
// Only entries shorter than their script's minimum token length rely on the glossary bypass, and
366+
// only those need whole-word matching so they don't fire inside longer words ("kv" in "mkv"). Longer
367+
// entries keep substring containment (the shipped behavior, e.g. "backup" tagging inside "backups").
368+
// Precomputed so derive() does not reclassify on every call.
369+
const GLOSSARY_ENTRIES = PROTECTED_GLOSSARY.map((entry) => ({
370+
entry,
371+
wholeWord: entry.length < minimumTokenLengthForScript(classifyConceptTagScript(entry)),
372+
}));
373+
374+
function isAlphanumericAt(source: string, index: number): boolean {
375+
const ch = source[index];
376+
return ch !== undefined && LETTER_OR_NUMBER_RE.test(ch);
377+
}
378+
379+
// True when `entry` occurs as a delimiter-bounded token, not inside a longer word. Keeps short
380+
// glossary entries like "kv"/"s3" from firing inside "mkv"/"css3" once they bypass the length gate.
381+
function includesStandaloneTerm(source: string, entry: string): boolean {
382+
let from = source.indexOf(entry);
383+
while (from !== -1) {
384+
if (!isAlphanumericAt(source, from - 1) && !isAlphanumericAt(source, from + entry.length)) {
385+
return true;
386+
}
387+
from = source.indexOf(entry, from + 1);
388+
}
389+
return false;
390+
}
391+
363392
function collectGlossaryMatches(source: string): string[] {
364393
const normalizedSource = normalizeLowercaseStringOrEmpty(source.normalize("NFKC"));
365394
const matches: string[] = [];
366-
for (const entry of PROTECTED_GLOSSARY) {
367-
if (!normalizedSource.includes(entry)) {
368-
continue;
395+
for (const { entry, wholeWord } of GLOSSARY_ENTRIES) {
396+
const present = wholeWord
397+
? includesStandaloneTerm(normalizedSource, entry)
398+
: normalizedSource.includes(entry);
399+
if (present) {
400+
matches.push(entry);
369401
}
370-
matches.push(entry);
371402
}
372403
return matches;
373404
}
@@ -385,8 +416,13 @@ function collectSegmentTokens(source: string): string[] {
385416
return source.split(/[^\p{L}\p{N}]+/u).filter(Boolean);
386417
}
387418

388-
function pushNormalizedTag(tags: string[], rawToken: string, limit: number): void {
389-
const normalized = normalizeConceptToken(rawToken);
419+
function pushNormalizedTag(
420+
tags: string[],
421+
rawToken: string,
422+
limit: number,
423+
fromGlossary = false,
424+
): void {
425+
const normalized = normalizeConceptToken(rawToken, fromGlossary);
390426
if (!normalized || tags.includes(normalized)) {
391427
return;
392428
}
@@ -410,14 +446,17 @@ export function deriveConceptTags(params: {
410446
}
411447

412448
const tags: string[] = [];
413-
for (const rawToken of [
414-
...collectGlossaryMatches(source),
415-
...collectCompoundTokens(source),
416-
...collectSegmentTokens(source),
417-
]) {
418-
pushNormalizedTag(tags, rawToken, limit);
419-
if (tags.length >= limit) {
420-
break;
449+
const tokenSources: Array<{ tokens: string[]; fromGlossary: boolean }> = [
450+
{ tokens: collectGlossaryMatches(source), fromGlossary: true },
451+
{ tokens: collectCompoundTokens(source), fromGlossary: false },
452+
{ tokens: collectSegmentTokens(source), fromGlossary: false },
453+
];
454+
for (const { tokens, fromGlossary } of tokenSources) {
455+
for (const rawToken of tokens) {
456+
pushNormalizedTag(tags, rawToken, limit, fromGlossary);
457+
if (tags.length >= limit) {
458+
return tags;
459+
}
421460
}
422461
}
423462
return tags;

‎extensions/memory-core/src/short-term-promotion.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3189,7 +3189,9 @@ describe("short-term promotion", () => {
31893189
path: "memory/2026-04-03.md",
31903190
snippet: "Move backups to S3 Glacier and sync QMD router notes.",
31913191
}),
3192-
).toStrictEqual(["backup", "backups", "glacier", "qmd", "router", "sync"]);
3192+
// "s3" is a protected-glossary term; it now surfaces as a standalone token past the
3193+
// per-script min-length gate (the longer terms still match as substrings).
3194+
).toStrictEqual(["backup", "backups", "glacier", "qmd", "router", "s3", "sync"]);
31933195
});
31943196

31953197
it("extracts multilingual concept tags across latin and cjk snippets", () => {

0 commit comments

Comments
 (0)