[PR #5114] [MERGED] fix(game): cap game auto-checkpoints per trigger type to bound checkpoint blob accumulation #6635

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

📋 Pull Request Information

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

Base: staging ← Head: fix/5110-checkpoint-retention


📝 Commits (10+)

  • c080283 fix(game): cap auto-checkpoints per trigger type to bound blob accumulation
  • ebb851c Merge remote-tracking branch 'origin/staging' into fix/5110-checkpoint-retention
  • 4ce2c54 fix(game): keep the checkpoint cap at exactly MAX when protecting the fresh row
  • fb85ffa Merge remote-tracking branch 'origin/staging' into fix/5110-checkpoint-retention
  • 9714a61 Merge remote-tracking branch 'origin/staging' into fix/5110-checkpoint-retention
  • 8243d2b Merge remote-tracking branch 'origin/staging' into fix/5110-checkpoint-retention
  • 534fa6e Merge remote-tracking branch 'origin/staging' into fix/5110-checkpoint-retention
  • 8ee4794 Merge remote-tracking branch 'origin/staging' into fix/5110-checkpoint-retention
  • f0d3502 Merge remote-tracking branch 'origin/staging' into fix/5110-checkpoint-retention
  • 91a0a78 Merge remote-tracking branch 'origin/staging' into fix/5110-checkpoint-retention

📊 Changes

4 files changed (+401 additions, -27 deletions)

View changed files

📝 CHANGELOG.md (+1 -0)
📝 package.json (+2 -1)
📝 packages/server/src/services/game/checkpoint.service.ts (+127 -26)
➕ scripts/regressions/checkpoint-retention.regression.ts (+271 -0)

📄 Description

Linked issue

Closes #5110

Why this change

Game checkpoints store full immutable copies of everything they capture — the tracker snapshot (snapshotData), the World Map snapshot (spatialSnapshotData), and (once #5102 lands) the turn-game/Experience engine-state blobs. Nothing bounded how many a chat accumulated: auto-checkpoints fire at session start, session end, and on every combat start and end, and only manual deletion removed one.

Because game_checkpoints is chat-sharded and the file store holds tables in memory, a long campaign multiplied every captured blob into permanent heap and re-serialized the chat's whole checkpoint shard on every new checkpoint — an O(n²) write pattern. With a game-surface Experience whose world save approaches its 256 KB cap, tens of sessions of auto-checkpoints add up to tens of megabytes duplicated per chat, worst on Android where the whole app fights a ~1 GB heap (#4730). listForChat also materialized every stored blob just to strip the fields before returning the list.

What changed

  • create() now prunes auto-checkpoints to the newest MAX_AUTO_CHECKPOINTS_PER_TRIGGER (5) per trigger type after each insert. The cap is row-level, so it is field-agnostic — it bounds snapshotData, spatialSnapshotData, and #5102's future engineStateData together with no per-field wiring, and covers engineStateData automatically once that column merges.
  • Manual checkpoints are exempt — they are the user's explicit save points and are never pruned.
  • The prune never drops the row create() just returned: a protectId guard covers the pathological same-millisecond createdAt collision, so a returned checkpoint id always resolves.
  • The scan is skipped entirely on manual creates (a manual save cannot grow an auto bucket).
  • listForChat now projects the list columns instead of copying the captured blobs out of the store only to strip them again; the store sorts full rows before projecting, so the newest-first order is unchanged (pinned by the regression).

Retention is per trigger type rather than a single total, so a burst of one kind of event — a dungeon's worth of combat_start/combat_end — cannot evict the last session_start rewind point.

Scope notes: I left the cap a named constant rather than an env var to keep this focused; a MARINARA_* override is an easy follow-up if users want deeper auto-history. Per-chat retention is the first (highest-value) of the three directions the issue lists; content-addressing captured blobs by hash is the natural next step if per-type retention proves too coarse.

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 (run locally against this branch, all green):

  • pnpm check
  • pnpm regression:checkpoint-retention (new lane; also wired into regression:issues)
  • pnpm regression:game-checkpoint-engine-state (sibling checkpoint lane, unaffected)
  • pnpm version:check

Still needs a manual browser pass (a human should verify — do not treat the boxes above as done):

  • In a Game Mode chat, enter and leave combat more than five times. Confirm only the newest 5 combat_start and 5 combat_end auto-checkpoints remain in the checkpoint list, and that any manual checkpoints you made are all still present.
  • Confirm the checkpoint list still shows newest-first and that loading an older surviving checkpoint restores the story, map, and (with #5102) engine state correctly.
  • Inspect the chat's game_checkpoints shard under data/storage/tables/ and confirm it stops growing without bound across many sessions.

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.md gets a Fixed entry. No user-facing docs under docs/ changed, so no docs-i18n mirroring is required. No version bump.

Summary by CodeRabbit

Summary by CodeRabbit

  • Bug Fixes

    • Automatic game checkpoints are limited to the five newest per trigger.
    • Manual checkpoints are preserved during automatic cleanup.
    • Newly created checkpoints remain protected during retention cleanup.
    • Checkpoint listings load more efficiently while maintaining newest-first ordering.
  • Tests

    • Added regression coverage for retention limits, manual checkpoint preservation, chat isolation, timestamp ties, and checkpoint creation.

🔄 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/5114 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 8/16/2026 **Status:** ✅ Merged **Merged:** 8/16/2026 **Merged by:** [@kolacheee](https://github.com/kolacheee) **Base:** `staging` ← **Head:** `fix/5110-checkpoint-retention` --- ### 📝 Commits (10+) - [`c080283`](https://github.com/Pasta-Devs/Marinara-Engine/commit/c080283c5ad8ce52261a914e65a93f4903d90597) fix(game): cap auto-checkpoints per trigger type to bound blob accumulation - [`ebb851c`](https://github.com/Pasta-Devs/Marinara-Engine/commit/ebb851c7a285a8c2b1eb7121d1264cdb6dbd956e) Merge remote-tracking branch 'origin/staging' into fix/5110-checkpoint-retention - [`4ce2c54`](https://github.com/Pasta-Devs/Marinara-Engine/commit/4ce2c54ed1d69a5fa27a99bb78f246332fbb4e9f) fix(game): keep the checkpoint cap at exactly MAX when protecting the fresh row - [`fb85ffa`](https://github.com/Pasta-Devs/Marinara-Engine/commit/fb85ffa40e7082cf8d38326949de503846d0ffe9) Merge remote-tracking branch 'origin/staging' into fix/5110-checkpoint-retention - [`9714a61`](https://github.com/Pasta-Devs/Marinara-Engine/commit/9714a61a8c0637f066c76ac4a1dcd9cb5195628a) Merge remote-tracking branch 'origin/staging' into fix/5110-checkpoint-retention - [`8243d2b`](https://github.com/Pasta-Devs/Marinara-Engine/commit/8243d2bc0b2dbe08c1c8c6558cd56f78b09bfba9) Merge remote-tracking branch 'origin/staging' into fix/5110-checkpoint-retention - [`534fa6e`](https://github.com/Pasta-Devs/Marinara-Engine/commit/534fa6eb18abed7a763e37e7ca69d1b097ddcd14) Merge remote-tracking branch 'origin/staging' into fix/5110-checkpoint-retention - [`8ee4794`](https://github.com/Pasta-Devs/Marinara-Engine/commit/8ee4794c371764ce7002d91476f3cb0f1e531a5b) Merge remote-tracking branch 'origin/staging' into fix/5110-checkpoint-retention - [`f0d3502`](https://github.com/Pasta-Devs/Marinara-Engine/commit/f0d35029703f88abfb1f1c15df78345c57261ed1) Merge remote-tracking branch 'origin/staging' into fix/5110-checkpoint-retention - [`91a0a78`](https://github.com/Pasta-Devs/Marinara-Engine/commit/91a0a781051cce87c6e5b14ff0acddcb2ba6f451) Merge remote-tracking branch 'origin/staging' into fix/5110-checkpoint-retention ### 📊 Changes **4 files changed** (+401 additions, -27 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+1 -0) 📝 `package.json` (+2 -1) 📝 `packages/server/src/services/game/checkpoint.service.ts` (+127 -26) ➕ `scripts/regressions/checkpoint-retention.regression.ts` (+271 -0) </details> ### 📄 Description ## Linked issue Closes #5110 ## Why this change Game checkpoints store full immutable copies of everything they capture — the tracker snapshot (`snapshotData`), the World Map snapshot (`spatialSnapshotData`), and (once #5102 lands) the turn-game/Experience engine-state blobs. Nothing bounded how many a chat accumulated: auto-checkpoints fire at session start, session end, and on every combat start and end, and only manual deletion removed one. Because `game_checkpoints` is chat-sharded and the file store holds tables in memory, a long campaign multiplied every captured blob into permanent heap and re-serialized the chat's whole checkpoint shard on every new checkpoint — an O(n²) write pattern. With a game-surface Experience whose world save approaches its 256 KB cap, tens of sessions of auto-checkpoints add up to tens of megabytes duplicated per chat, worst on Android where the whole app fights a ~1 GB heap (#4730). `listForChat` also materialized every stored blob just to strip the fields before returning the list. ## What changed - `create()` now prunes auto-checkpoints to the newest `MAX_AUTO_CHECKPOINTS_PER_TRIGGER` (5) **per trigger type** after each insert. The cap is row-level, so it is field-agnostic — it bounds `snapshotData`, `spatialSnapshotData`, and #5102's future `engineStateData` together with no per-field wiring, and covers `engineStateData` automatically once that column merges. - **Manual checkpoints are exempt** — they are the user's explicit save points and are never pruned. - The prune never drops the row `create()` just returned: a `protectId` guard covers the pathological same-millisecond `createdAt` collision, so a returned checkpoint id always resolves. - The scan is skipped entirely on manual creates (a manual save cannot grow an auto bucket). - `listForChat` now projects the list columns instead of copying the captured blobs out of the store only to strip them again; the store sorts full rows before projecting, so the newest-first order is unchanged (pinned by the regression). Retention is **per trigger type** rather than a single total, so a burst of one kind of event — a dungeon's worth of `combat_start`/`combat_end` — cannot evict the last `session_start` rewind point. Scope notes: I left the cap a named constant rather than an env var to keep this focused; a `MARINARA_*` override is an easy follow-up if users want deeper auto-history. Per-chat retention is the first (highest-value) of the three directions the issue lists; content-addressing captured blobs by hash is the natural next step if per-type retention proves too coarse. ## 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 (run locally against this branch, all green): - `pnpm check` - `pnpm regression:checkpoint-retention` (new lane; also wired into `regression:issues`) - `pnpm regression:game-checkpoint-engine-state` (sibling checkpoint lane, unaffected) - `pnpm version:check` Still needs a manual browser pass (a human should verify — do not treat the boxes above as done): - In a Game Mode chat, enter and leave combat more than five times. Confirm only the newest 5 `combat_start` and 5 `combat_end` auto-checkpoints remain in the checkpoint list, and that any manual checkpoints you made are all still present. - Confirm the checkpoint list still shows newest-first and that loading an older surviving checkpoint restores the story, map, and (with #5102) engine state correctly. - Inspect the chat's `game_checkpoints` shard under `data/storage/tables/` and confirm it stops growing without bound across many sessions. ## 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.md` gets a `Fixed` entry. No user-facing docs under `docs/` changed, so no `docs-i18n` mirroring is required. No version bump. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Summary by CodeRabbit * **Bug Fixes** * Automatic game checkpoints are limited to the five newest per trigger. * Manual checkpoints are preserved during automatic cleanup. * Newly created checkpoints remain protected during retention cleanup. * Checkpoint listings load more efficiently while maintaining newest-first ordering. * **Tests** * Added regression coverage for retention limits, manual checkpoint preservation, chat isolation, timestamp ties, and checkpoint creation. <!-- 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:12:16 +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#6635
No description provided.