[GH-ISSUE #6132] Turning a lorebook entry off for one chat leaves a hidden switch behind after the entry or the book is deleted #3409

Closed
opened 2026-10-21 06:02:48 +07:00 by bsakaguchi · 2 comments
Owner

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

Originally assigned to: @SpicyMarinara on GitHub.

When you turn a single lorebook entry off for one chat, that choice is saved on the chat and is never cleaned up. Delete the entry, or delete the whole lorebook, and the chat still carries the off-switch for something that no longer exists; add the same book back to the chat later and the entry comes back already turned off, quietly missing from the prompt.

How to reproduce (try it yourself)

  1. Open any chat and open the chat settings drawer (the gear icon in the chat header). Scroll to the Lorebooks section.
  2. Add a lorebook to this chat with the "+ Add lorebook" picker. Pick a book that has an entry you can recognise in a prompt, for example an entry named Dragon lore whose text contains DRAGON SECRET.
  3. Click the pencil icon on the attached book's row to open the inline entry list. Each entry has an on/off switch that applies to this chat only.
  4. Turn Dragon lore off.
  5. Remove the book from this chat: click the trash icon on the book's row ("Remove from chat"). That icon only appears for a book you added yourself; for a book attached because it is linked to the character or is global, the control is the crossed-out-eye "Disable in this chat" instead.
  6. Add the same book back with the "+ Add lorebook" picker.
  7. Open the prompt preview, or send a turn.
  8. Now go to Library -> Lorebooks, open that book, and delete the Dragon lore entry outright. Go back to the chat and look at its stored data: curl -s http://127.0.0.1:7860/api/chats/<chatId> | python3 -m json.tool | grep -A3 entryStateOverrides (or export the chat and read metadata.entryStateOverrides).

What you'll see: after step 7 the entry is still suppressed - its text is absent from the assembled prompt even though you just re-added the book as authored. After step 8 the chat still holds an off-switch keyed to an entry id that no longer exists anywhere, and deleting the entire lorebook does not clear it either. Nothing in the interface can reach it any more, so it stays forever. One key is stored per distinct entry you ever toggled (roughly 40 bytes each); a probe left 40 leftover keys in a chat whose book had zero entries remaining. The chat's list of attached books is cleaned up when a book is deleted - only these entry switches are not.

What should happen: deleting an entry, or deleting a lorebook, should remove the matching switches from every chat, so a chat only ever stores choices for entries that still exist. Re-adding a book should also make it obvious that some of its entries are turned off for this chat, rather than silently dropping them from the prompt.

Where it is

At staging 52799569c:

  • packages/server/src/services/storage/lorebooks.storage.ts:1129-1131 - removeEntry(id) is a bare row delete: await db.delete(lorebookEntries).where(eq(lorebookEntries.id, id));. No chat metadata is touched. (An earlier note called this deleteEntry; that symbol does not exist - the real one is removeEntry.)
  • packages/server/src/services/storage/lorebooks.storage.ts:612-618 - remove(id), the whole-book delete, has the same gap.
  • packages/server/src/routes/lorebooks.routes.ts:507 - book deletion already calls chatsStorage.removeLorebookFromChatMetadata(id), and packages/server/src/services/storage/chats.storage.ts:1627-1643 sweeps every chat but strips only activeLorebookIds. It also does if (!Array.isArray(metadata.activeLorebookIds)) continue;, so it skips chats that never pinned the book.
  • packages/server/src/services/lorebook/index.ts:1124-1141 - where the leftover switch is consumed: if (ov && ov.enabled === false) return false;, applied with no check that the entry still exists or that the book is in scope.
  • packages/server/src/services/lorebook/index.ts:1295 - updatedOverrides = { ...overrides };, so a normal turn copies the map forward and never prunes it.

What's happening

The per-chat on/off state lives in one map on the chat, keyed by entry id. Writes to that map have no matching cleanup anywhere: not when the entry is deleted, not when the book is deleted, not when the book is removed from the chat, and not on the read side when a turn is assembled. The book-delete path already has a sweep over all chats for exactly this kind of stale id, and it simply does not include this map.

The same missing lifecycle produces a second oddity: an override that says "on" also survives the entry being disabled globally afterwards, which is the exact state the route's "Enable this entry in its lorebook first" refusal exists to prevent - and that refusal then blocks the only write that could clear the key. The main prompt path still excludes the entry, but the knowledge-router path at packages/server/src/routes/generate.routes.ts:4864-4867 reads ov?.enabled ?? e.enabled !== false, so there the stale "on" wins over the author's global off. It stays inside the books the chat can already see, so nothing out of scope leaks in.

Suggested fix

Give the map a lifecycle tied to entry existence:

  • Extend the existing chat sweep (removeLorebookFromChatMetadata) to also drop entryStateOverrides keys belonging to the deleted book, and widen its early continue so chats that only auto-attached the book are swept too. That covers whole-book deletion cheaply, with no new pass.
  • Add the same pruning to single-entry deletion (removeEntry).
  • As a belt-and-braces alternative that fixes both at once: when the generation path loads entryStateOverrides, drop keys whose entry id is not in the resolved entry set and write the pruned map back.
  • Decide and document the remove/re-add policy. Either clear a chat's switches for a book when that book is removed from the chat, or leave them and surface a count in the panel ("3 entries are turned off for this chat") so the state is discoverable. Today the switch does render in its off position once the book is back, but nothing tells the player to go look.
  • Also stop storing a redundant "on" override when it matches the entry's global state, so the case above cannot strand a key the route then refuses to overwrite.

The only lane touching this route is scripts/regressions/chat-lorebook-entry-scope.regression.ts; it passes at staging and covers per-chat isolation and the global-disabled floor, not pruning. A fix needs a new case there.

Context

PR #5981 (merged 2026-09-09) implemented issue #5954, the per-chat entry switch. The map itself is older - the ephemeral-countdown writer has written these keys without pruning since before that PR - but #5981 is the first thing that lets a person create them by clicking, which is what makes the leftovers accumulate. Neither #5954 nor #5981 asked for cleanup, so this is a gap rather than a regression. Unreleased on staging (latest release is v2.4.4).

Found by an AI-assisted sweep of merged PR #5981 against issue #5954, then re-verified against staging 52799569c on 2026-09-13. Please verify before fixing.

Related issue from the same sweep: #6133 (the scope-validation gap on the same route).

Originally created by @kolacheee on GitHub (Sep 13, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/6132 Originally assigned to: @SpicyMarinara on GitHub. When you turn a single lorebook entry off for one chat, that choice is saved on the chat and is never cleaned up. Delete the entry, or delete the whole lorebook, and the chat still carries the off-switch for something that no longer exists; add the same book back to the chat later and the entry comes back already turned off, quietly missing from the prompt. ## How to reproduce (try it yourself) 1. Open any chat and open the chat settings drawer (the gear icon in the chat header). Scroll to the Lorebooks section. 2. Add a lorebook to this chat with the "+ Add lorebook" picker. Pick a book that has an entry you can recognise in a prompt, for example an entry named `Dragon lore` whose text contains `DRAGON SECRET`. 3. Click the pencil icon on the attached book's row to open the inline entry list. Each entry has an on/off switch that applies to this chat only. 4. Turn `Dragon lore` **off**. 5. Remove the book from this chat: click the trash icon on the book's row ("Remove from chat"). That icon only appears for a book you added yourself; for a book attached because it is linked to the character or is global, the control is the crossed-out-eye "Disable in this chat" instead. 6. Add the same book back with the "+ Add lorebook" picker. 7. Open the prompt preview, or send a turn. 8. Now go to Library -> Lorebooks, open that book, and delete the `Dragon lore` entry outright. Go back to the chat and look at its stored data: `curl -s http://127.0.0.1:7860/api/chats/<chatId> | python3 -m json.tool | grep -A3 entryStateOverrides` (or export the chat and read `metadata.entryStateOverrides`). **What you'll see:** after step 7 the entry is still suppressed - its text is absent from the assembled prompt even though you just re-added the book as authored. After step 8 the chat still holds an off-switch keyed to an entry id that no longer exists anywhere, and deleting the entire lorebook does not clear it either. Nothing in the interface can reach it any more, so it stays forever. One key is stored per distinct entry you ever toggled (roughly 40 bytes each); a probe left 40 leftover keys in a chat whose book had zero entries remaining. The chat's list of attached books *is* cleaned up when a book is deleted - only these entry switches are not. **What should happen:** deleting an entry, or deleting a lorebook, should remove the matching switches from every chat, so a chat only ever stores choices for entries that still exist. Re-adding a book should also make it obvious that some of its entries are turned off for this chat, rather than silently dropping them from the prompt. ## Where it is At staging `52799569c`: - `packages/server/src/services/storage/lorebooks.storage.ts:1129-1131` - `removeEntry(id)` is a bare row delete: `await db.delete(lorebookEntries).where(eq(lorebookEntries.id, id));`. No chat metadata is touched. (An earlier note called this `deleteEntry`; that symbol does not exist - the real one is `removeEntry`.) - `packages/server/src/services/storage/lorebooks.storage.ts:612-618` - `remove(id)`, the whole-book delete, has the same gap. - `packages/server/src/routes/lorebooks.routes.ts:507` - book deletion already calls `chatsStorage.removeLorebookFromChatMetadata(id)`, and `packages/server/src/services/storage/chats.storage.ts:1627-1643` sweeps every chat but strips only `activeLorebookIds`. It also does `if (!Array.isArray(metadata.activeLorebookIds)) continue;`, so it skips chats that never pinned the book. - `packages/server/src/services/lorebook/index.ts:1124-1141` - where the leftover switch is consumed: `if (ov && ov.enabled === false) return false;`, applied with no check that the entry still exists or that the book is in scope. - `packages/server/src/services/lorebook/index.ts:1295` - `updatedOverrides = { ...overrides };`, so a normal turn copies the map forward and never prunes it. ## What's happening The per-chat on/off state lives in one map on the chat, keyed by entry id. Writes to that map have no matching cleanup anywhere: not when the entry is deleted, not when the book is deleted, not when the book is removed from the chat, and not on the read side when a turn is assembled. The book-delete path already has a sweep over all chats for exactly this kind of stale id, and it simply does not include this map. The same missing lifecycle produces a second oddity: an override that says "on" also survives the entry being disabled globally afterwards, which is the exact state the route's "Enable this entry in its lorebook first" refusal exists to prevent - and that refusal then blocks the only write that could clear the key. The main prompt path still excludes the entry, but the knowledge-router path at `packages/server/src/routes/generate.routes.ts:4864-4867` reads `ov?.enabled ?? e.enabled !== false`, so there the stale "on" wins over the author's global off. It stays inside the books the chat can already see, so nothing out of scope leaks in. ## Suggested fix Give the map a lifecycle tied to entry existence: - Extend the existing chat sweep (`removeLorebookFromChatMetadata`) to also drop `entryStateOverrides` keys belonging to the deleted book, and widen its early `continue` so chats that only auto-attached the book are swept too. That covers whole-book deletion cheaply, with no new pass. - Add the same pruning to single-entry deletion (`removeEntry`). - As a belt-and-braces alternative that fixes both at once: when the generation path loads `entryStateOverrides`, drop keys whose entry id is not in the resolved entry set and write the pruned map back. - Decide and document the remove/re-add policy. Either clear a chat's switches for a book when that book is removed from the chat, or leave them and surface a count in the panel ("3 entries are turned off for this chat") so the state is discoverable. Today the switch does render in its off position once the book is back, but nothing tells the player to go look. - Also stop storing a redundant "on" override when it matches the entry's global state, so the case above cannot strand a key the route then refuses to overwrite. The only lane touching this route is `scripts/regressions/chat-lorebook-entry-scope.regression.ts`; it passes at staging and covers per-chat isolation and the global-disabled floor, not pruning. A fix needs a new case there. ## Context PR #5981 (merged 2026-09-09) implemented issue #5954, the per-chat entry switch. The map itself is older - the ephemeral-countdown writer has written these keys without pruning since before that PR - but #5981 is the first thing that lets a person create them by clicking, which is what makes the leftovers accumulate. Neither #5954 nor #5981 asked for cleanup, so this is a gap rather than a regression. Unreleased on staging (latest release is v2.4.4). Found by an AI-assisted sweep of merged PR #5981 against issue #5954, then re-verified against staging 52799569c on 2026-09-13. Please verify before fixing. Related issue from the same sweep: #6133 (the scope-validation gap on the same route).
bsakaguchi 2026-10-21 06:02:48 +07:00
Author
Owner

@SpicyMarinara commented on GitHub (Sep 13, 2026):

@SpicyMarinara is taking this report through the issue sweep in draft PR #6137, with Codex assisting. The affected current-code path has been checked; implementation and focused regression verification are in progress.

<!-- gh-comment-id:5651924235 --> @SpicyMarinara commented on GitHub (Sep 13, 2026): @SpicyMarinara is taking this report through the issue sweep in draft PR #6137, with Codex assisting. The affected current-code path has been checked; implementation and focused regression verification are in progress.
Author
Owner

@SpicyMarinara commented on GitHub (Sep 13, 2026):

Fixed in #6137, now merged to staging (e044997a2). Lore deletion and stale chat-state cleanup commit or roll back together; detachment also clears stale state.

<!-- gh-comment-id:5652785296 --> @SpicyMarinara commented on GitHub (Sep 13, 2026): Fixed in #6137, now merged to staging (`e044997a2`). Lore deletion and stale chat-state cleanup commit or roll back together; detachment also clears stale state.
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#3409
No description provided.