[PR #8254] [MERGED] perf(game): cache-stable GM prompt and Advanced Memory Recall for Game Mode #8563

Closed
opened 2026-11-07 16:45:34 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/8254
Author: @kolacheee
Created: 10/24/2026
Status: ✅ Merged
Merged: 10/24/2026
Merged by: @kolacheee

Base: staging ← Head: claude/project-thread-r412n9


📝 Commits (8)

  • acf39eb perf(game): keep the GM system prompt cache-stable
  • 0a6fcc4 Merge remote-tracking branch 'origin/staging' into claude/project-thread-r412n9
  • 58678df test(game): read passive perception from the GM scene state
  • 4d15929 feat(game): Advanced Memory Recall for Game Mode
  • 5498c70 Merge remote-tracking branch 'origin/staging' into claude/project-thread-r412n9
  • 6017515 fix(game): a new session leaves Advanced Memory's old recaps behind
  • 47ca20b Merge remote-tracking branch 'origin/staging' into claude/project-thread-r412n9
  • 67b3d8c fix(game): keep Chat Summary agent entries across sessions

📊 Changes

13 files changed (+738 additions, -136 deletions)

View changed files

📝 docs/agents/memory.md (+15 -3)
📝 packages/client/src/components/chat/AdvancedMemorySettings.tsx (+27 -17)
📝 packages/client/src/components/chat/ChatSettingsDrawer.tsx (+8 -5)
📝 packages/client/src/localization/locales/en.json (+1 -0)
📝 packages/server/src/routes/advanced-memory.routes.ts (+1 -1)
📝 packages/server/src/routes/game.routes.ts (+5 -1)
📝 packages/server/src/routes/generate.routes.ts (+37 -8)
📝 packages/server/src/services/advanced-memory.ts (+103 -17)
📝 packages/server/src/services/game/gm-prompts.ts (+94 -66)
➕ scripts/regressions/advanced-memory-game.regression.ts (+281 -0)
➕ scripts/regressions/game-gm-cache-stable-prompt.regression.ts (+159 -0)
📝 scripts/regressions/world-kit-battlefields.regression.ts (+4 -3)
📝 scripts/regressions/world-people.regression.ts (+3 -15)

📄 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 #8253

Why this change

A long Game session resends its whole history every turn, up to the connection's context limit. A player reported every message costing about 250k input tokens after leaving one session running. Two things make that worse than it needs to be:

  • The GM system prompt sits before the history but carried state that changes most turns, so any change made the provider miss its prompt cache for the entire session. On Claude, with caching on, each miss is also billed as a cache write.
  • Game history is never windowed: Advanced Memory Recall was Roleplay-only, so nothing held a Game session to a budget.

What changed

Cache-stable GM prompt

  • The GM system prompt no longer carries the scene's changing state, so it stays byte-identical from turn to turn and the history after it stays cacheable.
  • That state now reaches the GM in the turn reminder, after the history and before the format instructions: weather, passive perception hints, party morale, player notes, map state, tracked NPCs and the tactical board. The GM still gets all of it every turn.

Advanced Memory Recall in Game Mode (opt-in, off by default)

  • Game chats can turn on Advanced Memory Recall in Chat Settings → Memory Recall. The prompt is then held under its context cap: the current scene stays as live messages, earlier scenes go out as recaps plus the excerpts recall picks.
  • The GM reads the memory alone, as its narrator. The per-character settings (narrator, knowledge ranges, automatic message visibility, character recollections) don't apply to Game and are hidden there.
  • The summary and recalled scenes go after the history, just before the turn reminder, so they don't break the cache of the history in front of them.
  • Memory reads the history the GM is sent: segment edits applied, GM command tags removed. Checkpoint restore notes stay in the live window.
  • A new session keeps the Advanced Memory settings but leaves behind the previous session's memory progress and Advanced Memory's own recaps of its messages. Summary entries the player or a Chat Summary agent wrote carry over as before.
  • The optional Decision model works here too, and stays off by default.
  • Docs: a Game Mode section in docs/agents/memory.md.

Tests

  • New game-gm-cache-stable-prompt: the system prompt doesn't change when the scene does, and the state sits between <current_state> and the format instructions. world-people and world-kit-battlefields now read the scene state.
  • New advanced-memory-game: the effective Game settings, the shaped source (segment edits, stripped tags), settings saves on a Game chat, preparing a long Game chat under its cap with the checkpoint note kept, receipt validation, the new-session metadata carry-over, and the placement and drawer wiring.

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

  • tsc --noEmit for the server and the client: clean. Prettier: clean.
  • node scripts/run-regressions.mjs --filter memory: 29/29 passed; --filter session 5/5; --filter prompt 18/18; --filter game passed for the prompt move. A partial full local run failed only in files this PR doesn't touch (mari-guard-sibling-paths, capability-server-externals, file-store-load-heal-generation, launcher-browser-ready), which depend on the local filesystem and processes; CI's node-regressions passed on the feature commit.
  • Placement check for the state move: I built GM prompts with the real builders for two scenes where earlier turns hold stale facts and the current state contradicts them, and compared the old layout, state before the instructions (this PR) and state after the instructions, on Haiku and Sonnet. Every run used the current state over the stale history and kept the output format. The only miss was the old layout on Haiku, which left out the perception hint.
  • Not run: a real provider through the app. Prompt preview for a Game chat doesn't show Advanced Memory yet (the dry-run has no GM runtime); the final prompt inspector does.

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)

