[PR #3497] [MERGED] fix(lorebooks): preserve folder nesting in legacy profile import and the mari CLI (#3483) #5962

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

📋 Pull Request Information

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

Base: staging ← Head: fix/3483-lorebook-folder-nesting


📝 Commits (2)

  • 3921490 fix(lorebooks): preserve folder nesting in legacy profile import and the mari CLI
  • ac1ec17 fix(lorebooks): validate folder graph in legacy import and document --folder-id in group help

📊 Changes

3 files changed (+69 additions, -10 deletions)

View changed files

📝 packages/server/src/routes/backup.routes.ts (+38 -1)
📝 packages/server/src/services/mari-db/mari-db.service.ts (+25 -5)
📝 packages/shared/src/types/lorebook.ts (+6 -4)

📄 Description

Linked issue

Fixes #3483

Why this change

Nested lorebook folders shipped in #2602, but two code paths still silently flatten or drop folder placement, and the shared type contract still tells every reader (human or agent) that folders are flat. That stale contract is exactly the comment class that caused #3348. This PR makes all three tell the truth: the legacy import preserves nesting, the Mari CLI can place entries in folders, and the JSDoc describes the live behavior.

What changed

  • packages/server/src/routes/backup.routes.ts — the legacy (pre-snapshot) branch of POST /import-profile created every folder with parentFolderId: null even though it already built the old→new folderIdMap. Added a second pass that remaps parents through the fully-populated map, mirroring the exact two-pass pattern the preset-group import in the same handler already uses (including per-item try/catch and the lorebook-scoped updateFolder overload).
  • packages/server/src/services/mari-db/mari-db.service.ts — mari lorebooks add-entry / update-entry never read or assigned folderId, so folder placement was silently dropped. Both now accept --folder-id, validated against the entry's lorebook via the same getRawById(getMeta(...)) pattern the handlers already use (unknown folder = clear error, no dangling FK written); update-entry --folder-id none clears placement. Usage strings updated.
  • packages/shared/src/types/lorebook.ts — the LorebookFolder JSDoc claimed "Folders are flat in v1 … the server rejects non-null values" while lorebooks.routes.ts:752-773 validates and re-parents nested folders. Both comments now describe the live contract (parent must exist in the same lorebook; self-parent and cycles rejected via canReparentFolder).

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

Runtime-validated end to end against a scratch server (fresh DATA_DIR, port 7899), exercising the real routes and the real CLI rather than unit stubs:

  1. Legacy import preserves nesting: POSTed a hand-built legacy marinara_profile (no fileStorage snapshot, so it takes the legacy branch) containing a three-level folder chain (Parent → Child → Grandchild) plus an entry in the grandchild. After import, GET /api/lorebooks/:id/folders shows the chain intact with remapped ids (Child.parentFolderId = new Parent id, Grandchild.parentFolderId = new Child id) and the entry's folderId pointing at the new grandchild. Before this fix, all three parents were hardcoded null.
  2. Mari CLI folder placement: mari lorebooks add-entry <id> --folder-id <grandchild> --apply places the entry (verified via the entries endpoint); --folder-id not-a-real-folder is rejected with "Folder … not found in lorebook …" and writes nothing; update-entry --folder-id <parent> moves the entry and --folder-id none clears it to null (each state verified after the call).

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)

(The in-app lorebook docs already describe nested folders; this PR makes the code and contracts match them.)

UI evidence (if applicable)

N/A — server-side import/CLI behavior; the folder tree UI is unchanged.

Summary by CodeRabbit

  • New Features

    • Lorebook entries can now be assigned to folders when created or updated.
    • Folder assignments can be cleared when updating an entry.
    • Lorebook folder imports now preserve nested folder relationships.
  • Bug Fixes

    • Folder assignments are validated against the correct lorebook to prevent invalid associations.
  • Documentation

    • Updated lorebook folder documentation to describe nested folders, root folders, and nesting validation.

