mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[PR #749] [MERGED] refactor(folders): null-guard route handlers and atomicity for storage loops #4759
Labels
No labels
android
bug
bugfix
ci
client
code freeze pause
container
dependencies
docs
documentation
duplicate
enhancement
feature
feature
feature
feature-creep
fixed-in-refactor
fixed-in-staging
good first issue
help wanted
in-pr
in-pr
needs-codeowner-decision
normal-scope
old-codebase
old-codebase
pull-request
qol
question
refactor
regression
revisit later
roadmap
scope-risk
scope-risk
server
server
shared
soup
upstream
windows
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Pasta-Devs/Marinara-Engine#4759
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/749
Author: @Romuromylus
Created: 5/12/2026
Status: ✅ Merged
Merged: 5/12/2026
Merged by: @Romuromylus
Base:
main← Head:refactor/folder-atomicity📝 Commits (2)
08a4654refactor(folders): null-guard route handlers and atomicity for storage loopsfa4ac4drefactor(folders): also transaction-wrap /reorder-chats📊 Changes
5 files changed (+118 additions, -67 deletions)
View changed files
📝
packages/server/src/routes/chat-folders.routes.ts(+18 -9)📝
packages/server/src/routes/connection-folders.routes.ts(+4 -2)📝
packages/server/src/services/storage/chat-folders.storage.ts(+34 -20)📝
packages/server/src/services/storage/chats.storage.ts(+20 -9)📝
packages/server/src/services/storage/connection-folders.storage.ts(+42 -27)📄 Description
Linked issue
Closes #747
Closes #748
Why this change
Two follow-ups from CodeRabbit's review of #745, both of which I deferred at the time because applying them only to the new
connection-foldersfiles would have diverged from the existingchat-folderspattern. With #745 merged, this PR addresses both findings simultaneously in lockstep across both folder modules:folder!.collapsednon-null assertions. Safe under current semantics, but brittle to future error-handling changes.create(),reorder(), andreorderConnections()in the folder storage modules — plusPOST /api/chat-folders/reorder-chats— issued per-row UPDATE loops without transaction boundaries. A partial failure mid-loop could leavesort_order/folder_idcolumns in a mixed state.Folder counts in a single-user Marinara install are O(dozens), so the performance impact of the missing atomicity is negligible — but a transaction wrap gives us actual atomicity, matches the canonical pattern already used in
lorebooks.storage.ts/agents.storage.ts, and costs almost nothing.What changed
Routes — null-guard the create/update responses:
packages/server/src/routes/chat-folders.routes.ts— POST + PATCH: replacefolder!.collapsedwith an explicitif (!folder) return reply.status(500)...guard.packages/server/src/routes/connection-folders.routes.ts— same change.Storage — wrap per-row loops in
db.transaction():packages/server/src/services/storage/chat-folders.storage.ts—create()(shift + insert) andreorder()(per-row sort_order update).packages/server/src/services/storage/connection-folders.storage.ts—create(),reorder(), andreorderConnections()(the route is wired but UI for drag-within-folder is still deferred — wrapping it now means the eventual UI flip is pure-frontend).packages/server/src/services/storage/chats.storage.ts—update()andsetFolderForChat()now accept an optionaltxparameter (typed structurally asPick<DB, "select" | "update">, the same shape used bysyncLorebookLinksinlorebooks.storage.ts:154). Whentxis omitted the behaviour is identical to before.packages/server/src/routes/chat-folders.routes.ts—POST /reorder-chatsnow wraps the loop inapp.db.transaction()and threadstxthrough bothchatsStorage.updateandchatsStorage.setFolderForChat, so the sortOrder write and the group-sibling folder propagation commit atomically.Each transaction uses a single shared
now()timestamp so all rows within one reorder share anupdated_atvalue (intentional — the previous "later-timestamp-per-row" behaviour was an accidental side-effect of the per-row loop, not a deliberate semantic).Why not a single-query bulk UPDATE?
CodeRabbit's original suggestion was
tx.update(folders).set({ sortOrder: sql\${folders.sortOrder} + 1` })` to collapse the shift into one statement. That's a no-go on this codebase's default storage backend:STORAGE_BACKEND=files(seepackages/server/src/config/runtime-config.ts:245).packages/server/src/db/file-backed-store.ts:932intentionally treatsrun()as a no-op and.set()patches as plain row literals — a Drizzlesqltemplate silently breaks for any user not on opt-inSTORAGE_BACKEND=sqlite.The transaction wrap is portable across both backends (file-backed-store has its own
transactionimplementation at file-backed-store.ts:829 — a deep table snapshot with rollback-on-throw — and this PR's changes flow through it correctly).Validation
pnpm checkpasses locallyCONTRIBUTING.mdManual verification notes
Verified live against
me.romyromulus.comafter fork-deploy:GET /api/connection-foldersthat the new folder is atsortOrder=0and the 5 originals shifted to1..5exactly./api/chat-folders, no existing folders, new folder inserted atsortOrder=0.{orderedIds: [...].reverse()}to/api/connection-folders/reorder, confirmed full reverse applied (e.g.LinkAPI LLMfrom5 → 0,Tx Test Folderfrom0 → 5).Things that would benefit from human verification before merge:
Reorder.Groupintegration).Docs and release impact
UI evidence (if applicable)
No UI changes — pure backend/storage refactor. The connections panel and chat sidebar behaviour are identical to before from the user's point of view, just with stronger consistency guarantees under partial-failure scenarios.
Summary by CodeRabbit
Release Notes
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.