[GH-ISSUE #2977] [Issue]: Bulk entry create is non-atomic and uncaught: a foreign folderId yields a 500 plus a partially-imported batch #1551

Closed
opened 2026-10-21 05:38:57 +07:00 by bsakaguchi · 1 comment
Owner

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/bulk inserts entries one-by-one with no surrounding transaction and no route-level error handling. If any entry carries a folderId pointing to a folder in a different lorebook (or a non-existent folder), createEntry throws "folderId does not belong to this lorebook". The single-entry POST and PATCH routes 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)

  1. Create lorebooks A and B; create a folder in B and note its id (call it fB).
  2. POST /api/lorebooks/A/entries/bulk with {"entries":[{"name":"e1"},{"name":"e2"},{"name":"e3","folderId":"fB"}]}.
  3. 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

  • Bulk route with no try/catch: packages/server/src/routes/lorebooks.routes.ts:589-601. Compare the single-entry POST catch at :551-560 and PATCH catch at :563-575.
  • Storage loop with no transaction: packages/server/src/services/storage/lorebooks.storage.ts:682-689 (bulkCreateEntries calls this.createEntry in a plain loop).
  • Throw site: packages/server/src/services/storage/lorebooks.storage.ts:518-526 (createEntry foreign-folder guard, throw at :525).

What's happening

bulkCreateEntries iterates and calls createEntry sequentially with no transaction, so each insert commits independently. createEntry validates folderId against the entry's lorebook and throws for a foreign folder. The bulk route omits the try/catch the single-entry handlers use, so a foreign folderId in 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:

  1. Add the same try/catch the single-entry routes use to the bulk route, returning 400 when err.message === "folderId does not belong to this lorebook".
  2. For all-or-nothing semantics, validate every entry's folderId up 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.transaction is not sufficient on the opt-in STORAGE_BACKEND=sqlite backend — createEntry uses the storage's own db handle (not a passed-in tx), 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.

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/bulk` inserts entries one-by-one with no surrounding transaction and no route-level error handling. If any entry carries a `folderId` pointing to a folder in a different lorebook (or a non-existent folder), `createEntry` throws "folderId does not belong to this lorebook". The single-entry `POST` and `PATCH` routes 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)** 1. Create lorebooks A and B; create a folder in B and note its id (call it `fB`). 2. `POST /api/lorebooks/A/entries/bulk` with `{"entries":[{"name":"e1"},{"name":"e2"},{"name":"e3","folderId":"fB"}]}`. 3. `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** - Bulk route with no try/catch: `packages/server/src/routes/lorebooks.routes.ts:589-601`. Compare the single-entry POST catch at `:551-560` and PATCH catch at `:563-575`. - Storage loop with no transaction: `packages/server/src/services/storage/lorebooks.storage.ts:682-689` (`bulkCreateEntries` calls `this.createEntry` in a plain loop). - Throw site: `packages/server/src/services/storage/lorebooks.storage.ts:518-526` (`createEntry` foreign-folder guard, throw at `:525`). **What's happening** `bulkCreateEntries` iterates and calls `createEntry` sequentially with no transaction, so each insert commits independently. `createEntry` validates `folderId` against the entry's lorebook and throws for a foreign folder. The bulk route omits the try/catch the single-entry handlers use, so a foreign `folderId` in 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: 1. Add the same try/catch the single-entry routes use to the bulk route, returning 400 when `err.message === "folderId does not belong to this lorebook"`. 2. For all-or-nothing semantics, **validate every entry's `folderId` up 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.transaction` is **not** sufficient on the opt-in `STORAGE_BACKEND=sqlite` backend — `createEntry` uses the storage's own `db` handle (not a passed-in `tx`), 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.*
Author
Owner

@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.

<!-- gh-comment-id:4831162013 --> @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.
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#1551
No description provided.