[PR #5416] [MERGED] Experience-state saves: player-controlled deletion, export, and import #6757

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

📋 Pull Request Information

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

Base: staging ← Head: feat/5405-save-management


📝 Commits (7)

  • 15cfcad feat(game): add experience-save delete, export, and import verbs
  • 28726cc fix(game): harden experience-save transfer against data loss
  • 9453dc7 Merge upstream/staging into feat/5405-save-management
  • 9e72860 fix(game): reserve the imported: anchor prefix against forged message ids
  • 05ebf63 test(game): prove the reserved-prefix refusal writes no swipe either
  • 0bdaa63 Merge branch 'staging' into feat/5405-save-management
  • 86d05da Merge branch 'staging' into feat/5405-save-management

📊 Changes

9 files changed (+1243 additions, -21 deletions)

View changed files

📝 CHANGELOG.md (+1 -0)
📝 packages/server/src/db/file-backed-store.ts (+32 -0)
📝 packages/server/src/middleware/rate-limit.ts (+10 -0)
📝 packages/server/src/routes/game.routes.ts (+415 -15)
📝 packages/server/src/services/capability-packages/capability-persistence.service.ts (+15 -0)
📝 packages/server/src/services/mari-db/mari-db.service.ts (+8 -1)
📝 packages/server/src/services/storage/game-engine-state.storage.ts (+37 -5)
➕ scripts/regressions/experience-state-imported-anchors.regression.ts (+164 -0)
📝 scripts/regressions/experience-state.regression.ts (+561 -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 #5405

Why this change

  • Game-surface Experiences store per-anchor saves in game_engine_state (#5102) with no way to delete them, export them, or bring them back — so "New Game" inherits up to 100 rows of the old campaign, and a player cannot free device space without losing a game forever. Filed from the Pixelforge S5 planning work; the package's New Game flow builds on these verbs.

What changed

  • DELETE /api/game/:chatId/experience-state — removes the chat's experience-namespace rows ({ ok, deleted }), write-locked, count-then-delete atomic.
  • GET /api/game/:chatId/experience-state/export — every namespace row, oldest-write-first, read under the write lock so a backup is atomic against a concurrent save; unreadable rows (unparseable text or a non-string column — the export never re-assumes the stored type) carry the same stateUnparseable: true + capped rawState/rawStateTruncated shape as #5411's single-row GET, with state: null.
  • POST /api/game/:chatId/experience-state/import — accepts the export shape back ({ ok, imported, retained, pruned, skipped }), own bodyLimit, row-count + per-row size caps (whole-batch 422 on violation, duplicate anchors included), a flagged row with an untruncated rawState is restored VERBATIM (a faithful backup restores what was there — the row stays flagged on later reads); truncated captures are skipped and counted.
  • Import correctness hardening (from two adversarial review rounds): rows are re-stamped forward from a base strictly past the namespace's newest (honoring exported timestamps, or stamping backwards, provably loses rows to the anchor-cap prune — reproduced with a 100-row import); anchors that don't resolve to this chat's messages are rewritten to a synthetic imported: prefix so the globally-matched messages→engine-state cascade of a source chat can never destroy an imported campaign; the maintenance validator exempts the synthetic prefix from dangling-reference errors.
  • The PUT gains the same monotonic createdAt guard (tight write loops measurably tie timestamps otherwise, and ties resolve differently in-process vs after a shard reload); checkpoint-restore clones stamp through the same helper.
  • The transfer verbs get their own 20/min rate-limit class (they are ~99× the PUT's byte cost).
  • Import onto a full campaign evicts oldest-stamped (pre-existing) rows through the anchor cap — reported via pruned (which counts only cap evictions — same-anchor replacements are the intended write, so a same-chat re-import reports pruned: 0), stated in the route comment, and the evicted campaign's checkpoints still restore (state captured by value).
  • Regression lanes: experience-state extended (cases 14–23 after merging staging's #5407 case in as 13; case 21 pins the unreadable-row export/import round trip incl. a null-column sub-case) + new experience-state-imported-anchors lane; all guards proven fail-first.
  • CHANGELOG entry under Unreleased.

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

  • Agent-run (not manual): experience-state + experience-state-imported-anchors lanes green with fail-first proofs per guard; adjacent lanes (game, checkpoint, chat, rate-limit, store, shard, backup, db) green; pnpm lint (typecheck ×3) green; prettier clean on touched files. Tree-wide format:check false-fails in CRLF worktrees (environmental; CI on LF is authoritative — confirmed on #5411).
  • No UI surface — package-facing HTTP API; nothing to click through.
  • Reviewer note: this branch touches the checkpoint-restore block that #5406 (in flight) also modifies (it adds the write lock there); whichever merges second rebases that hunk.

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)

Summary by CodeRabbit

  • New Features

    • Added Experience save management, including chat-scoped deletion, campaign export, and import.
    • Added validation for imported saves, including size limits, duplicate anchors, corruption markers, and pruning.
    • Added dedicated rate limits for save export and import operations.
    • Preserved imported saves across chats with safe anchor handling.
  • Bug Fixes

    • Improved save ordering and checkpoint restoration for consistent recency.
    • Added clearer handling of corrupted state during reads and exports.
    • Prevented invalid imported anchor identifiers from being saved.

🔄 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/5416 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 8/22/2026 **Status:** ✅ Merged **Merged:** 8/22/2026 **Merged by:** [@kolacheee](https://github.com/kolacheee) **Base:** `staging` ← **Head:** `feat/5405-save-management` --- ### 📝 Commits (7) - [`15cfcad`](https://github.com/Pasta-Devs/Marinara-Engine/commit/15cfcad75f37c769b9b1c8d370919fa17f484fd4) feat(game): add experience-save delete, export, and import verbs - [`28726cc`](https://github.com/Pasta-Devs/Marinara-Engine/commit/28726cc154641d17ff2bb6692939d3211aeb2cbd) fix(game): harden experience-save transfer against data loss - [`9453dc7`](https://github.com/Pasta-Devs/Marinara-Engine/commit/9453dc75ef0ac4841c923471d7d3d0c6c58b14b4) Merge upstream/staging into feat/5405-save-management - [`9e72860`](https://github.com/Pasta-Devs/Marinara-Engine/commit/9e728606f29baa8a171b29ffe9837b347861ee84) fix(game): reserve the imported: anchor prefix against forged message ids - [`05ebf63`](https://github.com/Pasta-Devs/Marinara-Engine/commit/05ebf63cd78fb37b46ccc21b993c026ce55df700) test(game): prove the reserved-prefix refusal writes no swipe either - [`0bdaa63`](https://github.com/Pasta-Devs/Marinara-Engine/commit/0bdaa63ebf782e81a44aa61704c3236c551254d7) Merge branch 'staging' into feat/5405-save-management - [`86d05da`](https://github.com/Pasta-Devs/Marinara-Engine/commit/86d05da8648765a2c82831702b79f25ccd4da74a) Merge branch 'staging' into feat/5405-save-management ### 📊 Changes **9 files changed** (+1243 additions, -21 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+1 -0) 📝 `packages/server/src/db/file-backed-store.ts` (+32 -0) 📝 `packages/server/src/middleware/rate-limit.ts` (+10 -0) 📝 `packages/server/src/routes/game.routes.ts` (+415 -15) 📝 `packages/server/src/services/capability-packages/capability-persistence.service.ts` (+15 -0) 📝 `packages/server/src/services/mari-db/mari-db.service.ts` (+8 -1) 📝 `packages/server/src/services/storage/game-engine-state.storage.ts` (+37 -5) ➕ `scripts/regressions/experience-state-imported-anchors.regression.ts` (+164 -0) 📝 `scripts/regressions/experience-state.regression.ts` (+561 -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 #5405 ## Why this change - Game-surface Experiences store per-anchor saves in `game_engine_state` (#5102) with no way to delete them, export them, or bring them back — so "New Game" inherits up to 100 rows of the old campaign, and a player cannot free device space without losing a game forever. Filed from the Pixelforge S5 planning work; the package's New Game flow builds on these verbs. ## What changed - `DELETE /api/game/:chatId/experience-state` — removes the chat's experience-namespace rows (`{ ok, deleted }`), write-locked, count-then-delete atomic. - `GET /api/game/:chatId/experience-state/export` — every namespace row, oldest-write-first, read under the write lock so a backup is atomic against a concurrent save; unreadable rows (unparseable text or a non-string column — the export never re-assumes the stored type) carry the same `stateUnparseable: true` + capped `rawState`/`rawStateTruncated` shape as #5411's single-row GET, with `state: null`. - `POST /api/game/:chatId/experience-state/import` — accepts the export shape back (`{ ok, imported, retained, pruned, skipped }`), own bodyLimit, row-count + per-row size caps (whole-batch 422 on violation, duplicate anchors included), a flagged row with an untruncated `rawState` is restored VERBATIM (a faithful backup restores what was there — the row stays flagged on later reads); truncated captures are skipped and counted. - Import correctness hardening (from two adversarial review rounds): rows are re-stamped **forward** from a base strictly past the namespace's newest (honoring exported timestamps, or stamping backwards, provably loses rows to the anchor-cap prune — reproduced with a 100-row import); anchors that don't resolve to this chat's messages are rewritten to a synthetic `imported:` prefix so the **globally-matched** messages→engine-state cascade of a *source* chat can never destroy an imported campaign; the maintenance validator exempts the synthetic prefix from dangling-reference errors. - The PUT gains the same monotonic `createdAt` guard (tight write loops measurably tie timestamps otherwise, and ties resolve differently in-process vs after a shard reload); checkpoint-restore clones stamp through the same helper. - The transfer verbs get their own 20/min rate-limit class (they are ~99× the PUT's byte cost). - Import onto a full campaign evicts oldest-stamped (pre-existing) rows through the anchor cap — reported via `pruned` (which counts only cap evictions — same-anchor replacements are the intended write, so a same-chat re-import reports `pruned: 0`), stated in the route comment, and the evicted campaign's checkpoints still restore (state captured by value). - Regression lanes: `experience-state` extended (cases 14–23 after merging staging's #5407 case in as 13; case 21 pins the unreadable-row export/import round trip incl. a null-column sub-case) + new `experience-state-imported-anchors` lane; all guards proven fail-first. - CHANGELOG entry under Unreleased. ## 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 - Agent-run (not manual): `experience-state` + `experience-state-imported-anchors` lanes green with fail-first proofs per guard; adjacent lanes (game, checkpoint, chat, rate-limit, store, shard, backup, db) green; `pnpm lint` (typecheck ×3) green; prettier clean on touched files. Tree-wide `format:check` false-fails in CRLF worktrees (environmental; CI on LF is authoritative — confirmed on #5411). - No UI surface — package-facing HTTP API; nothing to click through. - Reviewer note: this branch touches the checkpoint-restore block that #5406 (in flight) also modifies (it adds the write lock there); whichever merges second rebases that hunk. ## Docs and release impact - [x] 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) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added Experience save management, including chat-scoped deletion, campaign export, and import. * Added validation for imported saves, including size limits, duplicate anchors, corruption markers, and pruning. * Added dedicated rate limits for save export and import operations. * Preserved imported saves across chats with safe anchor handling. * **Bug Fixes** * Improved save ordering and checkpoint restoration for consistent recency. * Added clearer handling of corrupted state during reads and exports. * Prevented invalid imported anchor identifiers from being saved. <!-- 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-23 04:13:26 +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#6757
No description provided.