[GH-ISSUE #6219] [Issue]: Depth-injected system entries are hoisted into the Anthropic system block and break the cached prefix #3468

Closed
opened 2026-10-21 06:03:25 +07:00 by bsakaguchi · 2 comments
Owner

Originally created by @kolacheee on GitHub (Sep 15, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/6219

Originally assigned to: @SpicyMarinara on GitHub.

Summary

The Anthropic adapter collects every role: "system" message in the whole array into the top-level system field. A depth-injected entry authored as system (a depth prompt section, a lorebook depth entry, a character advanced-prompt entry) therefore lands in the system block instead of at its depth, and anything that changes inside it invalidates the cached prefix for the whole system block.

Expected behavior

A depth entry sits at its configured depth in the message history on Anthropic, as it does on other providers, and a lorebook entry near the end of history changing does not throw away the cached system prefix.

Actual behavior

packages/server/src/services/llm/providers/anthropic.provider.ts:369 and :682 filter m.role === "system" across the entire array and hoist the result into system. The injector places each entry with its authored role (packages/server/src/services/prompt/assembler.ts:696-707, slot computed at packages/server/src/services/lorebook/prompt-injector.ts:105). Nothing checks where the entry ended up.

Steps to reproduce

  1. Anthropic API-key connection with Prompt caching on.
  2. Add a lorebook entry at depth 2 with role system whose text changes between turns (for example it contains {{time}}).
  3. Capture the request or open Peek Prompt: the entry is inside system, not in messages, and the next turn reports a cache write where a cache read was expected.

Environment

  • Marinara Engine version: 2.4.5 (staging at 29ee24fc2)
  • Install type: source
  • OS + version: Fedora 44
  • Browser or app shell: not browser-specific; verified by reading the code at the cited lines

Logs, screenshots, or video

Not applicable; the hoist is visible at the cited lines.

Additional context

The fix has API constraints. A system message inside messages is accepted only on some models, must follow a user turn (or an assistant turn ending in server tool use), and must be the last entry or be followed by an assistant turn, otherwise the request is a 400. The injector's slot math does not check neighbours, so the safe shape is to keep the entry in messages only when the slot satisfies the rule and otherwise convert it to a user-role entry at the same depth, which is also what unsupported models get. CHANGELOG.md line 825 records an earlier fix of the same class (volatile name lists moved out of the system prompt to keep caching stable).

Template check

Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked:

  • I DID NOT read this template and provide the requested details.
Originally created by @kolacheee on GitHub (Sep 15, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/6219 Originally assigned to: @SpicyMarinara on GitHub. ## Summary The Anthropic adapter collects every `role: "system"` message in the whole array into the top-level `system` field. A depth-injected entry authored as system (a depth prompt section, a lorebook depth entry, a character advanced-prompt entry) therefore lands in the system block instead of at its depth, and anything that changes inside it invalidates the cached prefix for the whole system block. ## Expected behavior A depth entry sits at its configured depth in the message history on Anthropic, as it does on other providers, and a lorebook entry near the end of history changing does not throw away the cached system prefix. ## Actual behavior `packages/server/src/services/llm/providers/anthropic.provider.ts:369` and `:682` filter `m.role === "system"` across the entire array and hoist the result into `system`. The injector places each entry with its authored role (`packages/server/src/services/prompt/assembler.ts:696-707`, slot computed at `packages/server/src/services/lorebook/prompt-injector.ts:105`). Nothing checks where the entry ended up. ## Steps to reproduce 1. Anthropic API-key connection with **Prompt caching** on. 2. Add a lorebook entry at depth 2 with role system whose text changes between turns (for example it contains `{{time}}`). 3. Capture the request or open Peek Prompt: the entry is inside `system`, not in `messages`, and the next turn reports a cache write where a cache read was expected. ## Environment - Marinara Engine version: 2.4.5 (staging at 29ee24fc2) - Install type: source - OS + version: Fedora 44 - Browser or app shell: not browser-specific; verified by reading the code at the cited lines ## Logs, screenshots, or video Not applicable; the hoist is visible at the cited lines. ## Additional context The fix has API constraints. A system message inside `messages` is accepted only on some models, must follow a user turn (or an assistant turn ending in server tool use), and must be the last entry or be followed by an assistant turn, otherwise the request is a 400. The injector's slot math does not check neighbours, so the safe shape is to keep the entry in `messages` only when the slot satisfies the rule and otherwise convert it to a user-role entry at the same depth, which is also what unsupported models get. `CHANGELOG.md` line 825 records an earlier fix of the same class (volatile name lists moved out of the system prompt to keep caching stable). ## Template check Please **uncheck (untick)** the box below before submitting so we know you read the template. It is intentionally pre-checked: - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 06:03:25 +07:00
Author
Owner

@SpicyMarinara commented on GitHub (Sep 15, 2026):

Owner: @SpicyMarinara, with Codex implementing this issue as part of the requested sweep in draft PR #6230 against staging. I checked the open PRs and issue-linked work before starting; no competing open PR was found.

<!-- gh-comment-id:5677750783 --> @SpicyMarinara commented on GitHub (Sep 15, 2026): Owner: @SpicyMarinara, with Codex implementing this issue as part of the requested sweep in draft PR #6230 against `staging`. I checked the open PRs and issue-linked work before starting; no competing open PR was found.
Author
Owner

@SpicyMarinara commented on GitHub (Sep 15, 2026):

Merged in #6230 to staging (c6685f7034).

Only leading system instructions enter Anthropic’s top-level system prefix. Depth instructions retain their history position, using supported history-level system messages or a compatible user-context fallback. Provider regressions cover both request paths, placement controls, and empty tool-result pairing.

<!-- gh-comment-id:5679124225 --> @SpicyMarinara commented on GitHub (Sep 15, 2026): Merged in #6230 to staging (c6685f7034d474bee00db124c4dc6398c5fc2bed). Only leading system instructions enter Anthropic’s top-level system prefix. Depth instructions retain their history position, using supported history-level system messages or a compatible user-context fallback. Provider regressions cover both request paths, placement controls, and empty tool-result pairing.
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#3468
No description provided.