UI evidence (if applicable)

The Game drawer shows the existing Advanced Memory Recall controls, minus the per-character ones, with one help line.


🔄 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/8254 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 10/24/2026 **Status:** ✅ Merged **Merged:** 10/24/2026 **Merged by:** [@kolacheee](https://github.com/kolacheee) **Base:** `staging` ← **Head:** `claude/project-thread-r412n9` --- ### 📝 Commits (8) - [`acf39eb`](https://github.com/Pasta-Devs/Marinara-Engine/commit/acf39ebfbe429cc95066741bedbf8798f28c4b04) perf(game): keep the GM system prompt cache-stable - [`0a6fcc4`](https://github.com/Pasta-Devs/Marinara-Engine/commit/0a6fcc462db8f55fe459ab8bca70c002f0ee1492) Merge remote-tracking branch 'origin/staging' into claude/project-thread-r412n9 - [`58678df`](https://github.com/Pasta-Devs/Marinara-Engine/commit/58678dfd8787d04d3ac2d0a10d832c9f7636a818) test(game): read passive perception from the GM scene state - [`4d15929`](https://github.com/Pasta-Devs/Marinara-Engine/commit/4d159290d2473505be7b28d1c34d02dd41efe064) feat(game): Advanced Memory Recall for Game Mode - [`5498c70`](https://github.com/Pasta-Devs/Marinara-Engine/commit/5498c70552dd96725f85d55c48aa74146cfe48d7) Merge remote-tracking branch 'origin/staging' into claude/project-thread-r412n9 - [`6017515`](https://github.com/Pasta-Devs/Marinara-Engine/commit/601751510036cf470f0a0022f2e75501e103ce87) fix(game): a new session leaves Advanced Memory's old recaps behind - [`47ca20b`](https://github.com/Pasta-Devs/Marinara-Engine/commit/47ca20b7bbddde2e1e3e0017ddbe388267acba79) Merge remote-tracking branch 'origin/staging' into claude/project-thread-r412n9 - [`67b3d8c`](https://github.com/Pasta-Devs/Marinara-Engine/commit/67b3d8c2406c21251a6afe28bb1e89d3fb7393be) fix(game): keep Chat Summary agent entries across sessions ### 📊 Changes **13 files changed** (+738 additions, -136 deletions) <details> <summary>View changed files</summary> 📝 `docs/agents/memory.md` (+15 -3) 📝 `packages/client/src/components/chat/AdvancedMemorySettings.tsx` (+27 -17) 📝 `packages/client/src/components/chat/ChatSettingsDrawer.tsx` (+8 -5) 📝 `packages/client/src/localization/locales/en.json` (+1 -0) 📝 `packages/server/src/routes/advanced-memory.routes.ts` (+1 -1) 📝 `packages/server/src/routes/game.routes.ts` (+5 -1) 📝 `packages/server/src/routes/generate.routes.ts` (+37 -8) 📝 `packages/server/src/services/advanced-memory.ts` (+103 -17) 📝 `packages/server/src/services/game/gm-prompts.ts` (+94 -66) ➕ `scripts/regressions/advanced-memory-game.regression.ts` (+281 -0) ➕ `scripts/regressions/game-gm-cache-stable-prompt.regression.ts` (+159 -0) 📝 `scripts/regressions/world-kit-battlefields.regression.ts` (+4 -3) 📝 `scripts/regressions/world-people.regression.ts` (+3 -15) </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 #8253 ## Why this change A long Game session resends its whole history every turn, up to the connection's context limit. A player reported every message costing about 250k input tokens after leaving one session running. Two things make that worse than it needs to be: - The GM system prompt sits before the history but carried state that changes most turns, so any change made the provider miss its prompt cache for the entire session. On Claude, with caching on, each miss is also billed as a cache write. - Game history is never windowed: Advanced Memory Recall was Roleplay-only, so nothing held a Game session to a budget. ## What changed **Cache-stable GM prompt** - The GM system prompt no longer carries the scene's changing state, so it stays byte-identical from turn to turn and the history after it stays cacheable. - That state now reaches the GM in the turn reminder, after the history and before the format instructions: weather, passive perception hints, party morale, player notes, map state, tracked NPCs and the tactical board. The GM still gets all of it every turn. **Advanced Memory Recall in Game Mode (opt-in, off by default)** - Game chats can turn on Advanced Memory Recall in Chat Settings → Memory Recall. The prompt is then held under its context cap: the current scene stays as live messages, earlier scenes go out as recaps plus the excerpts recall picks. - The GM reads the memory alone, as its narrator. The per-character settings (narrator, knowledge ranges, automatic message visibility, character recollections) don't apply to Game and are hidden there. - The summary and recalled scenes go after the history, just before the turn reminder, so they don't break the cache of the history in front of them. - Memory reads the history the GM is sent: segment edits applied, GM command tags removed. Checkpoint restore notes stay in the live window. - A new session keeps the Advanced Memory settings but leaves behind the previous session's memory progress and Advanced Memory's own recaps of its messages. Summary entries the player or a Chat Summary agent wrote carry over as before. - The optional Decision model works here too, and stays off by default. - Docs: a Game Mode section in `docs/agents/memory.md`. **Tests** - New `game-gm-cache-stable-prompt`: the system prompt doesn't change when the scene does, and the state sits between `<current_state>` and the format instructions. `world-people` and `world-kit-battlefields` now read the scene state. - New `advanced-memory-game`: the effective Game settings, the shaped source (segment edits, stripped tags), settings saves on a Game chat, preparing a long Game chat under its cap with the checkpoint note kept, receipt validation, the new-session metadata carry-over, and the placement and drawer wiring. ## 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) - [x] Above manual verification completed (describe below) - [x] Read and followed `CONTRIBUTING.md` ### Manual verification notes - `tsc --noEmit` for the server and the client: clean. Prettier: clean. - `node scripts/run-regressions.mjs --filter memory`: 29/29 passed; `--filter session` 5/5; `--filter prompt` 18/18; `--filter game` passed for the prompt move. A partial full local run failed only in files this PR doesn't touch (`mari-guard-sibling-paths`, `capability-server-externals`, `file-store-load-heal-generation`, `launcher-browser-ready`), which depend on the local filesystem and processes; CI's node-regressions passed on the feature commit. - Placement check for the state move: I built GM prompts with the real builders for two scenes where earlier turns hold stale facts and the current state contradicts them, and compared the old layout, state before the instructions (this PR) and state after the instructions, on Haiku and Sonnet. Every run used the current state over the stale history and kept the output format. The only miss was the old layout on Haiku, which left out the perception hint. - Not run: a real provider through the app. Prompt preview for a Game chat doesn't show Advanced Memory yet (the dry-run has no GM runtime); the final prompt inspector does. ## 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) ## UI evidence (if applicable) The Game drawer shows the existing Advanced Memory Recall controls, minus the per-character ones, with one help line. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-07 16:45:34 +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#8563
No description provided.