[PR #8786] [MERGED] fix(lorebooks): keep embedded and imported lorebook settings intact #8804

Closed
opened 2026-11-07 17:39:49 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/8786
Author: @SpicyMarinara
Created: 11/3/2026
Status: ✅ Merged
Merged: 11/3/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/lorebook-import-fidelity


📝 Commits (2)

  • c44a698 fix(lorebooks): keep embedded and imported lorebook settings intact
  • 3a58e67 fix(lorebooks): import a collapsed-scope native lorebook as non-global

📊 Changes

12 files changed (+485 additions, -51 deletions)

View changed files

📝 packages/server/src/routes/characters.routes.ts (+2 -5)
📝 packages/server/src/routes/import.routes.ts (+2 -2)
📝 packages/server/src/routes/lorebooks.routes.ts (+2 -16)
📝 packages/server/src/services/import/marinara.importer.ts (+34 -15)
📝 packages/server/src/services/import/st-character.importer.ts (+4 -2)
📝 packages/server/src/services/import/st-lorebook.importer.ts (+61 -8)
📝 packages/server/src/services/lorebook/character-book-sync.ts (+46 -3)
📝 packages/shared/src/schemas/character.schema.ts (+1 -0)
➕ scripts/regressions/character-book-fidelity.regression.ts (+143 -0)
➕ scripts/regressions/json-card-bom-import.regression.ts (+54 -0)
📝 scripts/regressions/lorebook-reimport-links.regression.ts (+43 -0)
➕ scripts/regressions/native-lorebook-import-links.regression.ts (+93 -0)

📄 Description

Important

Contributions target staging. Only SpicyMarinara may promote this repository's staging branch or a same-repository hotfix/* branch to main.

Linked issue

Closes #8785

Why this change

Everyday card and lorebook actions were quietly damaging lorebook settings. Saving a card in the Character editor turned Outlet entries into Before Char entries. SillyTavern/Chub cards imported with the wrong placement and triggers: @D entries landed After Char, probability gates were dropped, and NOT ANY became AND ANY. Their SillyTavern data was then erased from the card, so a Compatible export handed SillyTavern its defaults. Import/Reimport Embedded Lorebook reset the card's settings, and the user's lorebook name and settings, to import defaults. A native lorebook shared from another install could be hidden in every chat because its chat IDs belonged to that install. JSON cards saved with a BOM were rejected by the server even though the browser accepted them. None of these are new in this release. All came from a release-prep audit and were confirmed by two independent verifiers. No issue is linked yet; one should be opened before the PR is submitted.

What changed

  • Behaviour changes for existing users: (1) Reimport Embedded Lorebook no longer renames the linked lorebook or resets its category or max recursion depth. It replaces the entries, applies the card book's own settings and keeps everything else. (2) Import and Reimport Embedded Lorebook now use the card book's token budget. A book that only carries the schema default of 512 (for example one created in Marinara without a token budget) now imports at 512 instead of 2048, so fewer lore entries fit. (3) A native lorebook that was Global but limited to specific chats, none of which exist on the importing install, is imported as non-global with an All chats scope. It no longer joins every chat, and you add it where you want it. Nothing else that worked before stops working.
  • Outlet entries (position 7) in a card's embedded lorebook stay Outlet entries, with their outlet name, through Character editor saves, skip-lorebook imports, reimports and exports.
  • SillyTavern/Chub cards now import each embedded entry's real settings from extensions: @D position and depth, role, probability, selective logic, group and weight, sticky/cooldown/delay, scan depth, case and whole-word matching, the recursion flags, vectorized and outlet name.
  • The synced card book also writes these settings under each entry's extensions, in SillyTavern's numbering, so Compatible character exports keep their placement and triggers in SillyTavern.
  • Import Embedded Lorebook now uses the card book's description, scan depth, token budget and recursion setting instead of import defaults.
  • Importing a Marinara native lorebook keeps only the character, persona and chat links that exist on this install. A 'Specific chats' scope with none of its chats here becomes 'All chats', so the lorebook is no longer hidden everywhere and can be added to a chat.
  • JSON character cards and .charx card.json files saved with a UTF-8 byte order mark now inspect and import instead of failing with 'Invalid file format'.

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

  • Manually verify in the browser: in a character's linked embedded lorebook, set an entry's Position to Outlet and give it a name. Save the character in the Character editor, click Reimport Embedded Lorebook, and confirm the entry is still Outlet with the same name.
  • Manually verify: import a real SillyTavern-exported PNG card whose embedded lorebook uses @D depth/role, a probability below 100 and NOT ANY logic. Open the linked lorebook and confirm the position (At Depth), depth, role, probability and logic match SillyTavern.
  • Manually verify: export that character as Compatible, import it into SillyTavern, and check in its World Info editor that each entry's position, depth, role, probability and logic survived.
  • Manually verify: on a linked embedded lorebook, change the name, category, scan depth and token budget, then click Reimport Embedded Lorebook. Confirm the name and category are unchanged and the scan depth and token budget match the card.
  • Manually verify: create a character in Marinara with an embedded lorebook that never had a token budget set, click Import Embedded Lorebook, and confirm the linked lorebook's token budget is 512. Then decide whether 512 is acceptable or should stay 2048.
  • Manually verify: export a lorebook assigned with 'Specific chats' as Marinara Native and import it on a different install. Confirm it shows in Chat Settings > Lorebooks and can be added to a chat.
  • Manually verify: export a Global lorebook that is limited to 'Specific chats' as Marinara Native and import it on a different install. Confirm it is not Global there and its entries do not appear in a chat it was not added to.
  • Manually verify: save a SillyTavern V2 .json card with Notepad's 'UTF-8 with BOM' encoding, import it through Import Character, and confirm the inspect step lists it and the import succeeds.

Agent-run checks (for reference; each regression was shown to fail without its fix):

  • node ./scripts/pnpm-runner.mjs build:shared: passed. Rebuilt again after the review fix; the dist contains z.literal(7).
  • cd packages/server && npx tsc --noEmit: passed (no output), after the review fix
  • node ./scripts/pnpm-runner.mjs --filter @marinara-engine/server lint (tsc -b ../shared && tsc --noEmit && tsc -p tsconfig.character-activity.json): exit 0, after the review fix
  • node ./scripts/pnpm-runner.mjs --filter @marinara-engine/shared lint: exit 0 (first round; shared not touched in the review round)
  • node_modules/.bin/prettier --write, then --check, on the changed files: --check exit 0. In this round --write left both changed files unchanged.
  • eslint on changed files: Not run: only packages/client has an ESLint config, and no client files changed. The server and shared lint scripts (tsc) ran instead.
  • pnpm localization:check: Not run: no client copy changed.
  • node ./scripts/run-regressions.mjs --filter native-lorebook-import-links: 1/1 passed
  • Fails-before for the review fix: copy marinara.importer.ts aside, revert only the isGlobal guard with sed, run the regression, restore, then cmp: FAILED on 'a global book whose chats are all gone is imported as non-global' (actual true). With the isGlobal assertion also disabled temporarily, it FAILED on 'so it does not join chats it was never
  • node ./scripts/run-regressions.mjs --filter lorebook: 39/39 passed
  • node ./scripts/run-regressions.mjs --filter import: 19/19 passed
  • node ./scripts/run-regressions.mjs --filter character: 31/31 passed

Independent adversarial review: changes, 2 issue(s), all addressed in a fix-up pass.

Known limits and risks:

Both review issues held when checked against the code and are addressed.

(1) Global plus collapsed scope. listActiveEntries runs isLorebookScopeActiveForChat first and then if (b.isGlobal) return true (lorebooks.storage.ts:833-845). Commit 3a58e678d4 therefore imports a book as non-global when its specific scope collapses, with a regression case that calls listActiveEntries for an unrelated chat. Open edge case: a non-global book that keeps links to local characters but loses all of its scoped chats (only possible on the same install after those chats were deleted) becomes active in every chat with that character, where before it was hidden everywhere. I left that alone because it is the normal character-link behaviour and the finding did not cover it.

(2) token_budget. characterBookSchema defaults token_budget to 512 (character.schema.ts:148), and Import Embedded now passes the card book through. A book that was never given a budget and never synced from a lorebook (character-book-sync writes the lorebook's tokenBudget back to the card) now imports, and reimports, at 512 instead of 2048. I made the text fix the reviewer called sufficient: the first What changed bullet now lists it. Keeping 2048 would mean removing the schema default or recording whether token_budget was set explicitly. That is a larger change, left for the maintainer.

Other risks carried over:

  • The SillyTavern extension key names and numbering come from knowledge of SillyTavern's world-info.js (convertWorldInfoToCharacterBook / convertCharacterBook), not a local ST source. Both verifiers had the same limit.
  • Precedence: top-level values win. extensions.position applies only when the top-level position is missing or is the agreeing before_char/after_char string.
  • The schema always writes a top-level case_sensitive, so camelCase caseSensitive is filled from extensions.case_sensitive. A hand-edited top-level case_sensitive with a stale extensions value would lose.
  • The mirror adds about 19 keys per entry to data.character_book, so cards and PNG metadata grow.
  • ST extension keys Marinara does not model (display_index, automation_id, group_override, use_group_scoring, triggers, ignore_budget...) are still dropped by the sync.
  • vectorized is mirrored as !excludeFromVectorization, matching the existing Compatible World Info export.
  • Native lorebook entry characterFilterIds can still name foreign characters; that was outside the finding.

Not addressed (reviewer nits, outside the findings):

  • backup.routes.ts keeps its own copies of stSelectiveLogic/stPosition.
  • st-bulk.importer.ts still reads ST folder JSON with readFile('utf-8') + JSON.parse, so the same BOM problem remains there.
  • importSTLorebook's result.category reports detectedCategory on reimport although the category is no longer updated; no caller reads it on that path.

chat-request-validation times out at 90 s on this machine with and without these changes.

Not done: the user asked to make showing an outfit to a love interest in Otome Hearth give +5 affection instead of +20. That is outside this workflow step and worktree, and git grep finds no Otome Hearth outfit/affection logic in Marinara Engine. It belongs to the Otome Hearth (otome-forge) package repository, needs its own task, and must not be reported as done.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README) 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)

