[PR #8585] [MERGED] fix(llm): retell tool history for endpoints that refuse the tool role #8686

Closed
opened 2026-11-07 17:38:55 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/8585
Author: @ylli-selmani
Created: 10/31/2026
Status: ✅ Merged
Merged: 10/31/2026
Merged by: @ylli-selmani

Base: staging ← Head: fix/provider-tool-history


📝 Commits (6)

  • 24d93ab fix(llm): retell tool history for endpoints that refuse the tool role
  • 061f9d1 fix(llm): log the shaped request and cover Anthropic's tool-role wording
  • 838246a fix(llm): refusal-anchored tool-role detector, alternated retold history
  • a9abf67 fix(llm): require the verdict to follow the quoted tool role
  • 39f7297 fix(llm): make the tool-role verdict a refusal, not a permission
  • a868f95 fix(llm): require a stated refusal, not a conditional complaint

📊 Changes

2 files changed (+369 additions, -16 deletions)

View changed files

📝 packages/server/src/services/llm/providers/openai.provider.ts (+114 -16)
➕ scripts/regressions/provider-tool-history-shape.regression.ts (+255 -0)

📄 Description

Why

A reporter runs Professor Mari on a LinkAPI connection typed OpenAI. LinkAPI serves Claude, so it translates
Chat Completions into Anthropic's Messages API. Mari's first tool round works — tools go out, the model returns
tool_calls, she reads the lorebook — and the next round dies:

One of Professor Mari's tools could not run.
OpenAI API error 429: messages: Unexpected role "tool". Allowed roles are "user", "assistant" or "system"

Anthropic's Messages API has only user and assistant; tool results must be content inside a user turn. So the
replayed {role: "tool"} row is refused. Because the proxy reports it as HTTP 429, it is also treated as a rate
limit and retried with backoff (two different request ids in the banner, "Worked for 24s") before the run fails.

This is the second, distinct half of #8564. #8576 fixed tools being dropped for uncatalogued models; tools are
now sent, which is exactly what exposes the history shape. Same connection type, so it is not Mari-specific: game
mode tools and agent tools fail on the same second round.

Related: #8584.

What changed

packages/server/src/services/llm/providers/openai.provider.ts

  • Detect the refusal: isToolRoleRejection matches the role/tool wording LinkAPI and Anthropic use. It only
    fires when the request that failed actually carried a tool row, so a plain rate limit is never reshaped.
  • Retry once, retold: anthropicSafeWireMessages gains retellToolRounds, which retells every tool round as
    text — calls folded into the assistant turn (its words are kept when it has them), results folded into one user
    message. toolRoleSafeWireMessages is the always-retell entry point. Native tools stay in the body.
  • Remember it: refusedToolHistories, per gateway and model, an hour on the monotonic clock, bounded — later
    rounds send the retold shape immediately instead of losing one request to the refusal every round. The existing
    reasoning-effort refusal store now shares the same read/write helpers.
  • Wire it in: the retry sits in postChatCompletions beside the role-alternation and Anthropic-shape retries,
    but is not gated on status 400 (the proxy's status is 429).

scripts/regressions/provider-tool-history-shape.regression.ts

  • A fixture accepts tools, returns tool_calls, and refuses any {role: "tool"} with that exact 429 body.
  • Covers: recovery through the retry (with tools still requested, words kept, results still naming the call),
    a Claude-looking and a plain proxy-alias model id, the remembered endpoint skipping the refusal on the next
    round, the memory being scoped per model, and a plain 429 that must still surface as a typed error after exactly
    one request.

Verification

  • provider-tool-history-shape.regression.ts: passes (pre-fix the same round was refused, proven with a throwaway
    fixture against unmodified staging before the change).
  • Provider family (--filter provider-): 8/8 passed, including provider-unknown-model-tools (#8576) and
    provider-compat.
  • pnpm check: exit 0.
  • Local CodeRabbit on 24d93ab80: three findings. One taken (debug log of the shaped request), one partly taken
    (the regression now also feeds Anthropic's own wording), two declined with code reasons:
    • remember the refusal when the 400/"anthropic" retry succeeds — that retry also fires for unsigned thinking
      and textless rounds, so it does not prove this endpoint refuses the tool role; the new branch already remembers
      when the body names the tool role.
    • make the fixture answer 400 instead of 429 — the 429 is the reported behaviour and the reason the existing
      400-gated retries never fire. The fixture keeps it.

Manual verification

  • Manually verify each item below in a browser or against a real endpoint.
  • Manually verify Professor Mari on an Anthropic-backed OpenAI-compatible connection: ask her to read a
    lorebook entry, confirm the first round works and the second round no longer fails with Unexpected role.
  • Manually verify a plain OpenAI-typed connection still sends role: "tool" and is not reshaped (debug log
    should show no tool-role retry).
  • Manually verify a normal rate limit on any connection still shows the usual rate-limit behaviour.

Known limit

The retold history loses tool_use/tool_result pairing, so a model may repeat a read. That is what this endpoint
can take, and it matches the path the provider already used for tool rounds with no words.


🔄 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/8585 **Author:** [@ylli-selmani](https://github.com/ylli-selmani) **Created:** 10/31/2026 **Status:** ✅ Merged **Merged:** 10/31/2026 **Merged by:** [@ylli-selmani](https://github.com/ylli-selmani) **Base:** `staging` ← **Head:** `fix/provider-tool-history` --- ### 📝 Commits (6) - [`24d93ab`](https://github.com/Pasta-Devs/Marinara-Engine/commit/24d93ab800670720dffd17de2df6444c6ae7dee1) fix(llm): retell tool history for endpoints that refuse the tool role - [`061f9d1`](https://github.com/Pasta-Devs/Marinara-Engine/commit/061f9d11dbb28aade3899f5c10d1651396b02d4c) fix(llm): log the shaped request and cover Anthropic's tool-role wording - [`838246a`](https://github.com/Pasta-Devs/Marinara-Engine/commit/838246ab46f0af8b23072c1002decfed8ed58dcd) fix(llm): refusal-anchored tool-role detector, alternated retold history - [`a9abf67`](https://github.com/Pasta-Devs/Marinara-Engine/commit/a9abf67686b7ad41df9e02f016523bcb89f39db8) fix(llm): require the verdict to follow the quoted tool role - [`39f7297`](https://github.com/Pasta-Devs/Marinara-Engine/commit/39f72975ee269914ac08665494cc0b4946c0654b) fix(llm): make the tool-role verdict a refusal, not a permission - [`a868f95`](https://github.com/Pasta-Devs/Marinara-Engine/commit/a868f95f2e2d233ce2fe2545cb74e3d73237daef) fix(llm): require a stated refusal, not a conditional complaint ### 📊 Changes **2 files changed** (+369 additions, -16 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/services/llm/providers/openai.provider.ts` (+114 -16) ➕ `scripts/regressions/provider-tool-history-shape.regression.ts` (+255 -0) </details> ### 📄 Description ## Why A reporter runs Professor Mari on a LinkAPI connection typed **OpenAI**. LinkAPI serves Claude, so it translates Chat Completions into Anthropic's Messages API. Mari's first tool round works — `tools` go out, the model returns `tool_calls`, she reads the lorebook — and the next round dies: ``` One of Professor Mari's tools could not run. OpenAI API error 429: messages: Unexpected role "tool". Allowed roles are "user", "assistant" or "system" ``` Anthropic's Messages API has only `user` and `assistant`; tool results must be content inside a user turn. So the replayed `{role: "tool"}` row is refused. Because the proxy reports it as HTTP 429, it is also treated as a rate limit and retried with backoff (two different `request id`s in the banner, "Worked for 24s") before the run fails. This is the second, distinct half of #8564. #8576 fixed tools being *dropped* for uncatalogued models; tools are now sent, which is exactly what exposes the history shape. Same connection type, so it is not Mari-specific: game mode tools and agent tools fail on the same second round. Related: #8584. ## What changed `packages/server/src/services/llm/providers/openai.provider.ts` - **Detect the refusal**: `isToolRoleRejection` matches the role/tool wording LinkAPI and Anthropic use. It only fires when the request that failed actually carried a `tool` row, so a plain rate limit is never reshaped. - **Retry once, retold**: `anthropicSafeWireMessages` gains `retellToolRounds`, which retells every tool round as text — calls folded into the assistant turn (its words are kept when it has them), results folded into one user message. `toolRoleSafeWireMessages` is the always-retell entry point. Native `tools` stay in the body. - **Remember it**: `refusedToolHistories`, per gateway and model, an hour on the monotonic clock, bounded — later rounds send the retold shape immediately instead of losing one request to the refusal every round. The existing reasoning-effort refusal store now shares the same read/write helpers. - **Wire it in**: the retry sits in `postChatCompletions` beside the role-alternation and Anthropic-shape retries, but is not gated on status 400 (the proxy's status is 429). `scripts/regressions/provider-tool-history-shape.regression.ts` - A fixture accepts `tools`, returns `tool_calls`, and refuses any `{role: "tool"}` with that exact 429 body. - Covers: recovery through the retry (with tools still requested, words kept, results still naming the call), a Claude-looking and a plain proxy-alias model id, the remembered endpoint skipping the refusal on the next round, the memory being scoped per model, and a plain 429 that must still surface as a typed error after exactly one request. ## Verification - `provider-tool-history-shape.regression.ts`: passes (pre-fix the same round was refused, proven with a throwaway fixture against unmodified `staging` before the change). - Provider family (`--filter provider-`): 8/8 passed, including `provider-unknown-model-tools` (#8576) and `provider-compat`. - `pnpm check`: exit 0. - Local CodeRabbit on `24d93ab80`: three findings. One taken (debug log of the shaped request), one partly taken (the regression now also feeds Anthropic's own wording), two declined with code reasons: - *remember the refusal when the 400/"anthropic" retry succeeds* — that retry also fires for unsigned thinking and textless rounds, so it does not prove this endpoint refuses the tool role; the new branch already remembers when the body names the tool role. - *make the fixture answer 400 instead of 429* — the 429 is the reported behaviour and the reason the existing 400-gated retries never fire. The fixture keeps it. ## Manual verification - [ ] Manually verify each item below in a browser or against a real endpoint. - [ ] Manually verify Professor Mari on an Anthropic-backed OpenAI-compatible connection: ask her to read a lorebook entry, confirm the first round works and the second round no longer fails with `Unexpected role`. - [ ] Manually verify a plain OpenAI-typed connection still sends `role: "tool"` and is not reshaped (debug log should show no tool-role retry). - [ ] Manually verify a normal rate limit on any connection still shows the usual rate-limit behaviour. ## Known limit The retold history loses `tool_use`/`tool_result` pairing, so a model may repeat a read. That is what this endpoint can take, and it matches the path the provider already used for tool rounds with no words. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-07 17:38:55 +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#8686
No description provided.