mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[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
Labels
No labels
android
bug
bugfix
ci
client
code freeze pause
container
dependencies
docs
documentation
duplicate
enhancement
feature
feature
feature
feature-creep
fixed-in-refactor
fixed-in-staging
good first issue
help wanted
in-pr
in-pr
needs-codeowner-decision
normal-scope
old-codebase
old-codebase
pull-request
qol
question
refactor
regression
revisit later
roadmap
scope-risk
scope-risk
server
server
shared
soup
upstream
windows
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Pasta-Devs/Marinara-Engine#3409
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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)
Dragon lorewhose text containsDRAGON SECRET.Dragon loreoff.Dragon loreentry 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 readmetadata.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 thisdeleteEntry; that symbol does not exist - the real one isremoveEntry.)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 callschatsStorage.removeLorebookFromChatMetadata(id), andpackages/server/src/services/storage/chats.storage.ts:1627-1643sweeps every chat but strips onlyactiveLorebookIds. It also doesif (!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-4867readsov?.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:
removeLorebookFromChatMetadata) to also dropentryStateOverrideskeys belonging to the deleted book, and widen its earlycontinueso chats that only auto-attached the book are swept too. That covers whole-book deletion cheaply, with no new pass.removeEntry).entryStateOverrides, drop keys whose entry id is not in the resolved entry set and write the pruned map back.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
52799569con 2026-09-13. Please verify before fixing.Related issue from the same sweep: #6133 (the scope-validation gap on the same route).
@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.
@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.