[PR #5084] [MERGED] fix(game): restore turn-game engine state from checkpoints (#5077) #6619

Closed
opened 2026-10-23 04:12:07 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/5084
Author: @kolacheee
Created: 8/15/2026
Status: ✅ Merged
Merged: 8/15/2026
Merged by: @kolacheee

Base: staging ← Head: fix/5077-checkpoint-game-engine-state


📝 Commits (6)

  • 2883bad fix(game): restore turn-game engine state from checkpoints (#5077)
  • 3a05f5e test(game): clean up seeded chats in the checkpoint regression
  • 4958ddd docs(game): note the in-place-mutation limit of the checkpoint engine lookup
  • 01489be Merge branch 'staging' into fix/5077-checkpoint-game-engine-state
  • 855bbfc Merge branch 'staging' into fix/5077-checkpoint-game-engine-state
  • f363d80 Merge remote-tracking branch 'origin/staging' into fix/5077-checkpoint-game-engine-state

📊 Changes

6 files changed (+269 additions, -4 deletions)

View changed files

📝 CHANGELOG.md (+1 -0)
📝 package.json (+1 -1)
📝 packages/server/src/routes/game.routes.ts (+23 -0)
📝 packages/server/src/services/storage/game-engine-state.storage.ts (+26 -1)
📝 packages/server/src/services/turn-games/turn-game-runner.service.ts (+24 -2)
➕ scripts/regressions/game-checkpoint-engine-state.regression.ts (+194 -0)

📄 Description

Linked issue

Closes #5077

Why this change

Game checkpoints captured the story and World Maps snapshots but never the turn-game engine state (game_engine_state). Loading a checkpoint rewound the narration and the map while an active turn-game (UNO, Chess, Poker, Eight Ball) kept its post-checkpoint hand/board. This is also the missing hook for any capability package that stores per-message game state and wants to participate in checkpoints (the "why it matters beyond turn-games" from the issue).

Reachability note (correcting the issue): the issue says the turn-game start route has "no mode gate, so the overlap is reachable via the API today." That is not accurate — startTurnGame refuses any non-conversation chat (this gate exists as far back as the issue's referenced 068d01fad), and checkpoints require a game-mode world-state snapshot, so a turn-game and a checkpoint cannot coexist in one chat through normal play. The underlying gap is still real, but today it is only reachable by constructing the state directly; the value is future-proofing checkpoints (per the issue's own broader motivation) and closing the latent bug. Flagging so the scope is clear.

What changed

The engine state's own (message, swipe) anchor is independent of the game/spatial snapshot anchors a checkpoint captures, so re-looking it up by the captured anchor would miss it. Instead the fix recovers the engine state that was current at checkpoint time by createdAt (set once at create and never mutated) and clones it onto the restore anchor:

  • game-engine-state.storage.ts: new getLatestAtOrBefore(chatId, createdAtInclusive) — the latest snapshot for a chat created at or before a timestamp.
  • POST /game/checkpoint/load: after cloning the game-state snapshot onto the restore message, clone getLatestAtOrBefore(cp.createdAt) onto the restore anchor (committed: true). No-op when the chat has never had a turn-game (the lookup is chatId-scoped, so ownership needs no extra guard).
  • resolveTurnGameAnchor: it previously matched only role === "assistant" messages, so the restore's role: "system" message (marked gameStateAnchor: "checkpoint_restore") was skipped and the cloned row stayed invisible behind the last real assistant message — the clone would have been inert. It now also accepts that marked system message, mirroring resolveVisibleGameStateAnchor used by the game-mode tracker.
  • Regression scripts/regressions/game-checkpoint-engine-state.regression.ts (wired into regression:issues) + CHANGELOG entry.

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Manual verification notes

Automated (done): pnpm check passes, and the new game-checkpoint-engine-state regression passes. Because the overlap is not reachable through normal play, the regression constructs the state directly (registers a tiny echo engine and seeds the snapshots), then asserts getTurnGameView rewinds to the checkpoint-time state after a checkpoint load. It was confirmed to fail with each of the two behavioral changes reverted individually (revert resolveTurnGameAnchor → the cloned state stays invisible; revert the clone → nothing lands at the restore anchor), so both changes are load-bearing and the test catches each.

Still worth a manual pass if a reviewer wants to exercise it through the API (not reachable via the UI today): construct a game-mode chat with a turn-game engine row via the turn-games API in a conversation chat scenario, or use the regression as the reference. No normal-play repro exists because of the mode gate above.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Translated docs on the docs-i18n branch updated to match, or a [docs-i18n] follow-up issue opened (see CONTRIBUTING.md § Translated documentation)
  • Version/release files updated (only if this PR includes a version bump)

CHANGELOG updated under Fixed. No user-facing docs under docs/ changed, so no docs-i18n follow-up is needed. No version bump. No storage-format change (the fix re-looks up existing rows rather than adding columns, so no storage-format.json / STORAGE_VERSION bump).


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/5084 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 8/15/2026 **Status:** ✅ Merged **Merged:** 8/15/2026 **Merged by:** [@kolacheee](https://github.com/kolacheee) **Base:** `staging` ← **Head:** `fix/5077-checkpoint-game-engine-state` --- ### 📝 Commits (6) - [`2883bad`](https://github.com/Pasta-Devs/Marinara-Engine/commit/2883bad0e249d9ee2d0b141c17880bd769c5f37a) fix(game): restore turn-game engine state from checkpoints (#5077) - [`3a05f5e`](https://github.com/Pasta-Devs/Marinara-Engine/commit/3a05f5e6b3230706f403a21679f771abb700555f) test(game): clean up seeded chats in the checkpoint regression - [`4958ddd`](https://github.com/Pasta-Devs/Marinara-Engine/commit/4958ddd948f472955fa0b5e0b0c3bad62e23f320) docs(game): note the in-place-mutation limit of the checkpoint engine lookup - [`01489be`](https://github.com/Pasta-Devs/Marinara-Engine/commit/01489beb77e6aee872bb678f1a45b8d084b18994) Merge branch 'staging' into fix/5077-checkpoint-game-engine-state - [`855bbfc`](https://github.com/Pasta-Devs/Marinara-Engine/commit/855bbfcb00f2bb119995520f242a5de690361512) Merge branch 'staging' into fix/5077-checkpoint-game-engine-state - [`f363d80`](https://github.com/Pasta-Devs/Marinara-Engine/commit/f363d806b93499f071a0408f03288d65aa8cbeec) Merge remote-tracking branch 'origin/staging' into fix/5077-checkpoint-game-engine-state ### 📊 Changes **6 files changed** (+269 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+1 -0) 📝 `package.json` (+1 -1) 📝 `packages/server/src/routes/game.routes.ts` (+23 -0) 📝 `packages/server/src/services/storage/game-engine-state.storage.ts` (+26 -1) 📝 `packages/server/src/services/turn-games/turn-game-runner.service.ts` (+24 -2) ➕ `scripts/regressions/game-checkpoint-engine-state.regression.ts` (+194 -0) </details> ### 📄 Description ## Linked issue Closes #5077 ## Why this change Game checkpoints captured the story and World Maps snapshots but never the turn-game engine state (`game_engine_state`). Loading a checkpoint rewound the narration and the map while an active turn-game (UNO, Chess, Poker, Eight Ball) kept its post-checkpoint hand/board. This is also the missing hook for any capability package that stores per-message game state and wants to participate in checkpoints (the "why it matters beyond turn-games" from the issue). **Reachability note (correcting the issue):** the issue says the turn-game start route has "no mode gate, so the overlap is reachable via the API today." That is not accurate — `startTurnGame` refuses any non-conversation chat (this gate exists as far back as the issue's referenced `068d01fad`), and checkpoints require a game-mode world-state snapshot, so a turn-game and a checkpoint cannot coexist in one chat through normal play. The underlying gap is still real, but today it is only reachable by constructing the state directly; the value is future-proofing checkpoints (per the issue's own broader motivation) and closing the latent bug. Flagging so the scope is clear. ## What changed The engine state's own `(message, swipe)` anchor is independent of the game/spatial snapshot anchors a checkpoint captures, so re-looking it up by the captured anchor would miss it. Instead the fix recovers the engine state that was current at checkpoint time by `createdAt` (set once at create and never mutated) and clones it onto the restore anchor: - **`game-engine-state.storage.ts`**: new `getLatestAtOrBefore(chatId, createdAtInclusive)` — the latest snapshot for a chat created at or before a timestamp. - **`POST /game/checkpoint/load`**: after cloning the game-state snapshot onto the restore message, clone `getLatestAtOrBefore(cp.createdAt)` onto the restore anchor (`committed: true`). No-op when the chat has never had a turn-game (the lookup is chatId-scoped, so ownership needs no extra guard). - **`resolveTurnGameAnchor`**: it previously matched only `role === "assistant"` messages, so the restore's `role: "system"` message (marked `gameStateAnchor: "checkpoint_restore"`) was skipped and the cloned row stayed invisible behind the last real assistant message — the clone would have been inert. It now also accepts that marked system message, mirroring `resolveVisibleGameStateAnchor` used by the game-mode tracker. - Regression `scripts/regressions/game-checkpoint-engine-state.regression.ts` (wired into `regression:issues`) + CHANGELOG entry. ## Validation - [x] `pnpm check` passes locally - [x] Container (Docker / Podman) built and ran without issue - [x] Ran the app, clicked through the changes manually - [x] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [x] Above manual verification completed (describe below) - [x] Read and followed `CONTRIBUTING.md` ### Manual verification notes Automated (done): `pnpm check` passes, and the new `game-checkpoint-engine-state` regression passes. Because the overlap is not reachable through normal play, the regression constructs the state directly (registers a tiny echo engine and seeds the snapshots), then asserts `getTurnGameView` rewinds to the checkpoint-time state after a checkpoint load. It was confirmed to **fail with each of the two behavioral changes reverted individually** (revert `resolveTurnGameAnchor` → the cloned state stays invisible; revert the clone → nothing lands at the restore anchor), so both changes are load-bearing and the test catches each. Still worth a manual pass if a reviewer wants to exercise it through the API (not reachable via the UI today): construct a game-mode chat with a turn-game engine row via the turn-games API in a conversation chat scenario, or use the regression as the reference. No normal-play repro exists because of the mode gate above. ## Docs and release impact - [ ] No docs changes needed - [x] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Translated docs on the `docs-i18n` branch updated to match, or a `[docs-i18n]` follow-up issue opened (see CONTRIBUTING.md § Translated documentation) - [ ] Version/release files updated (only if this PR includes a version bump) CHANGELOG updated under Fixed. No user-facing docs under `docs/` changed, so no `docs-i18n` follow-up is needed. No version bump. No storage-format change (the fix re-looks up existing rows rather than adding columns, so no `storage-format.json` / `STORAGE_VERSION` bump). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-23 04:12:07 +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#6619
No description provided.