[PR #555] [MERGED] Fix embedded lorebook deletion and ghost editor states #4671

Closed
opened 2026-10-22 04:09:40 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/555
Author: @kolacheee
Created: 5/8/2026
Status: ✅ Merged
Merged: 5/8/2026
Merged by: @cha1latte

Base: main ← Head: fix/embedded-lorebook-sync


📝 Commits (3)

  • abc5655 Fix embedded lorebook deletion and ghost editor states
  • d94a85c Guard embedded lorebook sync by metadata pointer
  • e559183 Sync embedded lorebooks across transfer paths

📊 Changes

8 files changed (+310 additions, -11 deletions)

View changed files

📝 CHANGELOG.md (+1 -0)
📝 packages/client/src/components/characters/CharacterEditor.tsx (+12 -2)
📝 packages/client/src/components/lorebooks/LorebookEditor.tsx (+15 -1)
📝 packages/client/src/hooks/use-lorebooks.ts (+31 -2)
📝 packages/server/src/routes/lorebooks.routes.ts (+27 -2)
📝 packages/server/src/services/import/marinara.importer.ts (+18 -1)
📝 packages/server/src/services/import/st-character.importer.ts (+14 -3)
➕ packages/server/src/services/lorebook/character-book-sync.ts (+192 -0)

📄 Description

Linked issue

https://github.com/Pasta-Devs/Marinara-Engine/issues/554

Closes #554

Why this change

Edits made via "Edit Linked Lorebook" on a character with an embedded lorebook silently failed to persist: deleted entries reappeared after reopening the character, deleting the lorebook left ghost preview entries plus a phantom Reimport / Edit button, and clicking Edit on a fresh import sometimes opened an editor stuck on a permanent shimmer.

Three coupled root causes:

  • An embedded lorebook lives in two stores once imported (the V2 character_book inside the character JSON, and a standalone DB row), but /lorebooks/* mutations only ever wrote to the standalone row. The character editor's Lorebook tab reads character_book directly, so anything done via "Edit Linked Lorebook" appeared to roll back.
  • The character query and the lorebook detail/entries queries both set staleTime: 5 minutes. Lorebook delete invalidated only lorebookKeys.all, never characters, and TanStack returns previously cached data even after a refetch errors with 404 — so a stale pointer rendered a populated-looking ghost editor while the entries query honestly reported empty.
  • Imported character cards can carry extensions.importMetadata.embeddedLorebook.lorebookId baked in (any card round-tripped through another Marinara instance), and an auto-import that errored silently left the same orphan. LorebookEditor's loading branch is isLoading || !lorebook, which a 404'd query satisfies forever.

What changed

Server:

  • New services/lorebook/character-book-sync.ts mirrors entry create / update / delete / bulk back into character.data.character_book when the lorebook is character-linked, and clears character_book + extensions.importMetadata.embeddedLorebook when the linked lorebook is deleted. Sync errors are logged via Pino and never thrown — the user's primary mutation has already succeeded.
  • importSTCharacter and the marinara importer now strip any carried-over lorebookId from extensions.importMetadata.embeddedLorebook. The auto-import in importSTCharacter still writes a fresh ID when (and only when) it actually creates a row in this DB.

Client:

  • useDeleteLorebook removeQueries the deleted lorebook's detail and entries (eviction, not just invalidation, since 404'd queries hold their last-good data) and invalidates characterKeys.all so the character editor refetches and drops character_book + the stale embedded-lorebook pointer.
  • The four entry-mutation hooks (create / update / delete / bulk) also invalidate characterKeys.all, since the new server-side sync writes to character_book on every entry CRUD.
  • LorebookTab in CharacterEditor verifies the linked lorebook actually exists via useLorebook(rawLinkedLorebookId) before showing "Edit Linked Lorebook"; a 404 collapses the tab back to "Import Embedded Lorebook" so existing characters with stale pointers still recover.
  • LorebookEditor reads isError from useLorebook and, on error, fires a toast and calls closeDetail so the user is no longer stranded on a permanent shimmer if a stale pointer slips through.

Validation

  • pnpm check passes locally
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Read and followed CONTRIBUTING.md

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Version/release files updated (only if this PR includes a version bump)

Summary by CodeRabbit

  • Bug Fixes
    • Edits to linked lorebooks now persist to characters with embedded lorebooks.
    • Deleting a linked lorebook clears the embedded copy from the character and evicts cached lorebook detail.
    • Prevented stale UI states (phantom reimport buttons, infinite loading, ghost editor panels).
    • Imported character cards no longer retain incorrect embedded lorebook pointers.
    • "Edit Linked Lorebook" now validates existence and shows a not-found error when missing.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/555 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 5/8/2026 **Status:** ✅ Merged **Merged:** 5/8/2026 **Merged by:** [@cha1latte](https://github.com/cha1latte) **Base:** `main` ← **Head:** `fix/embedded-lorebook-sync` --- ### 📝 Commits (3) - [`abc5655`](https://github.com/Pasta-Devs/Marinara-Engine/commit/abc565528d996b8471607197d230f9a582a1c018) Fix embedded lorebook deletion and ghost editor states - [`d94a85c`](https://github.com/Pasta-Devs/Marinara-Engine/commit/d94a85c568156543ebf02a0ffc1e7dba78701c9c) Guard embedded lorebook sync by metadata pointer - [`e559183`](https://github.com/Pasta-Devs/Marinara-Engine/commit/e5591835ec60f4662a4732fce69c42d6bca477c4) Sync embedded lorebooks across transfer paths ### 📊 Changes **8 files changed** (+310 additions, -11 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+1 -0) 📝 `packages/client/src/components/characters/CharacterEditor.tsx` (+12 -2) 📝 `packages/client/src/components/lorebooks/LorebookEditor.tsx` (+15 -1) 📝 `packages/client/src/hooks/use-lorebooks.ts` (+31 -2) 📝 `packages/server/src/routes/lorebooks.routes.ts` (+27 -2) 📝 `packages/server/src/services/import/marinara.importer.ts` (+18 -1) 📝 `packages/server/src/services/import/st-character.importer.ts` (+14 -3) ➕ `packages/server/src/services/lorebook/character-book-sync.ts` (+192 -0) </details> ### 📄 Description ## Linked issue https://github.com/Pasta-Devs/Marinara-Engine/issues/554 Closes #554 ## Why this change Edits made via "Edit Linked Lorebook" on a character with an embedded lorebook silently failed to persist: deleted entries reappeared after reopening the character, deleting the lorebook left ghost preview entries plus a phantom Reimport / Edit button, and clicking Edit on a fresh import sometimes opened an editor stuck on a permanent shimmer. Three coupled root causes: - An embedded lorebook lives in two stores once imported (the V2 character_book inside the character JSON, and a standalone DB row), but /lorebooks/* mutations only ever wrote to the standalone row. The character editor's Lorebook tab reads character_book directly, so anything done via "Edit Linked Lorebook" appeared to roll back. - The character query and the lorebook detail/entries queries both set staleTime: 5 minutes. Lorebook delete invalidated only lorebookKeys.all, never characters, and TanStack returns previously cached `data` even after a refetch errors with 404 — so a stale pointer rendered a populated-looking ghost editor while the entries query honestly reported empty. - Imported character cards can carry extensions.importMetadata.embeddedLorebook.lorebookId baked in (any card round-tripped through another Marinara instance), and an auto-import that errored silently left the same orphan. LorebookEditor's loading branch is `isLoading || !lorebook`, which a 404'd query satisfies forever. ## What changed Server: - New services/lorebook/character-book-sync.ts mirrors entry create / update / delete / bulk back into character.data.character_book when the lorebook is character-linked, and clears character_book + extensions.importMetadata.embeddedLorebook when the linked lorebook is deleted. Sync errors are logged via Pino and never thrown — the user's primary mutation has already succeeded. - importSTCharacter and the marinara importer now strip any carried-over lorebookId from extensions.importMetadata.embeddedLorebook. The auto-import in importSTCharacter still writes a fresh ID when (and only when) it actually creates a row in this DB. Client: - useDeleteLorebook removeQueries the deleted lorebook's detail and entries (eviction, not just invalidation, since 404'd queries hold their last-good data) and invalidates characterKeys.all so the character editor refetches and drops character_book + the stale embedded-lorebook pointer. - The four entry-mutation hooks (create / update / delete / bulk) also invalidate characterKeys.all, since the new server-side sync writes to character_book on every entry CRUD. - LorebookTab in CharacterEditor verifies the linked lorebook actually exists via useLorebook(rawLinkedLorebookId) before showing "Edit Linked Lorebook"; a 404 collapses the tab back to "Import Embedded Lorebook" so existing characters with stale pointers still recover. - LorebookEditor reads isError from useLorebook and, on error, fires a toast and calls closeDetail so the user is no longer stranded on a permanent shimmer if a stale pointer slips through. ## Validation - [x] `pnpm check` passes locally - [x] Ran the app, clicked through the changes manually - [x] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [x] Read and followed `CONTRIBUTING.md` ## Docs and release impact - [x] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Version/release files updated (only if this PR includes a version bump) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Edits to linked lorebooks now persist to characters with embedded lorebooks. * Deleting a linked lorebook clears the embedded copy from the character and evicts cached lorebook detail. * Prevented stale UI states (phantom reimport buttons, infinite loading, ghost editor panels). * Imported character cards no longer retain incorrect embedded lorebook pointers. * "Edit Linked Lorebook" now validates existence and shows a not-found error when missing. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:09:40 +07:00
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#4671
No description provided.