[PR #7787] [MERGED] fix(game): checkpoint loads restore the State Editor's World Edits (#7783) #7928

Closed
opened 2026-10-24 04:08:54 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/7787
Author: @ylli-selmani
Created: 10/18/2026
Status: ✅ Merged
Merged: 10/18/2026
Merged by: @ylli-selmani

Base: staging ← Head: feat/checkpoint-world-edits


📝 Commits (6)

  • b22145c fix(game): a checkpoint rewind puts the world's hand edits back (#7783)
  • d4ebe0b docs(game): a checkpoint load undoes World Edits (#7783)
  • da893f4 fix(game): a checkpoint load only touches the world when its edits differ (#7783)
  • 2d2dcb5 fix(game): save the checkpoint's world snapshot inside the edits lock (#7783)
  • ecad34e fix(game): make the checkpoint restore and its world snapshot one write (#7783)
  • 4b6c2bc test(game): pin that a failed snapshot save leaves the restore unwritten (#7783)

📊 Changes

7 files changed (+495 additions, -21 deletions)

View changed files

📝 docs/game/sessions-and-saves.md (+1 -1)
📝 packages/client/src/localization/locales/en.json (+2 -2)
📝 packages/server/src/db/schema/checkpoints.ts (+4 -0)
📝 packages/server/src/routes/game.routes.ts (+68 -12)
📝 packages/server/src/services/game/checkpoint.service.ts (+80 -3)
📝 packages/server/src/services/storage/chats.storage.ts (+6 -3)
➕ scripts/regressions/game-checkpoint-world-edits.regression.ts (+334 -0)

📄 Description

Important

Contributions target staging. Only SpicyMarinara may promote this repository's staging branch or a same-repository hotfix/* branch to main.
Outside and first-time contributors also require an approving review from SpicyMarinara.

Linked issue

Closes #7783

On top of staging after #7773 (c8d108c7c). Commits: b22145c10 (capture + restore + regression), d4ebe0bc3 (copy), da893f470 (review: skip the world write when the layer is unchanged), 2d2dcb5da (review: snapshot save inside the world lock), ecad34e24 (review: the restore and its snapshot are one transaction), 4b6c2bcd0 (review: a failed snapshot save leaves the restore unwritten).

Why this change

  • The Game data editor takes a checkpoint before every save, and Game controls → Checkpoints → Load is how a bad edit is undone. It rewound everything except World Edits, so a Load could leave newer hand edits in place.
  • Worse than leaving them: a later edit that took away a place or person the restored story names made the restored story read as null. The Load reported success, and the game kept playing with no story at all until someone fixed the script or the edits.

What changed

  • A checkpoint now saves the world's hand-edits layer (gameWorld.edits) in its own field, and a Load puts it back together with the story from the same checkpoint, in one metadata write — so the pair captured together always fits each other afterwards.
  • The restore goes through the same path as saving World Edits by hand: the world lock, the edit and town checks, the story check, and a fresh world snapshot. The metadata restore and that snapshot save run in one database transaction, inside the same world lock PUT /world/edits holds, with the metadata queue taken before the transaction (the order chats.storage documents). So an editor save landing mid-Load can neither have its snapshot overwritten by the Load's older metadata, nor be rolled back by a failed Load, and a snapshot that cannot be saved takes the whole restore back instead of leaving the restored story beside the old world. A checkpoint whose layer cannot be put back safely leaves the current layer alone, and the rest of the Load still happens.
  • When the chat already carries the captured layer (the common rewind: the tracker and story moved, the hand edits did not) the Load writes no world and re-saves no snapshot, so it costs no lock and no write.
  • Checkpoints from before the field keep the old behaviour for this one field: a Load leaves the current World Edits untouched.
  • Every place that said a Load leaves the World Edits alone now says it restores them: the Load confirmation in the checkpoints dialog (ui.game.checkpoints.loadMessage, which #7773 had just worded as "but not your World Edits"), the World Edits section description (keeping #7743's wording, minus "stay when you load a checkpoint"), and the checkpoint paragraph in docs/game/sessions-and-saves.md.

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

Proven locally:

  • pnpm check passes on the final tree (and on the same tree before the review fix).
  • node scripts/run-regressions.mjs --filter checkpoint → 5/5 passed, including the new game-checkpoint-world-edits.regression.ts.
  • That regression drives the real POST /game/checkpoint/load route: it seeds a world with a story and a hand-added person, takes a checkpoint, then rewrites the story and removes the person, seeds a stale snapshot without the layer (so loadGameWorld cannot fall back to the metadata), then loads. After the load the layer is back, gameStoryFromMetadata is not null (the story plays again), and the world snapshot — read back through loadGameWorld — describes the restored layer, which only a re-save by the Load can produce. It also covers: clearing a layer added after an empty capture; an empty capture over a chat whose layer is already absent (nothing written); a legacy checkpoint leaving the layer alone; a chat with no world; a capture refused by the story check; and a chat carrying a world from a generator this Engine no longer reads, which loads fine and keeps its world exactly as it was.
  • pnpm localization:check passes after the copy change.

Local CodeRabbit (one batched run on the 3-commit diff, base origin/staging), 3 findings:

  • major — "skip restoring when the reconstructed game fails worldMadeByThisGenerator": already true above this helper; gameWorldFromMetadata returns null for any world whose generator version this Engine does not read, so the restore refuses before applying an edit. Pinned by regression part 4. The second half of the finding (skip the write when the edits match) was real: fixed in da893f470, with pure and end-to-end assertions.
  • minor — translated docs: already tracked by #7774, which this PR's doc change is noted on (and in the Docs section below).
  • trivial — the snapshot assertion and the different-generator case it asked for: the different-generator case is the new regression part 4; the snapshot assertion was real and is now made honest by seeding a stale snapshot first (2d2dcb5da).

GitHub review (CodeRabbit, after the first push), 3 findings:

  • minor — the snapshot save and its rollback happened after the world lock was released, so a concurrent editor save could have its snapshot overwritten (and, on a failed save, its edits rolled back). Real; both now run inside the lock (2d2dcb5da).
  • trivial — the regression's snapshot check could pass on the metadata fallback; fixed as above.
  • minor — Marinara-Agents vendors this schema and pins its Engine fingerprint in scripts/vendored-engine-baseline.json (the checkpoints.ts entry is drifted). Adding the column will make their Noodle vendored-engine check want that entry refreshed after this lands on staging. Nothing to change in this PR; see the cross-repo note under Docs and release impact.

GitHub review, round 2 (CodeRabbit), 1 finding:

  • minor — "wrap the checkpoint metadata and snapshot writes in one transaction": real, and the reasoning held up — a throw from the snapshot save left the restored story committed beside the old snapshot, which plays the story against a world it was not written for. Fixed in ecad34e24: both writes run in one app.db.transaction; the metadata queue is taken before it and the patch runs with metadataQueueHeld, the ordering chats.storage documents and advanced-memory already uses, and patchMetadataWithCharacterIds now accepts that opt the way patchMetadata does. The compensating gameWorld-only rollback is gone, so a failed snapshot save rolls the whole restore back.

GitHub review, round 3 (CodeRabbit), 1 finding:

  • trivial — "add a route-level rollback regression for a failed snapshot save": the failure itself cannot be injected at the route — the store's failure hooks exist only when the database is created and fire on flush, not on an insert — so the regression pins the contract the load depends on instead: a patch written inside the restore's transaction is undone when the save after it throws, with the metadata compared before and after (4b6c2bcd0, regression part 5). Moving the patch out of the transaction, or dropping metadataQueueHeld, makes it fail.

Not verified by me (please do in the app):

  • A real game with a generated world and a story, clicked through in a browser: save a World Edits change, make another change, Load the earlier checkpoint, and confirm the World Edits section, the map and the story all show the earlier state. The worktree's data copy has no game with a world and a story, so this was not exercised in the app.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README) 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)

docs/game/sessions-and-saves.md is already tracked by the open [docs-i18n] follow-up #7774. This PR changes that file's English text (the "doesn't undo World Edits" exception is gone), and #7774's thread now carries the final English of the changed paragraph and the two UI strings, so the translated packs are not edited here.

Cross-repo note: the new column changes packages/server/src/db/schema/checkpoints.ts, which Marinara-Agents vendors under sources/engine/ and pins in scripts/vendored-engine-baseline.json (that path's entry is drifted today). Once this lands on staging, their Noodle vendored-engine check will see the new host fingerprint, so that snapshot and baseline want a separate Marinara-Agents change (re-vendor the file and drop the resolved entry — their validator allows the baseline only to shrink). No Engine-side action; flagged here so it is not a surprise after merge.

UI evidence (if applicable)

No new UI surface: three strings changed (two in the Game data editor and checkpoints dialog, one doc paragraph). No screenshot.

Summary by CodeRabbit

  • New Features
    • Checkpoints now save and restore World Edits alongside the map, tracker, and story. Loading a checkpoint with an empty saved edits layer clears edits made afterward.
    • If saved World Edits are invalid or incompatible with the restored story, those edits remain unchanged while the rest of the checkpoint loads.
  • Documentation
    • Updated checkpoint and World Edits descriptions to clarify that edits are restored when loading a checkpoint.

🔄 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/7787 **Author:** [@ylli-selmani](https://github.com/ylli-selmani) **Created:** 10/18/2026 **Status:** ✅ Merged **Merged:** 10/18/2026 **Merged by:** [@ylli-selmani](https://github.com/ylli-selmani) **Base:** `staging` ← **Head:** `feat/checkpoint-world-edits` --- ### 📝 Commits (6) - [`b22145c`](https://github.com/Pasta-Devs/Marinara-Engine/commit/b22145c104361e8c9dcdb33e7f2aa0b097bf6e87) fix(game): a checkpoint rewind puts the world's hand edits back (#7783) - [`d4ebe0b`](https://github.com/Pasta-Devs/Marinara-Engine/commit/d4ebe0bc394d21cbc9edf65e544cc117afa0d63f) docs(game): a checkpoint load undoes World Edits (#7783) - [`da893f4`](https://github.com/Pasta-Devs/Marinara-Engine/commit/da893f47037b70f8d725676d53cc03e2c801cfa2) fix(game): a checkpoint load only touches the world when its edits differ (#7783) - [`2d2dcb5`](https://github.com/Pasta-Devs/Marinara-Engine/commit/2d2dcb5dae3a113138dc1c102f7c613634034392) fix(game): save the checkpoint's world snapshot inside the edits lock (#7783) - [`ecad34e`](https://github.com/Pasta-Devs/Marinara-Engine/commit/ecad34e24fcb3c489c166216ad51bd14ab00734f) fix(game): make the checkpoint restore and its world snapshot one write (#7783) - [`4b6c2bc`](https://github.com/Pasta-Devs/Marinara-Engine/commit/4b6c2bcd000091865e983f2679c7d4373b56811c) test(game): pin that a failed snapshot save leaves the restore unwritten (#7783) ### 📊 Changes **7 files changed** (+495 additions, -21 deletions) <details> <summary>View changed files</summary> 📝 `docs/game/sessions-and-saves.md` (+1 -1) 📝 `packages/client/src/localization/locales/en.json` (+2 -2) 📝 `packages/server/src/db/schema/checkpoints.ts` (+4 -0) 📝 `packages/server/src/routes/game.routes.ts` (+68 -12) 📝 `packages/server/src/services/game/checkpoint.service.ts` (+80 -3) 📝 `packages/server/src/services/storage/chats.storage.ts` (+6 -3) ➕ `scripts/regressions/game-checkpoint-world-edits.regression.ts` (+334 -0) </details> ### 📄 Description > [!IMPORTANT] > Contributions target `staging`. Only `SpicyMarinara` may promote this repository's `staging` branch or a same-repository `hotfix/*` branch to `main`. > Outside and first-time contributors also require an approving review from `SpicyMarinara`. ## Linked issue Closes #7783 On top of `staging` after #7773 (`c8d108c7c`). Commits: `b22145c10` (capture + restore + regression), `d4ebe0bc3` (copy), `da893f470` (review: skip the world write when the layer is unchanged), `2d2dcb5da` (review: snapshot save inside the world lock), `ecad34e24` (review: the restore and its snapshot are one transaction), `4b6c2bcd0` (review: a failed snapshot save leaves the restore unwritten). ## Why this change - The Game data editor takes a checkpoint before every save, and **Game controls → Checkpoints → Load** is how a bad edit is undone. It rewound everything except **World Edits**, so a Load could leave newer hand edits in place. - Worse than leaving them: a later edit that took away a place or person the restored story names made the restored story read as null. The Load reported success, and the game kept playing with no story at all until someone fixed the script or the edits. ## What changed - A checkpoint now saves the world's hand-edits layer (`gameWorld.edits`) in its own field, and a Load puts it back **together with the story from the same checkpoint**, in one metadata write — so the pair captured together always fits each other afterwards. - The restore goes through the same path as saving World Edits by hand: the world lock, the edit and town checks, the story check, and a fresh world snapshot. The metadata restore and that snapshot save run in **one database transaction**, inside the same world lock `PUT /world/edits` holds, with the metadata queue taken before the transaction (the order `chats.storage` documents). So an editor save landing mid-Load can neither have its snapshot overwritten by the Load's older metadata, nor be rolled back by a failed Load, and a snapshot that cannot be saved takes the whole restore back instead of leaving the restored story beside the old world. A checkpoint whose layer cannot be put back safely leaves the current layer alone, and the rest of the Load still happens. - When the chat already carries the captured layer (the common rewind: the tracker and story moved, the hand edits did not) the Load writes no world and re-saves no snapshot, so it costs no lock and no write. - Checkpoints from before the field keep the old behaviour for this one field: a Load leaves the current World Edits untouched. - Every place that said a Load leaves the World Edits alone now says it restores them: the Load confirmation in the checkpoints dialog (`ui.game.checkpoints.loadMessage`, which #7773 had just worded as "but not your World Edits"), the World Edits section description (keeping #7743's wording, minus "stay when you load a checkpoint"), and the checkpoint paragraph in `docs/game/sessions-and-saves.md`. ## 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 Proven locally: - `pnpm check` passes on the final tree (and on the same tree before the review fix). - `node scripts/run-regressions.mjs --filter checkpoint` → **5/5 passed**, including the new `game-checkpoint-world-edits.regression.ts`. - That regression drives the real `POST /game/checkpoint/load` route: it seeds a world with a story and a hand-added person, takes a checkpoint, then rewrites the story and removes the person, seeds a **stale snapshot without the layer** (so `loadGameWorld` cannot fall back to the metadata), then loads. After the load the layer is back, `gameStoryFromMetadata` is not null (the story plays again), and the world snapshot — read back through `loadGameWorld` — describes the restored layer, which only a re-save by the Load can produce. It also covers: clearing a layer added after an empty capture; an empty capture over a chat whose layer is already absent (nothing written); a legacy checkpoint leaving the layer alone; a chat with no world; a capture refused by the story check; and a chat carrying a world from a generator this Engine no longer reads, which loads fine and keeps its world exactly as it was. - `pnpm localization:check` passes after the copy change. Local CodeRabbit (one batched run on the 3-commit diff, base `origin/staging`), 3 findings: - *major* — "skip restoring when the reconstructed game fails `worldMadeByThisGenerator`": already true above this helper; `gameWorldFromMetadata` returns null for any world whose generator version this Engine does not read, so the restore refuses before applying an edit. Pinned by regression part 4. The second half of the finding (skip the write when the edits match) was real: fixed in `da893f470`, with pure and end-to-end assertions. - *minor* — translated docs: already tracked by #7774, which this PR's doc change is noted on (and in the Docs section below). - *trivial* — the snapshot assertion and the different-generator case it asked for: the different-generator case is the new regression part 4; the snapshot assertion was real and is now made honest by seeding a stale snapshot first (`2d2dcb5da`). GitHub review (CodeRabbit, after the first push), 3 findings: - *minor* — the snapshot save and its rollback happened after the world lock was released, so a concurrent editor save could have its snapshot overwritten (and, on a failed save, its edits rolled back). Real; both now run inside the lock (`2d2dcb5da`). - *trivial* — the regression's snapshot check could pass on the metadata fallback; fixed as above. - *minor* — Marinara-Agents vendors this schema and pins its Engine fingerprint in `scripts/vendored-engine-baseline.json` (the `checkpoints.ts` entry is `drifted`). Adding the column will make their Noodle vendored-engine check want that entry refreshed after this lands on `staging`. Nothing to change in this PR; see the cross-repo note under Docs and release impact. GitHub review, round 2 (CodeRabbit), 1 finding: - *minor* — "wrap the checkpoint metadata and snapshot writes in one transaction": real, and the reasoning held up — a throw from the snapshot save left the restored story committed beside the old snapshot, which plays the story against a world it was not written for. Fixed in `ecad34e24`: both writes run in one `app.db.transaction`; the metadata queue is taken before it and the patch runs with `metadataQueueHeld`, the ordering `chats.storage` documents and `advanced-memory` already uses, and `patchMetadataWithCharacterIds` now accepts that opt the way `patchMetadata` does. The compensating `gameWorld`-only rollback is gone, so a failed snapshot save rolls the whole restore back. GitHub review, round 3 (CodeRabbit), 1 finding: - *trivial* — "add a route-level rollback regression for a failed snapshot save": the failure itself cannot be injected at the route — the store's failure hooks exist only when the database is created and fire on flush, not on an insert — so the regression pins the contract the load depends on instead: a patch written inside the restore's transaction is undone when the save after it throws, with the metadata compared before and after (`4b6c2bcd0`, regression part 5). Moving the patch out of the transaction, or dropping `metadataQueueHeld`, makes it fail. Not verified by me (please do in the app): - A real game with a generated world and a story, clicked through in a browser: save a World Edits change, make another change, Load the earlier checkpoint, and confirm the World Edits section, the map and the story all show the earlier state. The worktree's data copy has no game with a world and a story, so this was not exercised in the app. ## Docs and release impact - [ ] No docs changes needed - [x] Updated docs (README / CONTRIBUTING / android/README) 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) `docs/game/sessions-and-saves.md` is already tracked by the open `[docs-i18n]` follow-up #7774. This PR changes that file's English text (the "doesn't undo World Edits" exception is gone), and #7774's thread now carries the final English of the changed paragraph and the two UI strings, so the translated packs are not edited here. **Cross-repo note:** the new column changes `packages/server/src/db/schema/checkpoints.ts`, which [Marinara-Agents](https://github.com/Pasta-Devs/Marinara-Agents) vendors under `sources/engine/` and pins in `scripts/vendored-engine-baseline.json` (that path's entry is `drifted` today). Once this lands on `staging`, their Noodle vendored-engine check will see the new host fingerprint, so that snapshot and baseline want a separate Marinara-Agents change (re-vendor the file and drop the resolved entry — their validator allows the baseline only to shrink). No Engine-side action; flagged here so it is not a surprise after merge. ## UI evidence (if applicable) No new UI surface: three strings changed (two in the Game data editor and checkpoints dialog, one doc paragraph). No screenshot. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Checkpoints now save and restore World Edits alongside the map, tracker, and story. Loading a checkpoint with an empty saved edits layer clears edits made afterward. * If saved World Edits are invalid or incompatible with the restored story, those edits remain unchanged while the rest of the checkpoint loads. * **Documentation** * Updated checkpoint and World Edits descriptions to clarify that edits are restored when loading a checkpoint. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-24 04:08:54 +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#7928
No description provided.