[PR #8310] [MERGED] perf(game): cache the GM's standing instructions with the system prompt; repair drifted speaker lines #8267

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

📋 Pull Request Information

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

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


📝 Commits (3)

  • 4ac581e Cache the GM's standing instructions with the system prompt; repair drifted speaker lines
  • d77556a Count repaired speaker lines the same in segment edits and the Interrupt cut
  • 549d0a9 Name the truncation offset helper's parameter apart from the scan position

📊 Changes

10 files changed (+1023 additions, -530 deletions)

View changed files

📝 docs/development/game-rulesets-and-sheets-implementation.md (+2 -2)
📝 packages/client/src/components/game/GameNarration.tsx (+1 -181)
➕ packages/client/src/lib/narration-truncation.ts (+220 -0)
📝 packages/client/src/lib/party-dialogue-parser.ts (+2 -2)
📝 packages/server/src/routes/generate.routes.ts (+120 -108)
📝 packages/server/src/services/game/gm-prompts.ts (+334 -233)
📝 packages/server/src/services/game/segment-edits.ts (+8 -1)
📝 packages/shared/src/utils/game-narration-text.ts (+78 -3)
➕ scripts/regressions/game-gm-reminder-split.regression.ts (+105 -0)
➕ scripts/regressions/game-speaker-line-repair.regression.ts (+153 -0)

📄 Description

Linked issue

Closes #8309

Why this change

  • Each Game turn sent the whole format reminder after the history. Providers cache a prompt's prefix, so only the system prompt was cached and the history behind it was re-read every turn. On ordinary consecutive turns about 30% of the prompt came from cache.
  • A few speaker-line shapes models write showed as raw brackets or the wrong speaker, and a [whisper:Target] line lost its target to the unknown-tag stripper.

What changed

  • A Game turn now sends the format reminder in two parts. The instructions that hold for the whole game (output format, party rules, commands, ruleset guidance, special instructions) go into the system prompt, before any lore written after the GM's prompt. Only what changes turn to turn follows the history: state, scene, sheets, inventory, the turn's mode, a thrown die, and a closing line that points back at the output format.
  • buildGmFormatReminder takes part: "whole" | "stable" | "turn". "whole" is the default and renders exactly what it did before.
  • Speaker lines are repaired before they are shown. Two type slots keep the first ([main] [side]), a merged slot splits ([main|thinking] becomes type main, sprite thinking), and a dash where the colon goes counts as the colon. Lines already in shape, lines with no type slot and prose are left alone. The party dialogue parser uses the same repair.
  • [whisper:Target] right after a speaker's bracket is kept as the line's type instead of being stripped as an unknown tag, so a whisper shows as one again. A whisper command anywhere else is still stripped.

Validation

  • Server and client tsc --noEmit pass, and Prettier is clean on the changed files
  • New regression game-gm-reminder-split: the stable part is the same bytes whatever the turn holds, carries no live state, and the turn part carries all of it; the placement goes before lore and leaves history alone
  • New regression game-speaker-line-repair: each drifted shape, lines left alone, whisper targets kept, whisper commands still stripped, and bounded time on runaway output
  • Every regression matching game, party, narration, whisper, gm-, prompt, tts and dialogue passes (391)
  • Container (Docker / Podman) built and ran without issue
  • Ran the app and checked the change manually (described below)
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths): not applicable, no UI change
  • Read and followed CONTRIBUTING.md

