[GH-ISSUE #3013] [Issue]: save_lorebook_entry caps keys to 24 BEFORE de-duplicating, silently dropping valid distinct keys #1585

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

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

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong

normalizeLorebookEntryKeys trims/filters the raw keys array, then slice(0, MAX_LOREBOOK_ENTRY_KEYS=24), and ONLY THEN de-duplicates with Array.from(new Set(...)). Because the cap is applied before dedup, leading duplicate keys consume slots in the cap window and push distinct, valid keys past index 24 out of the result entirely. The model can supply 24+ unique trigger keys yet end up with far fewer — or even a single key — when duplicates appear early in the array. Trigger keys silently missing from a lorebook entry means the entry will not activate on those keywords.

How to reproduce (try it yourself)

  1. Call save_lorebook_entry({ name:"X", content:"...", keys:["a","a","a", ...22 more "a"..., "important_key", "another_key"] }) so the 24-element slice window is filled entirely by the duplicate "a" before reaching the distinct keys.

You'll see: the result entry's keys = ["a"] (after slice-then-dedup); important_key and another_key are dropped, even though there were only 3 distinct keys total.

Expected: up to 24 DISTINCT keys are retained regardless of duplicate ordering, so all 3 distinct keys are kept.

Where it is

packages/server/src/services/tools/tool-executor.ts:625-629 (normalizeLorebookEntryKeys), used at :665 in saveLorebookEntry; MAX_LOREBOOK_ENTRY_KEYS = 24 at line 72.

What's happening

De-duplication is performed after the cap (.slice(0, MAX_LOREBOOK_ENTRY_KEYS)) instead of before it, so duplicates count against the limit and can crowd out distinct keys.

Suggested fix

Move de-duplication ahead of the cap while keeping the existing non-string handling and the fallback-to-name branch:

function normalizeLorebookEntryKeys(value: unknown, fallbackName: string): string[] {
  const raw = Array.isArray(value) ? value : [];
  const keys = Array.from(
    new Set(
      raw
        .map((entry) => (typeof entry === "string" ? entry.trim() : ""))
        .filter((entry) => entry.length > 0),
    ),
  ).slice(0, MAX_LOREBOOK_ENTRY_KEYS);
  if (keys.length > 0) return keys;
  return fallbackName ? [fallbackName] : [];
}

This dedups distinct trimmed keys first, then caps to 24, so up to 24 distinct keys are always retained.

Regression avoided: the original one-line suggestion, taken as a full-function replacement, would (1) lose the fallback-to-name (returning [] for empty/invalid input, producing entries with zero trigger keys — the same class of bug being fixed) and (2) lose non-string safety (a bare raw.map(trim) throws when the model supplies a non-string key). The corrected version only reorders dedup before the cap and preserves both the typeof entry === "string" guard and the [fallbackName] fallback.


AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.

Originally created by @kolacheee on GitHub (Jun 29, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3013 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** `normalizeLorebookEntryKeys` trims/filters the raw keys array, then `slice(0, MAX_LOREBOOK_ENTRY_KEYS=24)`, and ONLY THEN de-duplicates with `Array.from(new Set(...))`. Because the cap is applied before dedup, leading duplicate keys consume slots in the cap window and push distinct, valid keys past index 24 out of the result entirely. The model can supply 24+ unique trigger keys yet end up with far fewer — or even a single key — when duplicates appear early in the array. Trigger keys silently missing from a lorebook entry means the entry will not activate on those keywords. **How to reproduce (try it yourself)** 1. Call `save_lorebook_entry({ name:"X", content:"...", keys:["a","a","a", ...22 more "a"..., "important_key", "another_key"] })` so the 24-element slice window is filled entirely by the duplicate `"a"` before reaching the distinct keys. You'll see: the result entry's `keys = ["a"]` (after slice-then-dedup); `important_key` and `another_key` are dropped, even though there were only 3 distinct keys total. Expected: up to 24 DISTINCT keys are retained regardless of duplicate ordering, so all 3 distinct keys are kept. **Where it is** `packages/server/src/services/tools/tool-executor.ts:625-629` (`normalizeLorebookEntryKeys`), used at `:665` in `saveLorebookEntry`; `MAX_LOREBOOK_ENTRY_KEYS = 24` at line 72. **What's happening** De-duplication is performed after the cap (`.slice(0, MAX_LOREBOOK_ENTRY_KEYS)`) instead of before it, so duplicates count against the limit and can crowd out distinct keys. **Suggested fix** Move de-duplication ahead of the cap while keeping the existing non-string handling and the fallback-to-name branch: ```ts function normalizeLorebookEntryKeys(value: unknown, fallbackName: string): string[] { const raw = Array.isArray(value) ? value : []; const keys = Array.from( new Set( raw .map((entry) => (typeof entry === "string" ? entry.trim() : "")) .filter((entry) => entry.length > 0), ), ).slice(0, MAX_LOREBOOK_ENTRY_KEYS); if (keys.length > 0) return keys; return fallbackName ? [fallbackName] : []; } ``` This dedups distinct trimmed keys first, then caps to 24, so up to 24 distinct keys are always retained. *Regression avoided:* the original one-line suggestion, taken as a full-function replacement, would (1) lose the fallback-to-name (returning `[]` for empty/invalid input, producing entries with zero trigger keys — the same class of bug being fixed) and (2) lose non-string safety (a bare `raw.map(trim)` throws when the model supplies a non-string key). The corrected version only reorders dedup before the cap and preserves both the `typeof entry === "string"` guard and the `[fallbackName]` fallback. --- *AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.*
Author
Owner

@SpicyMarinara commented on GitHub (Jun 29, 2026):

Fixed in merged staging PR #3018.

<!-- gh-comment-id:4833018899 --> @SpicyMarinara commented on GitHub (Jun 29, 2026): Fixed in merged staging PR #3018.
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#1585
No description provided.