[PR #8276] [MERGED] fix(llm): send a prompt cache key on OpenAI and ChatGPT requests #8260

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

📋 Pull Request Information

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

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


📝 Commits (10+)

  • 6e67660 Eval-only GM tail layouts and a harness to compare them
  • a69a88d Send a prompt cache key on OpenAI and ChatGPT requests
  • 2bfcba8 Log the Responses request body in Debug mode
  • 3cb6d90 Eval: find where consecutive request bodies stop sharing a prefix
  • 516e5ba Eval: strip logger colour codes before reading request bodies
  • ac03fb6 Revert "Eval: strip logger colour codes before reading request bodies"
  • ad8db36 Revert "Eval: find where consecutive request bodies stop sharing a prefix"
  • 45bc3b1 Revert "Eval-only GM tail layouts and a harness to compare them"
  • c95910e Merge remote-tracking branch 'origin/staging' into claude/project-thread-r412n9
  • bd45af3 Say the logged Responses body is the one built, before any retry

📊 Changes

2 files changed (+146 additions, -13 deletions)

View changed files

📝 packages/server/src/services/llm/providers/openai.provider.ts (+76 -13)
➕ scripts/regressions/openai-prompt-cache-key.regression.ts (+70 -0)

📄 Description

Linked issue

Closes #8275

Why this change

  • OpenAI routes a request by prompt_cache_key to the machine that already holds its prefix. Marinara never sent one, so the turns of a chat landed on different machines. On the ChatGPT (Codex) route, Game Master turns with a byte-identical ~7k-token prefix reported 0 cached tokens.

What changed

  • Every OpenAI request from a chat now carries the same prompt_cache_key, on chat completions and the Responses API alike. The key is marinara- plus 32 hex characters of a hash of the chat id, so the chat id itself never leaves the server. A request outside any chat gets no key.
  • Only OpenAI's own endpoints get the key: the ChatGPT (Codex) route, and the openai provider when it points at https://api.openai.com. A custom or compatible endpoint may refuse a field it doesn't know, so it never receives one.
  • Debug mode now logs the provider's raw usage object ([debug/openai] raw usage). It also logs the Responses request body as built ([debug/openai] responses request body), so a cache miss can be traced to the first byte where two requests differ.

Validation

  • Server tsc --noEmit passes, and Prettier is clean on the changed files
  • New regression openai-prompt-cache-key: one chat keeps one key, another chat gets another key, no chat means no key, the raw chat id never appears in the key, and compatible endpoints never get it
  • All 35 regressions that use the OpenAI provider pass
  • 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 ChatGPT (Codex) connection, gpt-6-sol, with Debug on. Before the change, every turn showed 0 cached tokens. After it, six turns about 70 s apart cached 0, 2816, 2816, 0, 0 and 2816 tokens.
  • A diff of the logged request bodies showed every request had the same key and byte-identical instructions. The shared prefix was 10–11k tokens, ending at the previous player message. The remaining shortfall comes from the ChatGPT backend's own caching, not from the request.

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)

The branch history includes an eval prototype that is reverted inside this PR, so the net diff is only the two files above. A squash merge keeps staging history clean.


🔄 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/8276 **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 (10+) - [`6e67660`](https://github.com/Pasta-Devs/Marinara-Engine/commit/6e676608bf8297e963f3069ba542d20892d20ea1) Eval-only GM tail layouts and a harness to compare them - [`a69a88d`](https://github.com/Pasta-Devs/Marinara-Engine/commit/a69a88de449ae479246e78f2165f2ec7be0ef03e) Send a prompt cache key on OpenAI and ChatGPT requests - [`2bfcba8`](https://github.com/Pasta-Devs/Marinara-Engine/commit/2bfcba8f4bf1c6ded6e4c3222427120d9c6fadda) Log the Responses request body in Debug mode - [`3cb6d90`](https://github.com/Pasta-Devs/Marinara-Engine/commit/3cb6d908f3659ed09b3c73f7257af4250a27956b) Eval: find where consecutive request bodies stop sharing a prefix - [`516e5ba`](https://github.com/Pasta-Devs/Marinara-Engine/commit/516e5ba4733831e126c48e3624a5b1d2bfa82958) Eval: strip logger colour codes before reading request bodies - [`ac03fb6`](https://github.com/Pasta-Devs/Marinara-Engine/commit/ac03fb61c1eb19e3dec375c8dcf1c8cbe8da4a06) Revert "Eval: strip logger colour codes before reading request bodies" - [`ad8db36`](https://github.com/Pasta-Devs/Marinara-Engine/commit/ad8db36e0854bcbf73e95530735d8bfdb2d983cc) Revert "Eval: find where consecutive request bodies stop sharing a prefix" - [`45bc3b1`](https://github.com/Pasta-Devs/Marinara-Engine/commit/45bc3b1e6accdfbd88b9e1ce00b32770c0d81414) Revert "Eval-only GM tail layouts and a harness to compare them" - [`c95910e`](https://github.com/Pasta-Devs/Marinara-Engine/commit/c95910e54ce635fa27e338da17a48c198b977579) Merge remote-tracking branch 'origin/staging' into claude/project-thread-r412n9 - [`bd45af3`](https://github.com/Pasta-Devs/Marinara-Engine/commit/bd45af30e8fa800ec2dd4ebf1caee82d16737b77) Say the logged Responses body is the one built, before any retry ### 📊 Changes **2 files changed** (+146 additions, -13 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/services/llm/providers/openai.provider.ts` (+76 -13) ➕ `scripts/regressions/openai-prompt-cache-key.regression.ts` (+70 -0) </details> ### 📄 Description ## Linked issue Closes #8275 ## Why this change - OpenAI routes a request by `prompt_cache_key` to the machine that already holds its prefix. Marinara never sent one, so the turns of a chat landed on different machines. On the ChatGPT (Codex) route, Game Master turns with a byte-identical ~7k-token prefix reported 0 cached tokens. ## What changed - Every OpenAI request from a chat now carries the same `prompt_cache_key`, on chat completions and the Responses API alike. The key is `marinara-` plus 32 hex characters of a hash of the chat id, so the chat id itself never leaves the server. A request outside any chat gets no key. - Only OpenAI's own endpoints get the key: the ChatGPT (Codex) route, and the `openai` provider when it points at `https://api.openai.com`. A custom or compatible endpoint may refuse a field it doesn't know, so it never receives one. - Debug mode now logs the provider's raw usage object (`[debug/openai] raw usage`). It also logs the Responses request body as built (`[debug/openai] responses request body`), so a cache miss can be traced to the first byte where two requests differ. ## Validation - [x] Server `tsc --noEmit` passes, and Prettier is clean on the changed files - [x] New regression `openai-prompt-cache-key`: one chat keeps one key, another chat gets another key, no chat means no key, the raw chat id never appears in the key, and compatible endpoints never get it - [x] All 35 regressions that use the OpenAI provider pass - [ ] 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 ChatGPT (Codex) connection, gpt-6-sol, with Debug on. Before the change, every turn showed 0 cached tokens. After it, six turns about 70 s apart cached 0, 2816, 2816, 0, 0 and 2816 tokens. - A diff of the logged request bodies showed every request had the same key and byte-identical instructions. The shared prefix was 10–11k tokens, ending at the previous player message. The remaining shortfall comes from the ChatGPT backend's own caching, not from the request. ## Docs and release impact - [x] 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) The branch history includes an eval prototype that is reverted inside this PR, so the net diff is only the two files above. A squash merge keeps `staging` history clean. --- <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:11 +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#8260
No description provided.