[GH-ISSUE #2979] [Issue]: Whitespace-only / newline-only lorebook keyword matches almost any text, activating the entry every turn #1554

Closed
opened 2026-10-21 05:39:00 +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/2979

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong

A lorebook entry whose primary key is a single space " " (or any whitespace-only string, including a literal newline) matches virtually any multi-word chat text, so the entry activates on essentially every generation. testKeyword() only rejects the empty string, not whitespace-only keys, and keys are never trimmed/filtered on save. Because the scanner joins messages with \n, a key of \n likewise matches any multi-message scan window. Note: the in-app KeysEditor trims and rejects empty/whitespace input, so this is reachable via the API and JSON import paths rather than the normal editor.

How to reproduce (try it yourself)

  1. Via the API (or a JSON import), create an entry with keys: [" "] (a single space), default matching options. Example: POST /api/lorebooks/:id/entries with {"keys":[" "]}.
  2. Send any normal multi-word message like "hello world".
  3. Trigger a generation.

You'll see: the entry activates because the substring " " is present. (Directly: testKeyword(" ", "hello world", { useRegex:false, matchWholeWords:false, caseSensitive:false }) === true; testKeyword("\n", "line1\nline2", ...) === true.)
Expected: a whitespace-only key is treated as a non-key and does not activate the entry.

Where it is

  • packages/shared/src/utils/lorebook-keyword-matching.ts:33 — the guard is only if (!keyword) return false;, which rejects "" but lets " " / "\n" fall through to literalMatch → haystack.includes(needle) (:25-29).
  • Key passed unmodified at packages/server/src/services/lorebook/keyword-scanner.ts:511 (testPrimaryKeys).
  • Persistence keeps whitespace keys: packages/server/src/services/storage/lorebooks.storage.ts:138-147 (parseStringArray uses .filter(Boolean), which drops "" but keeps " ").
  • Schema accepts arbitrary strings: packages/shared/src/schemas/lorebook.schema.ts:161 (z.array(z.string()).default([])).

What's happening

testKeyword's only guard rejects "" but treats any whitespace-only string as a real needle and falls through to a substring match. Since whitespace is ubiquitous in chat text (and messages are joined with \n), a whitespace key matches almost unconditionally. Nothing upstream trims or drops blank/whitespace keys before scanning.

Suggested fix

Treat whitespace-only keys as non-keys: change the guard to if (!keyword.trim()) return false, and/or filter out empty/whitespace entries when persisting keys/secondaryKeys (e.g. .map((k) => k.trim()).filter(Boolean) in the create/update entry path). Either change alone prevents the spurious always-on activation, and neither affects real keys (a key with content plus incidental spaces still trims to a non-empty needle).


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/2979 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** A lorebook entry whose primary key is a single space `" "` (or any whitespace-only string, including a literal newline) matches virtually any multi-word chat text, so the entry activates on essentially every generation. `testKeyword()` only rejects the empty string, not whitespace-only keys, and keys are never trimmed/filtered on save. Because the scanner joins messages with `\n`, a key of `\n` likewise matches any multi-message scan window. Note: the in-app KeysEditor trims and rejects empty/whitespace input, so this is reachable via the API and JSON import paths rather than the normal editor. **How to reproduce (try it yourself)** 1. Via the API (or a JSON import), create an entry with `keys: [" "]` (a single space), default matching options. Example: `POST /api/lorebooks/:id/entries` with `{"keys":[" "]}`. 2. Send any normal multi-word message like "hello world". 3. Trigger a generation. **You'll see:** the entry activates because the substring `" "` is present. (Directly: `testKeyword(" ", "hello world", { useRegex:false, matchWholeWords:false, caseSensitive:false }) === true`; `testKeyword("\n", "line1\nline2", ...) === true`.) **Expected:** a whitespace-only key is treated as a non-key and does not activate the entry. **Where it is** - `packages/shared/src/utils/lorebook-keyword-matching.ts:33` — the guard is only `if (!keyword) return false;`, which rejects "" but lets `" "` / `"\n"` fall through to `literalMatch` → `haystack.includes(needle)` (`:25-29`). - Key passed unmodified at `packages/server/src/services/lorebook/keyword-scanner.ts:511` (`testPrimaryKeys`). - Persistence keeps whitespace keys: `packages/server/src/services/storage/lorebooks.storage.ts:138-147` (`parseStringArray` uses `.filter(Boolean)`, which drops "" but keeps " "). - Schema accepts arbitrary strings: `packages/shared/src/schemas/lorebook.schema.ts:161` (`z.array(z.string()).default([])`). **What's happening** `testKeyword`'s only guard rejects "" but treats any whitespace-only string as a real needle and falls through to a substring match. Since whitespace is ubiquitous in chat text (and messages are joined with `\n`), a whitespace key matches almost unconditionally. Nothing upstream trims or drops blank/whitespace keys before scanning. **Suggested fix** Treat whitespace-only keys as non-keys: change the guard to `if (!keyword.trim()) return false`, and/or filter out empty/whitespace entries when persisting keys/secondaryKeys (e.g. `.map((k) => k.trim()).filter(Boolean)` in the create/update entry path). Either change alone prevents the spurious always-on activation, and neither affects real keys (a key with content plus incidental spaces still trims to a non-empty needle). --- *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: stored keyword arrays are trimmed on read and blank keywords are rejected by the shared matcher, so whitespace-only keys cannot activate every scan. Validated with pnpm check and pnpm regression:prompt.

<!-- gh-comment-id:4831163685 --> @SpicyMarinara commented on GitHub (Jun 29, 2026): Fixed in the v2.0.7 branch: stored keyword arrays are trimmed on read and blank keywords are rejected by the shared matcher, so whitespace-only keys cannot activate every scan. 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#1554
No description provided.