🤖 Generated with Claude Code


🔄 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/8786 **Author:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Created:** 11/3/2026 **Status:** ✅ Merged **Merged:** 11/3/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/lorebook-import-fidelity` --- ### 📝 Commits (2) - [`c44a698`](https://github.com/Pasta-Devs/Marinara-Engine/commit/c44a69899f966ea9e8006047fb41643391504189) fix(lorebooks): keep embedded and imported lorebook settings intact - [`3a58e67`](https://github.com/Pasta-Devs/Marinara-Engine/commit/3a58e678d44cde2d2afbc44605da486d1aafc33b) fix(lorebooks): import a collapsed-scope native lorebook as non-global ### 📊 Changes **12 files changed** (+485 additions, -51 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/routes/characters.routes.ts` (+2 -5) 📝 `packages/server/src/routes/import.routes.ts` (+2 -2) 📝 `packages/server/src/routes/lorebooks.routes.ts` (+2 -16) 📝 `packages/server/src/services/import/marinara.importer.ts` (+34 -15) 📝 `packages/server/src/services/import/st-character.importer.ts` (+4 -2) 📝 `packages/server/src/services/import/st-lorebook.importer.ts` (+61 -8) 📝 `packages/server/src/services/lorebook/character-book-sync.ts` (+46 -3) 📝 `packages/shared/src/schemas/character.schema.ts` (+1 -0) ➕ `scripts/regressions/character-book-fidelity.regression.ts` (+143 -0) ➕ `scripts/regressions/json-card-bom-import.regression.ts` (+54 -0) 📝 `scripts/regressions/lorebook-reimport-links.regression.ts` (+43 -0) ➕ `scripts/regressions/native-lorebook-import-links.regression.ts` (+93 -0) </details> ### 📄 Description > [!IMPORTANT] > Contributions target `staging`. Only `SpicyMarinara` may promote this repository's `staging` branch or a same-repository `hotfix/*` branch to `main`. ## Linked issue Closes #8785 ## Why this change Everyday card and lorebook actions were quietly damaging lorebook settings. Saving a card in the Character editor turned Outlet entries into Before Char entries. SillyTavern/Chub cards imported with the wrong placement and triggers: @D entries landed After Char, probability gates were dropped, and NOT ANY became AND ANY. Their SillyTavern data was then erased from the card, so a Compatible export handed SillyTavern its defaults. Import/Reimport Embedded Lorebook reset the card's settings, and the user's lorebook name and settings, to import defaults. A native lorebook shared from another install could be hidden in every chat because its chat IDs belonged to that install. JSON cards saved with a BOM were rejected by the server even though the browser accepted them. None of these are new in this release. All came from a release-prep audit and were confirmed by two independent verifiers. No issue is linked yet; one should be opened before the PR is submitted. ## What changed - Behaviour changes for existing users: (1) Reimport Embedded Lorebook no longer renames the linked lorebook or resets its category or max recursion depth. It replaces the entries, applies the card book's own settings and keeps everything else. (2) Import and Reimport Embedded Lorebook now use the card book's token budget. A book that only carries the schema default of 512 (for example one created in Marinara without a token budget) now imports at 512 instead of 2048, so fewer lore entries fit. (3) A native lorebook that was Global but limited to specific chats, none of which exist on the importing install, is imported as non-global with an All chats scope. It no longer joins every chat, and you add it where you want it. Nothing else that worked before stops working. - Outlet entries (position 7) in a card's embedded lorebook stay Outlet entries, with their outlet name, through Character editor saves, skip-lorebook imports, reimports and exports. - SillyTavern/Chub cards now import each embedded entry's real settings from `extensions`: @D position and depth, role, probability, selective logic, group and weight, sticky/cooldown/delay, scan depth, case and whole-word matching, the recursion flags, vectorized and outlet name. - The synced card book also writes these settings under each entry's `extensions`, in SillyTavern's numbering, so Compatible character exports keep their placement and triggers in SillyTavern. - Import Embedded Lorebook now uses the card book's description, scan depth, token budget and recursion setting instead of import defaults. - Importing a Marinara native lorebook keeps only the character, persona and chat links that exist on this install. A 'Specific chats' scope with none of its chats here becomes 'All chats', so the lorebook is no longer hidden everywhere and can be added to a chat. - JSON character cards and .charx card.json files saved with a UTF-8 byte order mark now inspect and import instead of failing with 'Invalid file format'. ## Validation <!-- These are human tasks. Never tick a box for a check that was not actually performed. --> - [ ] `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 - Manually verify in the browser: in a character's linked embedded lorebook, set an entry's Position to Outlet and give it a name. Save the character in the Character editor, click Reimport Embedded Lorebook, and confirm the entry is still Outlet with the same name. - Manually verify: import a real SillyTavern-exported PNG card whose embedded lorebook uses @D depth/role, a probability below 100 and NOT ANY logic. Open the linked lorebook and confirm the position (At Depth), depth, role, probability and logic match SillyTavern. - Manually verify: export that character as Compatible, import it into SillyTavern, and check in its World Info editor that each entry's position, depth, role, probability and logic survived. - Manually verify: on a linked embedded lorebook, change the name, category, scan depth and token budget, then click Reimport Embedded Lorebook. Confirm the name and category are unchanged and the scan depth and token budget match the card. - Manually verify: create a character in Marinara with an embedded lorebook that never had a token budget set, click Import Embedded Lorebook, and confirm the linked lorebook's token budget is 512. Then decide whether 512 is acceptable or should stay 2048. - Manually verify: export a lorebook assigned with 'Specific chats' as Marinara Native and import it on a different install. Confirm it shows in Chat Settings > Lorebooks and can be added to a chat. - Manually verify: export a Global lorebook that is limited to 'Specific chats' as Marinara Native and import it on a different install. Confirm it is not Global there and its entries do not appear in a chat it was not added to. - Manually verify: save a SillyTavern V2 .json card with Notepad's 'UTF-8 with BOM' encoding, import it through Import Character, and confirm the inspect step lists it and the import succeeds. Agent-run checks (for reference; each regression was shown to fail without its fix): - `node ./scripts/pnpm-runner.mjs build:shared`: passed. Rebuilt again after the review fix; the dist contains z.literal(7). - `cd packages/server && npx tsc --noEmit`: passed (no output), after the review fix - `node ./scripts/pnpm-runner.mjs --filter @marinara-engine/server lint (tsc -b ../shared && tsc --noEmit && tsc -p tsconfig.character-activity.json)`: exit 0, after the review fix - `node ./scripts/pnpm-runner.mjs --filter @marinara-engine/shared lint`: exit 0 (first round; shared not touched in the review round) - `node_modules/.bin/prettier --write, then --check, on the changed files`: --check exit 0. In this round --write left both changed files unchanged. - `eslint on changed files`: Not run: only packages/client has an ESLint config, and no client files changed. The server and shared lint scripts (tsc) ran instead. - `pnpm localization:check`: Not run: no client copy changed. - `node ./scripts/run-regressions.mjs --filter native-lorebook-import-links`: 1/1 passed - `Fails-before for the review fix: copy marinara.importer.ts aside, revert only the isGlobal guard with sed, run the regression, restore, then cmp`: FAILED on 'a global book whose chats are all gone is imported as non-global' (actual true). With the isGlobal assertion also disabled temporarily, it FAILED on 'so it does not join chats it was never - `node ./scripts/run-regressions.mjs --filter lorebook`: 39/39 passed - `node ./scripts/run-regressions.mjs --filter import`: 19/19 passed - `node ./scripts/run-regressions.mjs --filter character`: 31/31 passed Independent adversarial review: changes, 2 issue(s), all addressed in a fix-up pass. Known limits and risks: Both review issues held when checked against the code and are addressed. (1) Global plus collapsed scope. listActiveEntries runs isLorebookScopeActiveForChat first and then `if (b.isGlobal) return true` (lorebooks.storage.ts:833-845). Commit 3a58e678d4 therefore imports a book as non-global when its specific scope collapses, with a regression case that calls listActiveEntries for an unrelated chat. Open edge case: a non-global book that keeps links to local characters but loses all of its scoped chats (only possible on the same install after those chats were deleted) becomes active in every chat with that character, where before it was hidden everywhere. I left that alone because it is the normal character-link behaviour and the finding did not cover it. (2) token_budget. characterBookSchema defaults token_budget to 512 (character.schema.ts:148), and Import Embedded now passes the card book through. A book that was never given a budget and never synced from a lorebook (character-book-sync writes the lorebook's tokenBudget back to the card) now imports, and reimports, at 512 instead of 2048. I made the text fix the reviewer called sufficient: the first What changed bullet now lists it. Keeping 2048 would mean removing the schema default or recording whether token_budget was set explicitly. That is a larger change, left for the maintainer. Other risks carried over: - The SillyTavern extension key names and numbering come from knowledge of SillyTavern's world-info.js (convertWorldInfoToCharacterBook / convertCharacterBook), not a local ST source. Both verifiers had the same limit. - Precedence: top-level values win. extensions.position applies only when the top-level position is missing or is the agreeing before_char/after_char string. - The schema always writes a top-level case_sensitive, so camelCase caseSensitive is filled from extensions.case_sensitive. A hand-edited top-level case_sensitive with a stale extensions value would lose. - The mirror adds about 19 keys per entry to data.character_book, so cards and PNG metadata grow. - ST extension keys Marinara does not model (display_index, automation_id, group_override, use_group_scoring, triggers, ignore_budget...) are still dropped by the sync. - `vectorized` is mirrored as !excludeFromVectorization, matching the existing Compatible World Info export. - Native lorebook entry characterFilterIds can still name foreign characters; that was outside the finding. Not addressed (reviewer nits, outside the findings): - backup.routes.ts keeps its own copies of stSelectiveLogic/stPosition. - st-bulk.importer.ts still reads ST folder JSON with readFile('utf-8') + JSON.parse, so the same BOM problem remains there. - importSTLorebook's result.category reports detectedCategory on reimport although the category is no longer updated; no caller reads it on that path. chat-request-validation times out at 90 s on this machine with and without these changes. Not done: the user asked to make showing an outfit to a love interest in Otome Hearth give +5 affection instead of +20. That is outside this workflow step and worktree, and git grep finds no Otome Hearth outfit/affection logic in Marinara Engine. It belongs to the Otome Hearth (otome-forge) package repository, needs its own task, and must not be reported as done. ## Docs and release impact - [ ] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README) 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) 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-07 17:39:49 +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#8804
No description provided.