[PR #7866] [MERGED] fix(claude-subscription): mark a history turn so the next request reuses the cached chat #7974

Closed
opened 2026-10-24 04:09:19 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/7866
Author: @stonebystone
Created: 10/20/2026
Status: ✅ Merged
Merged: 10/20/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/claude-sub-history-cache


📝 Commits (7)

  • 6a5cbef fix(claude-subscription): mark a history turn so the next request reuses the cached chat
  • 86e4359 fix(claude-subscription): add the history marker only with the 1-hour cache on
  • dd26228 docs(claude-subscription): say the 1-hour cache also lets history be reused
  • 5f164aa fix(claude-subscription): keep the history marker on tool-only replies
  • f8e47c3 fix(claude-subscription): pass the history marker past empty replies; plain-English docs
  • 1853177 fix(claude-subscription): log where the history marker lands; clearer docs wording
  • 3f2421d fix(claude-subscription): one-based entry position in the cache-marker debug line

📊 Changes

4 files changed (+104 additions, -4 deletions)

View changed files

📝 docs/connections/subscription-clis.md (+2 -0)
📝 packages/server/src/services/llm/providers/claude-subscription.provider.ts (+13 -4)
📝 packages/server/src/services/llm/providers/claude-subscription/jsonl-entries.ts (+21 -0)
📝 scripts/regressions/claude-subscription-cache.regression.ts (+68 -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 #7865

Why this change

On Claude (Subscription), every turn re-writes the whole chat history to the prompt cache and reads back only the system prompt. Claude Code marks just the system prompt and the current turn, and Marinara's current turn carries per-turn blocks (tracker context, <last_message>, depth-0 sections) that are gone from it a turn later. So each turn re-writes the whole history in the prompt, up to Claude's context limit on a long chat, against the user's subscription. In the reported 130k-token chat that was about 125k cache-write tokens per turn.

What changed

  • assembleEntries (claude-subscription jsonl-entries.ts) takes an optional historyCacheControl and puts it on the newest assistant reply that is not the last history entry. The last entry is skipped because prompt wrappers such as xml </chat_history> ride on it and move every turn.
  • The provider passes the marker on the resume path only when the connection's 1-hour cache is on. There, Claude Code's own markers are 1h as well, and the extra one is accepted. With the setting off, Claude Code picks an account-dependent TTL and a plain extra marker fails live with "A maximum of 4 blocks with cache_control may be provided. Found 5", so that mode is left as it was (marked with a ponytail: comment).
  • docs/connections/subscription-clis.md: one paragraph saying the 1-hour cache setting also lets the chat history be reused, and that with it off only the system prompt is. Translations follow in #7867.
  • claude-subscription-cache regression: checks there is exactly one 1h history marker, on the right reply, and none with the 1-hour cache off.

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

Agent-run checks (for the reviewer, not ticked above):

  • pnpm check: passes (one existing client lint warning, unrelated).
  • claude-subscription-cache regression passes, and fails with the marker removed. The other five Claude regressions pass.
  • Live run against a real subscription (Haiku 5.5, 26k-token prompt, three turns, xml-style </chat_history> on the last history message):
    Turn 2 read / write Turn 3 read / write
    Without marker 2,398 / 23,655 2,398 / 23,704
    With marker (1-hour cache on) 24,963 / 1,095 25,944 / 160
    1-hour cache off (unchanged) 2,399 / 23,663 2,399 / 23,714

Manual verification notes

  • Manually verify in the app: on a Claude (Subscription) connection with the 1-hour cache on, in a chat with at least four messages, send two turns. On the second reply the usage line should show most of the prompt as cached (tokensCachedPrompt close to the prompt size).
  • Manually verify a chat whose history has fewer than two assistant replies still sends normally (no marker is added).
  • Manually verify a Claude (Subscription) connection with the 1-hour cache off still sends normally.

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)

cc @SpicyMarinara

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Improvements
    • Claude subscription conversations now consistently honor the extended cache TTL setting when resuming sessions.
    • When enabled, resumed conversation history can use a 1-hour cache window; when disabled, extended history caching is not applied.
    • Cache markers are placed correctly in resumed conversation history, including tool-only replies, for more reliable cache behavior.
  • Documentation
    • Updated Claude subscription CLI documentation to explain extended token caching and its effect on conversation history.

🔄 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/7866 **Author:** [@stonebystone](https://github.com/stonebystone) **Created:** 10/20/2026 **Status:** ✅ Merged **Merged:** 10/20/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/claude-sub-history-cache` --- ### 📝 Commits (7) - [`6a5cbef`](https://github.com/Pasta-Devs/Marinara-Engine/commit/6a5cbef8783848f79acbf62d69b8aabe5283df09) fix(claude-subscription): mark a history turn so the next request reuses the cached chat - [`86e4359`](https://github.com/Pasta-Devs/Marinara-Engine/commit/86e435940dd8e0832516738c3d8457d9f7e83cd1) fix(claude-subscription): add the history marker only with the 1-hour cache on - [`dd26228`](https://github.com/Pasta-Devs/Marinara-Engine/commit/dd2622879b95160c125fcc99b3ceee7f470bfd00) docs(claude-subscription): say the 1-hour cache also lets history be reused - [`5f164aa`](https://github.com/Pasta-Devs/Marinara-Engine/commit/5f164aa1d4a66134071d5ff3ede0fb1af0342456) fix(claude-subscription): keep the history marker on tool-only replies - [`f8e47c3`](https://github.com/Pasta-Devs/Marinara-Engine/commit/f8e47c3717ef3bf578fc6698b9e622065145e855) fix(claude-subscription): pass the history marker past empty replies; plain-English docs - [`1853177`](https://github.com/Pasta-Devs/Marinara-Engine/commit/185317781795735b97499a4895288aca0e115ac6) fix(claude-subscription): log where the history marker lands; clearer docs wording - [`3f2421d`](https://github.com/Pasta-Devs/Marinara-Engine/commit/3f2421d2021c47210f5df095c903cef9dd7a0ae3) fix(claude-subscription): one-based entry position in the cache-marker debug line ### 📊 Changes **4 files changed** (+104 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `docs/connections/subscription-clis.md` (+2 -0) 📝 `packages/server/src/services/llm/providers/claude-subscription.provider.ts` (+13 -4) 📝 `packages/server/src/services/llm/providers/claude-subscription/jsonl-entries.ts` (+21 -0) 📝 `scripts/regressions/claude-subscription-cache.regression.ts` (+68 -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 #7865 ## Why this change On Claude (Subscription), every turn re-writes the whole chat history to the prompt cache and reads back only the system prompt. Claude Code marks just the system prompt and the current turn, and Marinara's current turn carries per-turn blocks (tracker context, `<last_message>`, depth-0 sections) that are gone from it a turn later. So each turn re-writes the whole history in the prompt, up to Claude's context limit on a long chat, against the user's subscription. In the reported 130k-token chat that was about 125k cache-write tokens per turn. ## What changed - `assembleEntries` (claude-subscription `jsonl-entries.ts`) takes an optional `historyCacheControl` and puts it on the newest assistant reply that is not the last history entry. The last entry is skipped because prompt wrappers such as xml `</chat_history>` ride on it and move every turn. - The provider passes the marker on the resume path only when the connection's 1-hour cache is on. There, Claude Code's own markers are 1h as well, and the extra one is accepted. With the setting off, Claude Code picks an account-dependent TTL and a plain extra marker fails live with "A maximum of 4 blocks with cache_control may be provided. Found 5", so that mode is left as it was (marked with a `ponytail:` comment). - `docs/connections/subscription-clis.md`: one paragraph saying the 1-hour cache setting also lets the chat history be reused, and that with it off only the system prompt is. Translations follow in #7867. - `claude-subscription-cache` regression: checks there is exactly one 1h history marker, on the right reply, and none with the 1-hour cache off. ## Validation - [x] `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) - [x] Read and followed `CONTRIBUTING.md` Agent-run checks (for the reviewer, not ticked above): - `pnpm check`: passes (one existing client lint warning, unrelated). - `claude-subscription-cache` regression passes, and fails with the marker removed. The other five Claude regressions pass. - Live run against a real subscription (Haiku 5.5, 26k-token prompt, three turns, xml-style `</chat_history>` on the last history message): | | Turn 2 read / write | Turn 3 read / write | | --- | --- | --- | | Without marker | 2,398 / 23,655 | 2,398 / 23,704 | | With marker (1-hour cache on) | 24,963 / 1,095 | 25,944 / 160 | | 1-hour cache off (unchanged) | 2,399 / 23,663 | 2,399 / 23,714 | ### Manual verification notes - Manually verify in the app: on a Claude (Subscription) connection with the 1-hour cache on, in a chat with at least four messages, send two turns. On the second reply the usage line should show most of the prompt as cached (`tokensCachedPrompt` close to the prompt size). - Manually verify a chat whose history has fewer than two assistant replies still sends normally (no marker is added). - Manually verify a Claude (Subscription) connection with the 1-hour cache off still sends normally. ## Docs and release impact - [ ] No docs changes needed - [x] Updated docs (README / CONTRIBUTING / android/README) as needed - [x] 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) cc @SpicyMarinara 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Improvements** - Claude subscription conversations now consistently honor the extended cache TTL setting when resuming sessions. - When enabled, resumed conversation history can use a 1-hour cache window; when disabled, extended history caching is not applied. - Cache markers are placed correctly in resumed conversation history, including tool-only replies, for more reliable cache behavior. - **Documentation** - Updated Claude subscription CLI documentation to explain extended token caching and its effect on conversation history. <!-- 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-24 04:09:19 +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#7974
No description provided.