[GH-ISSUE #6149] Raw tool call text can appear in Game narration when a separate Game tool connection is used with a local model #3422

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/6149

Originally assigned to: @SpicyMarinara on GitHub.

In a Game chat where the narrator runs on a local model and a separate connection is chosen to handle tools, the Game Master's story text can come back with a literal <tool_call>{"name": "roll_dice", "arguments": {...}}</tool_call> block sitting in the middle of the prose. It stays in the saved message, so it is still there after a reload. The dice themselves still resolve correctly on the tool connection, so the leaked block is a stray duplicate of a roll that already happened, not a lost action.

How to reproduce (try it yourself)

  1. Set up two connections. First, an OpenAI-compatible connection pointed at a local backend (llama.cpp, Ollama, KoboldCpp, LM Studio) with "Treat as local endpoint" turned ON in its settings. Second, any connection whose provider supports native tool calls, for example an OpenAI or Gemini key.
  2. Create a Game mode chat and set its connection to the local one. You do not need to switch Tool Use on, a Game chat attaches the dice tool by itself.
  3. Control run first. Leave Chat Settings, Function Calling, "Game tool connection" unset. Send I try to force the gate.
    What you'll see: clean narration, a dice result, no raw JSON. This is the correct behavior.
  4. Now open Chat Settings, Function Calling, and set "Game tool connection" to the tool-capable connection.
  5. Send I try to force the gate. again, and repeat for a few turns if the first one comes back clean.
    What you'll see: on turns where the local model takes the bait, the saved Game Master message contains a literal <tool_call>{"name": "roll_dice", "arguments": {"notation": "1d20"}}</tool_call> string inside the story text. Reload the chat and it is still there.
    What should happen: the narration should contain only prose plus the dice result that the tool connection already rolled, with no tool call syntax visible.

How often it shows depends on the model: the leak is intermittent because the same prompt also hands the narrator the finished results and tells it not to reroll. The underlying prompt defect is on every turn in this configuration. Roleplay mode is not affected.

Where it is

At staging ac14dd704:

  • packages/server/src/routes/generate.routes.ts:5240-5251, appendLocalEndpointTools. Its early return at :5241 is if (!toolsAttached || !definitions?.length || conn.treatAsLocalEndpoint !== "true") return;, with no check for a Game tool connection. It injects the <available_functions> block that instructs the model to emit <tool_call> blocks.
  • packages/server/src/routes/generate.routes.ts:7429-7432, the narration branch if (!responderToolsAttached || gameToolPlan), which routes narration through the streaming provider.chat generator.
  • packages/server/src/services/llm/providers/openai.provider.ts:1652-1654 and :1851-1853, the only two parseTextualToolCalls call sites, both inside chatComplete and both gated on options.tools?.length. The streaming generator at :1176 does not parse.

What's happening

The narrator's system prompt still gets the local endpoint instruction block telling it to write <tool_call> JSON, even though a separate connection is doing the tool work. When a Game tool connection is set, narration is streamed through the generator path, and that path is the one place that never parses textual tool calls. So the model is asked for a syntax that nothing downstream consumes, and whatever it emits is streamed straight into the saved message. Nothing on the server or the client strips it afterwards.

The path is doubly blocked: even if narration went through chatComplete here, the options object built at generate.routes.ts:6921-6944 carries no tools key, and both parser call sites require one.

Suggested fix

Stop injecting the block instead of trying to parse it, since on this path the tools deliberately belong to the planner connection. Skip appendLocalEndpointTools whenever a Game tool connection is set, which is a || gameToolConnection added to the existing early return at generate.routes.ts:5241. That mirrors the guard the same feature already applies to the native tool channel, tools: gameToolConnection ? undefined : responderToolDefs at :6743, :6783 and :6797. After the change, the narrator prompt should still carry <available_functions> with no Game tool connection set, and should not carry it when one is set.

Worth pinning with regression coverage: game-tool-controls.regression.ts uses a plain OpenAI narrator with no local endpoint flag, so it passes while this defect is live and would not catch a reintroduction.

Context

Introduced by the Game tool planning work in PR #5982, which implemented feature request #5957. The narration branch condition if (!toolsAttached || gameToolPlan) came in with commit 5e41bf8ff; before it, a local endpoint Game narrator with tools attached always went through chatComplete, where the injected block was parsed. This also means the textual tool call handling added for #2792 is inert on the new path. PR #6137 did not touch this area: its only Game tool connection changes are the Peek Prompt planner accounting for #6130, and both cited call sites are byte identical at the new head, only renumbered. Unreleased on staging, it landed after v2.4.4.

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

Originally created by @kolacheee on GitHub (Sep 13, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/6149 Originally assigned to: @SpicyMarinara on GitHub. In a Game chat where the narrator runs on a local model and a separate connection is chosen to handle tools, the Game Master's story text can come back with a literal `<tool_call>{"name": "roll_dice", "arguments": {...}}</tool_call>` block sitting in the middle of the prose. It stays in the saved message, so it is still there after a reload. The dice themselves still resolve correctly on the tool connection, so the leaked block is a stray duplicate of a roll that already happened, not a lost action. ## How to reproduce (try it yourself) 1. Set up two connections. First, an OpenAI-compatible connection pointed at a local backend (llama.cpp, Ollama, KoboldCpp, LM Studio) with "Treat as local endpoint" turned ON in its settings. Second, any connection whose provider supports native tool calls, for example an OpenAI or Gemini key. 2. Create a Game mode chat and set its connection to the local one. You do not need to switch Tool Use on, a Game chat attaches the dice tool by itself. 3. Control run first. Leave Chat Settings, Function Calling, "Game tool connection" unset. Send `I try to force the gate.` **What you'll see:** clean narration, a dice result, no raw JSON. This is the correct behavior. 4. Now open Chat Settings, Function Calling, and set "Game tool connection" to the tool-capable connection. 5. Send `I try to force the gate.` again, and repeat for a few turns if the first one comes back clean. **What you'll see:** on turns where the local model takes the bait, the saved Game Master message contains a literal `<tool_call>{"name": "roll_dice", "arguments": {"notation": "1d20"}}</tool_call>` string inside the story text. Reload the chat and it is still there. **What should happen:** the narration should contain only prose plus the dice result that the tool connection already rolled, with no tool call syntax visible. How often it shows depends on the model: the leak is intermittent because the same prompt also hands the narrator the finished results and tells it not to reroll. The underlying prompt defect is on every turn in this configuration. Roleplay mode is not affected. ## Where it is At staging ac14dd704: - `packages/server/src/routes/generate.routes.ts:5240-5251`, `appendLocalEndpointTools`. Its early return at :5241 is `if (!toolsAttached || !definitions?.length || conn.treatAsLocalEndpoint !== "true") return;`, with no check for a Game tool connection. It injects the `<available_functions>` block that instructs the model to emit `<tool_call>` blocks. - `packages/server/src/routes/generate.routes.ts:7429-7432`, the narration branch `if (!responderToolsAttached || gameToolPlan)`, which routes narration through the streaming `provider.chat` generator. - `packages/server/src/services/llm/providers/openai.provider.ts:1652-1654` and `:1851-1853`, the only two `parseTextualToolCalls` call sites, both inside `chatComplete` and both gated on `options.tools?.length`. The streaming generator at `:1176` does not parse. ## What's happening The narrator's system prompt still gets the local endpoint instruction block telling it to write `<tool_call>` JSON, even though a separate connection is doing the tool work. When a Game tool connection is set, narration is streamed through the generator path, and that path is the one place that never parses textual tool calls. So the model is asked for a syntax that nothing downstream consumes, and whatever it emits is streamed straight into the saved message. Nothing on the server or the client strips it afterwards. The path is doubly blocked: even if narration went through `chatComplete` here, the options object built at `generate.routes.ts:6921-6944` carries no `tools` key, and both parser call sites require one. ## Suggested fix Stop injecting the block instead of trying to parse it, since on this path the tools deliberately belong to the planner connection. Skip `appendLocalEndpointTools` whenever a Game tool connection is set, which is a `|| gameToolConnection` added to the existing early return at `generate.routes.ts:5241`. That mirrors the guard the same feature already applies to the native tool channel, `tools: gameToolConnection ? undefined : responderToolDefs` at `:6743`, `:6783` and `:6797`. After the change, the narrator prompt should still carry `<available_functions>` with no Game tool connection set, and should not carry it when one is set. Worth pinning with regression coverage: `game-tool-controls.regression.ts` uses a plain OpenAI narrator with no local endpoint flag, so it passes while this defect is live and would not catch a reintroduction. ## Context Introduced by the Game tool planning work in PR #5982, which implemented feature request #5957. The narration branch condition `if (!toolsAttached || gameToolPlan)` came in with commit 5e41bf8ff; before it, a local endpoint Game narrator with tools attached always went through `chatComplete`, where the injected block was parsed. This also means the textual tool call handling added for #2792 is inert on the new path. PR #6137 did not touch this area: its only Game tool connection changes are the Peek Prompt planner accounting for #6130, and both cited call sites are byte identical at the new head, only renumbered. Unreleased on staging, it landed after v2.4.4. Found by an AI-assisted sweep and re-verified against staging ac14dd704 on 2026-09-13. Please verify before fixing.
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:5654636802 --> @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#3422
No description provided.