[GH-ISSUE #5077] Game checkpoints don't capture turn-game state (game_engine_state) — restore rewinds the story but not the game #2809

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

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

Originally assigned to: @kolacheee on GitHub.

What happens

Game checkpoints capture the story state and the World Maps location, but not the turn-game engine state table. Loading a checkpoint rewinds the narration and the map — while an active turn-game (UNO, Chess, Poker, Eight Ball) keeps its newest state as if nothing happened.

Try it yourself

Turn-games are normally started from conversation chats (where checkpoints don't exist), but the start route has no mode gate, so the overlap is reachable via the API today:

  1. Create a Game Mode game, note the chat id, play a turn so a game-state snapshot exists.
  2. Start a turn game and make a couple of moves:
curl -s -X POST http://localhost:3000/api/turn-games/<chatId>/start \
  -H "Content-Type: application/json" -d '{"gameType":"uno"}'
  1. Create a checkpoint (POST /api/game/checkpoint), then make more moves.
  2. Load the checkpoint (POST /api/game/checkpoint/load). The story gets its [Checkpoint restored] marker and the game-state + spatial snapshots are restored — but GET /api/turn-games/<chatId>/state still shows the post-checkpoint hand/board.

Why it happens

checkpoint.service.ts knows about exactly two snapshot stores: packages/server/src/services/game/checkpoint.service.ts:10 imports only gameCheckpoints, gameStateSnapshots, spatialContextSnapshots, and create() (:57-100) captures only those two rows. The game_engine_state table (packages/server/src/db/schema/game-engine-state.ts) — which is per-(chatId, messageId, swipeIndex) and already correctly wired into swipe delete/re-index, message/chat/group delete prunes, and branch re-anchoring — is never captured or restored by checkpoints.

Why it matters beyond turn-games

This is also the missing hook for any current or future capability package that stores per-message game state. A Game Mode "Experience" package (e.g. the pixel-RPG design being explored) has no way to participate in checkpoints at all: its world state survives a checkpoint load un-rewound, silently diverging from the restored story and map.

Suggested fix

Capture the game_engine_state anchor alongside the existing two snapshots and re-look-up on restore (keying by chatId/messageId/swipeIndex), rather than denormalizing new columns onto game_checkpoints — the re-lookup variant changes no on-disk table shape, so no storage-format.json / STORAGE_VERSION bump is needed. The existing cross-chat ownership guards in create() (:63-66, :85-88) should be mirrored for the new capture.

Verified against staging @ 068d01fad.

Originally created by @kolacheee on GitHub (Aug 15, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/5077 Originally assigned to: @kolacheee on GitHub. ### What happens Game checkpoints capture the story state and the World Maps location, but not the turn-game engine state table. Loading a checkpoint rewinds the narration and the map — while an active turn-game (UNO, Chess, Poker, Eight Ball) keeps its newest state as if nothing happened. ### Try it yourself Turn-games are normally started from conversation chats (where checkpoints don't exist), but the start route has no mode gate, so the overlap is reachable via the API today: 1. Create a Game Mode game, note the chat id, play a turn so a game-state snapshot exists. 2. Start a turn game and make a couple of moves: ``` curl -s -X POST http://localhost:3000/api/turn-games/<chatId>/start \ -H "Content-Type: application/json" -d '{"gameType":"uno"}' ``` 3. Create a checkpoint (`POST /api/game/checkpoint`), then make more moves. 4. Load the checkpoint (`POST /api/game/checkpoint/load`). The story gets its `[Checkpoint restored]` marker and the game-state + spatial snapshots are restored — but `GET /api/turn-games/<chatId>/state` still shows the post-checkpoint hand/board. ### Why it happens `checkpoint.service.ts` knows about exactly two snapshot stores: `packages/server/src/services/game/checkpoint.service.ts:10` imports only `gameCheckpoints, gameStateSnapshots, spatialContextSnapshots`, and `create()` (`:57-100`) captures only those two rows. The `game_engine_state` table (`packages/server/src/db/schema/game-engine-state.ts`) — which is per-`(chatId, messageId, swipeIndex)` and already correctly wired into swipe delete/re-index, message/chat/group delete prunes, and branch re-anchoring — is never captured or restored by checkpoints. ### Why it matters beyond turn-games This is also the missing hook for any current or future capability package that stores per-message game state. A Game Mode "Experience" package (e.g. the pixel-RPG design being explored) has no way to participate in checkpoints at all: its world state survives a checkpoint load un-rewound, silently diverging from the restored story and map. ### Suggested fix Capture the `game_engine_state` anchor alongside the existing two snapshots and re-look-up on restore (keying by `chatId`/`messageId`/`swipeIndex`), rather than denormalizing new columns onto `game_checkpoints` — the re-lookup variant changes no on-disk table shape, so no `storage-format.json` / `STORAGE_VERSION` bump is needed. The existing cross-chat ownership guards in `create()` (`:63-66`, `:85-88`) should be mirrored for the new capture. Verified against `staging` @ 068d01fad.
bsakaguchi 2026-10-21 05:55:58 +07:00
Author
Owner

@kolacheee commented on GitHub (Aug 15, 2026):

Reachability correction while implementing this (PR #5084):

The repro as written does not run today. startTurnGame refuses any non-conversation chat ("Turn-games can only be started in conversation mode.") and this gate is present as far back as the referenced 068d01fad, so step 2 (starting UNO in a Game Mode chat) fails. Checkpoints in turn need a game-mode world-state snapshot. So a turn-game and a checkpoint cannot coexist in one chat through normal play — the turn-game <-> checkpoint overlap is not reachable via the API today.

The underlying gap is still real (checkpoint create/load completely ignore game_engine_state), and the fix ships it as the future-proofing hook this issue's "why it matters beyond turn-games" section describes, plus closing the latent bug. Flagging so the scope is honest.

Implementation note: the engine state's own (message, swipe) anchor is independent of the game/spatial snapshot anchors a checkpoint captures, so re-lookup by the captured anchor would miss it. The PR keys off createdAt <= checkpoint.createdAt instead, and also teaches resolveTurnGameAnchor to honour the checkpoint_restore marked system message (otherwise the cloned row stays invisible behind the last real assistant message).

<!-- gh-comment-id:5303729934 --> @kolacheee commented on GitHub (Aug 15, 2026): Reachability correction while implementing this (PR #5084): The repro as written does not run today. `startTurnGame` refuses any non-conversation chat ("Turn-games can only be started in conversation mode.") and this gate is present as far back as the referenced `068d01fad`, so step 2 (starting UNO in a Game Mode chat) fails. Checkpoints in turn need a game-mode world-state snapshot. So a turn-game and a checkpoint cannot coexist in one chat through normal play — the turn-game <-> checkpoint overlap is not reachable via the API today. The underlying gap is still real (checkpoint create/load completely ignore `game_engine_state`), and the fix ships it as the future-proofing hook this issue's "why it matters beyond turn-games" section describes, plus closing the latent bug. Flagging so the scope is honest. Implementation note: the engine state's own (message, swipe) anchor is independent of the game/spatial snapshot anchors a checkpoint captures, so re-lookup by the captured anchor would miss it. The PR keys off `createdAt <= checkpoint.createdAt` instead, and also teaches `resolveTurnGameAnchor` to honour the `checkpoint_restore` marked system message (otherwise the cloned row stays invisible behind the last real assistant message).
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#2809
No description provided.