[GH-ISSUE #2712] [Issue]: Game checkpoint restore uses messageId+swipe:0 instead of snapshotId — fails after a message is deleted/swiped #1356

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

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

Originally assigned to: @SpicyMarinara on GitHub.

Summary

Loading a game checkpoint resolves the saved snapshot by messageId + swipeIndex:0 instead of the stored snapshotId, so a checkpoint loads the wrong swipe's state — or fails permanently — once the anchored message is deleted, regenerated, or was taken on a swiped message. The checkpoint's own snapshotId (still valid) is never used.

Expected behavior

Loading a checkpoint should restore the exact snapshot it captured (via its stored snapshotId), regardless of later message edits/deletions/swipes.

Actual behavior

checkpoint.service.ts persists both snapshotId and messageId, but /game/checkpoint/load resolves the snapshot via stateStore.getByMessage(cp.messageId, 0) and throws "Checkpoint snapshot no longer exists" if that returns null — the stored cp.snapshotId is never used. getByMessage filters by messageId AND swipeIndex (hardcoded 0). Two failure modes: (1) a checkpoint taken on a swiped assistant message (swipeIndex > 0) loads the wrong snapshot or none; (2) if the original message was later deleted/regenerated/pruned, getByMessage returns null and restore throws permanently — even though cp.snapshotId still points at a valid, recoverable snapshot row.

How it breaks the pipeline

Code trace (staging @ b826f8f4):

  • packages/server/src/services/game/checkpoint.service.ts:52-69 (persists both snapshotId and messageId).
  • packages/server/src/routes/game.routes.ts:7824 (stateStore.getByMessage(cp.messageId, 0)), :7825 (throws if null) — cp.snapshotId unused.
  • packages/server/src/services/game/game-state.storage.ts:189 (getByMessage filters by messageId AND swipeIndex, here 0).

Steps to reproduce

  1. Create a checkpoint, then delete or regenerate the message it was anchored to (or take the checkpoint on a swiped message).
  2. POST /game/checkpoint/load with that checkpoint → a 500 "Checkpoint snapshot no longer exists", or restoration of the wrong swipe's state.

Suggested fix

Resolve the snapshot by cp.snapshotId first (add a getById to game-state.storage and use it), falling back to getByMessage only if needed. At minimum, don't hardcode swipeIndex 0 — resolve the snapshot the checkpoint actually captured.

Environment

  • Marinara Engine version: 2.0.0 (staging @ b826f8f4)
  • Install type: source
  • OS + version: All (code-level defect, platform-independent)
  • Browser or app shell: All

Additional context

Found via a systematic multi-agent audit of Game Mode. Presents as "load checkpoint does nothing / errors" and effectively corrupt/unloadable saves. Part of a set of game-mode issues filed together.

Template check

Please uncheck (untick) 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 @kolacheee on GitHub (Jun 21, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2712 Originally assigned to: @SpicyMarinara on GitHub. ## Summary Loading a game checkpoint resolves the saved snapshot by `messageId` + `swipeIndex:0` instead of the stored `snapshotId`, so a checkpoint loads the wrong swipe's state — or fails permanently — once the anchored message is deleted, regenerated, or was taken on a swiped message. The checkpoint's own `snapshotId` (still valid) is never used. ## Expected behavior Loading a checkpoint should restore the exact snapshot it captured (via its stored `snapshotId`), regardless of later message edits/deletions/swipes. ## Actual behavior `checkpoint.service.ts` persists both `snapshotId` and `messageId`, but `/game/checkpoint/load` resolves the snapshot via `stateStore.getByMessage(cp.messageId, 0)` and throws "Checkpoint snapshot no longer exists" if that returns null — the stored `cp.snapshotId` is never used. `getByMessage` filters by `messageId` AND `swipeIndex` (hardcoded `0`). Two failure modes: (1) a checkpoint taken on a swiped assistant message (`swipeIndex > 0`) loads the wrong snapshot or none; (2) if the original message was later deleted/regenerated/pruned, `getByMessage` returns null and restore throws permanently — even though `cp.snapshotId` still points at a valid, recoverable snapshot row. ## How it breaks the pipeline Code trace (staging @ `b826f8f4`): - `packages/server/src/services/game/checkpoint.service.ts:52-69` (persists both `snapshotId` and `messageId`). - `packages/server/src/routes/game.routes.ts:7824` (`stateStore.getByMessage(cp.messageId, 0)`), `:7825` (throws if null) — `cp.snapshotId` unused. - `packages/server/src/services/game/game-state.storage.ts:189` (`getByMessage` filters by `messageId` AND `swipeIndex`, here `0`). ## Steps to reproduce 1. Create a checkpoint, then delete or regenerate the message it was anchored to (or take the checkpoint on a swiped message). 2. `POST /game/checkpoint/load` with that checkpoint → a 500 "Checkpoint snapshot no longer exists", or restoration of the wrong swipe's state. ## Suggested fix Resolve the snapshot by `cp.snapshotId` first (add a `getById` to `game-state.storage` and use it), falling back to `getByMessage` only if needed. At minimum, don't hardcode `swipeIndex 0` — resolve the snapshot the checkpoint actually captured. ## Environment - Marinara Engine version: 2.0.0 (staging @ `b826f8f4`) - Install type: source - OS + version: All (code-level defect, platform-independent) - Browser or app shell: All ## Additional context Found via a systematic multi-agent audit of Game Mode. Presents as "load checkpoint does nothing / errors" and effectively corrupt/unloadable saves. Part of a set of game-mode issues filed together. ## Template check Please **uncheck (untick)** 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.
Author
Owner

@kolacheee commented on GitHub (Jun 21, 2026):

Veracity self-review — correction / clarification
Automated post-filing review of this issue against the current working tree. The reported bug remains valid; the notes below refine its scope, accuracy, or suggested fix for whoever implements it.

Two corrections and a fix note to keep this actionable (verified against the current tree):

  1. Strongest repro / missed instance: swipe deletion re-indexes snapshots. removeSwipe (packages/server/src/services/storage/chats.storage.ts:902-927) deletes the removed swipe's snapshot AND decrements swipeIndex for every higher snapshot on that message. So after deleting an earlier swipe, the snapshot the checkpoint captured now sits at a lower swipeIndex, and getByMessage(cp.messageId, 0) resolves the wrong one. Note the captured snapshot's row id is unchanged (only swipe_index shifts), so cp.snapshotId is still recoverable here — which is exactly why resolving by snapshotId fixes it. Add this as the primary reproduction.

  2. Correction to failure mode 2: a message delete/regenerate/prune does NOT remove the snapshot. removeMessage/removeMessages (chats.storage.ts:752-782) delete only message/swipe rows; there is no messages -> game_state_snapshots cascade (file-backed-store.ts:205-227 cascades only chats->snapshots) and no DB-level FK cascade (game-state.ts:9-10: messageId is plain notNull, "cascade handled at application level"). Regeneration reuses the same messageId via addSwipe (generate.routes.ts:6397), leaving swipe-0's snapshot intact. So in those cases getByMessage(cp.messageId, 0) returns a STALE swipe-0 snapshot (wrong-state restore), not null. The only path that produces "snapshot no longer exists / permanent throw" is swipe-deleting the exact captured swipe — and there cp.snapshotId's row is deleted too, so it is NOT recoverable via snapshotId either. Reword the "throws permanently / cp.snapshotId still valid" claim: those two never co-occur. The bug primarily manifests as restoring the WRONG swipe's state (silent corruption); hard failure is the narrow swipe-delete-of-the-captured-swipe case.

  3. Fix note: when resolving by cp.snapshotId (add getById to game-state.storage — there is no snapshot getById today; the existing checkpoint.service.ts:79 getById is for the checkpoint row), do NOT silently fall back to getByMessage(cp.messageId, 0) on null. The only time getById(cp.snapshotId) is null is when the captured snapshot row was actually deleted, and in that case getByMessage(..., 0) would restore a DIFFERENT, wrong snapshot. Resolve strictly by snapshotId and, if the row is gone, throw a clear "snapshot was deleted" error instead of falling back.

(Minor file-path nits, no impact on the diagnosis: the snapshot storage is at packages/server/src/services/storage/game-state.storage.ts, not services/game/...; and the load route is game.routes.ts:7813-7825 in the current tree.)

<!-- gh-comment-id:4761102694 --> @kolacheee commented on GitHub (Jun 21, 2026): > **Veracity self-review — correction / clarification** > _Automated post-filing review of this issue against the current working tree. The reported bug remains valid; the notes below refine its scope, accuracy, or suggested fix for whoever implements it._ Two corrections and a fix note to keep this actionable (verified against the current tree): 1. Strongest repro / missed instance: swipe deletion re-indexes snapshots. `removeSwipe` (packages/server/src/services/storage/chats.storage.ts:902-927) deletes the removed swipe's snapshot AND decrements `swipeIndex` for every higher snapshot on that message. So after deleting an earlier swipe, the snapshot the checkpoint captured now sits at a lower `swipeIndex`, and `getByMessage(cp.messageId, 0)` resolves the wrong one. Note the captured snapshot's row id is unchanged (only `swipe_index` shifts), so `cp.snapshotId` is still recoverable here — which is exactly why resolving by `snapshotId` fixes it. Add this as the primary reproduction. 2. Correction to failure mode 2: a message delete/regenerate/prune does NOT remove the snapshot. `removeMessage`/`removeMessages` (chats.storage.ts:752-782) delete only message/swipe rows; there is no `messages -> game_state_snapshots` cascade (file-backed-store.ts:205-227 cascades only chats->snapshots) and no DB-level FK cascade (game-state.ts:9-10: messageId is plain notNull, "cascade handled at application level"). Regeneration reuses the same messageId via `addSwipe` (generate.routes.ts:6397), leaving swipe-0's snapshot intact. So in those cases `getByMessage(cp.messageId, 0)` returns a STALE swipe-0 snapshot (wrong-state restore), not null. The only path that produces "snapshot no longer exists / permanent throw" is swipe-deleting the exact captured swipe — and there `cp.snapshotId`'s row is deleted too, so it is NOT recoverable via snapshotId either. Reword the "throws permanently / cp.snapshotId still valid" claim: those two never co-occur. The bug primarily manifests as restoring the WRONG swipe's state (silent corruption); hard failure is the narrow swipe-delete-of-the-captured-swipe case. 3. Fix note: when resolving by `cp.snapshotId` (add `getById` to game-state.storage — there is no snapshot `getById` today; the existing checkpoint.service.ts:79 getById is for the checkpoint row), do NOT silently fall back to `getByMessage(cp.messageId, 0)` on null. The only time `getById(cp.snapshotId)` is null is when the captured snapshot row was actually deleted, and in that case `getByMessage(..., 0)` would restore a DIFFERENT, wrong snapshot. Resolve strictly by `snapshotId` and, if the row is gone, throw a clear "snapshot was deleted" error instead of falling back. (Minor file-path nits, no impact on the diagnosis: the snapshot storage is at packages/server/src/services/storage/game-state.storage.ts, not services/game/...; and the load route is game.routes.ts:7813-7825 in the current tree.)
Author
Owner

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

Fixed in aedab566. Checkpoint load now resolves the exact captured game-state snapshot by checkpoint.snapshotId, verifies it belongs to the chat, and no longer falls back to messageId + swipeIndex 0, avoiding wrong-swipe restores after swipe reindexing.

<!-- gh-comment-id:4761650343 --> @SpicyMarinara commented on GitHub (Jun 21, 2026): Fixed in aedab566. Checkpoint load now resolves the exact captured game-state snapshot by checkpoint.snapshotId, verifies it belongs to the chat, and no longer falls back to messageId + swipeIndex 0, avoiding wrong-swipe restores after swipe reindexing.
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#1356
No description provided.