[GH-ISSUE #2035] Lorebook activation scans re-read entries once per active book #997

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

Originally created by @Xelvanis on GitHub (Jun 3, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2035

Originally assigned to: @cha1latte on GitHub.

Problem

Lorebook activation reads currently scan the entry collection once per active lorebook.

Current refactor evidence on origin/refactor:

  • src/engine/generation/active-lorebook-scanner.ts:525 lists all lorebooks, then filters active/scoped books.
  • src/engine/generation/active-lorebook-scanner.ts:558-560 maps each active lorebook through loadLorebookEntriesForActivation.
  • src/engine/generation/active-lorebook-scanner.ts:419-426 calls storage.listLorebookEntries(id) for one lorebook at a time.
  • src/shared/api/storage-api.ts implements listLorebookEntries as a filtered storage.list("lorebook-entries", { filters: { lorebookId } }), so each active book re-enters the generic list/filter path.
  • src-tauri/crates/storage/src/lib.rs:100 exposes list_where; on uncached file storage this scans the collection file.

Legacy had a batched shape for this class of work:

  • origin/main:packages/server/src/services/storage/lorebooks.storage.ts:321-328 lists entries by multiple lorebook IDs in one query.
  • origin/main:packages/server/src/services/storage/lorebooks.storage.ts:409-411 reads enabled entries for active books with one inArray(lorebookId, bookIds) query.

Scratch large-profile timing on the current Target 2 batch 4 base, using synthetic JSON collections:

  • One-book filtered entry read over 5,000 entries, returning 125 rows: 215.76ms cold / 20.22ms warm, 1,311,385 payload bytes.
  • With many active lorebooks, the current per-book loop multiplies that collection scan cost.

This affects generation prompt assembly, active lorebook info, built-in lorebook search, and any runtime path that scans active lore.

Expected

Activation should read eligible entries for all active lorebooks in one storage call, then group in memory. The cost should scale with one collection scan, not one scan per active book.

Suggested Fix

  • Add a batched storage gateway method such as listLorebookEntriesByLorebookIds(ids) using the existing projected/where-in backend shape.
  • Load folders for active lorebooks in a batched way too, or otherwise avoid one file scan per book.
  • Reuse the batched path in activation, active-info scans, tool lorebook search, and related runtime scans.
  • Add cold/warm proof with many active lorebooks and thousands of entries.
Originally created by @Xelvanis on GitHub (Jun 3, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2035 Originally assigned to: @cha1latte on GitHub. ## Problem Lorebook activation reads currently scan the entry collection once per active lorebook. Current refactor evidence on `origin/refactor`: - `src/engine/generation/active-lorebook-scanner.ts:525` lists all lorebooks, then filters active/scoped books. - `src/engine/generation/active-lorebook-scanner.ts:558-560` maps each active lorebook through `loadLorebookEntriesForActivation`. - `src/engine/generation/active-lorebook-scanner.ts:419-426` calls `storage.listLorebookEntries(id)` for one lorebook at a time. - `src/shared/api/storage-api.ts` implements `listLorebookEntries` as a filtered `storage.list("lorebook-entries", { filters: { lorebookId } })`, so each active book re-enters the generic list/filter path. - `src-tauri/crates/storage/src/lib.rs:100` exposes `list_where`; on uncached file storage this scans the collection file. Legacy had a batched shape for this class of work: - `origin/main:packages/server/src/services/storage/lorebooks.storage.ts:321-328` lists entries by multiple lorebook IDs in one query. - `origin/main:packages/server/src/services/storage/lorebooks.storage.ts:409-411` reads enabled entries for active books with one `inArray(lorebookId, bookIds)` query. Scratch large-profile timing on the current Target 2 batch 4 base, using synthetic JSON collections: - One-book filtered entry read over 5,000 entries, returning 125 rows: `215.76ms` cold / `20.22ms` warm, `1,311,385` payload bytes. - With many active lorebooks, the current per-book loop multiplies that collection scan cost. This affects generation prompt assembly, active lorebook info, built-in lorebook search, and any runtime path that scans active lore. ## Expected Activation should read eligible entries for all active lorebooks in one storage call, then group in memory. The cost should scale with one collection scan, not one scan per active book. ## Suggested Fix - Add a batched storage gateway method such as `listLorebookEntriesByLorebookIds(ids)` using the existing projected/where-in backend shape. - Load folders for active lorebooks in a batched way too, or otherwise avoid one file scan per book. - Reuse the batched path in activation, active-info scans, tool lorebook search, and related runtime scans. - Add cold/warm proof with many active lorebooks and thousands of entries.
bsakaguchi 2026-10-21 05:24:58 +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#997
No description provided.