mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[GH-ISSUE #2979] [Issue]: Whitespace-only / newline-only lorebook keyword matches almost any text, activating the entry every turn #1554
Labels
No labels
android
bug
bugfix
ci
client
code freeze pause
container
dependencies
docs
documentation
duplicate
enhancement
feature
feature
feature
feature-creep
fixed-in-refactor
fixed-in-staging
good first issue
help wanted
in-pr
in-pr
needs-codeowner-decision
normal-scope
old-codebase
old-codebase
pull-request
qol
question
refactor
regression
revisit later
roadmap
scope-risk
scope-risk
server
server
shared
soup
upstream
windows
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Pasta-Devs/Marinara-Engine#1554
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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\nlikewise 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)
keys: [" "](a single space), default matching options. Example:POST /api/lorebooks/:id/entrieswith{"keys":[" "]}.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 onlyif (!keyword) return false;, which rejects "" but lets" "/"\n"fall through toliteralMatch→haystack.includes(needle)(:25-29).packages/server/src/services/lorebook/keyword-scanner.ts:511(testPrimaryKeys).packages/server/src/services/storage/lorebooks.storage.ts:138-147(parseStringArrayuses.filter(Boolean), which drops "" but keeps " ").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.
@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.