[GH-ISSUE #5513] Every prompt build scans the whole lorebook_entries table for the {{lorebooksize}} macro, used or not #3064

Closed
opened 2026-10-21 05:59:18 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @kolacheee on GitHub (Aug 25, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/5513

Originally assigned to: @kolacheee on GitHub.

Found while auditing the #5506 commit window for memory-pressure amplifiers.

Since 34ecca6e9 (#5454, the {{lorebooksize::ID}} macro), buildPromptMacroContext unconditionally runs lorebooks.countAllEntriesByLorebook() on every prompt build (packages/server/src/services/prompt/macro-context.ts:658-665 on staging). That helper materializes one row object per entry across all lorebooks (packages/server/src/services/storage/lorebooks.storage.ts:624-632 — a full lorebook_entries select on the file-backed store), and the context is built for main generations, retries, and each agent prompt, so the scan runs several times per user message.

Almost no prompt uses the macro, but every generation pays the full-table allocation churn. On large mobile profiles that already sit near the Node heap ceiling (see #5506), this is avoidable GC pressure at exactly the worst moment.

Fix shape: make the count lazy — only call countAllEntriesByLorebook() when the assembled template actually contains lorebooksize (same pattern as other conditionally-computed macro inputs).

Originally created by @kolacheee on GitHub (Aug 25, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/5513 Originally assigned to: @kolacheee on GitHub. Found while auditing the #5506 commit window for memory-pressure amplifiers. Since `34ecca6e9` (#5454, the `{{lorebooksize::ID}}` macro), `buildPromptMacroContext` unconditionally runs `lorebooks.countAllEntriesByLorebook()` on every prompt build (`packages/server/src/services/prompt/macro-context.ts:658-665` on staging). That helper materializes one row object per entry across **all** lorebooks (`packages/server/src/services/storage/lorebooks.storage.ts:624-632` — a full `lorebook_entries` select on the file-backed store), and the context is built for main generations, retries, and each agent prompt, so the scan runs several times per user message. Almost no prompt uses the macro, but every generation pays the full-table allocation churn. On large mobile profiles that already sit near the Node heap ceiling (see #5506), this is avoidable GC pressure at exactly the worst moment. Fix shape: make the count lazy — only call `countAllEntriesByLorebook()` when the assembled template actually contains `lorebooksize` (same pattern as other conditionally-computed macro inputs).
bsakaguchi 2026-10-21 05:59:18 +07:00
Author
Owner

@SpicyMarinara commented on GitHub (Aug 25, 2026):

@kolacheee I am taking the minimal implementation into one release-hardening PR against staging now, using the fix boundaries from your audit. I will keep your diagnosis credited in the PR and avoid expanding scope beyond the four verified paths.

<!-- gh-comment-id:5409720256 --> @SpicyMarinara commented on GitHub (Aug 25, 2026): @kolacheee I am taking the minimal implementation into one release-hardening PR against staging now, using the fix boundaries from your audit. I will keep your diagnosis credited in the PR and avoid expanding scope beyond the four verified paths.
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#3064
No description provided.