[PR #2542] [MERGED] perf(lorebook): bound the default per-turn keyword-scan window #5645

Closed
opened 2026-10-22 04:18:05 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/2542
Author: @Romuromylus
Created: 6/9/2026
Status: ✅ Merged
Merged: 6/9/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/lorebook-scan-depth-cap


📝 Commits (2)

  • 2c98c1d perf(lorebook): bound the default per-turn keyword-scan window
  • 491c85a Merge branch 'staging' into fix/lorebook-scan-depth-cap

📊 Changes

3 files changed (+24 additions, -9 deletions)

View changed files

📝 packages/server/src/services/lorebook/index.ts (+6 -2)
📝 packages/server/src/services/lorebook/keyword-scanner.ts (+12 -7)
📝 packages/shared/src/constants/defaults.ts (+6 -0)

📄 Description

Linked issue

Closes #2526

Why this change

The generate-path lorebook scan set scanOpts.scanDepth = 0 ("scan all messages"). In keyword-scanner.ts, an entry whose effective scanDepth resolved to null fell back to combinedText, which — with the global scanDepth of 0 — was the entire chat history. So each turn re-scanned the full transcript for those entries (O(history × entries)), and the cost grew with transcript length.

The common case is already bounded: a lorebook's scanDepth DB-defaults to 2 and applyLorebookDefaults propagates it to null-scanDepth entries before the scan, so those slice to the last 2 messages. The unbounded full-history scan only fires for an entry whose effective depth is genuinely unset (null) or an explicit 0.

What changed

Maintainer note / wants your eyes: this touches user-facing scan semantics, so I kept it deliberately conservative. Two things to sanity-check: (1) the default value 10, and (2) the rule below that an explicit scanDepth of 0 is preserved as "scan all". Happy to adjust the constant or the policy.

  • Added LIMITS.LOREBOOK_DEFAULT_SCAN_DEPTH = 10 (packages/shared/src/constants/defaults.ts).
  • packages/server/src/services/lorebook/index.ts: the generate-path scanOpts.scanDepth now uses LIMITS.LOREBOOK_DEFAULT_SCAN_DEPTH instead of 0, so an entry that leaves scanDepth unset caps its scan to the last 10 messages instead of the whole history.
  • packages/server/src/services/lorebook/keyword-scanner.ts: the per-entry scan-window selection now distinguishes the three cases explicitly:
    • scanDepth === 0 → scan full history (a deliberate "scan all" choice, preserved),
    • scanDepth > 0 → scan that many recent messages (unchanged),
    • scanDepth === null → inherit the now-bounded global default.

So a user who intentionally sets a lorebook/entry to scanDepth 0 is unaffected; only the genuinely-unset fallback is bounded. The knowledge-router scan path passes no scanDepth and keeps the scanner's own default, so it is unchanged.

Not a regression: byte-identical on main; long-standing shared-code perf characteristic.

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Manual verification notes

pnpm check passes locally. Added a local-only node:test (scan-depth.test.ts, gitignored) driving scanForActivatedEntries directly with a keyword present only in the oldest of 15 messages and the new bounded global default:

  • null entry scanDepth → keyword 10+ messages back is not scanned (the fix),
  • explicit entry scanDepth 0 → still reaches the oldest message (deliberate scan-all preserved),
  • entry scanDepth larger than history → scans everything (override honored),
  • a keyword inside the bounded window still activates (negative control).

Local run: 4/4 pass.

Still to verify manually against the deploy:

  • A normal chat where recent-turn lorebook entries still activate as before.
  • A lorebook/entry explicitly set to scanDepth 0 still matches keywords far back in history.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Version/release files updated (only if this PR includes a version bump)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/2542 **Author:** [@Romuromylus](https://github.com/Romuromylus) **Created:** 6/9/2026 **Status:** ✅ Merged **Merged:** 6/9/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/lorebook-scan-depth-cap` --- ### 📝 Commits (2) - [`2c98c1d`](https://github.com/Pasta-Devs/Marinara-Engine/commit/2c98c1d36c89119b58ae674f9e9b7eb9bacb4d2a) perf(lorebook): bound the default per-turn keyword-scan window - [`491c85a`](https://github.com/Pasta-Devs/Marinara-Engine/commit/491c85ae8db529784e349261874e69e059a4c864) Merge branch 'staging' into fix/lorebook-scan-depth-cap ### 📊 Changes **3 files changed** (+24 additions, -9 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/services/lorebook/index.ts` (+6 -2) 📝 `packages/server/src/services/lorebook/keyword-scanner.ts` (+12 -7) 📝 `packages/shared/src/constants/defaults.ts` (+6 -0) </details> ### 📄 Description ## Linked issue Closes #2526 ## Why this change The generate-path lorebook scan set `scanOpts.scanDepth = 0` ("scan all messages"). In `keyword-scanner.ts`, an entry whose effective `scanDepth` resolved to `null` fell back to `combinedText`, which — with the global `scanDepth` of 0 — was the **entire** chat history. So each turn re-scanned the full transcript for those entries (O(history × entries)), and the cost grew with transcript length. The common case is already bounded: a lorebook's `scanDepth` DB-defaults to 2 and `applyLorebookDefaults` propagates it to null-`scanDepth` entries before the scan, so those slice to the last 2 messages. The unbounded full-history scan only fires for an entry whose effective depth is genuinely unset (`null`) **or** an explicit `0`. ## What changed > **Maintainer note / wants your eyes:** this touches user-facing scan semantics, so I kept it deliberately conservative. Two things to sanity-check: (1) the default value `10`, and (2) the rule below that an explicit `scanDepth` of `0` is preserved as "scan all". Happy to adjust the constant or the policy. - Added `LIMITS.LOREBOOK_DEFAULT_SCAN_DEPTH = 10` (`packages/shared/src/constants/defaults.ts`). - `packages/server/src/services/lorebook/index.ts`: the generate-path `scanOpts.scanDepth` now uses `LIMITS.LOREBOOK_DEFAULT_SCAN_DEPTH` instead of `0`, so an entry that leaves `scanDepth` unset caps its scan to the last 10 messages instead of the whole history. - `packages/server/src/services/lorebook/keyword-scanner.ts`: the per-entry scan-window selection now distinguishes the three cases explicitly: - `scanDepth === 0` → **scan full history** (a deliberate "scan all" choice, preserved), - `scanDepth > 0` → scan that many recent messages (unchanged), - `scanDepth === null` → inherit the now-bounded global default. So a user who intentionally sets a lorebook/entry to `scanDepth 0` is unaffected; only the genuinely-unset fallback is bounded. The knowledge-router scan path passes no `scanDepth` and keeps the scanner's own default, so it is unchanged. Not a regression: byte-identical on `main`; long-standing shared-code perf characteristic. ## Validation - [x] `pnpm check` passes locally - [x] Container (Docker / Podman) built and ran without issue - [x] Ran the app, clicked through the changes manually - [x] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [x] Above manual verification completed (describe below) - [x] Read and followed `CONTRIBUTING.md` ### Manual verification notes `pnpm check` passes locally. Added a local-only node:test (`scan-depth.test.ts`, gitignored) driving `scanForActivatedEntries` directly with a keyword present only in the oldest of 15 messages and the new bounded global default: - null entry `scanDepth` → keyword 10+ messages back is **not** scanned (the fix), - explicit entry `scanDepth 0` → still reaches the oldest message (deliberate scan-all preserved), - entry `scanDepth` larger than history → scans everything (override honored), - a keyword inside the bounded window still activates (negative control). Local run: **4/4 pass**. Still to verify manually against the deploy: - A normal chat where recent-turn lorebook entries still activate as before. - A lorebook/entry explicitly set to `scanDepth 0` still matches keywords far back in history. ## Docs and release impact - [x] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Version/release files updated (only if this PR includes a version bump) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:18:05 +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#5645
No description provided.