[GH-ISSUE #3347] [Issue]: Native lorebook import flattens nested folders (parentFolderId dropped) #1815

Closed
opened 2026-10-21 05:42:58 +07:00 by bsakaguchi · 0 comments
Owner

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

Originally assigned to: @kolacheee on GitHub.

Summary

Nested lorebook folders (a shipped feature — you can drag a folder into another folder in the editor to nest it) are flattened to the root level when a lorebook is imported from a native (.marinara.json) export. The export correctly records each folder's parentFolderId, but the importer hardcodes parentFolderId: null, so the entire folder hierarchy collapses to a single flat level on import.

Expected behavior

Importing a natively-exported lorebook reproduces the exact folder tree, including sub-folders nested under their parents. A native export is Marinara's own round-trip format, so it should preserve everything Marinara supports — and folders can nest.

Actual behavior

Every imported folder lands at the top level; all nesting is lost. Entries still land inside the correct folder (folder IDs are remapped), but the parent/child relationship between folders is gone — a sub-folder that was inside "Locations" is now a sibling of it at the root.

Steps to reproduce

  1. Create a lorebook. Add a folder "Locations", then add a folder "Cities" and drag it into "Locations" so "Cities" is nested under it.
  2. Export the lorebook in Native (.marinara.json) format.
  3. (Optional) Inspect the file: data.folders[] contains "Cities" with parentFolderId set to "Locations"' id — the nesting is in the export.
  4. Import the file into a fresh instance.
  5. "Locations" and "Cities" both appear at the top level — "Cities" is no longer inside "Locations".

Environment

  • Marinara Engine version: 2.1.1 (staging)
  • Install type: source
  • OS + version: N/A (server-side data-shape issue)
  • Browser or app shell: N/A

Logs, screenshots, or video

N/A (data-shape issue). Code references:

  • Export carries parentFolderId. Native lorebook export emits data: { lorebook, entries, folders } (packages/server/src/routes/lorebooks.routes.ts:521 and :556), where folders = storage.listFolders(id) → parseFolderRow includes parentFolderId (packages/server/src/services/storage/lorebooks.storage.ts:216, :664).
  • Import discards it. importLorebookPayload's folder-restore loop hardcodes parentFolderId: null (packages/server/src/services/import/marinara.importer.ts:508, comment // v1 ignores nesting on import).

Nesting is fully implemented end-to-end, so the "v1 is flat" assumption behind that line no longer holds:

  • Create route validates the parent exists (lorebooks.routes.ts:752-759); PATCH reparent runs a full descendant-cycle check (canReparentFolder, lorebooks.routes.ts:770).
  • Storage createFolder / updateFolder both honor parentFolderId (lorebooks.storage.ts:967, :984); cloneFolder deep-clones subtrees; delete supports ?cascade subtree deletion.
  • Client drag-to-nest drives it (packages/client/src/components/lorebooks/LorebookEditor.tsx:1081, :1232, :1255).

The "flat in v1 / reserved / always null" comments in db/schema/lorebooks.ts:68-69 & :82, lorebooks.storage.ts:966, and marinara.importer.ts:508 are all stale — nested folders shipped after those comments were written.

Additional context

Two import paths hit the same flattening (both funnel through importLorebookPayload):

  1. The standalone lorebook native export/import round-trip (primary; lorebooks.routes.ts export → marinara.importer.ts import).
  2. #3342's character-bundled lorebook import (importBundledLorebooks → same importLorebookPayload).

This is independent of the linked-lorebook export bug (#3345, "revert #3342's attached-lorebook bundling"): even if that bundling is reverted, the standalone native round-trip still flattens nesting. It is also distinct from the embed feature (#3341 / #3343), where folders are dropped entirely because the V2 character_book shape has no folder concept — a separate limitation, not this flattening.

Suggested fix — make the importer's folder restore a two-pass operation:

  1. Pass 1 (unchanged): create all folders at root, building the existing folderIdRemap (old id → new id).
  2. Pass 2: walk d.folders again and, for each folder whose exported parentFolderId is non-null, call updateFolder(newId, { parentFolderId: folderIdRemap.get(oldParentId) }).

Two passes make it order-independent (every new id exists before any reparent), and reusing the existing canReparentFolder validation keeps it safe against malformed exports. While there, refresh the stale "v1 / flat / reserved / always null" comments in marinara.importer.ts:508, lorebooks.storage.ts:966, and db/schema/lorebooks.ts:68-69 & :82.

Template check

Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked:

  • I DID NOT read this template and provide the requested details.
Originally created by @kolacheee on GitHub (Jul 7, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3347 Originally assigned to: @kolacheee on GitHub. ## Summary Nested lorebook folders (a shipped feature — you can drag a folder into another folder in the editor to nest it) are **flattened to the root level** when a lorebook is imported from a **native** (`.marinara.json`) export. The export correctly records each folder's `parentFolderId`, but the importer hardcodes `parentFolderId: null`, so the entire folder hierarchy collapses to a single flat level on import. ## Expected behavior Importing a natively-exported lorebook reproduces the exact folder tree, including sub-folders nested under their parents. A native export is Marinara's own round-trip format, so it should preserve everything Marinara supports — and folders can nest. ## Actual behavior Every imported folder lands at the top level; all nesting is lost. Entries still land inside the correct folder (folder IDs are remapped), but the parent/child relationship *between* folders is gone — a sub-folder that was inside "Locations" is now a sibling of it at the root. ## Steps to reproduce 1. Create a lorebook. Add a folder "Locations", then add a folder "Cities" and drag it into "Locations" so "Cities" is nested under it. 2. Export the lorebook in **Native** (`.marinara.json`) format. 3. (Optional) Inspect the file: `data.folders[]` contains "Cities" with `parentFolderId` set to "Locations"' id — the nesting is in the export. 4. Import the file into a fresh instance. 5. "Locations" and "Cities" both appear at the **top level** — "Cities" is no longer inside "Locations". ## Environment - Marinara Engine version: 2.1.1 (staging) - Install type: source - OS + version: N/A (server-side data-shape issue) - Browser or app shell: N/A ## Logs, screenshots, or video N/A (data-shape issue). Code references: - **Export carries `parentFolderId`.** Native lorebook export emits `data: { lorebook, entries, folders }` (`packages/server/src/routes/lorebooks.routes.ts:521` and `:556`), where `folders` = `storage.listFolders(id)` → `parseFolderRow` includes `parentFolderId` (`packages/server/src/services/storage/lorebooks.storage.ts:216`, `:664`). - **Import discards it.** `importLorebookPayload`'s folder-restore loop hardcodes `parentFolderId: null` (`packages/server/src/services/import/marinara.importer.ts:508`, comment `// v1 ignores nesting on import`). Nesting is fully implemented end-to-end, so the "v1 is flat" assumption behind that line no longer holds: - Create route validates the parent exists (`lorebooks.routes.ts:752-759`); PATCH reparent runs a full descendant-cycle check (`canReparentFolder`, `lorebooks.routes.ts:770`). - Storage `createFolder` / `updateFolder` both honor `parentFolderId` (`lorebooks.storage.ts:967`, `:984`); `cloneFolder` deep-clones subtrees; delete supports `?cascade` subtree deletion. - Client drag-to-nest drives it (`packages/client/src/components/lorebooks/LorebookEditor.tsx:1081`, `:1232`, `:1255`). The "flat in v1 / reserved / always null" comments in `db/schema/lorebooks.ts:68-69` & `:82`, `lorebooks.storage.ts:966`, and `marinara.importer.ts:508` are all **stale** — nested folders shipped after those comments were written. ## Additional context Two import paths hit the same flattening (both funnel through `importLorebookPayload`): 1. The **standalone** lorebook native export/import round-trip (primary; `lorebooks.routes.ts` export → `marinara.importer.ts` import). 2. #3342's **character-bundled** lorebook import (`importBundledLorebooks` → same `importLorebookPayload`). This is **independent** of the linked-lorebook export bug (#3345, "revert #3342's attached-lorebook bundling"): even if that bundling is reverted, the standalone native round-trip still flattens nesting. It is also distinct from the embed feature (#3341 / #3343), where folders are dropped *entirely* because the V2 `character_book` shape has no folder concept — a separate limitation, not this flattening. **Suggested fix** — make the importer's folder restore a two-pass operation: 1. Pass 1 (unchanged): create all folders at root, building the existing `folderIdRemap` (old id → new id). 2. Pass 2: walk `d.folders` again and, for each folder whose exported `parentFolderId` is non-null, call `updateFolder(newId, { parentFolderId: folderIdRemap.get(oldParentId) })`. Two passes make it order-independent (every new id exists before any reparent), and reusing the existing `canReparentFolder` validation keeps it safe against malformed exports. While there, refresh the stale "v1 / flat / reserved / always null" comments in `marinara.importer.ts:508`, `lorebooks.storage.ts:966`, and `db/schema/lorebooks.ts:68-69` & `:82`. ## Template check Please **uncheck (untick)** the box below before submitting so we know you read the template. It is intentionally pre-checked: - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:42:58 +07:00
  • closed this issue
  • added the
    in-pr
    bug
    labels
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#1815
No description provided.