[GH-ISSUE #2981] [Issue]: Entry move transfer is non-atomic: a failure after copying duplicates entries into both lorebooks #1556

Closed
opened 2026-10-21 05:39:00 +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/2981

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong

POST /api/lorebooks/:id/entries/transfer with operation:"move" first creates copies of every source entry in the target lorebook, then in a separate loop deletes the originals from the source. There is no transaction spanning the two phases. If any removeEntry in the delete phase throws, or the process is interrupted between the two loops, the entries exist in both the target and the source: a "move" silently degrades into a duplicate, and the created copies are never cleaned up. (A successful move with valid inputs works correctly; this surfaces only on partial failure.)

How to reproduce (try it yourself)

  1. Lorebook A has entries E1, E2.
  2. POST /api/lorebooks/A/entries/transfer { "targetLorebookId": B, "entryIds":[E1,E2], "operation":"move" }.
  3. Induce a fault in the delete phase (a removeEntry failure, or kill the server between the create and delete loops).

You'll see: B has copies of E1+E2 and A still has the originals — duplicates across two lorebooks.
Expected: either the whole move applies or none of it does; entries end up only in B.

Where it is

  • packages/server/src/routes/lorebooks.routes.ts:639-656 — the create-into-target loop (:641-648) then a separate delete-from-source loop (:650-653), with no transaction around them.
  • Storage: packages/server/src/services/storage/lorebooks.storage.ts:510 (createEntry, single insert) and :740-742 (removeEntry, single delete).

What's happening

The transfer route performs create (in target) and delete (from source) as two independent sequential loops with no transaction wrapping them, so a partial failure leaves both sides populated. Unlike create()/update()/remove() in the storage layer, this multi-step cross-lorebook mutation is not transactional.

Suggested fix

Make the move atomic at the storage layer. Add a transactional move method that threads a tx handle through the copy-into-target and delete-from-source steps (mirroring how create()/update()/remove() already use db.transaction with tx.insert/tx.delete), so a failure in the delete phase rolls back the inserts. This is the backend-agnostic fix — note that wrapping the existing route-level storage.createEntry/storage.removeEntry calls in db.transaction would only roll back on the default file-native backend (whose rollback is async-context-based), not on the opt-in STORAGE_BACKEND=sqlite backend, because those storage methods use their own db handle rather than the transaction's tx. As a lighter alternative, perform compensating cleanup: if the delete phase throws, delete the copies just created in the target.


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/2981 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** `POST /api/lorebooks/:id/entries/transfer` with `operation:"move"` first creates copies of every source entry in the target lorebook, then in a separate loop deletes the originals from the source. There is no transaction spanning the two phases. If any `removeEntry` in the delete phase throws, or the process is interrupted between the two loops, the entries exist in **both** the target and the source: a "move" silently degrades into a duplicate, and the created copies are never cleaned up. (A successful move with valid inputs works correctly; this surfaces only on partial failure.) **How to reproduce (try it yourself)** 1. Lorebook A has entries E1, E2. 2. `POST /api/lorebooks/A/entries/transfer` `{ "targetLorebookId": B, "entryIds":[E1,E2], "operation":"move" }`. 3. Induce a fault in the delete phase (a `removeEntry` failure, or kill the server between the create and delete loops). **You'll see:** B has copies of E1+E2 **and** A still has the originals — duplicates across two lorebooks. **Expected:** either the whole move applies or none of it does; entries end up only in B. **Where it is** - `packages/server/src/routes/lorebooks.routes.ts:639-656` — the create-into-target loop (`:641-648`) then a separate delete-from-source loop (`:650-653`), with no transaction around them. - Storage: `packages/server/src/services/storage/lorebooks.storage.ts:510` (`createEntry`, single insert) and `:740-742` (`removeEntry`, single delete). **What's happening** The transfer route performs create (in target) and delete (from source) as two independent sequential loops with no transaction wrapping them, so a partial failure leaves both sides populated. Unlike `create()`/`update()`/`remove()` in the storage layer, this multi-step cross-lorebook mutation is not transactional. **Suggested fix** Make the move atomic at the storage layer. Add a transactional move method that threads a `tx` handle through the copy-into-target and delete-from-source steps (mirroring how `create()`/`update()`/`remove()` already use `db.transaction` with `tx.insert`/`tx.delete`), so a failure in the delete phase rolls back the inserts. This is the backend-agnostic fix — note that wrapping the existing route-level `storage.createEntry`/`storage.removeEntry` calls in `db.transaction` would only roll back on the default file-native backend (whose rollback is async-context-based), not on the opt-in `STORAGE_BACKEND=sqlite` backend, because those storage methods use their own `db` handle rather than the transaction's `tx`. As a lighter alternative, perform compensating cleanup: if the delete phase throws, delete the copies just created in the target. --- *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: entry transfer now cleans up target copies if any later transfer/delete step fails, preventing stranded duplicates from failed moves. Validated with pnpm check.

<!-- gh-comment-id:4831163687 --> @SpicyMarinara commented on GitHub (Jun 29, 2026): Fixed in the v2.0.7 branch: entry transfer now cleans up target copies if any later transfer/delete step fails, preventing stranded duplicates from failed moves. 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#1556
No description provided.