[GH-ISSUE #2526] [Issue]: Lorebook keyword scan re-scans full chat history every turn (O(history × entries)) #1253

Closed
opened 2026-10-21 05:30:00 +07:00 by bsakaguchi · 0 comments
Owner

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 is 0 ("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-411 builds the scan text from the full history when scanDepth is 0, and the per-entry loop (:426-496) tests each non-constant entry against it:

const messagesToScan = scanDepth > 0 ? messages.slice(-scanDepth) : messages;
const combinedText = messagesToScan.map((m) => m.content).join("\n");

The driver passes scanDepth: 0 (packages/server/src/services/lorebook/index.ts:844-845, // Scan all messages), and contextMessageLimit is off by default (generate.routes.ts:2017-2019), so the scan covers the entire conversation. processLorebooks runs per turn (generate.routes.ts:3617/3673/4896). The MAX_LOREBOOK_ENTRIES cap (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

  1. Open a long chat (e.g. a months-old roleplay with thousands of messages) that has many active keyword lorebook entries and no contextMessageLimit set.
  2. Send a message / regenerate.
  3. Each turn re-scans the full history against every active entry before the model call; the per-turn cost grows as the chat lengthens.

Environment

  • Marinara Engine version: 1.6.1 (staging @ a9fdae8)
  • Install type: source
  • OS + version: n/a — static source audit
  • Browser or app shell: n/a

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-845 instead of scanDepth: 0, rather than relying on the off-by-default contextMessageLimit. Per-entry scanDepth overrides already exist (keyword-scanner.ts:457-463) and constant/sticky entries bypass keyword matching, so a default depth bounds per-turn cost to O(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.ts is byte-identical on main (65a0395) and the scanDepth: 0 driver/default-off contextMessageLimit are the same on both branches, so this is shared baseline behavior, not a main→staging regression.

Template check

Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked:

  • I DID NOT read this template and provide the requested details.
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 is `0` ("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-411` builds the scan text from the full history when `scanDepth` is 0, and the per-entry loop (`:426-496`) tests each non-constant entry against it: ```ts const messagesToScan = scanDepth > 0 ? messages.slice(-scanDepth) : messages; const combinedText = messagesToScan.map((m) => m.content).join("\n"); ``` The driver passes `scanDepth: 0` (`packages/server/src/services/lorebook/index.ts:844-845`, `// Scan all messages`), and `contextMessageLimit` is off by default (`generate.routes.ts:2017-2019`), so the scan covers the entire conversation. `processLorebooks` runs per turn (`generate.routes.ts:3617/3673/4896`). The `MAX_LOREBOOK_ENTRIES` cap (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 1. Open a long chat (e.g. a months-old roleplay with thousands of messages) that has many active keyword lorebook entries and no `contextMessageLimit` set. 2. Send a message / regenerate. 3. Each turn re-scans the full history against every active entry before the model call; the per-turn cost grows as the chat lengthens. ## Environment - Marinara Engine version: 1.6.1 (`staging @ a9fdae8`) - Install type: source - OS + version: n/a — static source audit - Browser or app shell: n/a ## 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-845` instead of `scanDepth: 0`, rather than relying on the off-by-default `contextMessageLimit`. Per-entry `scanDepth` overrides already exist (`keyword-scanner.ts:457-463`) and constant/sticky entries bypass keyword matching, so a default depth bounds per-turn cost to `O(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.ts` is byte-identical on `main` (`65a0395`) and the `scanDepth: 0` driver/default-off `contextMessageLimit` are the same on both branches, so this is shared baseline behavior, not a `main`→`staging` regression. ## Template check Please **uncheck (untick)** the box below before submitting so we know you read the template. It is intentionally pre-checked: - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:30:00 +07:00
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#1253
No description provided.