mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[GH-ISSUE #2526] [Issue]: Lorebook keyword scan re-scans full chat history every turn (O(history × entries)) #1253
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#1253
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 @Romuromylus on GitHub (Jun 9, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2526
Originally assigned to: @Romuromylus on GitHub.
Summary
Severity: Medium — performance. On every generation turn, the lorebook keyword scanner concatenates the entire chat history into one string and runs a keyword test over it once per active entry, synchronously on the Node event loop before the model call. Cost is
O(total history chars × active entry count)and grows linearly with both chat length and lorebook size, because the global scan depth is0("scan all messages") and the per-chat context limit is off by default.Expected behavior
The lorebook keyword scan window should be bounded independently of the prompt context window — scan only the last N messages (a SillyTavern-style global scan depth) so per-turn cost is
O(scanDepth × entryCount), constant in chat length. Keys typically appear in recent turns, and per-entry scan-depth overrides plus constant/sticky entries already exist, so a sane default would not change activation for the common case.Actual behavior
packages/server/src/services/lorebook/keyword-scanner.ts:410-411builds the scan text from the full history whenscanDepthis 0, and the per-entry loop (:426-496) tests each non-constant entry against it:The driver passes
scanDepth: 0(packages/server/src/services/lorebook/index.ts:844-845,// Scan all messages), andcontextMessageLimitis off by default (generate.routes.ts:2017-2019), so the scan covers the entire conversation.processLorebooksruns per turn (generate.routes.ts:3617/3673/4896). TheMAX_LOREBOOK_ENTRIEScap (100) bounds budget selection, not the keyword scan, and there is no memoization of the activated set across swipes/regenerates — so a long roleplay with a large worldbook re-runs ~100 full-history substring/regex scans every turn, blocking the event loop and stalling other users on the shared server.Steps to reproduce
contextMessageLimitset.Environment
staging @ a9fdae8)Logs, screenshots, or video
Static source audit, cited to file:line above.
Additional context
Fix: give the global scan a sane default depth (e.g. 2–10) at
index.ts:844-845instead ofscanDepth: 0, rather than relying on the off-by-defaultcontextMessageLimit. Per-entryscanDepthoverrides already exist (keyword-scanner.ts:457-463) and constant/sticky entries bypass keyword matching, so a default depth bounds per-turn cost toO(scanDepth × entryCount)without changing common-case activation. Alternatively, memoize the activated-entry set per(latest-message-id, entry-set-hash)so unchanged history is not re-scanned on every regenerate/swipe.Regression notes: found via static audit;
keyword-scanner.tsis byte-identical onmain(65a0395) and thescanDepth: 0driver/default-offcontextMessageLimitare the same on both branches, so this is shared baseline behavior, not amain→stagingregression.Template check
Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked: