[PR #363] [MERGED] fix(chats): preserve folder categorization across branch create/delete #4579

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

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/363
Author: @LukaTheHero
Created: 5/3/2026
Status: ✅ Merged
Merged: 5/3/2026
Merged by: @LukaTheHero

Base: main ← Head: fix/branch-ops-preserve-folder-categorization


📝 Commits (1)

  • 68547b8 fix(chats): preserve folder categorization across branch create/delete

📊 Changes

3 files changed (+35 additions, -3 deletions)

View changed files

📝 packages/server/src/routes/chat-folders.routes.ts (+8 -2)
📝 packages/server/src/routes/chats.routes.ts (+4 -1)
📝 packages/server/src/services/storage/chats.storage.ts (+23 -0)

📄 Description

Closes #340

Why this change

When a user moves an RP into a folder, only the currently visible representative branch gets folderId set. The sidebar collapses every chat sharing a groupId into a single row and reads folderId from whichever branch is currently the most-recently-updated representative (packages/client/src/components/layout/ChatSidebar.tsx displayChats + folderChatsMap).

That coupling produced two failure modes that always dropped the tree back into Uncategorized:

  1. Branch creation. POST /:id/branch calls storage.create(...), which does not accept folderId, so the new branch lands with folderId = null. The post-create storage.update(newChat.id, {}) bumps updatedAt to now, making the new branch the most-recently-updated row in its group — so the sidebar reads folderId = null from it and the whole tree appears uncategorized.
  2. Branch deletion. Deleting any branch can surface a sibling whose folderId was never set (because the move-to-folder mutation only ever wrote the field on one branch). The replacement representative has folderId = null → tree falls out of its folder.

What changed

  • chats.storage.ts — new setFolderForChat(chatId, folderId) helper. When the chat has a groupId, it writes folderId to every branch in the group without touching siblings' updatedAt (so categorizing doesn't silently reorder branch history), then bumps updatedAt on just the targeted chat to preserve the existing "moved chat goes to top" behavior. Falls back to a single-chat update when there's no groupId.
  • chats.routes.ts — branch creation passes folderId: sourceChat.folderId ?? null into the existing post-create storage.update call so the new branch inherits the source's folder. Combined with setFolderForChat propagation at move time, every branch in a group now always carries the same folderId.
  • chat-folders.routes.ts — /move-chat and /reorder-chats route through setFolderForChat so any folder change applies to the whole group, not just the visible representative.

Compatibility

  • Existing single-branch chats (no groupId) take the unchanged single-row path.
  • Existing groups whose branches already have mismatched folderId heal on the next move-to-folder action.
  • No schema migration required — the fields already exist; only write paths changed.

Summary by CodeRabbit

  • Bug Fixes

    • Improved folder assignment handling during chat reorganization to ensure consistency across related chats.
    • Added validation when moving chats to folders; requests to non-existent folders now return appropriate errors.
  • Improvements

    • Chat branches now automatically inherit the folder assignment from their source chat, maintaining folder organization across the chat tree.

🔄 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/363 **Author:** [@LukaTheHero](https://github.com/LukaTheHero) **Created:** 5/3/2026 **Status:** ✅ Merged **Merged:** 5/3/2026 **Merged by:** [@LukaTheHero](https://github.com/LukaTheHero) **Base:** `main` ← **Head:** `fix/branch-ops-preserve-folder-categorization` --- ### 📝 Commits (1) - [`68547b8`](https://github.com/Pasta-Devs/Marinara-Engine/commit/68547b89f6a8c10091d532564d72e4e42178ef74) fix(chats): preserve folder categorization across branch create/delete ### 📊 Changes **3 files changed** (+35 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/routes/chat-folders.routes.ts` (+8 -2) 📝 `packages/server/src/routes/chats.routes.ts` (+4 -1) 📝 `packages/server/src/services/storage/chats.storage.ts` (+23 -0) </details> ### 📄 Description Closes #340 ## Why this change When a user moves an RP into a folder, only the *currently visible representative* branch gets `folderId` set. The sidebar collapses every chat sharing a `groupId` into a single row and reads `folderId` from whichever branch is currently the most-recently-updated representative (`packages/client/src/components/layout/ChatSidebar.tsx` `displayChats` + `folderChatsMap`). That coupling produced two failure modes that always dropped the tree back into Uncategorized: 1. **Branch creation.** `POST /:id/branch` calls `storage.create(...)`, which does not accept `folderId`, so the new branch lands with `folderId = null`. The post-create `storage.update(newChat.id, {})` bumps `updatedAt` to now, making the new branch the most-recently-updated row in its group — so the sidebar reads `folderId = null` from it and the whole tree appears uncategorized. 2. **Branch deletion.** Deleting any branch can surface a sibling whose `folderId` was never set (because the move-to-folder mutation only ever wrote the field on one branch). The replacement representative has `folderId = null` → tree falls out of its folder. ## What changed - `chats.storage.ts` — new `setFolderForChat(chatId, folderId)` helper. When the chat has a `groupId`, it writes `folderId` to every branch in the group without touching siblings' `updatedAt` (so categorizing doesn't silently reorder branch history), then bumps `updatedAt` on just the targeted chat to preserve the existing "moved chat goes to top" behavior. Falls back to a single-chat update when there's no `groupId`. - `chats.routes.ts` — branch creation passes `folderId: sourceChat.folderId ?? null` into the existing post-create `storage.update` call so the new branch inherits the source's folder. Combined with `setFolderForChat` propagation at move time, every branch in a group now always carries the same `folderId`. - `chat-folders.routes.ts` — `/move-chat` and `/reorder-chats` route through `setFolderForChat` so any folder change applies to the whole group, not just the visible representative. ## Compatibility - Existing single-branch chats (no `groupId`) take the unchanged single-row path. - Existing groups whose branches already have mismatched `folderId` heal on the next move-to-folder action. - No schema migration required — the fields already exist; only write paths changed. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved folder assignment handling during chat reorganization to ensure consistency across related chats. * Added validation when moving chats to folders; requests to non-existent folders now return appropriate errors. * **Improvements** * Chat branches now automatically inherit the folder assignment from their source chat, maintaining folder organization across the chat tree. <!-- 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:08: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#4579
No description provided.