mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[GH-ISSUE #2612] [Issue]: file-backed-store transaction() rollback restores the whole table map — drops concurrent writes + clones all tables every call #1286
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#1286
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 @Romuromylus on GitHub (Jun 18, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2612
Originally assigned to: @Romuromylus on GitHub.
Summary
Severity: Medium — data-loss / concurrency + performance.
FileTableStore.transaction()snapshots the entire table map at start and restores all of it on rollback. This (a) silently discards concurrent writes to tables the transaction never touched when the callback throws, and (b) deep-clones every row of all ~43 tables on every call — including the per-agent-turn memory write path — producing O(total-rows) allocation/GC churn on a hot path.Expected behavior
A transaction rollback should revert only what the transaction changed, and the snapshot it takes for rollback should not clone tables the transaction will never mutate. Concurrent writes committed to unrelated tables during the transaction's
awaitpoints must survive a rollback.Actual behavior
transaction()deep-clones the whole table map synchronously at start, then awaitsfn(tx), and on any throw reverts wholesale:txis the samedbobject, and each op resolves on a microtask (Promise.resolve().then(operation)), so everyawaitinsidefnyields the event loop. A concurrent non-transactional write (e.g.createMessageinchats.storage.ts:503) committed during that window lands in the live map but not intableSnapshot, so the wholesale revert silently discards it — the losing request already received a resolved/success value. No mutex/queue serializes writes.The same snapshot is the perf cost:
transaction()is used byagents.storage.setMemories()(wrapped indb.transaction, called per agent turn viaPATCH /memory/:agentType/:chatIdfor director/secret-plot-driver memory), lorebook create/update/remove, chat-folder reorder, and MariapplyPlan. On a profile with a largemessagestable, every such call shallow-clones every row of every table into a throwaway snapshot.Steps to reproduce
messages/lorebook_entriesdataset, run normal generation with a director/secret-plot agent sosetMemoriesfires per turn — each turn deep-clones the entire dataset.Environment
staging @ 56f700cd(v2.0.0)Logs, screenshots, or video
Static source audit, cited to file:line above.
Additional context
Fix sketch. Make the rollback snapshot lazy/copy-on-write: clone a table only the first time the transaction mutates it (track a dirty set), and on rollback restore only the dirty tables (or the specific rows touched). This fixes both facets at once — an untouched
messagestable is never cloned (kills the per-call O(total-rows) cost), and concurrent writes to untouched tables survive a rollback (kills the lost-write race). Alternatively, apply transaction mutations to a private working copy merged on success.Regression notes. Not a regression —
transaction()and the executable/op code are byte-identical onmain(file-backed-store.ts), andsetMemoriesusesdb.transactiononmaintoo. Long-standing behavior on both branches. Distinct from #2528 (flush serialization on the event loop), which is a different code path.Template check
@Romuromylus commented on GitHub (Jun 18, 2026):
Related to #2613 — same storage-concurrency lost-write family (rollback snapshot here, unqueued
updateMetadatathere).@Romuromylus commented on GitHub (Jun 18, 2026):
Fixed in #2623, merged to
staging(e1132e5b). Closing manually since the PR targetedstagingrather than the default branch.