[GH-ISSUE #6148] A separate Game tool connection breaks the assistant prefill and doubles the [USER] marker in the narrator prompt #3421

Closed
opened 2026-10-21 06:02:55 +07:00 by bsakaguchi · 1 comment
Owner

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

Originally assigned to: @SpicyMarinara on GitHub.

When a Game chat is set to run its tool planning on a second connection, the prompt that gets sent to the narrator is built wrong in two ways. If you use an assistant prefill, the reply no longer continues from it: the message shows your prefill text followed by a reply that starts over from scratch. And if you have "Single user message" formatting on, the squashed transcript sent to the model now begins with two [USER] lines instead of one. Neither shows an error, so the only way to notice is to open Peek Prompt.

How to reproduce (try it yourself)

Shared setup for both cases:

  1. Create two API connections: one for narration, and a second tool-capable one for planning.
  2. Open or create a Game chat and put it on the narrator connection.
  3. Go to Chat Settings, Function Calling, and set "Game tool connection" to the second connection (not "Same as narrator").
  4. Turn on debug mode, or plan to use the message's Peek prompt action, so you can read the outbound prompt.

Case 1, the prefill stops being the last turn:

  1. In the chat's prompt preset, set a non-empty Assistant Prefill, for example *Gwen steadies her grip.*. Leave "Single user message" off.
  2. Play a turn that needs a roll, for example type I force the gate.
  3. Open Peek prompt for that narration.

What you'll see: the message list ends with user, then assistant (your prefill), then another user message carrying the tool results. The prefill sits in the middle of the conversation instead of at the end. The saved narration begins with the prefill text, because the client already streamed it, but the model's reply starts a fresh sentence, so the prefill reads as a dangling fragment.

What should happen: the prefill is the last message the narrator sees and the reply continues from it, exactly as it does when "Game tool connection" is set back to "Same as narrator". Switch it back and replay the same turn to see the correct behavior.

Case 2, the doubled role marker:

  1. In the same preset, turn ON "Single user message" under the generation parameters.
  2. Play the same turn again and open Peek prompt.

What you'll see: the single squashed user block starts with two consecutive [USER] lines before your first line of text.

What should happen: one [USER] line, the same as with "Same as narrator".

Where it is

packages/server/src/routes/generate.routes.ts, the planner branch at lines 7318 to 7329:

narratorMessages = prepareProviderMessages(
  await fitPromptForSend([
    ...initialProviderMessages,
    { role: "user", content: "The engine ran a separate tool-planning pass..." },
  ]),
);

initialProviderMessages is already the output of prepareProviderMessages (same file, lines 6804 to 6806, and via the toProviderMessages callback at 6754 on the Advanced Memory path). The prefill is pushed as the last array element by appendGenerationTailMessages in packages/server/src/routes/generate/generate-route-utils.ts lines 1042 to 1052. The non-idempotent line is generate-route-utils.ts:677, inside postProcessMessages, which prefixes [ROLE]\n with no check for a marker already present.

What's happening

The planner branch takes a message array that has already been formatted and runs it through the formatter a second time, then appends the tool-results message to the very end. The second formatting pass re-prefixes the already-marked transcript, which is where the extra [USER] line comes from. The append lands after the assistant prefill, which was the last element, so the prefill is no longer the trailing assistant turn the model is meant to continue. Meanwhile the prefill text has already been streamed into the message the user is watching (generate.routes.ts:6820 to 6824), and the de-duplication guard at :7605 only fires when the model repeats the prefill word for word, so the user ends up with prefill plus a restarted reply.

The double pass changes the prompt in every formatting mode, not only under "Single user message": under the default formatting it also merges the tool-results message into the preceding user turn instead of leaving it as its own message.

Suggested fix

Build the narrator prompt the way the sibling post-roll continuation in the same file already does at lines 7979 to 7986: await fitPromptForSend([...]) only, with no second prepareProviderMessages pass over already-prepared content. In the same edit, insert the tool-results message before the trailing assistant prefill rather than after it (or re-append the prefill afterwards), so the prefill stays the last turn. Making postProcessMessages idempotent is worth doing too, but on its own it only fixes the marker half; the ordering still needs the insert-before.

Context

Introduced by merged PR #5982, which implemented feature request #5957 (running Game tool calls on a different connection than narration). It is unreleased on staging: the latest release tag is v2.4.4. PR #6137 did not touch this area; its generate.routes.ts hunks land nowhere near the planner region, and the Peek Prompt work it did for #6130 is confined to PeekPromptModal.tsx. Closed issue #1461 ("Peek Prompt and generation use inconsistent prompt formatting parameters") is the closest conceptual relative of the double-formatting half. The shipped game-tool-controls regression lane passes because it never sets an assistant prefill and never enables "Single user message".

Found by an AI-assisted sweep and re-verified against staging ac14dd704 on 2026-09-13. Please verify before fixing.

Related issue from the same sweep: #6150 (the planning instruction being hoisted into the system prompt on Claude and Gemini); #6155 (the stale tool_calls finish reason from the same planner path).

Originally created by @kolacheee on GitHub (Sep 13, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/6148 Originally assigned to: @SpicyMarinara on GitHub. When a Game chat is set to run its tool planning on a second connection, the prompt that gets sent to the narrator is built wrong in two ways. If you use an assistant prefill, the reply no longer continues from it: the message shows your prefill text followed by a reply that starts over from scratch. And if you have "Single user message" formatting on, the squashed transcript sent to the model now begins with two `[USER]` lines instead of one. Neither shows an error, so the only way to notice is to open Peek Prompt. ## How to reproduce (try it yourself) Shared setup for both cases: 1. Create two API connections: one for narration, and a second tool-capable one for planning. 2. Open or create a Game chat and put it on the narrator connection. 3. Go to Chat Settings, Function Calling, and set "Game tool connection" to the second connection (not "Same as narrator"). 4. Turn on debug mode, or plan to use the message's Peek prompt action, so you can read the outbound prompt. Case 1, the prefill stops being the last turn: 5. In the chat's prompt preset, set a non-empty Assistant Prefill, for example `*Gwen steadies her grip.*`. Leave "Single user message" off. 6. Play a turn that needs a roll, for example type `I force the gate.` 7. Open Peek prompt for that narration. **What you'll see:** the message list ends with user, then assistant (your prefill), then another user message carrying the tool results. The prefill sits in the middle of the conversation instead of at the end. The saved narration begins with the prefill text, because the client already streamed it, but the model's reply starts a fresh sentence, so the prefill reads as a dangling fragment. **What should happen:** the prefill is the last message the narrator sees and the reply continues from it, exactly as it does when "Game tool connection" is set back to "Same as narrator". Switch it back and replay the same turn to see the correct behavior. Case 2, the doubled role marker: 8. In the same preset, turn ON "Single user message" under the generation parameters. 9. Play the same turn again and open Peek prompt. **What you'll see:** the single squashed user block starts with two consecutive `[USER]` lines before your first line of text. **What should happen:** one `[USER]` line, the same as with "Same as narrator". ## Where it is `packages/server/src/routes/generate.routes.ts`, the planner branch at lines 7318 to 7329: ```ts narratorMessages = prepareProviderMessages( await fitPromptForSend([ ...initialProviderMessages, { role: "user", content: "The engine ran a separate tool-planning pass..." }, ]), ); ``` `initialProviderMessages` is already the output of `prepareProviderMessages` (same file, lines 6804 to 6806, and via the `toProviderMessages` callback at 6754 on the Advanced Memory path). The prefill is pushed as the last array element by `appendGenerationTailMessages` in `packages/server/src/routes/generate/generate-route-utils.ts` lines 1042 to 1052. The non-idempotent line is `generate-route-utils.ts:677`, inside `postProcessMessages`, which prefixes `[ROLE]\n` with no check for a marker already present. ## What's happening The planner branch takes a message array that has already been formatted and runs it through the formatter a second time, then appends the tool-results message to the very end. The second formatting pass re-prefixes the already-marked transcript, which is where the extra `[USER]` line comes from. The append lands after the assistant prefill, which was the last element, so the prefill is no longer the trailing assistant turn the model is meant to continue. Meanwhile the prefill text has already been streamed into the message the user is watching (generate.routes.ts:6820 to 6824), and the de-duplication guard at :7605 only fires when the model repeats the prefill word for word, so the user ends up with prefill plus a restarted reply. The double pass changes the prompt in every formatting mode, not only under "Single user message": under the default formatting it also merges the tool-results message into the preceding user turn instead of leaving it as its own message. ## Suggested fix Build the narrator prompt the way the sibling post-roll continuation in the same file already does at lines 7979 to 7986: `await fitPromptForSend([...])` only, with no second `prepareProviderMessages` pass over already-prepared content. In the same edit, insert the tool-results message before the trailing assistant prefill rather than after it (or re-append the prefill afterwards), so the prefill stays the last turn. Making `postProcessMessages` idempotent is worth doing too, but on its own it only fixes the marker half; the ordering still needs the insert-before. ## Context Introduced by merged PR #5982, which implemented feature request #5957 (running Game tool calls on a different connection than narration). It is unreleased on staging: the latest release tag is v2.4.4. PR #6137 did not touch this area; its `generate.routes.ts` hunks land nowhere near the planner region, and the Peek Prompt work it did for #6130 is confined to `PeekPromptModal.tsx`. Closed issue #1461 ("Peek Prompt and generation use inconsistent prompt formatting parameters") is the closest conceptual relative of the double-formatting half. The shipped `game-tool-controls` regression lane passes because it never sets an assistant prefill and never enables "Single user message". Found by an AI-assisted sweep and re-verified against staging ac14dd704 on 2026-09-13. Please verify before fixing. Related issue from the same sweep: #6150 (the planning instruction being hoisted into the system prompt on Claude and Gemini); #6155 (the stale tool_calls finish reason from the same planner path).
bsakaguchi 2026-10-21 06:02:55 +07:00
Author
Owner

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

Codex is addressing this report on behalf of @SpicyMarinara in draft PR #6168 as part of the requested issue sweep. I will verify the reported path against staging and preserve existing behavior outside the narrow fix.

<!-- gh-comment-id:5654636934 --> @SpicyMarinara commented on GitHub (Sep 13, 2026): Codex is addressing this report on behalf of @SpicyMarinara in draft PR #6168 as part of the requested issue sweep. I will verify the reported path against staging and preserve existing behavior outside the narrow fix.
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#3421
No description provided.