[PR #2353] [MERGED] feat: nested lorebook folders #5555

Closed
opened 2026-10-22 04:17:20 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/2353
Author: @kolacheee
Created: 6/5/2026
Status: ✅ Merged
Merged: 6/6/2026
Merged by: @Xelvanis

Base: refactor ← Head: feat/lorebook-nested-folders


📝 Commits (8)

  • 15687d7 feat: nested lorebook folders
  • 713a0ff Address nested-folder review feedback
  • 80ea2ac Validate lorebook folder ancestry at the storage path
  • 087f521 Refine lorebook folder validation and forest rescue from review
  • 490a9d5 Make a lorebook folder's lorebookId immutable on patch
  • dc9ec7b Address lorebook folder review findings
  • 9b2153a Fix lorebook folder source reorder
  • 2ea62e5 Tighten lorebook folder test fixtures

📊 Changes

15 files changed (+1275 additions, -334 deletions)

View changed files

📝 src-tauri/src/commands/storage/commands/entities.rs (+647 -84)
📝 src-tauri/src/http_dispatch.rs (+19 -0)
📝 src-tauri/src/lib.rs (+1 -0)
📝 src/engine/contracts/schemas/lorebook.schema.ts (+5 -3)
📝 src/engine/contracts/types/lorebook.ts (+12 -10)
📝 src/engine/generation/active-lorebook-scanner.ts (+37 -6)
📝 src/features/catalog/lorebooks/components/editor/LorebookEditor.tsx (+19 -6)
📝 src/features/catalog/lorebooks/components/editor/LorebookEntriesTab.tsx (+165 -137)
📝 src/features/catalog/lorebooks/components/editor/use-lorebook-editor-drag-drop.ts (+133 -32)
📝 src/features/catalog/lorebooks/components/editor/use-lorebook-entry-selection.ts (+13 -3)
📝 src/features/catalog/lorebooks/components/entries/LorebookFolderRow.tsx (+71 -37)
📝 src/features/catalog/lorebooks/hooks/use-lorebooks.ts (+22 -16)
➕ src/features/catalog/lorebooks/lib/lorebook-folder-tree.ts (+119 -0)
➕ src/shared/api/lorebook-folder-api.ts (+11 -0)
📝 src/shared/api/remote-runtime.ts (+1 -0)

📄 Description

Linked issue

Closes #2127 — this is the nested-folders half. (The intra-lorebook entry-duplication half shipped earlier in #2157.)

Why this change

  • #2127 asked for lorebook sub-folders. Folders were flat — parentFolderId existed but was reserved/always-null. This ships nesting end-to-end: organize a lorebook's folders into a tree, set parentage by dropdown or drag, and (the load-bearing part) extend the folder-disabled activation gate to disabled ancestors, so nesting can never silently leak gated content into the prompt.

What changed

  • Scanner gate (active-lorebook-scanner.ts) — activation now resolves "effective disabled" by walking each entry's folder parentFolderId chain (with a cycle guard): a folder OR any of its ancestors being disabled gates the entry, regardless of the entry's own flag.
  • Validation + tree model (lib/lorebook-folder-tree.ts) — canReparentFolder is the single write-time guard (same lorebook, no self-parent, no cycles, with reasons); buildFolderForest groups folders into { roots, childrenByParent } for rendering. A dangling/missing parent falls back to root, which auto-promotes a deleted folder's children to the top level.
  • Storage (hooks/use-lorebooks.ts) — reorderLorebookFolders can now also set parentFolderId, so nest/un-nest + per-parent ordering happen in one pass; reorder/reparent invalidates the activation cache.
  • Editor UI — recursive folder-tree render with depth indent + collapse-hides-subtree (LorebookEntriesTab.tsx); a per-folder parent dropdown filtered to valid parents (LorebookFolderRow.tsx); drag-to-nest via the header (top = reorder before, middle = nest, bottom = reorder after) and via a folder's body including the empty placeholder, with the left-indent margin targeting shallower ancestors; drag to the root area to un-nest (use-lorebook-editor-drag-drop.ts, LorebookEditor.tsx).
  • Folder type/schema comments updated to describe real nesting (no longer "reserved / always null").

Refactor impact

Primary owner: Catalog / Lorebooks editor (frontend), plus the activation scanner (engine generation)

Impact areas reviewed:

  • The lorebook editor (recursive render + folder drag-drop), the new folder-tree lib, the activation scanner's disabled-folder gate, and the folder reorder hook. No engine/shared-API/Rust/remote-runtime changes; no storage-contract or src-tauri/src/lib.rs change.

Boundary notes:

  • Confined to the lorebooks feature UI + its React Query layer + the activation scanner's normalization step. Reuses canReparentFolder as the one write-time guard and the existing generic storage gateway; no new storage collection, command, or cross-boundary path.

Pressure points touched:

  • The activation scanner (generation path) — specifically the disabled-folder gate, now ancestor-aware. No ModeSurface/GameSurface/shared mode UI, no src-tauri/src/lib.rs, no imports, no version-bearing files.

Validation

  • Matching validation command passes locally (for example pnpm typecheck, pnpm build, pnpm check:architecture, pnpm check:docs, or full pnpm check when warranted)
  • Full pnpm check passes before PR push/handoff
  • Human/manual validation completed by contributor or reviewer

Manual verification notes

  • pnpm typecheck plus check:architecture / check:frontend / check:rust / check:discovery / check:docs / check:launcher-safety / check:unused all pass locally. check:line-endings fails only on pre-existing CRLF in files this PR doesn't touch (DESIGN.md, custom_components/*.py); their committed versions are LF, so CI is unaffected.
  • Manually verified in the running Tauri build: nesting via the parent dropdown and via drag (header three-zone, folder body, empty placeholder, left-indent targeting); un-nesting via the root area and the dropdown; cycle / self / cross-lorebook moves are never offered as drop targets; collapsing a parent hides its subtree; deleting a parent promotes its children to root; checked light + dark and at ~400px width.

Feature Discoverability

  • Updated src/features/shell/discovery/ because this PR adds or materially changes a user-discoverable feature, workflow, setting, mode, panel, import path, agent, media capability, or advanced tool.
  • N/A because this PR is only a bugfix, refactor, test, docs, internal wiring, visual polish, copy edit, or compatibility fix and does not add a new thing users need to find.

Reason:

  • N/A — sub-folders are an enhancement within the already-registered Lorebooks surface (the lorebooks discovery entry opens the panel). The registry is surface-level, not per-action, so no new entry is required and check:discovery passes unchanged (same call as the entry-duplication PR #2157).

Docs and release impact

  • No docs changes needed
  • Updated README.md
  • Updated CONTRIBUTING.md
  • Updated docs/developer/
  • Updated repo skills or AGENTS.md
  • Confirmed this PR does not restore old staging/package-workspace/release claims

Proof

Core claim:

  • Lorebook folders nest into a tree; a disabled folder or any disabled ancestor gates its entries at activation time; re-parenting is validated at write time (no self-parent / cross-lorebook / cycles) and the scanner can't loop or leak on malformed data.

Verification run:

  • The full check suite minus the pre-existing line-endings artifact (see Manual verification notes), plus hands-on testing in the running Tauri build.

Evidence:

  • Manual: a disabled parent/grandparent hides entries that live in an enabled child folder at generation time; the parent dropdown and every drag path only offer valid targets; collapse / delete / un-nest behave as described above, in light + dark and at ~400px.
  • Local unit tests (uncommitted — the repo gitignores *.test.ts, and the template asks not to submit them as proof) cover the scanner's ancestor chain-walk and the canReparentFolder / buildFolderForest edge cases (self, cross-lorebook, direct + deep cycles, dangling-parent→root). Happy to fold them in if the team wants them tracked.

Design notes (recorded for review):

  • Client-side validation + scanner backstop, by design. canReparentFolder runs client-side because the lorebook-folder collection is stored generically with no server-side field validation — matching how every other lorebook edit is validated. The activation scanner independently resolves disabled ancestors and guards against cycles at read time, so a malformed parent introduced by an import or a direct write can never hang generation or leak gated content. If the lorebook layer later moves into Rust, the same three checks (same lorebook, no self-parent, no cycle) port directly.
  • Deleting a parent folder promotes its children to the top level (orphan fallback in buildFolderForest) rather than cascade-deleting — consistent with how an entry whose folder is gone falls back to root.

Manual verification requested:

  • Reviewers can confirm by nesting a few folders (dropdown + drag), disabling a parent and checking that a nested entry does not inject in a chat, and deleting a parent to watch its children rise to the top level.

UI evidence

{FD93A120-C1AF-48A8-BB3C-F10246B0A196} {0B080A73-73DE-45C6-B1EC-BD85E6AF4E53} {A77F01B1-B64E-4715-A0A6-0854A38C40A4} {C7CCFAC4-8BF0-4B8A-942A-6F1039F7DD1E}

Summary by CodeRabbit

  • New Features

    • Added support for nested folders within loreBooks, allowing hierarchical organization of folder structures.
    • Enhanced drag-and-drop functionality with improved drop-zone targeting ("before," "inside," "after" positions).
    • Added parent folder selector dropdown in the folder editor for easy reparenting.
  • Bug Fixes

    • Fixed folder disablement logic to cascade down—entries under disabled folders or their ancestors are now properly skipped during activation.
  • Documentation

    • Updated inline documentation to clarify nested-folder behavior and write-time validation rules.

🔄 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/2353 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 6/5/2026 **Status:** ✅ Merged **Merged:** 6/6/2026 **Merged by:** [@Xelvanis](https://github.com/Xelvanis) **Base:** `refactor` ← **Head:** `feat/lorebook-nested-folders` --- ### 📝 Commits (8) - [`15687d7`](https://github.com/Pasta-Devs/Marinara-Engine/commit/15687d792e5187c5f7297cb0e3d9302711ceb3c5) feat: nested lorebook folders - [`713a0ff`](https://github.com/Pasta-Devs/Marinara-Engine/commit/713a0fff3d062b3a5e3667222c50c826d420749c) Address nested-folder review feedback - [`80ea2ac`](https://github.com/Pasta-Devs/Marinara-Engine/commit/80ea2aca44aad9cc0fd829a91c92f1ac05b1c5c8) Validate lorebook folder ancestry at the storage path - [`087f521`](https://github.com/Pasta-Devs/Marinara-Engine/commit/087f521afa582e1ff046afbc5492dad2625edb80) Refine lorebook folder validation and forest rescue from review - [`490a9d5`](https://github.com/Pasta-Devs/Marinara-Engine/commit/490a9d5b997fe93f1f24b1a963ace54a39e8fbc3) Make a lorebook folder's lorebookId immutable on patch - [`dc9ec7b`](https://github.com/Pasta-Devs/Marinara-Engine/commit/dc9ec7bf52fd0f4ff6f14528c5e7d88137d77b2a) Address lorebook folder review findings - [`9b2153a`](https://github.com/Pasta-Devs/Marinara-Engine/commit/9b2153a2ee296952b4dc6aad7c9d576c1ecfb96b) Fix lorebook folder source reorder - [`2ea62e5`](https://github.com/Pasta-Devs/Marinara-Engine/commit/2ea62e56559f82927c7df4ee90bf4260c0284f6f) Tighten lorebook folder test fixtures ### 📊 Changes **15 files changed** (+1275 additions, -334 deletions) <details> <summary>View changed files</summary> 📝 `src-tauri/src/commands/storage/commands/entities.rs` (+647 -84) 📝 `src-tauri/src/http_dispatch.rs` (+19 -0) 📝 `src-tauri/src/lib.rs` (+1 -0) 📝 `src/engine/contracts/schemas/lorebook.schema.ts` (+5 -3) 📝 `src/engine/contracts/types/lorebook.ts` (+12 -10) 📝 `src/engine/generation/active-lorebook-scanner.ts` (+37 -6) 📝 `src/features/catalog/lorebooks/components/editor/LorebookEditor.tsx` (+19 -6) 📝 `src/features/catalog/lorebooks/components/editor/LorebookEntriesTab.tsx` (+165 -137) 📝 `src/features/catalog/lorebooks/components/editor/use-lorebook-editor-drag-drop.ts` (+133 -32) 📝 `src/features/catalog/lorebooks/components/editor/use-lorebook-entry-selection.ts` (+13 -3) 📝 `src/features/catalog/lorebooks/components/entries/LorebookFolderRow.tsx` (+71 -37) 📝 `src/features/catalog/lorebooks/hooks/use-lorebooks.ts` (+22 -16) ➕ `src/features/catalog/lorebooks/lib/lorebook-folder-tree.ts` (+119 -0) ➕ `src/shared/api/lorebook-folder-api.ts` (+11 -0) 📝 `src/shared/api/remote-runtime.ts` (+1 -0) </details> ### 📄 Description ## Linked issue Closes #2127 — this is the **nested-folders** half. (The intra-lorebook entry-duplication half shipped earlier in #2157.) ## Why this change - #2127 asked for lorebook sub-folders. Folders were flat — `parentFolderId` existed but was reserved/always-null. This ships nesting end-to-end: organize a lorebook's folders into a tree, set parentage by dropdown or drag, and (the load-bearing part) extend the folder-disabled activation gate to disabled **ancestors**, so nesting can never silently leak gated content into the prompt. ## What changed - **Scanner gate (`active-lorebook-scanner.ts`)** — activation now resolves "effective disabled" by walking each entry's folder `parentFolderId` chain (with a cycle guard): a folder OR any of its ancestors being disabled gates the entry, regardless of the entry's own flag. - **Validation + tree model (`lib/lorebook-folder-tree.ts`)** — `canReparentFolder` is the single write-time guard (same lorebook, no self-parent, no cycles, with reasons); `buildFolderForest` groups folders into `{ roots, childrenByParent }` for rendering. A dangling/missing parent falls back to root, which auto-promotes a deleted folder's children to the top level. - **Storage (`hooks/use-lorebooks.ts`)** — `reorderLorebookFolders` can now also set `parentFolderId`, so nest/un-nest + per-parent ordering happen in one pass; reorder/reparent invalidates the activation cache. - **Editor UI** — recursive folder-tree render with depth indent + collapse-hides-subtree (`LorebookEntriesTab.tsx`); a per-folder **parent dropdown** filtered to valid parents (`LorebookFolderRow.tsx`); **drag-to-nest** via the header (top = reorder before, middle = nest, bottom = reorder after) and via a folder's **body** including the empty placeholder, with the left-indent margin targeting shallower ancestors; **drag to the root area to un-nest** (`use-lorebook-editor-drag-drop.ts`, `LorebookEditor.tsx`). - Folder type/schema comments updated to describe real nesting (no longer "reserved / always null"). ## Refactor impact Primary owner: Catalog / Lorebooks editor (frontend), plus the activation scanner (engine generation) Impact areas reviewed: - The lorebook editor (recursive render + folder drag-drop), the new folder-tree lib, the activation scanner's disabled-folder gate, and the folder reorder hook. No engine/shared-API/Rust/remote-runtime changes; no storage-contract or `src-tauri/src/lib.rs` change. Boundary notes: - Confined to the lorebooks feature UI + its React Query layer + the activation scanner's normalization step. Reuses `canReparentFolder` as the one write-time guard and the existing generic storage gateway; no new storage collection, command, or cross-boundary path. Pressure points touched: - The activation scanner (generation path) — specifically the disabled-folder gate, now ancestor-aware. No `ModeSurface`/`GameSurface`/shared mode UI, no `src-tauri/src/lib.rs`, no imports, no version-bearing files. ## Validation - [x] Matching validation command passes locally (for example `pnpm typecheck`, `pnpm build`, `pnpm check:architecture`, `pnpm check:docs`, or full `pnpm check` when warranted) - [x] Full `pnpm check` passes before PR push/handoff - [x] Human/manual validation completed by contributor or reviewer ### Manual verification notes - `pnpm typecheck` plus `check:architecture` / `check:frontend` / `check:rust` / `check:discovery` / `check:docs` / `check:launcher-safety` / `check:unused` all pass locally. `check:line-endings` fails only on pre-existing CRLF in files this PR doesn't touch (`DESIGN.md`, `custom_components/*.py`); their committed versions are LF, so CI is unaffected. - Manually verified in the running Tauri build: nesting via the parent dropdown and via drag (header three-zone, folder body, empty placeholder, left-indent targeting); un-nesting via the root area and the dropdown; cycle / self / cross-lorebook moves are never offered as drop targets; collapsing a parent hides its subtree; deleting a parent promotes its children to root; checked light + dark and at ~400px width. ## Feature Discoverability - [ ] Updated `src/features/shell/discovery/` because this PR adds or materially changes a user-discoverable feature, workflow, setting, mode, panel, import path, agent, media capability, or advanced tool. - [x] N/A because this PR is only a bugfix, refactor, test, docs, internal wiring, visual polish, copy edit, or compatibility fix and does not add a new thing users need to find. Reason: - N/A — sub-folders are an enhancement *within* the already-registered Lorebooks surface (the `lorebooks` discovery entry opens the panel). The registry is surface-level, not per-action, so no new entry is required and `check:discovery` passes unchanged (same call as the entry-duplication PR #2157). ## Docs and release impact - [x] No docs changes needed - [ ] Updated `README.md` - [ ] Updated `CONTRIBUTING.md` - [ ] Updated `docs/developer/` - [ ] Updated repo skills or `AGENTS.md` - [x] Confirmed this PR does not restore old staging/package-workspace/release claims ## Proof Core claim: - Lorebook folders nest into a tree; a disabled folder or any disabled ancestor gates its entries at activation time; re-parenting is validated at write time (no self-parent / cross-lorebook / cycles) and the scanner can't loop or leak on malformed data. Verification run: - The full check suite minus the pre-existing line-endings artifact (see Manual verification notes), plus hands-on testing in the running Tauri build. Evidence: - Manual: a disabled parent/grandparent hides entries that live in an enabled child folder at generation time; the parent dropdown and every drag path only offer valid targets; collapse / delete / un-nest behave as described above, in light + dark and at ~400px. - Local unit tests (uncommitted — the repo gitignores `*.test.ts`, and the template asks not to submit them as proof) cover the scanner's ancestor chain-walk and the `canReparentFolder` / `buildFolderForest` edge cases (self, cross-lorebook, direct + deep cycles, dangling-parent→root). Happy to fold them in if the team wants them tracked. Design notes (recorded for review): - **Client-side validation + scanner backstop, by design.** `canReparentFolder` runs client-side because the lorebook-folder collection is stored generically with no server-side field validation — matching how every other lorebook edit is validated. The activation scanner independently resolves disabled ancestors and guards against cycles at read time, so a malformed parent introduced by an import or a direct write can never hang generation or leak gated content. If the lorebook layer later moves into Rust, the same three checks (same lorebook, no self-parent, no cycle) port directly. - Deleting a parent folder promotes its children to the top level (orphan fallback in `buildFolderForest`) rather than cascade-deleting — consistent with how an entry whose folder is gone falls back to root. Manual verification requested: - Reviewers can confirm by nesting a few folders (dropdown + drag), disabling a parent and checking that a nested entry does not inject in a chat, and deleting a parent to watch its children rise to the top level. ## UI evidence <img width="647" height="346" alt="{FD93A120-C1AF-48A8-BB3C-F10246B0A196}" src="https://github.com/user-attachments/assets/aa292a56-aa2a-40f4-a03d-1859baa5c57c" /> <img width="631" height="327" alt="{0B080A73-73DE-45C6-B1EC-BD85E6AF4E53}" src="https://github.com/user-attachments/assets/1c15fc37-8bbc-47f0-adab-762551659d9c" /> <img width="532" height="388" alt="{A77F01B1-B64E-4715-A0A6-0854A38C40A4}" src="https://github.com/user-attachments/assets/10b3276c-a4fc-4da4-bec5-6aba33ab38c9" /> <img width="631" height="325" alt="{C7CCFAC4-8BF0-4B8A-942A-6F1039F7DD1E}" src="https://github.com/user-attachments/assets/6b074ae0-8ff9-4d75-bf31-80b7d2ecff11" /> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added support for nested folders within loreBooks, allowing hierarchical organization of folder structures. * Enhanced drag-and-drop functionality with improved drop-zone targeting ("before," "inside," "after" positions). * Added parent folder selector dropdown in the folder editor for easy reparenting. * **Bug Fixes** * Fixed folder disablement logic to cascade down—entries under disabled folders or their ancestors are now properly skipped during activation. * **Documentation** * Updated inline documentation to clarify nested-folder behavior and write-time validation rules. <!-- 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-22 04:17:20 +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#5555
No description provided.