[GH-ISSUE #2973] [Issue]: Whole-word keyword matching silently fails for keywords starting/ending with a non-ASCII letter (accented names, CJK) #1549

Closed
opened 2026-10-21 05:38:53 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @kolacheee on GitHub (Jun 29, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2973

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong

When a lorebook entry has "Match whole words" enabled and its keyword starts or ends with a non-ASCII word character — accented Latin like "café", "José", "Zoë", "élan", or CJK like "北京" — the entry never activates, even when the keyword appears verbatim in the chat. World-info entries for non-English character and place names silently fail. The same keyword activates fine with whole-words off, and interior accents (e.g. "Müller") still match.

How to reproduce (try it yourself)

  1. Create a lorebook entry with key "café" (or "Zoë", "José", "北京"), any content, and toggle on "Match whole words".
  2. Send a chat message containing the word verbatim, e.g. "let's meet at the café".
  3. Trigger a generation.

You'll see: the entry does not activate. (Directly: testKeyword("café", "a café here", { matchWholeWords: true }) === false; same for "Zoë", "José", "北京", "élan".)
Expected: the entry activates, as it does with "Match whole words" off, and as it does for interior accents like "Müller".

Where it is

  • packages/shared/src/utils/lorebook-keyword-matching.ts:49-56 — the matchWholeWords branch of testKeyword builds new RegExp("\\b" + escaped + "\\b", flags).
  • Reached from packages/server/src/services/lorebook/keyword-scanner.ts:511 (testPrimaryKeys) and :518 (testSecondaryKeys), with matchOptions built at :503-508.

What's happening

JavaScript's \b boundary is defined only against the ASCII word class [A-Za-z0-9_]. Accented Latin, CJK, Cyrillic, etc. count as non-word characters. So when a keyword ends in such a character, the trailing \b sits between two non-word characters (e.g. "é" and a space) and never matches; same for a leading non-ASCII letter against the opening \b. Keywords with a non-ASCII char only in the interior still match because the boundaries fall on the ASCII first/last letters.

Suggested fix

Stop relying on ASCII \b. Build the pattern with the Unicode flag and lookaround boundaries against a Unicode letter/number class:

new RegExp("(?<![\\p{L}\\p{N}_])" + escaped + "(?![\\p{L}\\p{N}_])", caseSensitive ? "u" : "iu")

This util also runs client-side (the in-editor keyword-test preview), so confirm the lookbehind target is acceptable — lookbehind and \p{...} are supported in all current evergreen browsers and Node 18+. If support for very old browsers is required, an equivalent without lookbehind (capture the preceding char and assert it's a boundary, or test the match index against a Unicode boundary manually) achieves the same result. Add tests for café/José/Zoë/北京/élan with matchWholeWords enabled.


AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.

Originally created by @kolacheee on GitHub (Jun 29, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2973 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** When a lorebook entry has "Match whole words" enabled and its keyword **starts or ends with a non-ASCII word character** — accented Latin like "café", "José", "Zoë", "élan", or CJK like "北京" — the entry never activates, even when the keyword appears verbatim in the chat. World-info entries for non-English character and place names silently fail. The same keyword activates fine with whole-words off, and interior accents (e.g. "Müller") still match. **How to reproduce (try it yourself)** 1. Create a lorebook entry with key "café" (or "Zoë", "José", "北京"), any content, and toggle **on** "Match whole words". 2. Send a chat message containing the word verbatim, e.g. "let's meet at the café". 3. Trigger a generation. **You'll see:** the entry does **not** activate. (Directly: `testKeyword("café", "a café here", { matchWholeWords: true }) === false`; same for "Zoë", "José", "北京", "élan".) **Expected:** the entry activates, as it does with "Match whole words" off, and as it does for interior accents like "Müller". **Where it is** - `packages/shared/src/utils/lorebook-keyword-matching.ts:49-56` — the `matchWholeWords` branch of `testKeyword` builds `new RegExp("\\b" + escaped + "\\b", flags)`. - Reached from `packages/server/src/services/lorebook/keyword-scanner.ts:511` (`testPrimaryKeys`) and `:518` (`testSecondaryKeys`), with `matchOptions` built at `:503-508`. **What's happening** JavaScript's `\b` boundary is defined only against the ASCII word class `[A-Za-z0-9_]`. Accented Latin, CJK, Cyrillic, etc. count as **non-word** characters. So when a keyword ends in such a character, the trailing `\b` sits between two non-word characters (e.g. "é" and a space) and never matches; same for a leading non-ASCII letter against the opening `\b`. Keywords with a non-ASCII char only in the interior still match because the boundaries fall on the ASCII first/last letters. **Suggested fix** Stop relying on ASCII `\b`. Build the pattern with the Unicode flag and lookaround boundaries against a Unicode letter/number class: ``` new RegExp("(?<![\\p{L}\\p{N}_])" + escaped + "(?![\\p{L}\\p{N}_])", caseSensitive ? "u" : "iu") ``` This util also runs client-side (the in-editor keyword-test preview), so confirm the lookbehind target is acceptable — lookbehind and `\p{...}` are supported in all current evergreen browsers and Node 18+. If support for very old browsers is required, an equivalent without lookbehind (capture the preceding char and assert it's a boundary, or test the match index against a Unicode boundary manually) achieves the same result. Add tests for café/José/Zoë/北京/élan with `matchWholeWords` enabled. --- *AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.*
Author
Owner

@SpicyMarinara commented on GitHub (Jun 29, 2026):

Fixed in the v2.0.7 branch: whole-word lorebook keyword matching now uses Unicode-aware word-character boundaries and trims blank keywords before matching. Validated with pnpm check and pnpm regression:prompt.

<!-- gh-comment-id:4831160184 --> @SpicyMarinara commented on GitHub (Jun 29, 2026): Fixed in the v2.0.7 branch: whole-word lorebook keyword matching now uses Unicode-aware word-character boundaries and trims blank keywords before matching. Validated with pnpm check and pnpm regression:prompt.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#1549
No description provided.