🔄 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/3497 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 7/11/2026 **Status:** ✅ Merged **Merged:** 7/11/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/3483-lorebook-folder-nesting` --- ### 📝 Commits (2) - [`3921490`](https://github.com/Pasta-Devs/Marinara-Engine/commit/3921490713e5dfd33324dc9f668acbddfd63261d) fix(lorebooks): preserve folder nesting in legacy profile import and the mari CLI - [`ac1ec17`](https://github.com/Pasta-Devs/Marinara-Engine/commit/ac1ec17fc41ed1de670ed6a579efdb5c3178536d) fix(lorebooks): validate folder graph in legacy import and document --folder-id in group help ### 📊 Changes **3 files changed** (+69 additions, -10 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/routes/backup.routes.ts` (+38 -1) 📝 `packages/server/src/services/mari-db/mari-db.service.ts` (+25 -5) 📝 `packages/shared/src/types/lorebook.ts` (+6 -4) </details> ### 📄 Description ## Linked issue Fixes #3483 ## Why this change Nested lorebook folders shipped in #2602, but two code paths still silently flatten or drop folder placement, and the shared type contract still tells every reader (human or agent) that folders are flat. That stale contract is exactly the comment class that caused #3348. This PR makes all three tell the truth: the legacy import preserves nesting, the Mari CLI can place entries in folders, and the JSDoc describes the live behavior. ## What changed - `packages/server/src/routes/backup.routes.ts` — the legacy (pre-snapshot) branch of `POST /import-profile` created every folder with `parentFolderId: null` even though it already built the old→new `folderIdMap`. Added a second pass that remaps parents through the fully-populated map, mirroring the exact two-pass pattern the preset-group import in the same handler already uses (including per-item try/catch and the lorebook-scoped `updateFolder` overload). - `packages/server/src/services/mari-db/mari-db.service.ts` — `mari lorebooks add-entry` / `update-entry` never read or assigned `folderId`, so folder placement was silently dropped. Both now accept `--folder-id`, validated against the entry's lorebook via the same `getRawById(getMeta(...))` pattern the handlers already use (unknown folder = clear error, no dangling FK written); `update-entry --folder-id none` clears placement. Usage strings updated. - `packages/shared/src/types/lorebook.ts` — the `LorebookFolder` JSDoc claimed "Folders are flat in v1 … the server rejects non-null values" while `lorebooks.routes.ts:752-773` validates and re-parents nested folders. Both comments now describe the live contract (parent must exist in the same lorebook; self-parent and cycles rejected via `canReparentFolder`). ## Validation - [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 Runtime-validated end to end against a scratch server (fresh `DATA_DIR`, port 7899), exercising the real routes and the real CLI rather than unit stubs: 1. **Legacy import preserves nesting:** POSTed a hand-built legacy `marinara_profile` (no `fileStorage` snapshot, so it takes the legacy branch) containing a three-level folder chain (Parent → Child → Grandchild) plus an entry in the grandchild. After import, `GET /api/lorebooks/:id/folders` shows the chain intact with remapped ids (`Child.parentFolderId` = new Parent id, `Grandchild.parentFolderId` = new Child id) and the entry's `folderId` pointing at the new grandchild. Before this fix, all three parents were hardcoded null. 2. **Mari CLI folder placement:** `mari lorebooks add-entry <id> --folder-id <grandchild> --apply` places the entry (verified via the entries endpoint); `--folder-id not-a-real-folder` is rejected with "Folder … not found in lorebook …" and writes nothing; `update-entry --folder-id <parent>` moves the entry and `--folder-id none` clears it to null (each state verified after the call). ## 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) (The in-app lorebook docs already describe nested folders; this PR makes the code and contracts match them.) ## UI evidence (if applicable) N/A — server-side import/CLI behavior; the folder tree UI is unchanged. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Lorebook entries can now be assigned to folders when created or updated. - Folder assignments can be cleared when updating an entry. - Lorebook folder imports now preserve nested folder relationships. - **Bug Fixes** - Folder assignments are validated against the correct lorebook to prevent invalid associations. - **Documentation** - Updated lorebook folder documentation to describe nested folders, root folders, and nesting validation. <!-- 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:05: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#5962
No description provided.