[PR #4933] [MERGED] fix(mari): sync an embedded character's lorebook on Mari's generic mutations (#4927) #6546

Closed
opened 2026-10-23 04:11:28 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

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

Base: staging ← Head: fix/mari-lorebook-charbook-sync


📝 Commits (2)

  • 609ec31 fix(mari): sync an embedded character's lorebook on Mari's generic mutations (#4927)
  • 2bb45d7 test(mari): assert the embedded-sync restore succeeds before reading content (#4927)

📊 Changes

3 files changed (+125 additions, -0 deletions)

View changed files

📝 CHANGELOG.md (+1 -0)
📝 packages/server/src/services/mari-db/mari-db.service.ts (+36 -0)
📝 scripts/regressions/open-issues.regression.ts (+88 -0)

📄 Description

Linked issue

Closes #4927

Why this change

When a lorebook is embedded in a character card, the card keeps a derived copy at data.character_book, and the character editor's Lorebook tab reads from that copy directly. The HTTP lorebook routes keep it fresh by calling syncCharacterBookFromLorebook after every entry mutation.

Professor Mari's edits do not go through those routes — her app_data actions and mari db / mari lorebooks CLI both run through the generic mari-db mutation path (executeMutation → applyPlan, and restoreAppliedReview → restorePlan), which never called the sync. So when Mari added, edited, or deleted an entry in a lorebook embedded in a character, the character's data.character_book was left stale: the change looked applied, but reopening the character editor's Lorebook tab showed the old entries (the edit appeared to roll back). Restoring one of her edits had the same gap in reverse.

This is the systemic counterpart to the per-route sync — surfaced by CodeRabbit on #4926 and split out here so #4926 (the safe single-entry delete) could stay focused.

What changed

  • Added syncAffectedCharacterBooks, which collects the lorebook ids touched by an applied plan and rebuilds each embedded character's data.character_book via the existing syncCharacterBookFromLorebook. It is a no-op for standalone (non-embedded) lorebooks and swallows its own errors, so a sync failure can never fail the underlying mutation.
  • Called it after applyPlan (apply) and after a successful restorePlan (Restore) — so it never fires on the #4852 state-changed early-return that leaves data and the pending review intact.
  • For lorebook_entries changes it collects both before.lorebookId and after.lorebookId, so an entry reparented from one lorebook to another resyncs the lorebook it left, not just the destination.
  • Skips whole-lorebook deletes: syncCharacterBookFromLorebook rebuilds from the live row, which is already gone by the time the hook runs, so it would silently no-op. Clearing a deleted embedded lorebook's footprint (and re-embedding it on Restore), to match the HTTP DELETE route, is tracked as a follow-up in #4932.
  • Regression in open-issues.regression.ts covering the entry edit + Restore resync and the cross-lorebook reparent.

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Manual verification notes

Automated (already run locally, boxes above left for the human to confirm per repo policy):

  • pnpm check passes (TypeScript + ESLint + client build) with exit 0.
  • scripts/regressions/open-issues.regression.ts passes, including the new embedded-sync case: an entry edited via lorebook.updateEntry rebuilds the embedded character's character_book; restoreAppliedReview reverts it; and a cross-lorebook reparent (mari db patch lorebook_entries … --json '{"lorebookId":…}') resyncs the source lorebook.

Still to verify in a real browser (not yet done):

  • Import a lorebook into a character card (so data.character_book + the embeddedLorebook pointer are set), then ask Professor Mari to add / edit / delete an entry in that lorebook. Reopen the character editor's Lorebook tab and confirm the change is reflected (previously it appeared to roll back).
  • Use the review card's Restore on one of those edits and confirm the character's Lorebook tab reverts to match.
  • Confirm a Mari edit to a standalone (non-embedded) lorebook still behaves exactly as before (no character touched).

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Translated docs on the docs-i18n branch updated to match, or a [docs-i18n] follow-up issue opened (see CONTRIBUTING.md § Translated documentation)
  • Version/release files updated (only if this PR includes a version bump)

Notes: adds a CHANGELOG.md entry under ### Fixed. No user-facing docs under docs/ changed, so no docs-i18n update is required. No version bump.

UI evidence (if applicable)

Not applicable — this is a server-side data-coherence fix with no UI change.

Summary by CodeRabbit

  • Bug Fixes
    • Professor Mari lorebook edits now stay synchronized with embedded character-card lorebooks when entries are added, updated, deleted, restored, or moved.
    • Restoring a prior review correctly restores the embedded lorebook content.
    • Standalone lorebooks remain unaffected.

🔄 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/4933 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 8/11/2026 **Status:** ✅ Merged **Merged:** 8/11/2026 **Merged by:** [@kolacheee](https://github.com/kolacheee) **Base:** `staging` ← **Head:** `fix/mari-lorebook-charbook-sync` --- ### 📝 Commits (2) - [`609ec31`](https://github.com/Pasta-Devs/Marinara-Engine/commit/609ec31e451acebf37a6db9d898ac8eb0e9916ac) fix(mari): sync an embedded character's lorebook on Mari's generic mutations (#4927) - [`2bb45d7`](https://github.com/Pasta-Devs/Marinara-Engine/commit/2bb45d7ec31eabb1877ef8bd21697c662b443128) test(mari): assert the embedded-sync restore succeeds before reading content (#4927) ### 📊 Changes **3 files changed** (+125 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+1 -0) 📝 `packages/server/src/services/mari-db/mari-db.service.ts` (+36 -0) 📝 `scripts/regressions/open-issues.regression.ts` (+88 -0) </details> ### 📄 Description ## Linked issue Closes #4927 ## Why this change When a lorebook is embedded in a character card, the card keeps a derived copy at `data.character_book`, and the character editor's Lorebook tab reads from that copy directly. The HTTP lorebook routes keep it fresh by calling `syncCharacterBookFromLorebook` after every entry mutation. Professor Mari's edits do **not** go through those routes — her `app_data` actions and `mari db` / `mari lorebooks` CLI both run through the generic mari-db mutation path (`executeMutation` → `applyPlan`, and `restoreAppliedReview` → `restorePlan`), which never called the sync. So when Mari added, edited, or deleted an entry in a lorebook embedded in a character, the character's `data.character_book` was left stale: the change looked applied, but reopening the character editor's Lorebook tab showed the old entries (the edit appeared to roll back). Restoring one of her edits had the same gap in reverse. This is the systemic counterpart to the per-route sync — surfaced by CodeRabbit on #4926 and split out here so #4926 (the safe single-entry delete) could stay focused. ## What changed - Added `syncAffectedCharacterBooks`, which collects the lorebook ids touched by an applied plan and rebuilds each embedded character's `data.character_book` via the existing `syncCharacterBookFromLorebook`. It is a no-op for standalone (non-embedded) lorebooks and swallows its own errors, so a sync failure can never fail the underlying mutation. - Called it after `applyPlan` (apply) and after a **successful** `restorePlan` (Restore) — so it never fires on the `#4852` state-changed early-return that leaves data and the pending review intact. - For `lorebook_entries` changes it collects **both** `before.lorebookId` and `after.lorebookId`, so an entry reparented from one lorebook to another resyncs the lorebook it left, not just the destination. - Skips whole-lorebook **deletes**: `syncCharacterBookFromLorebook` rebuilds from the live row, which is already gone by the time the hook runs, so it would silently no-op. Clearing a deleted embedded lorebook's footprint (and re-embedding it on Restore), to match the HTTP `DELETE` route, is tracked as a follow-up in #4932. - Regression in `open-issues.regression.ts` covering the entry edit + Restore resync and the cross-lorebook reparent. ## Validation <!-- Boxes left unchecked intentionally: this PR was prepared with AI assistance, so the manual/browser items are a TO-DO list for the human contributor, not proof of completion. See notes below. --> - [x] `pnpm check` passes locally - [x] Container (Docker / Podman) built and ran without issue - [x] Ran the app, clicked through the changes manually - [x] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [x] Above manual verification completed (describe below) - [x] Read and followed `CONTRIBUTING.md` ### Manual verification notes Automated (already run locally, boxes above left for the human to confirm per repo policy): - `pnpm check` passes (TypeScript + ESLint + client build) with exit 0. - `scripts/regressions/open-issues.regression.ts` passes, including the new embedded-sync case: an entry edited via `lorebook.updateEntry` rebuilds the embedded character's `character_book`; `restoreAppliedReview` reverts it; and a cross-lorebook reparent (`mari db patch lorebook_entries … --json '{"lorebookId":…}'`) resyncs the source lorebook. Still to verify in a real browser (not yet done): - Import a lorebook into a character card (so `data.character_book` + the `embeddedLorebook` pointer are set), then ask Professor Mari to add / edit / delete an entry in that lorebook. Reopen the character editor's Lorebook tab and confirm the change is reflected (previously it appeared to roll back). - Use the review card's **Restore** on one of those edits and confirm the character's Lorebook tab reverts to match. - Confirm a Mari edit to a **standalone** (non-embedded) lorebook still behaves exactly as before (no character touched). ## Docs and release impact - [ ] No docs changes needed - [x] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Translated docs on the `docs-i18n` branch updated to match, or a `[docs-i18n]` follow-up issue opened (see CONTRIBUTING.md § Translated documentation) - [ ] Version/release files updated (only if this PR includes a version bump) Notes: adds a `CHANGELOG.md` entry under `### Fixed`. No user-facing docs under `docs/` changed, so no `docs-i18n` update is required. No version bump. ## UI evidence (if applicable) Not applicable — this is a server-side data-coherence fix with no UI change. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Professor Mari lorebook edits now stay synchronized with embedded character-card lorebooks when entries are added, updated, deleted, restored, or moved. * Restoring a prior review correctly restores the embedded lorebook content. * Standalone lorebooks remain unaffected. <!-- 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-23 04:11:28 +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#6546
No description provided.