[PR #3348] [MERGED] fix(lorebooks): preserve nested folders on native import #5892

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

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/3348
Author: @kolacheee
Created: 7/7/2026
Status: ✅ Merged
Merged: 7/7/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/3347-lorebook-folder-nesting-import


📝 Commits (2)

  • df66f3e fix(lorebooks): preserve nested folders on native import (#3347)
  • b1d02d7 Merge branch 'staging' into fix/3347-lorebook-folder-nesting-import

📊 Changes

5 files changed (+48 additions, -14 deletions)

View changed files

📝 CHANGELOG.md (+1 -0)
📝 packages/server/src/db/schema/lorebooks.ts (+4 -3)
📝 packages/server/src/services/import/marinara.importer.ts (+36 -6)
📝 packages/server/src/services/storage/lorebooks.storage.ts (+2 -1)
📝 packages/shared/src/schemas/lorebook.schema.ts (+5 -4)

📄 Description

Linked issue

Closes #3347

Why this change

Lorebook folders can be nested — you drag one folder into another in the editor, the server validates the move with canReparentFolder, and cloneFolder deep-copies subtrees. A native (.marinara.json) lorebook export records every folder's parentFolderId, so the nesting is present in the exported file. But the importer hardcoded parentFolderId: null, so on import every nested folder was flattened to the top level — silently losing structure the user built. This hits the standalone lorebook native round-trip (and any path through importLorebookPayload).

What changed

  • packages/server/src/services/import/marinara.importer.ts — folder restore is now two passes: pass 1 creates every folder at root and builds the old→new id remap (a child can be listed before its parent, so parents can't be set inline); pass 2 re-parents each folder through the remap. Every move is validated with canReparentFolder (the same guard the folder PATCH route uses), so a malformed or hand-edited export can never persist a cycle — an unresolvable or cyclic parent just leaves that folder at root, matching the editor's buildFolderForest behavior.
  • Refreshed four now-stale comments that claimed folders are "flat in v1 / reserved / always null": the DB schema (db/schema/lorebooks.ts), the canonical shared Zod folder schema (packages/shared/src/schemas/lorebook.schema.ts), createFolder in lorebooks.storage.ts, and the importer. Nesting shipped after those comments were written.

No schema or API change. Entries' folder assignment already round-tripped correctly (via the same folderIdRemap) and is unchanged.

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

  • Server typecheck passes locally: pnpm --filter @marinara-engine/server lint (tsc). Full pnpm check is blocked on Windows by the pre-existing v2.1.0 build.mjs execPath spawn bug, which is unrelated to this change.
  • Manually verify in-app: create a lorebook, make a folder "Cities" and nest it inside "Locations", export in Native format, import into a fresh instance, and confirm "Cities" is still nested inside "Locations" (not a root-level sibling).
  • Manually verify an older export (no folders, or folders without parentFolderId) still imports with all entries at root and no error.
  • Manually verify a hand-edited export containing a cyclic parentFolderId imports without hanging — the cyclic folder lands at root.

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)

CHANGELOG updated with a Fixed entry. No version bump.

UI evidence (if applicable)

N/A — server-side import logic; no UI change.


🔄 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/3348 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 7/7/2026 **Status:** ✅ Merged **Merged:** 7/7/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/3347-lorebook-folder-nesting-import` --- ### 📝 Commits (2) - [`df66f3e`](https://github.com/Pasta-Devs/Marinara-Engine/commit/df66f3e254c97f7d39ddf819b256cc0c12170f3c) fix(lorebooks): preserve nested folders on native import (#3347) - [`b1d02d7`](https://github.com/Pasta-Devs/Marinara-Engine/commit/b1d02d7408ac3a023e790baefea76d6972b613c3) Merge branch 'staging' into fix/3347-lorebook-folder-nesting-import ### 📊 Changes **5 files changed** (+48 additions, -14 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+1 -0) 📝 `packages/server/src/db/schema/lorebooks.ts` (+4 -3) 📝 `packages/server/src/services/import/marinara.importer.ts` (+36 -6) 📝 `packages/server/src/services/storage/lorebooks.storage.ts` (+2 -1) 📝 `packages/shared/src/schemas/lorebook.schema.ts` (+5 -4) </details> ### 📄 Description ## Linked issue Closes #3347 ## Why this change Lorebook folders can be **nested** — you drag one folder into another in the editor, the server validates the move with `canReparentFolder`, and `cloneFolder` deep-copies subtrees. A native (`.marinara.json`) lorebook export records every folder's `parentFolderId`, so the nesting is present in the exported file. But the importer hardcoded `parentFolderId: null`, so on import every nested folder was flattened to the top level — silently losing structure the user built. This hits the standalone lorebook native round-trip (and any path through `importLorebookPayload`). ## What changed - `packages/server/src/services/import/marinara.importer.ts` — folder restore is now **two passes**: pass 1 creates every folder at root and builds the old→new id remap (a child can be listed before its parent, so parents can't be set inline); pass 2 re-parents each folder through the remap. Every move is validated with `canReparentFolder` (the same guard the folder `PATCH` route uses), so a malformed or hand-edited export can never persist a cycle — an unresolvable or cyclic parent just leaves that folder at root, matching the editor's `buildFolderForest` behavior. - Refreshed four now-stale comments that claimed folders are "flat in v1 / reserved / always null": the DB schema (`db/schema/lorebooks.ts`), the canonical shared Zod folder schema (`packages/shared/src/schemas/lorebook.schema.ts`), `createFolder` in `lorebooks.storage.ts`, and the importer. Nesting shipped after those comments were written. No schema or API change. Entries' folder assignment already round-tripped correctly (via the same `folderIdRemap`) and is unchanged. ## 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 - Server typecheck passes locally: `pnpm --filter @marinara-engine/server lint` (tsc). Full `pnpm check` is blocked on Windows by the pre-existing v2.1.0 `build.mjs` execPath spawn bug, which is unrelated to this change. - Manually verify in-app: create a lorebook, make a folder "Cities" and nest it inside "Locations", export in **Native** format, import into a fresh instance, and confirm "Cities" is still nested inside "Locations" (not a root-level sibling). - Manually verify an **older** export (no `folders`, or folders without `parentFolderId`) still imports with all entries at root and no error. - Manually verify a hand-edited export containing a **cyclic** `parentFolderId` imports without hanging — the cyclic folder lands at root. ## 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) CHANGELOG updated with a Fixed entry. No version bump. ## UI evidence (if applicable) N/A — server-side import logic; no UI change. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-23 04:04:53 +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#5892
No description provided.