[GH-ISSUE #2327] [Issue]: Debounced manual world-state patch can overwrite chat.gameState with a stale target after a concurrent turn #1153

Closed
opened 2026-10-21 05:28:01 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @Romuromylus on GitHub (Jun 5, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2327

Originally assigned to: @Romuromylus on GitHub.

Summary

Severity: Low — Concurrency. The debounced manual world-state patcher captures its patch target (messageId/swipeIndex) at edit time. If an assistant turn completes during the 500ms debounce window, the queued flush still writes the old target's snapshot to chat.gameState, clobbering the now-current turn's persisted state. The live UI is largely masked by re-resolution on read, but the persisted chat.gameState becomes inconsistent.

Expected behavior

The flush should only write chat.gameState when its resolved target is still the current visible target; otherwise it should write only the per-message snapshot and let the visible-read path own chat.gameState.

Actual behavior

src/features/runtime/world-state/api/world-state-api.ts:281-283:

const saved = target ? await trackerSnapshotApi.save(chatId, next) : null;
throwIfAborted(init);
await storageApi.update("chats", chatId, { gameState: saved ?? next });

target is the requestedTarget captured at edit time by the debounced patcher (use-world-state-patcher.ts, 500ms debounce, getPatchTarget at edit time). When a turn for a newer message completes inside the window (itself writing chat.gameState), the queued flush saves correctly to the old message's snapshot but then unconditionally overwrites chat.gameState with that stale snapshot. There is no comparison between target and the current visible target.

Mitigations exist (so impact is bounded): the live UI reads from the store guarded by reconcileVisibleGameState; getWorldState re-resolves the visible snapshot on read; the generation baseline is snapshot-keyed (not chat.gameState); the next turn re-writes chat.gameState. The one genuinely-affected consumer is gameStateCarryoverPatch (game-api.ts:1150-1162), used when starting a new multi-session game — if chat.gameState is clobbered and a new session starts before another turn heals it, the new session starts from stale state.

Steps to reproduce

  1. Edit a tracker field.
  2. Send/receive a new assistant message within ~500ms before the debounced flush fires.
  3. The persisted chat.gameState is overwritten with the prior message's snapshot.

Impact area

  • Game mode
  • Runtime systems: generation, world-state, visuals, tracker, or haptics

Environment

  • Branch or commit: refactor @ b0e5561d
  • Run mode: Tauri desktop (shared frontend; also server mode)
  • OS + version: not relevant (static source audit)
  • Node + pnpm versions: not relevant
  • Rust version, if relevant: not relevant
  • Remote Runtime URL configured? not relevant (applies to both)
  • Provider, model, or integration, if relevant: world-state tracker

Logs, screenshots, or video

Static source audit; cited to file:line above.

Regression notes

Found via static audit of the refactor branch; not verified against main. Transient persisted inconsistency that the next turn heals, with only the rare new-session-carryover-during-window path being user-visible, hence low.

Additional context

Suggested fix: re-resolve latestAssistantTarget at flush time and only write chat.gameState when the resolved target equals the current visible target; or skip the chat.gameState write entirely for non-visible requested targets and let the visible-read path own it.

Template check

Please uncheck the box below before submitting so we know you read the template. It is intentionally pre-checked:

  • I DID NOT read this template and provide the requested details.
Originally created by @Romuromylus on GitHub (Jun 5, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2327 Originally assigned to: @Romuromylus on GitHub. ## Summary **Severity: Low — Concurrency.** The debounced manual world-state patcher captures its patch target (messageId/swipeIndex) at edit time. If an assistant turn completes during the 500ms debounce window, the queued flush still writes the old target's snapshot to `chat.gameState`, clobbering the now-current turn's persisted state. The live UI is largely masked by re-resolution on read, but the persisted `chat.gameState` becomes inconsistent. ## Expected behavior The flush should only write `chat.gameState` when its resolved target is still the current visible target; otherwise it should write only the per-message snapshot and let the visible-read path own `chat.gameState`. ## Actual behavior `src/features/runtime/world-state/api/world-state-api.ts:281-283`: ```ts const saved = target ? await trackerSnapshotApi.save(chatId, next) : null; throwIfAborted(init); await storageApi.update("chats", chatId, { gameState: saved ?? next }); ``` `target` is the `requestedTarget` captured at edit time by the debounced patcher (`use-world-state-patcher.ts`, 500ms debounce, `getPatchTarget` at edit time). When a turn for a newer message completes inside the window (itself writing `chat.gameState`), the queued flush saves correctly to the old message's snapshot but then unconditionally overwrites `chat.gameState` with that stale snapshot. There is no comparison between `target` and the current visible target. Mitigations exist (so impact is bounded): the live UI reads from the store guarded by `reconcileVisibleGameState`; `getWorldState` re-resolves the visible snapshot on read; the generation baseline is snapshot-keyed (not `chat.gameState`); the next turn re-writes `chat.gameState`. The one genuinely-affected consumer is `gameStateCarryoverPatch` (`game-api.ts:1150-1162`), used when starting a new multi-session game — if `chat.gameState` is clobbered and a new session starts before another turn heals it, the new session starts from stale state. ## Steps to reproduce 1. Edit a tracker field. 2. Send/receive a new assistant message within ~500ms before the debounced flush fires. 3. The persisted `chat.gameState` is overwritten with the prior message's snapshot. ## Impact area - [x] Game mode - [x] Runtime systems: generation, world-state, visuals, tracker, or haptics ## Environment - Branch or commit: refactor @ b0e5561d - Run mode: Tauri desktop (shared frontend; also server mode) - OS + version: not relevant (static source audit) - Node + pnpm versions: not relevant - Rust version, if relevant: not relevant - Remote Runtime URL configured? not relevant (applies to both) - Provider, model, or integration, if relevant: world-state tracker ## Logs, screenshots, or video Static source audit; cited to file:line above. ## Regression notes Found via static audit of the refactor branch; not verified against main. Transient persisted inconsistency that the next turn heals, with only the rare new-session-carryover-during-window path being user-visible, hence low. ## Additional context Suggested fix: re-resolve `latestAssistantTarget` at flush time and only write `chat.gameState` when the resolved target equals the current visible target; or skip the `chat.gameState` write entirely for non-visible requested targets and let the visible-read path own it. ## Template check Please uncheck the box below before submitting so we know you read the template. It is intentionally pre-checked: - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:28:01 +07:00
Author
Owner

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

Fixed by #2423, merged into refactor.

<!-- gh-comment-id:4637901572 --> @Romuromylus commented on GitHub (Jun 6, 2026): Fixed by #2423, merged into `refactor`.
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#1153
No description provided.