[GH-ISSUE #2961] [Issue]: Turn-game (UNO) engine state isn't deleted with its message/chat — it leaks and can resurface #1537

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

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

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong

The per-turn game-engine snapshots used by turn-games (UNO) are never cleaned up when a message or a whole chat is deleted. Two consequences:

  • Resurfacing: in an edge case (e.g. deleting all assistant turns), a deleted turn's engine state can come back to life, because the resolver falls back to the newest engine row by timestamp without checking whether its message still exists.
  • Leaked rows: the orphaned gameEngineState rows accumulate forever. On the default "files" backend chat-deletion cleans them up, but on the opt-in STORAGE_BACKEND=sqlite (libSQL) backend they're left behind on chat/group deletion too (no FK cascade, not in the manual cleanup list).

How to reproduce (try it yourself)

Resurfacing (edge case):

  1. Play a turn-game (UNO) chat so gameEngineState rows accumulate.
  2. Delete all assistant turn messages (or bulk-delete leaving a surviving predecessor with no engine row).
  3. Continue / trigger the next turn.
    You'll see: the engine resolver falls back to the orphaned (deleted) state instead of resetting to the opening/empty state.
    Expected: deleting turn messages removes their engine state too.

(The common case — deleting only the most-recent assistant turn — does not resurface, because the surviving prior turn has its own anchored engine row that wins.)

Leaked rows: deleting a game chat (or branch group) leaves its game_engine_state rows in the DB; on STORAGE_BACKEND=sqlite they're never removed.

Where it is

packages/server/src/services/storage/chats.storage.ts — deleteGameStateForMessages (248-268) deletes gameStateSnapshots + gameCheckpoints but never gameEngineState; it's called by removeMessage (979-986) / removeMessages (988-1011). Contrast removeSwipe (~1160-1172), which does prune gameEngineState — so the omission is inconsistent. For whole-chat delete, remove() (585-598) and removeGroup() (601-615) also skip gameEngineState, and the libSQL table (migrate.ts ~367-377) has a bare chat_id with no REFERENCES chats(id) ON DELETE CASCADE. The resolver fallback that resurfaces orphans is game-engine-state.storage.ts getLatest/getLatestCommitted (~28-46), which selects by chatId + createdAt with no message-existence check.

What's happening

gameEngineState is the one chats child table missing from both the manual per-message/per-chat cleanup lists and (on libSQL) the FK cascade set, so its rows are orphaned on delete. The turn-game runner first tries an anchored lookup by the surviving message id, so single deletes are safe; when that misses it falls through to the unanchored newest-by-createdAt row, which can be the orphan.

Suggested fix

Add await db.delete(gameEngineState).where(inArray(gameEngineState.messageId, chunk)); inside deleteGameStateForMessages (mirroring the prune already in removeSwipe), and add await db.delete(gameEngineState).where(eq(gameEngineState.chatId, id)); to remove() and the removeGroup() per-chat loop (or add the FK cascade to the libSQL table). The "files" backend cascade already handles chat-level deletes.


AI-investigated against v2.0.6; the cleanup helper and the removeSwipe contrast confirmed. Please verify before fixing.

Originally created by @kolacheee on GitHub (Jun 28, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2961 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** The per-turn game-engine snapshots used by turn-games (UNO) are never cleaned up when a message or a whole chat is deleted. Two consequences: - **Resurfacing:** in an edge case (e.g. deleting all assistant turns), a deleted turn's engine state can come back to life, because the resolver falls back to the newest engine row by timestamp without checking whether its message still exists. - **Leaked rows:** the orphaned `gameEngineState` rows accumulate forever. On the default "files" backend chat-deletion cleans them up, but on the opt-in `STORAGE_BACKEND=sqlite` (libSQL) backend they're left behind on chat/group deletion too (no FK cascade, not in the manual cleanup list). **How to reproduce (try it yourself)** Resurfacing (edge case): 1. Play a turn-game (UNO) chat so `gameEngineState` rows accumulate. 2. Delete **all** assistant turn messages (or bulk-delete leaving a surviving predecessor with no engine row). 3. Continue / trigger the next turn. **You'll see:** the engine resolver falls back to the orphaned (deleted) state instead of resetting to the opening/empty state. **Expected:** deleting turn messages removes their engine state too. (The common case — deleting only the most-recent assistant turn — does *not* resurface, because the surviving prior turn has its own anchored engine row that wins.) Leaked rows: deleting a game chat (or branch group) leaves its `game_engine_state` rows in the DB; on `STORAGE_BACKEND=sqlite` they're never removed. **Where it is** `packages/server/src/services/storage/chats.storage.ts` — `deleteGameStateForMessages` (248-268) deletes `gameStateSnapshots` + `gameCheckpoints` but **never** `gameEngineState`; it's called by `removeMessage` (979-986) / `removeMessages` (988-1011). Contrast `removeSwipe` (~1160-1172), which *does* prune `gameEngineState` — so the omission is inconsistent. For whole-chat delete, `remove()` (585-598) and `removeGroup()` (601-615) also skip `gameEngineState`, and the libSQL table (`migrate.ts` ~367-377) has a bare `chat_id` with no `REFERENCES chats(id) ON DELETE CASCADE`. The resolver fallback that resurfaces orphans is `game-engine-state.storage.ts` `getLatest`/`getLatestCommitted` (~28-46), which selects by `chatId + createdAt` with no message-existence check. **What's happening** `gameEngineState` is the one chats child table missing from both the manual per-message/per-chat cleanup lists and (on libSQL) the FK cascade set, so its rows are orphaned on delete. The turn-game runner first tries an anchored lookup by the surviving message id, so single deletes are safe; when that misses it falls through to the unanchored newest-by-`createdAt` row, which can be the orphan. **Suggested fix** Add `await db.delete(gameEngineState).where(inArray(gameEngineState.messageId, chunk));` inside `deleteGameStateForMessages` (mirroring the prune already in `removeSwipe`), and add `await db.delete(gameEngineState).where(eq(gameEngineState.chatId, id));` to `remove()` and the `removeGroup()` per-chat loop (or add the FK cascade to the libSQL table). The "files" backend cascade already handles chat-level deletes. --- *AI-investigated against v2.0.6; the cleanup helper and the removeSwipe contrast confirmed. Please verify before fixing.*
Author
Owner

@SpicyMarinara commented on GitHub (Jun 29, 2026):

Fixed in staging: turn-game engine snapshots are removed on message/chat/group deletion and pruned/shifted with swipe deletion. Validated with pnpm check and pnpm regression:prompt.

<!-- gh-comment-id:4830956936 --> @SpicyMarinara commented on GitHub (Jun 29, 2026): Fixed in staging: turn-game engine snapshots are removed on message/chat/group deletion and pruned/shifted with swipe deletion. Validated with pnpm check and pnpm regression:prompt.
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#1537
No description provided.