[PR #2092] [MERGED] fix(otome-forge): a chapter's calls share one cached story context for an hour; no repeated last-chapter summary (0.7.6) #2112

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

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Agents/pull/2092
Author: @SpicyMarinara
Created: 11/3/2026
Status: ✅ Merged
Merged: 11/3/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/otome-cache-stable-prefix


📝 Commits (4)

  • 25e803b chore(otome-forge): start the 0.7.6 caching work
  • 3761600 fix(otome-forge): a chapter's calls share one cached story context for an hour (0.7.6)
  • ea430bb fix(otome-forge): the cached part holds shared canon only; the last chapter's summary is never repeated (0.7.6)
  • aa7d580 fix(otome-forge): a cached story context worked out per call, the same for every cast (0.7.6)

📊 Changes

23 files changed (+585 additions, -253 deletions)

View changed files

➕ artifacts/otome-forge-0.7.6.zip (+0 -0)
📝 catalog/preview/catalog.json (+6 -6)
📝 catalog/preview/notes.json (+6 -6)
📝 catalog/preview/v2/catalog.json (+6 -6)
📝 catalog/preview/v2/notes.json (+6 -6)
📝 catalog/preview/v3/catalog.json (+6 -6)
📝 catalog/preview/v3/notes.json (+6 -6)
📝 packages/otome-forge/CHANGELOG.md (+5 -0)
📝 packages/otome-forge/manifest.json (+3 -3)
📝 packages/otome-forge/server.mjs (+133 -130)
📝 packages/otome-forge/src/engine/packages/server/src/services/otome-forge/generation.ts (+14 -6)
📝 packages/otome-forge/src/engine/packages/server/src/services/otome-forge/memory/memory.ts (+11 -8)
📝 packages/otome-forge/src/engine/packages/server/src/services/otome-forge/memory/timeline.ts (+28 -9)
📝 packages/otome-forge/src/engine/packages/server/src/services/otome-forge/prompts.ts (+36 -15)
📝 packages/otome-forge/src/engine/packages/server/src/services/otome-forge/story/beats.ts (+4 -2)
📝 packages/otome-forge/src/engine/packages/server/src/services/otome-forge/story/canon.ts (+87 -28)
📝 packages/otome-forge/src/engine/packages/server/src/services/otome-forge/story/chapters.ts (+2 -2)
📝 scripts/build-feature-packages.mjs (+1 -1)
📝 tests/otome-forge-beats.regression.ts (+148 -5)
📝 tests/otome-forge-date-night.regression.ts (+4 -1)

...and 3 more files

📄 Description

Why

Otome Hearth's prompt caching stopped paying off. The maintainer's Anthropic usage showed cache writes on most calls and almost no cache reads. A chapter's plan, scenes and climax are meant to share one cached story context: rules, contract, world, protagonist, the arc's line, story memory and the previous chapter. Three things kept breaking that:

  1. The cache only lasted five minutes. A player reads scenes further apart than that, so each scene paid to write the cache again.
  2. The cached story memory changed between a chapter's calls. It followed each call's cast: a scene with a different love interest pulled in different private recent canon. It also followed the memory's state at that moment. A chapter summary finishing in the background, or the memory becoming ready after the canonical-history fallback, changed the cached prefix in the middle of a chapter.
  3. The chapter's last scene had its own instructions. The kit's "This is the LAST / NOT the last story beat" line sat inside the cached instructions, so the last scene missed the cache the earlier scenes had written.

The maintainer also asked that the last chapter's summary never be sent beside that chapter in full. A main prompt sends the previous main chapter in full and leaves its timeline entry out. However, condensing a long timeline folded everything but the newest entry into the condensed head. When a route chapter or Character Story came after the newest main chapter, that main chapter's summary went into the head and was repeated.

Closes #2091

What changed

  • Hour-long cache. generateJson asks for cacheTtl: "1h" on any prompt with cache marks. That covers the plan (P5), the scenes (P6), the Confrontation (P12) and the Closer (P12c). The option needs capability API 1.815, from Engine PR Pasta-Devs/Marinara-Engine#8748 (merged). An older Engine ignores it and keeps the five-minute cache, so the package's capability floor stays at 1.386.
  • A cached story context that's the same whatever the cast. recallText takes a new cached option, which replaces apart:
    • A cached prompt gets only what every call of its chapter may know: the timeline, the previous chapter in full, and shared canon the timeline doesn't cover yet.
    • In a love interest's own route, repair, date or story chapter, his own canon is cached as well, since he is in every node.
    • What only some may know goes with the recalls after the cached part, for each call's own cast, exactly as before. That covers another love interest's own chapters before the timeline covers them, his dates, his small memories, and his chapters in full in the canonical history. A date still reaches no one else's prompt.
    • Within the canonical-history bound, the shared chapters are taken first, and each part keeps its own newest summary. That way, what every call gets never depends on who knows the rest.
    • There is no state between calls. A summary that joins the timeline changes the cached part once, as it must.
  • Fit order. The fitter gets the recalls' context first, so a cached prompt's own canon gives way before the previous chapter's earliest scenes, which is the documented order.
  • Single-use marks removed. P5, P12 and P12c mark only their story context. No later call shares their instructions or scenes, and a mark is a cache write at twice the price for the hour-long cache.
  • One set of instructions for every scene. The terminal/not-terminal choice line moves from the cached system instructions into the node's task. The shared kit (player-choices-server.mjs) is unchanged.
  • The last chapter's summary is never repeated. Condensing the timeline now keeps the newest main chapter's entry and everything after it apart. With nothing before that entry to condense, it falls back to the old rule: everything but the newest entry. A ponytail: note in timeline.ts lists the cases that can still repeat a summary: an older previous route chapter, and the newest main chapter once enough side chapters follow it.
  • Version 0.7.6, with release notes, rebuilt bundle, catalogs and artifact.

Validation

  • pnpm test:otome-forge: 637 + 13 pass, 0 fail.
  • node scripts/typecheck-packages.mjs otome-forge: no strict errors.
  • Prettier check and ESLint on the changed files: no errors. Four existing unused-variable warnings remain in chapters.ts.
  • Regressions:
    • The cached-prefix test now reads the whole chapter, including the Confrontation and the last scene. It checks that messages[0] is byte-identical across the plan, every scene and the Confrontation, and that messages[1] is identical across every scene. It also checks that the LAST/NOT-last line is in the tail, that only messages[0] is marked on the plan and Confrontation, and that every marked call asks for cacheTtl: "1h". It fails without the fix.
    • The cached-prefix test has a private route chapter for Mireille. Her chapter goes only with the task of the scenes she is in, never in messages[0]. It fails if P6 stops passing cached.
    • a cached story context is what every call of its chapter may know… checks that the context is byte-identical across casts, with private canon and recalls following each call. It also checks the chapter's own love interest's canon is cached in his own chapter, and that a chapter joining the timeline is there for every call. In the canonical history, a long date doesn't push Night 1 out of the shared part. That last check fails without the shared-first bound.
    • condensing keeps the newest main chapter's entry apart… puts a Character Story after the newest main chapter. The condensing must leave that chapter's entry apart, and the main story's prompt must never carry its summary. It fails without the fix. A unit test covers the fallback branch.
    • The generation regression checks for cacheTtl: "1h" on marked prompts and no TTL on unmarked ones.
  • The SFW-defaults prompt pins for beat/beatWithCg were updated, because the line moved.
  • Local CodeRabbit: the first run had two findings, about an unbounded held state and stale recalls. Both are moot now that there is no held state. The second run had 0 findings.
  • Local multi-lens review (5 lenses, each finding adversarially verified): the confirmed findings drove the stateless redesign, the fit order, the single-use marks, the wiring and fallback tests, and the doc fixes. A second round is running on this version.

Not in this PR (maintainer's call)

  • Stable love-interest cards in the cached part: dropped.
  • Keeping the prompt fitter from cutting a cached part: skipped.
  • Route and repair plans don't use the cached layout yet.

Manual verification

  • On an Anthropic connection with debug mode on, play a main chapter. Check the debug log's "Prompt cache" lines: the plan writes the cache, and the later scenes and the Confrontation or Closer read it, including scenes read more than five minutes apart.
  • Play a scene with a different love interest from the one before it, and check the next scene still reads the cache.
  • Go on a date with one love interest, then play a main-chapter scene without them. Check the date isn't in that scene's prompt (debug log).
  • Check the chapter's last scene still offers a big choice when the story earns one, and earlier scenes never do.

🤖 Generated with Claude Code


🔄 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-Agents/pull/2092 **Author:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Created:** 11/3/2026 **Status:** ✅ Merged **Merged:** 11/3/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/otome-cache-stable-prefix` --- ### 📝 Commits (4) - [`25e803b`](https://github.com/Pasta-Devs/Marinara-Agents/commit/25e803b4219063892a9758ee43f756d7422210b4) chore(otome-forge): start the 0.7.6 caching work - [`3761600`](https://github.com/Pasta-Devs/Marinara-Agents/commit/3761600c6faaf800839a72aeae552ce4569ba17f) fix(otome-forge): a chapter's calls share one cached story context for an hour (0.7.6) - [`ea430bb`](https://github.com/Pasta-Devs/Marinara-Agents/commit/ea430bb410bda41ba76809f9aec521a42312404c) fix(otome-forge): the cached part holds shared canon only; the last chapter's summary is never repeated (0.7.6) - [`aa7d580`](https://github.com/Pasta-Devs/Marinara-Agents/commit/aa7d5802ab631821e6bd8f74e6984c2435b36ecd) fix(otome-forge): a cached story context worked out per call, the same for every cast (0.7.6) ### 📊 Changes **23 files changed** (+585 additions, -253 deletions) <details> <summary>View changed files</summary> ➕ `artifacts/otome-forge-0.7.6.zip` (+0 -0) 📝 `catalog/preview/catalog.json` (+6 -6) 📝 `catalog/preview/notes.json` (+6 -6) 📝 `catalog/preview/v2/catalog.json` (+6 -6) 📝 `catalog/preview/v2/notes.json` (+6 -6) 📝 `catalog/preview/v3/catalog.json` (+6 -6) 📝 `catalog/preview/v3/notes.json` (+6 -6) 📝 `packages/otome-forge/CHANGELOG.md` (+5 -0) 📝 `packages/otome-forge/manifest.json` (+3 -3) 📝 `packages/otome-forge/server.mjs` (+133 -130) 📝 `packages/otome-forge/src/engine/packages/server/src/services/otome-forge/generation.ts` (+14 -6) 📝 `packages/otome-forge/src/engine/packages/server/src/services/otome-forge/memory/memory.ts` (+11 -8) 📝 `packages/otome-forge/src/engine/packages/server/src/services/otome-forge/memory/timeline.ts` (+28 -9) 📝 `packages/otome-forge/src/engine/packages/server/src/services/otome-forge/prompts.ts` (+36 -15) 📝 `packages/otome-forge/src/engine/packages/server/src/services/otome-forge/story/beats.ts` (+4 -2) 📝 `packages/otome-forge/src/engine/packages/server/src/services/otome-forge/story/canon.ts` (+87 -28) 📝 `packages/otome-forge/src/engine/packages/server/src/services/otome-forge/story/chapters.ts` (+2 -2) 📝 `scripts/build-feature-packages.mjs` (+1 -1) 📝 `tests/otome-forge-beats.regression.ts` (+148 -5) 📝 `tests/otome-forge-date-night.regression.ts` (+4 -1) _...and 3 more files_ </details> ### 📄 Description ## Why Otome Hearth's prompt caching stopped paying off. The maintainer's Anthropic usage showed cache writes on most calls and almost no cache reads. A chapter's plan, scenes and climax are meant to share one cached story context: rules, contract, world, protagonist, the arc's line, story memory and the previous chapter. Three things kept breaking that: 1. **The cache only lasted five minutes.** A player reads scenes further apart than that, so each scene paid to write the cache again. 2. **The cached story memory changed between a chapter's calls.** It followed each call's cast: a scene with a different love interest pulled in different private recent canon. It also followed the memory's state at that moment. A chapter summary finishing in the background, or the memory becoming ready after the canonical-history fallback, changed the cached prefix in the middle of a chapter. 3. **The chapter's last scene had its own instructions.** The kit's "This is the LAST / NOT the last story beat" line sat inside the cached instructions, so the last scene missed the cache the earlier scenes had written. The maintainer also asked that the last chapter's summary never be sent beside that chapter in full. A main prompt sends the previous main chapter in full and leaves its timeline entry out. However, condensing a long timeline folded everything but the newest entry into the condensed head. When a route chapter or Character Story came after the newest main chapter, that main chapter's summary went into the head and was repeated. Closes #2091 ## What changed - **Hour-long cache.** `generateJson` asks for `cacheTtl: "1h"` on any prompt with cache marks. That covers the plan (P5), the scenes (P6), the Confrontation (P12) and the Closer (P12c). The option needs capability API 1.815, from Engine PR Pasta-Devs/Marinara-Engine#8748 (merged). An older Engine ignores it and keeps the five-minute cache, so the package's capability floor stays at 1.386. - **A cached story context that's the same whatever the cast.** `recallText` takes a new `cached` option, which replaces `apart`: - A cached prompt gets only what every call of its chapter may know: the timeline, the previous chapter in full, and shared canon the timeline doesn't cover yet. - In a love interest's own route, repair, date or story chapter, his own canon is cached as well, since he is in every node. - What only some may know goes with the recalls after the cached part, for each call's own cast, exactly as before. That covers another love interest's own chapters before the timeline covers them, his dates, his small memories, and his chapters in full in the canonical history. A date still reaches no one else's prompt. - Within the canonical-history bound, the shared chapters are taken first, and each part keeps its own newest summary. That way, what every call gets never depends on who knows the rest. - There is no state between calls. A summary that joins the timeline changes the cached part once, as it must. - **Fit order.** The fitter gets the recalls' context first, so a cached prompt's own canon gives way before the previous chapter's earliest scenes, which is the documented order. - **Single-use marks removed.** P5, P12 and P12c mark only their story context. No later call shares their instructions or scenes, and a mark is a cache write at twice the price for the hour-long cache. - **One set of instructions for every scene.** The terminal/not-terminal choice line moves from the cached system instructions into the node's task. The shared kit (`player-choices-server.mjs`) is unchanged. - **The last chapter's summary is never repeated.** Condensing the timeline now keeps the newest main chapter's entry and everything after it apart. With nothing before that entry to condense, it falls back to the old rule: everything but the newest entry. A `ponytail:` note in `timeline.ts` lists the cases that can still repeat a summary: an older previous route chapter, and the newest main chapter once enough side chapters follow it. - Version 0.7.6, with release notes, rebuilt bundle, catalogs and artifact. ## Validation - `pnpm test:otome-forge`: 637 + 13 pass, 0 fail. - `node scripts/typecheck-packages.mjs otome-forge`: no strict errors. - Prettier check and ESLint on the changed files: no errors. Four existing unused-variable warnings remain in `chapters.ts`. - Regressions: - The cached-prefix test now reads the whole chapter, including the Confrontation and the last scene. It checks that messages[0] is byte-identical across the plan, every scene and the Confrontation, and that messages[1] is identical across every scene. It also checks that the LAST/NOT-last line is in the tail, that only messages[0] is marked on the plan and Confrontation, and that every marked call asks for `cacheTtl: "1h"`. It fails without the fix. - The cached-prefix test has a private route chapter for Mireille. Her chapter goes only with the task of the scenes she is in, never in messages[0]. It fails if P6 stops passing `cached`. - `a cached story context is what every call of its chapter may know…` checks that the context is byte-identical across casts, with private canon and recalls following each call. It also checks the chapter's own love interest's canon is cached in his own chapter, and that a chapter joining the timeline is there for every call. In the canonical history, a long date doesn't push Night 1 out of the shared part. That last check fails without the shared-first bound. - `condensing keeps the newest main chapter's entry apart…` puts a Character Story after the newest main chapter. The condensing must leave that chapter's entry apart, and the main story's prompt must never carry its summary. It fails without the fix. A unit test covers the fallback branch. - The generation regression checks for `cacheTtl: "1h"` on marked prompts and no TTL on unmarked ones. - The SFW-defaults prompt pins for `beat`/`beatWithCg` were updated, because the line moved. - Local CodeRabbit: the first run had two findings, about an unbounded held state and stale recalls. Both are moot now that there is no held state. The second run had 0 findings. - Local multi-lens review (5 lenses, each finding adversarially verified): the confirmed findings drove the stateless redesign, the fit order, the single-use marks, the wiring and fallback tests, and the doc fixes. A second round is running on this version. ## Not in this PR (maintainer's call) - Stable love-interest cards in the cached part: dropped. - Keeping the prompt fitter from cutting a cached part: skipped. - Route and repair plans don't use the cached layout yet. ## Manual verification - [ ] On an Anthropic connection with debug mode on, play a main chapter. Check the debug log's "Prompt cache" lines: the plan writes the cache, and the later scenes and the Confrontation or Closer read it, including scenes read more than five minutes apart. - [ ] Play a scene with a different love interest from the one before it, and check the next scene still reads the cache. - [ ] Go on a date with one love interest, then play a main-chapter scene without them. Check the date isn't in that scene's prompt (debug log). - [ ] Check the chapter's last scene still offers a big choice when the story earns one, and earlier scenes never do. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-07 16:39:35 +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-Agents#2112
No description provided.