[GH-ISSUE #2613] [Issue]: Chat metadata written via unqueued updateMetadata (blind overwrite) instead of queue-serialized patchMetadata — lost-write race #1285

Closed
opened 2026-10-21 05:30:41 +07:00 by bsakaguchi · 2 comments
Owner

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 of chats.patchMetadata (which serializes read-modify-write via withMetadataPatchQueue and 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: patchMetadata serializes per-chat writes and merges a patch onto a fresh read.

Actual behavior

updateMetadata is a blind full-row overwrite outside the queue; patchMetadata is the only serialized writer:

// packages/server/src/services/storage/chats.storage.ts
updateMetadata(chatId, metadata) // ~320-326: set({ metadata: JSON.stringify(metadata) }) — NO queue
patchMetadata(chatId, patch)     // ~328-350: withMetadataPatchQueue(...) re-reads fresh + merges

Game-route handlers do meta = parseMeta(chat.metadata) then chats.updateMetadata(chatId, { ...meta, ...changes }) at many sites, e.g. game.routes.ts /session/conclude (read ~3988, multi-second provider.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 queued patchMetadata (generate.routes.ts:1959,6385) with blind updateMetadata (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 only daySummaries/weekSummaries, but still persists via the unqueued updateMetadata, so a queued patchMetadata (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

  1. In game mode, trigger a long side-action (Recruit / Conclude — each runs a multi-second LLM call) while the active turn's tracker/journal persist runs, or fire /journal/entry + /state/transition close together.
  2. Or: manually edit a day/week summary (PATCH /chats/:id/summaries) while the autonomous poller issues a patchMetadata for the same conversation chat.
  3. Observe one writer's metadata changes silently reverted (dropped journal/quest/party/morale/summary/unread state).

Environment

  • Marinara Engine version: staging @ 56f700cd (v2.0.0)
  • Install type: source
  • OS + version: n/a — static source audit
  • Browser or app shell: n/a

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-chat metadataPatchQueue and 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 a patchMetadata updater that reads fresh metadata. This is a codebase-wide class: grep shows ~60 updateMetadata callers across game.routes.ts, scene.routes.ts, generate.routes.ts, conversation.routes.ts, and several generation runtimes — the high-stale-window LLM-bearing game routes and /summaries are 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 on main. Long-standing behavior.

Template check

  • I DID NOT read this template and provide the requested details.
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 of `chats.patchMetadata` (which serializes read-modify-write via `withMetadataPatchQueue` and 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: `patchMetadata` serializes per-chat writes and merges a patch onto a fresh read. ## Actual behavior `updateMetadata` is a blind full-row overwrite outside the queue; `patchMetadata` is the only serialized writer: ```ts // packages/server/src/services/storage/chats.storage.ts updateMetadata(chatId, metadata) // ~320-326: set({ metadata: JSON.stringify(metadata) }) — NO queue patchMetadata(chatId, patch) // ~328-350: withMetadataPatchQueue(...) re-reads fresh + merges ``` Game-route handlers do `meta = parseMeta(chat.metadata)` then `chats.updateMetadata(chatId, { ...meta, ...changes })` at many sites, e.g. `game.routes.ts` `/session/conclude` (read ~3988, multi-second `provider.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 queued `patchMetadata` (`generate.routes.ts:1959,6385`) with blind `updateMetadata` (`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 only `daySummaries`/`weekSummaries`, but still persists via the unqueued `updateMetadata`, so a queued `patchMetadata` (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 1. In game mode, trigger a long side-action (Recruit / Conclude — each runs a multi-second LLM call) while the active turn's tracker/journal persist runs, or fire `/journal/entry` + `/state/transition` close together. 2. Or: manually edit a day/week summary (`PATCH /chats/:id/summaries`) while the autonomous poller issues a `patchMetadata` for the same conversation chat. 3. Observe one writer's metadata changes silently reverted (dropped journal/quest/party/morale/summary/unread state). ## Environment - Marinara Engine version: `staging @ 56f700cd` (v2.0.0) - Install type: source - OS + version: n/a — static source audit - Browser or app shell: n/a ## 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-chat `metadataPatchQueue` and 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 a `patchMetadata` updater that reads fresh metadata. This is a codebase-wide class: `grep` shows ~60 `updateMetadata` callers across `game.routes.ts`, `scene.routes.ts`, `generate.routes.ts`, `conversation.routes.ts`, and several generation runtimes — the high-stale-window LLM-bearing game routes and `/summaries` are 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 on `main`. Long-standing behavior. ## Template check - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:30:41 +07:00
  • closed this issue
  • added the
    server
    bug
    labels
Author
Owner

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

<!-- gh-comment-id:4740873410 --> @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.
Author
Owner

@Romuromylus commented on GitHub (Jun 18, 2026):

Fixed in #2623, merged to staging (e1132e5b). Closing manually since the PR targeted staging rather than the default branch.

<!-- gh-comment-id:4742294502 --> @Romuromylus commented on GitHub (Jun 18, 2026): Fixed in #2623, merged to `staging` (e1132e5b). Closing manually since the PR targeted `staging` rather than the default branch.
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#1285
No description provided.