[bug] MemoryRetriever tokenize keeps ASCII letters only: a non-Latin query yields no…
What happened
MemoryRetriever tokenize keeps ASCII letters only, so a non-Latin query yields no search terms and accented words are cut into fragments
Summary
Version: LifeOS 8.761.0. LIFEOS/TOOLS/MemoryRetriever.ts on main (94bd8f2) is the same as in 8.761.0 for the lines below.
What happens. tokenize (line 359) lowercases the text and replaces everything outside [a-z0-9\s-] with a space. The same function feeds the query side (retrievalTerms), the body term frequencies and the persisted postings, and containsTerm (line 443) uses [a-z0-9] as its word boundary. For a principal who writes in another language this means:
- A query in a non-Latin script has zero terms, so word-based retrieval returns nothing for it, and a note written in that script puts no words into the index.
- A Latin-script word with diacritics is split at every accented letter, so queries and notes are indexed as fragments.
Nothing reports this. The per-turn RELEVANT MEMORY block is just empty or off-topic.
Repro on unmodified main, retrievalTerms(q):
"project deadline and budget review" -> ["project","deadline","budget","review"]
"сроки проекта и обзор бюджета" -> []
"Projektübersicht für die Budgetprüfung" -> ["projekt","bersicht","die","budgetpr","fung"]
"résumé des décisions sur le budget" -> ["sum","des","cisions","sur","le","budget"]
"项目期限和预算审查" -> []
What we did locally (offered as a starting point, not a demand). For Russian only: a run of Cyrillic letters is taken out of the text and stemmed with a small Russian stemmer, and every other token goes through the shipped path, so English output stays byte-identical. A Russian stop-word list mirrors QUERY_GLUE, containsTerm compares stems when the term is Cyrillic, and INDEX_VERSION is bumped so the index rebuilds. After it the Russian query above yields ["срок","проект","обзор","бюджет"] and the English one is unchanged. This does nothing for German, French or Chinese.
Question. Would you take Unicode-aware tokenization upstream, for example \p{L}\p{N} in tokenize and in the containsTerm boundary, with stemming and stop words per language as an optional extension point? Or do you prefer a different mechanism? Related English-only reports: #2236, #2634, #2920, #2937.
Details
- Kind: bug
- LifeOS version: 8.761.0
Filed with the LifeOS /feedback command. The reporter approved this exact text before it was sent; paths, secrets and personal details were stripped locally.
One thing worth flagging before settling on \p{L}\p{N}: it fixes the Cyrillic query and the accented-Latin fragments, but not the 项目期限和预算审查 case. CJK (and Japanese, Thai) has no spaces, so the whole query becomes a single 9-character token that only matches a note containing that exact string. Retrieval stays silently empty for those users.
Two dependency-free options that cover all scripts:
1. Intl.Segmenter (built into Bun/JSC and V8, ICU-backed):
const seg = new Intl.Segmenter(undefined, { granularity: "word" });
const tokenize = (s: string) =>
[...seg.segment(s.normalize("NFKC").toLowerCase())]
.filter((x) => x.isWordLike)
.map((x) => x.segment);
The Chinese query comes out roughly as 项目 / 期限 / 和 / 预算 / 审查, and Projektübersicht stays one word. ICU's dictionaries change between engine versions, so it's worth folding the runtime version into INDEX_VERSION.
2. CJK bigrams (what Lucene's CJKAnalyzer does): split on /[^\p{L}\p{N}]+/u, then expand any run of \p{Script=Han}, Hiragana or Katakana into overlapping 2-grams. It's deterministic across runtimes and needs no dictionary. Recall is a bit noisier.
Two smaller things that bite with either option:
containsTerm's boundary can't simply become\b. In JS\bis ASCII-only even with theuflag. Use(?<![\p{L}\p{N}])term(?![\p{L}\p{N}])withu.- Apply NFKC before lowercasing. Otherwise a decomposed
e + U+0301and a precomposedéindex as different terms.
Your Russian stemmer would slot in after either option as the per-language extension point. Keeping segmentation and stemming as separate stages means the next language (German compound splitting, for example) becomes a plugin instead of another tokenize rewrite. A cheap regression guard is a test asserting that a non-empty query in each supported script yields at least one term. That turns this whole class of silent failure into a red test.
Notes from working on cross-session memory retrieval in SwarmAI. Discussion: Memory Four-Layer Architecture
[!NOTE] 🤖 Kai (AI assistant) responding on behalf of Daniel Miessler
Thanks for reporting this. It's fixed in our tree, in a commit that landed after the 8.761.0 cut, so it ships in the next public release. Closing for now; reopen if you still see it on that release.
Fix: 8e28decb7a, 22f62657a2 (Unicode tokenizer). CJK word segmentation is a separate, larger change we have noted.