Manual verification notes

  • Real Game Mode turns on a 13-turn 5e game with the same reminder split, 441 runs across Gemini 3.8 Flash, 3.5 Flash-Lite, 3.1 Pro, Claude Haiku 5.5, Sonnet 5.5, Opus 5.5 and GPT-5.6 Terra. Seven scenarios were run: stealth, search, a healing spell, giving an item, party talk, an out-of-character sheet question and NPC talk.
  • Cached share of the prompt on consecutive turns rose from 30% to 68% overall. Per model: Flash 0% to 87%, Flash-Lite 44% to 83%, Gemini Pro 13% to 69%, each Claude model 43–44% to 64%, Terra 5% to 48%.
  • Automatic scoring (format, tags, checks, sheet numbers, spell slots) and a blind model judge found no difference beyond noise between the old and new layouts.
  • The speaker-line shapes repaired here are the ones those runs produced, 0–2 lines per 21 runs on either layout.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README) as needed: docs/development/game-rulesets-and-sheets-implementation.md says where the sheet guidance and the sheets now go
  • Translated docs on the docs-i18n branch updated to match, or a [docs-i18n] follow-up issue opened (see CONTRIBUTING.md § Translated documentation): noted on #7512
  • Version/release files updated (only if this PR includes a 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/8310 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 10/25/2026 **Status:** ✅ Merged **Merged:** 10/25/2026 **Merged by:** [@kolacheee](https://github.com/kolacheee) **Base:** `staging` ← **Head:** `claude/project-thread-r412n9` --- ### 📝 Commits (3) - [`4ac581e`](https://github.com/Pasta-Devs/Marinara-Engine/commit/4ac581effee5791bd317ec85ddb35a3e2cccd1d8) Cache the GM's standing instructions with the system prompt; repair drifted speaker lines - [`d77556a`](https://github.com/Pasta-Devs/Marinara-Engine/commit/d77556acf72c7fcf3a8d2f3434b66d6fcee1521b) Count repaired speaker lines the same in segment edits and the Interrupt cut - [`549d0a9`](https://github.com/Pasta-Devs/Marinara-Engine/commit/549d0a9ca7c15f192d14d5ca2c9328478c46f924) Name the truncation offset helper's parameter apart from the scan position ### 📊 Changes **10 files changed** (+1023 additions, -530 deletions) <details> <summary>View changed files</summary> 📝 `docs/development/game-rulesets-and-sheets-implementation.md` (+2 -2) 📝 `packages/client/src/components/game/GameNarration.tsx` (+1 -181) ➕ `packages/client/src/lib/narration-truncation.ts` (+220 -0) 📝 `packages/client/src/lib/party-dialogue-parser.ts` (+2 -2) 📝 `packages/server/src/routes/generate.routes.ts` (+120 -108) 📝 `packages/server/src/services/game/gm-prompts.ts` (+334 -233) 📝 `packages/server/src/services/game/segment-edits.ts` (+8 -1) 📝 `packages/shared/src/utils/game-narration-text.ts` (+78 -3) ➕ `scripts/regressions/game-gm-reminder-split.regression.ts` (+105 -0) ➕ `scripts/regressions/game-speaker-line-repair.regression.ts` (+153 -0) </details> ### 📄 Description ## Linked issue Closes #8309 ## Why this change - Each Game turn sent the whole format reminder after the history. Providers cache a prompt's prefix, so only the system prompt was cached and the history behind it was re-read every turn. On ordinary consecutive turns about 30% of the prompt came from cache. - A few speaker-line shapes models write showed as raw brackets or the wrong speaker, and a `[whisper:Target]` line lost its target to the unknown-tag stripper. ## What changed - A Game turn now sends the format reminder in two parts. The instructions that hold for the whole game (output format, party rules, commands, ruleset guidance, special instructions) go into the system prompt, before any lore written after the GM's prompt. Only what changes turn to turn follows the history: state, scene, sheets, inventory, the turn's mode, a thrown die, and a closing line that points back at the output format. - `buildGmFormatReminder` takes `part: "whole" | "stable" | "turn"`. `"whole"` is the default and renders exactly what it did before. - Speaker lines are repaired before they are shown. Two type slots keep the first (`[main] [side]`), a merged slot splits (`[main|thinking]` becomes type `main`, sprite `thinking`), and a dash where the colon goes counts as the colon. Lines already in shape, lines with no type slot and prose are left alone. The party dialogue parser uses the same repair. - `[whisper:Target]` right after a speaker's bracket is kept as the line's type instead of being stripped as an unknown tag, so a whisper shows as one again. A whisper command anywhere else is still stripped. ## Validation - [x] Server and client `tsc --noEmit` pass, and Prettier is clean on the changed files - [x] New regression `game-gm-reminder-split`: the stable part is the same bytes whatever the turn holds, carries no live state, and the turn part carries all of it; the placement goes before lore and leaves history alone - [x] New regression `game-speaker-line-repair`: each drifted shape, lines left alone, whisper targets kept, whisper commands still stripped, and bounded time on runaway output - [x] Every regression matching game, party, narration, whisper, gm-, prompt, tts and dialogue passes (391) - [ ] Container (Docker / Podman) built and ran without issue - [x] Ran the app and checked the change manually (described below) - [ ] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths): not applicable, no UI change - [x] Read and followed `CONTRIBUTING.md` ### Manual verification notes - Real Game Mode turns on a 13-turn 5e game with the same reminder split, 441 runs across Gemini 3.8 Flash, 3.5 Flash-Lite, 3.1 Pro, Claude Haiku 5.5, Sonnet 5.5, Opus 5.5 and GPT-5.6 Terra. Seven scenarios were run: stealth, search, a healing spell, giving an item, party talk, an out-of-character sheet question and NPC talk. - Cached share of the prompt on consecutive turns rose from 30% to 68% overall. Per model: Flash 0% to 87%, Flash-Lite 44% to 83%, Gemini Pro 13% to 69%, each Claude model 43–44% to 64%, Terra 5% to 48%. - Automatic scoring (format, tags, checks, sheet numbers, spell slots) and a blind model judge found no difference beyond noise between the old and new layouts. - The speaker-line shapes repaired here are the ones those runs produced, 0–2 lines per 21 runs on either layout. ## Docs and release impact - [ ] No docs changes needed - [x] Updated docs (README / CONTRIBUTING / android/README) as needed: `docs/development/game-rulesets-and-sheets-implementation.md` says where the sheet guidance and the sheets now go - [x] Translated docs on the `docs-i18n` branch updated to match, or a `[docs-i18n]` follow-up issue opened (see CONTRIBUTING.md § Translated documentation): noted on #7512 - [ ] Version/release files updated (only if this PR includes a version bump) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-26 04:04:14 +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#8267
No description provided.