mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[GH-ISSUE #2977] [Issue]: Bulk entry create is non-atomic and uncaught: a foreign folderId yields a 500 plus a partially-imported batch #1551
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#1551
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?
Originally created by @kolacheee on GitHub (Jun 29, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2977
Originally assigned to: @SpicyMarinara on GitHub.
What's wrong
POST /api/lorebooks/:id/entries/bulkinserts entries one-by-one with no surrounding transaction and no route-level error handling. If any entry carries afolderIdpointing to a folder in a different lorebook (or a non-existent folder),createEntrythrows "folderId does not belong to this lorebook". The single-entryPOSTandPATCHroutes catch this exact error and return a clean 400, but the bulk route does not — so the caller gets an opaque 500, and every entry created before the throwing one is already committed, leaving a partial, half-imported batch with no rollback.How to reproduce (try it yourself)
fB).POST /api/lorebooks/A/entries/bulkwith{"entries":[{"name":"e1"},{"name":"e2"},{"name":"e3","folderId":"fB"}]}.GET /api/lorebooks/A/entries.You'll see: step 2 returns HTTP 500; step 3 shows e1 and e2 persisted in A while e3 does not exist — a partial write with no rollback.
Expected: a 400 (matching the single-entry route) and either all-or-nothing insertion.
Where it is
packages/server/src/routes/lorebooks.routes.ts:589-601. Compare the single-entry POST catch at:551-560and PATCH catch at:563-575.packages/server/src/services/storage/lorebooks.storage.ts:682-689(bulkCreateEntriescallsthis.createEntryin a plain loop).packages/server/src/services/storage/lorebooks.storage.ts:518-526(createEntryforeign-folder guard, throw at:525).What's happening
bulkCreateEntriesiterates and callscreateEntrysequentially with no transaction, so each insert commits independently.createEntryvalidatesfolderIdagainst the entry's lorebook and throws for a foreign folder. The bulk route omits the try/catch the single-entry handlers use, so a foreignfolderIdin any element produces a 500 plus a partial, un-rolled-back write. The same path is used by the importer.Suggested fix
Two complementary changes:
err.message === "folderId does not belong to this lorebook".folderIdup front (one query for all referenced folder ids, confirm each belongs to this lorebook) before inserting any row. This is backend-agnostic.Note: simply wrapping the route body in
db.transactionis not sufficient on the opt-inSTORAGE_BACKEND=sqlitebackend —createEntryuses the storage's owndbhandle (not a passed-intx), so its inserts would run outside a route-level transaction. (On the default file-native backend, rollback is async-context-based, so a wrap would happen to work there — but the up-front validation above is robust on both.)AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.
@SpicyMarinara commented on GitHub (Jun 29, 2026):
Fixed in the v2.0.7 branch: bulk lorebook entry creation prevalidates every folderId before writing and returns a 400 for foreign folders instead of partially importing entries. Validated with pnpm check.