[GH-ISSUE #5076] Advancing game time or weather can silently wipe concurrent chat metadata writes (can permanently break World Map movement) #2810

Closed
opened 2026-10-21 05:55:59 +07:00 by bsakaguchi · 0 comments
Owner

Originally created by @kolacheee on GitHub (Aug 15, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/5076

Originally assigned to: @kolacheee on GitHub.

What happens

Advancing game time or updating weather can silently undo any other change to the same chat's settings that lands at the same moment. Most changes to a chat's metadata go through a queued, merge-on-latest patch path — but these two game endpoints read the whole metadata blob, modify one key, and write the entire blob back with no queue. Anything written by another request in between is reverted without any error.

The worst case involves World Maps: the map definition (including its revision counter) lives in that same metadata blob, while the party's current location lives in a separate snapshots table. If a World Maps definition write (say, a newly discovered location bumping the revision) lands inside the time-advance window, the definition is rolled back while the snapshot keeps the newer revision number. From then on every map move fails validation as spatial_transition_stale_definition — a permanent, silent movement lock with no visible cause.

Try it yourself

  1. Create a Game Mode game and note its chat id.
  2. Fire these two requests at the same time (a loop of ~10 attempts makes the window easy to hit):
# terminal A – patch a harmless custom key (this uses the queued path)
curl -s -X PATCH http://localhost:3000/api/chats/<chatId>/metadata \
  -H "Content-Type: application/json" -d '{"raceProbe":"I should survive"}'

# terminal B – advance time (this uses the unqueued whole-blob path)
curl -s -X POST http://localhost:3000/api/game/time/advance \
  -H "Content-Type: application/json" -d '{"chatId":"<chatId>","action":"rest"}'
  1. GET /api/chats/<chatId> and look at the metadata: when the requests interleave, raceProbe is gone — the time handler wrote back the stale blob it read before the patch landed.

The same shape exists with normal app usage (no curl needed): the time/weather buttons in the game UI (packages/client/src/hooks/use-game.ts:851,872) racing any other metadata write, including the World Maps agent's definition updates during a turn.

Why it happens

  • POST /game/time/advance reads meta, then calls chats.updateMetadata(chatId, { ...meta, gameTime }) — packages/server/src/routes/game.routes.ts:9741 (read) and :9767 (write). POST /game/weather/update does the same at :9802 and :9818.
  • updateMetadata is a bare whole-blob db.update with no withChatMetadataPatchQueue — packages/server/src/services/storage/chats.storage.ts:744-750. Compare patchMetadata directly below it (:752-773), which queues and re-reads before merging.
  • The stale-definition consequence: validateSpatialTransition compares the definition revision from metadata against the snapshot's definitionRevision — packages/shared/src/utils/spatial-context.ts:565-570.

Suggested fix

Convert both handlers to patchMetadata with a narrow patch ({ gameTime } / { gameWeather }) instead of spreading the stale meta. Other updateMetadata call sites in game.routes.ts with the same read-modify-write shape are probably worth a follow-up audit, but these two are the ones a player hits every session.

Found while exploring the pixel-RPG Experience design (which would call time-advance per move and made this race near-certain); verified against staging @ 068d01fad.

Originally created by @kolacheee on GitHub (Aug 15, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/5076 Originally assigned to: @kolacheee on GitHub. ### What happens Advancing game time or updating weather can silently undo any other change to the same chat's settings that lands at the same moment. Most changes to a chat's metadata go through a queued, merge-on-latest patch path — but these two game endpoints read the whole metadata blob, modify one key, and write the **entire blob** back with no queue. Anything written by another request in between is reverted without any error. The worst case involves World Maps: the map definition (including its `revision` counter) lives in that same metadata blob, while the party's current location lives in a separate snapshots table. If a World Maps definition write (say, a newly discovered location bumping the revision) lands inside the time-advance window, the definition is rolled back while the snapshot keeps the newer revision number. From then on **every map move fails validation as `spatial_transition_stale_definition`** — a permanent, silent movement lock with no visible cause. ### Try it yourself 1. Create a Game Mode game and note its chat id. 2. Fire these two requests at the same time (a loop of ~10 attempts makes the window easy to hit): ``` # terminal A – patch a harmless custom key (this uses the queued path) curl -s -X PATCH http://localhost:3000/api/chats/<chatId>/metadata \ -H "Content-Type: application/json" -d '{"raceProbe":"I should survive"}' # terminal B – advance time (this uses the unqueued whole-blob path) curl -s -X POST http://localhost:3000/api/game/time/advance \ -H "Content-Type: application/json" -d '{"chatId":"<chatId>","action":"rest"}' ``` 3. `GET /api/chats/<chatId>` and look at the metadata: when the requests interleave, `raceProbe` is gone — the time handler wrote back the stale blob it read before the patch landed. The same shape exists with normal app usage (no curl needed): the time/weather buttons in the game UI (`packages/client/src/hooks/use-game.ts:851,872`) racing any other metadata write, including the World Maps agent's definition updates during a turn. ### Why it happens - `POST /game/time/advance` reads `meta`, then calls `chats.updateMetadata(chatId, { ...meta, gameTime })` — `packages/server/src/routes/game.routes.ts:9741` (read) and `:9767` (write). `POST /game/weather/update` does the same at `:9802` and `:9818`. - `updateMetadata` is a bare whole-blob `db.update` with **no** `withChatMetadataPatchQueue` — `packages/server/src/services/storage/chats.storage.ts:744-750`. Compare `patchMetadata` directly below it (`:752-773`), which queues and re-reads before merging. - The stale-definition consequence: `validateSpatialTransition` compares the definition `revision` from metadata against the snapshot's `definitionRevision` — `packages/shared/src/utils/spatial-context.ts:565-570`. ### Suggested fix Convert both handlers to `patchMetadata` with a narrow patch (`{ gameTime }` / `{ gameWeather }`) instead of spreading the stale `meta`. Other `updateMetadata` call sites in `game.routes.ts` with the same read-modify-write shape are probably worth a follow-up audit, but these two are the ones a player hits every session. Found while exploring the pixel-RPG Experience design (which would call time-advance per move and made this race near-certain); verified against `staging` @ 068d01fad.
bsakaguchi 2026-10-21 05:55:59 +07:00
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#2810
No description provided.