[GH-ISSUE #4932] Mari: whole-lorebook delete/restore leaves an embedded character's character_book + pointer stale #2735

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

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

Originally assigned to: @SpicyMarinara on GitHub.

Summary

When Professor Mari deletes a whole lorebook that is embedded in a character card (or restores such a delete) through her generic mutation path (mari lorebooks delete <id>, mari db delete lorebooks <id>), the character's derived data.character_book and its extensions.importMetadata.embeddedLorebook.lorebookId forward pointer are left stale. The character editor's Lorebook tab then shows phantom entries and an "Edit Embedded Lorebook" button pointing at a lorebook that no longer exists.

This is the whole-lorebook counterpart of #4927 (which keeps the embedded copy in sync for entry add/update/delete + restore). It is a pre-existing gap — Mari's generic path never synced embedded character books at all before #4927 — and #4927 is deliberately scoped to entry-level edits, so it is being tracked separately here.

Why the #4927 sync hook can't cover it

#4927 added syncAffectedCharacterBooks, which calls syncCharacterBookFromLorebook(db, lorebookId) after apply/restore. That helper is a rebuild-from-the-live-row function: it starts with getById(lorebookId) and returns early if the row is gone. On a whole-lorebook delete, the row is already removed by the time the hook runs, so the rebuild no-ops — it can neither clear the character's character_book nor drop the dangling pointer. The #4927 lorebooks branch is therefore intentionally narrowed to non-delete changes.

What the HTTP route does (the parity target)

DELETE /lorebooks/:id (packages/server/src/routes/lorebooks.routes.ts) handles this deliberately: it resolves the embedding character before removal and then calls clearCharacterEmbeddedLorebook(db, characterId, lorebookId) to null character_book and drop the pointer. The generic mari-db mutation path has no equivalent.

Suggested approach

In packages/server/src/services/mari-db/mari-db.service.ts syncAffectedCharacterBooks, handle lorebooks action: "delete" changes:

  • Apply direction (row now absent): resolve the character via resolveEmbeddedCharacterId(db, id, change.before) (the pre-delete row is a valid lorebookHint, and the character still carries the forward pointer at apply time), then clearCharacterEmbeddedLorebook(db, characterId, id).
  • Restore direction (row re-inserted by restorePlan): the clear dropped the forward pointer, so resolveEmbeddedCharacterId can no longer find it — re-embed via embedLorebookIntoCharacter(db, change.before.characterId, id) to restore both character_book and the pointer. Direction can be inferred from whether getById(id) currently returns the row.

Edge cases to cover in the fix: multi-linked lorebooks (which character actually embeds it), and a --cascade delete vs. the empty-embedded-book case. Add an open-issues.regression.ts case that deletes an embedded lorebook via Mari, asserts the character is cleared, then restores and asserts it is re-embedded.

Acceptance

  • Mari deleting an embedded lorebook clears the character's character_book + embed pointer (no phantom entries, no dangling "Edit Embedded Lorebook").
  • Restoring that delete re-embeds the character book from the re-inserted lorebook.
  • Regression coverage for both directions.
Originally created by @kolacheee on GitHub (Aug 11, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/4932 Originally assigned to: @SpicyMarinara on GitHub. ### Summary When Professor Mari **deletes a whole lorebook** that is embedded in a character card (or **restores** such a delete) through her generic mutation path (`mari lorebooks delete <id>`, `mari db delete lorebooks <id>`), the character's derived `data.character_book` and its `extensions.importMetadata.embeddedLorebook.lorebookId` forward pointer are left stale. The character editor's Lorebook tab then shows phantom entries and an "Edit Embedded Lorebook" button pointing at a lorebook that no longer exists. This is the whole-lorebook counterpart of #4927 (which keeps the embedded copy in sync for entry add/update/delete + restore). It is a **pre-existing** gap — Mari's generic path never synced embedded character books at all before #4927 — and #4927 is deliberately scoped to entry-level edits, so it is being tracked separately here. ### Why the #4927 sync hook can't cover it `#4927` added `syncAffectedCharacterBooks`, which calls `syncCharacterBookFromLorebook(db, lorebookId)` after apply/restore. That helper is a *rebuild-from-the-live-row* function: it starts with `getById(lorebookId)` and returns early if the row is gone. On a whole-lorebook **delete**, the row is already removed by the time the hook runs, so the rebuild no-ops — it can neither clear the character's `character_book` nor drop the dangling pointer. The #4927 `lorebooks` branch is therefore intentionally narrowed to non-delete changes. ### What the HTTP route does (the parity target) `DELETE /lorebooks/:id` (`packages/server/src/routes/lorebooks.routes.ts`) handles this deliberately: it resolves the embedding character **before** removal and then calls `clearCharacterEmbeddedLorebook(db, characterId, lorebookId)` to null `character_book` and drop the pointer. The generic mari-db mutation path has no equivalent. ### Suggested approach In `packages/server/src/services/mari-db/mari-db.service.ts` `syncAffectedCharacterBooks`, handle `lorebooks` `action: "delete"` changes: - **Apply direction** (row now absent): resolve the character via `resolveEmbeddedCharacterId(db, id, change.before)` (the pre-delete row is a valid `lorebookHint`, and the character still carries the forward pointer at apply time), then `clearCharacterEmbeddedLorebook(db, characterId, id)`. - **Restore direction** (row re-inserted by `restorePlan`): the clear dropped the forward pointer, so `resolveEmbeddedCharacterId` can no longer find it — re-embed via `embedLorebookIntoCharacter(db, change.before.characterId, id)` to restore both `character_book` and the pointer. Direction can be inferred from whether `getById(id)` currently returns the row. Edge cases to cover in the fix: multi-linked lorebooks (which character actually embeds it), and a `--cascade` delete vs. the empty-embedded-book case. Add an `open-issues.regression.ts` case that deletes an embedded lorebook via Mari, asserts the character is cleared, then restores and asserts it is re-embedded. ### Acceptance - Mari deleting an embedded lorebook clears the character's `character_book` + embed pointer (no phantom entries, no dangling "Edit Embedded Lorebook"). - Restoring that delete re-embeds the character book from the re-inserted lorebook. - Regression coverage for both directions.
bsakaguchi 2026-10-21 05:54:07 +07:00
Author
Owner

@kolacheee commented on GitHub (Aug 11, 2026):

i got this confused with the other one whoops this still needs fixed

<!-- gh-comment-id:5259031968 --> @kolacheee commented on GitHub (Aug 11, 2026): i got this confused with the other one whoops this still needs fixed
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#2735
No description provided.