mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[GH-ISSUE #2613] [Issue]: Chat metadata written via unqueued updateMetadata (blind overwrite) instead of queue-serialized patchMetadata — lost-write race #1285
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#1285
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/2613
Originally assigned to: @Romuromylus on GitHub.
Summary
Severity: Medium — data-loss / concurrency. Many chat-metadata mutations persist via
chats.updateMetadata(a blind, full-blob overwrite that does not go through the per-chat patch queue) instead ofchats.patchMetadata(which serializes read-modify-write viawithMetadataPatchQueueand merges onto a fresh in-lock read). A blind write built from a stale read can interleave with — and silently overwrite — concurrent metadata writes, dropping journal entries, quest progress, NPCs, party arcs, morale, summaries, unread counters, or inherited schedules.Expected behavior
Concurrent metadata writers on the same chat should not lose each other's changes. The intended pattern already exists:
patchMetadataserializes per-chat writes and merges a patch onto a fresh read.Actual behavior
updateMetadatais a blind full-row overwrite outside the queue;patchMetadatais the only serialized writer:Game-route handlers do
meta = parseMeta(chat.metadata)thenchats.updateMetadata(chatId, { ...meta, ...changes })at many sites, e.g.game.routes.ts/session/conclude(read ~3988, multi-secondprovider.chatComplete~4069, blind write ~4116),/party/recruit(write ~5081),/morale(~5299),/state/transition(~5316),/journal/entry(~5874),/notes(~5906),/widgets(~5922),/party/remove(~5186),/checkpoint/load(~7726). Meanwhile the generate pipeline writes the same chat metadata, mixing queuedpatchMetadata(generate.routes.ts:1959,6385) with blindupdateMetadata(6002,7117,7642,9631) during a game turn.PATCH /chats/:id/summaries(chats.routes.ts:631-645) is the same class — it re-reads fresh and merges onlydaySummaries/weekSummaries, but still persists via the unqueuedupdateMetadata, so a queuedpatchMetadata(clear/markAutonomousUnread, schedule-inherit, agent summary append) can interleave between its read and write.The maintainer hazard comment is already present (
generate.routes.ts:7113-7114— "Re-fetch fresh metadata before write so we do not clobber concurrent updates"), but the re-fetch only narrows the window; it does not serialize, and most game-route side-actions do not even apply it. Last (stale) writer wins on the whole blob.Steps to reproduce
/journal/entry+/state/transitionclose together.PATCH /chats/:id/summaries) while the autonomous poller issues apatchMetadatafor the same conversation chat.Environment
staging @ 56f700cd(v2.0.0)Logs, screenshots, or video
Static source audit, cited to file:line above.
Additional context
Fix sketch. Route these metadata mutations through
chats.patchMetadata(id, (freshMeta) => ({ ...changes }))so they share the per-chatmetadataPatchQueueand re-read inside the lock, instead of building{ ...stale meta, ...changes }+updateMetadata. For handlers that need an LLM call mid-flow (recruit/conclude), compute the changes first, then apply them inside apatchMetadataupdater that reads fresh metadata. This is a codebase-wide class:grepshows ~60updateMetadatacallers acrossgame.routes.ts,scene.routes.ts,generate.routes.ts,conversation.routes.ts, and several generation runtimes — the high-stale-window LLM-bearing game routes and/summariesare the priority instances.Regression notes. Not a regression — the storage layer (
updateMetadata/patchMetadata/withMetadataPatchQueue) and the same blind-overwrite call sites + hazard comment exist identically onmain. Long-standing behavior.Template check
@Romuromylus commented on GitHub (Jun 18, 2026):
Related to #2612 — both are lost-write defects in the storage/concurrency layer (this one at the chat-metadata write path, #2612 at the file-backed-store
transaction()rollback). Worth fixing together since they share the per-chat serialization / copy-on-write theme.@Romuromylus commented on GitHub (Jun 18, 2026):
Fixed in #2623, merged to
staging(e1132e5b). Closing manually since the PR targetedstagingrather than the default branch.