mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[PR #5920] [MERGED] fix(providers): stream Gemini and Anthropic turns that carry tools #6990
Labels
No labels
android
bug
bugfix
ci
client
code freeze pause
container
dependencies
docs
documentation
duplicate
enhancement
feature
feature
feature
feature-creep
fixed-in-refactor
fixed-in-staging
good first issue
help wanted
in-pr
in-pr
needs-codeowner-decision
normal-scope
old-codebase
old-codebase
pull-request
qol
question
refactor
regression
revisit later
roadmap
scope-risk
scope-risk
server
server
shared
soup
upstream
windows
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Pasta-Devs/Marinara-Engine#6990
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/5920
Author: @kolacheee
Created: 9/7/2026
Status: ✅ Merged
Merged: 9/8/2026
Merged by: @kolacheee
Base:
staging← Head:fix/provider-tools-streaming📝 Commits (6)
a4046e2fix(providers): stream Gemini and Anthropic rounds that carry toolsa77e93eMerge remote-tracking branch 'origin/staging' into fix/provider-tools-streaminge2f58a1Merge remote-tracking branch 'origin/staging' into codex/review-pr-5920-sep8c615348fix(providers): release interrupted tool streams and report aborts6b8d40eMerge remote-tracking branch 'origin/staging' into codex/review-pr-5920-sep8836db41Merge remote-tracking branch 'origin/staging' into codex/review-pr-5920-sep8📊 Changes
5 files changed (+1006 additions, -43 deletions)
View changed files
📝
CHANGELOG.md(+3 -0)📝
e2e/core-flows.e2e.ts(+5 -0)📝
packages/server/src/services/llm/providers/anthropic.provider.ts(+187 -24)📝
packages/server/src/services/llm/providers/google.provider.ts(+178 -19)📝
scripts/regressions/provider-compat.regression.ts(+633 -0)📄 Description
Linked issue
Closes #5918
There is no linked issue yet. One should be opened before this PR is submitted — this is a provider-adapter bug with no existing report.
Two references, neither of which this PR closes:
Why this change
Both adapters abandoned streaming the moment a tool was attached. On a Gemini or Anthropic connection with any tool enabled, the reply bubble stayed empty for the entire generation and then filled all at once. The tool loop never asked for that — it passes a token sink and no streaming flag at all — so this was the adapters not listening, not the APIs refusing.
The regression users feel is time to first token, not chunk granularity: the token sink fans out through the same chunker either way, so the whole reply eventually arrives identically. What was lost was every second between "send" and "the first word appears", on turns that are already the slowest ones.
What changed
Each provider's
chatCompletenow carries a streaming branch beside its existing whole-response branch, mirroring the pattern the OpenAI adapter already ships.chat()is untouched on purpose: swapping its serializer would change the wire shape of every ordinary streaming turn on these connections, including the Anthropic string-content shape that prompt caching keys its prefixes on. The textual fallback parser, the local-endpoint prompt injection and the two subscription providers are equally untouched.Gemini. A
?alt=ssestreaming URL, built the same way the existing streaming path builds its own. Tool calls are collected through the adapter's existing part converter with an index that runs across frames, so two calls that arrive without ids cannot collide on a synthesized one. The empty-response throw, ported as-is, would have ended a round that is nothing but a tool call and legitimately carries no prose, so the streamed branch clears that guard on tool calls as well — worth stating precisely, because it is easy to misread as a user-facing bug being fixed: it never was one. The existing buffered branch already counted tool calls as output, so nothing shipped ever threw there; this is a porting hazard the new branch had to avoid, and the regression covers it as a property of the new path.Gemini with thinking still buffers. That rule exists because some proxies strip thought parts out of SSE streams and return them whole in a non-streaming response. It predates tools, it is deliberate, and it survives this PR untouched, pinned by an assertion. If a live Gemini connection runs with thinking or a reasoning effort set, this change does nothing for it — the honest framing is that it removes the penalty for everyone else.
Anthropic.
stream: useStreamreplaces the hardcodedfalse, and the fetch's buffering flag follows it, because the buffered fetch mode drains the whole body before the provider can read a byte — a streamed request that still asked for buffering would parse the same frames and hand them over all at once anyway. Tool input is accumulated from the partial-JSON deltas keyed by content-block index: parallel tool calls are legal (the Engine never disables them), so the single current-block slot the non-tool path uses would merge two calls into one. The empty-text guard is the existing one with a tool-calls term added, because it leans on a stop reason arriving and a tool-only round need not send one.One pre-existing bug carried forward, not created. The tools path never returned provider metadata, so there were no stored Gemini parts to replay and a multi-round Gemini 3 turn lost its thought signatures. Both branches now return the reconstructed parts, which the tool loop replays onto the assistant message; a tool-call part keeps its own signature rather than having it moved onto the text part. This was not free, and the earlier design notes that called it a five-line freebie were wrong: adding the replay meant the call-id-to-name map had to move above the replay branch, which returns early. Without that move, a tool result falls back to the literal name
tool_result, and Gemini pairs a result to its call by name — so the model would read a reply attributed to a function nobody called. The move and a regression case that runs a genuine second round are both in this PR.The conservative gate, and every caller's behavior
Streaming is gated on
!!options.onToken && options.stream !== falserather than the base formula (options.stream ?? !!options.onToken). The difference matters for exactly one caller shape: a caller that sets a streaming flag but supplies no token sink of its own keeps its buffered path.Every
chatCompleteconsumer was swept; only two pass tools at all:generate.routes.tsagent-executor.tsProviders that cannot do native tool calls are structurally untouched: the two subscription providers hardcode tools away and keep the base streaming path, and the local sidecar delegates to an internal OpenAI provider. The OpenAI family already streamed with tools attached and is not edited.
What this PR does not claim: that tool calls are now cheaper than a bracket tag. They are not, on any provider — a tool round is an extra full round-trip and extra input tokens, always. What it removes is a penalty on top of that, which was an adapter gap rather than physics. The win is correctness in one beat, not efficiency.
Validation
pnpm checkpasses locallyCONTRIBUTING.mdManual verification notes
The live smokes are the merge gate. Everything else is secondary.
pnpm checkand confirm it exits clean.node ./scripts/run-regressions.mjs --filter provider-compatand confirm it passes.What the offline regression proves.
provider-compat.regression.tsdrives both adapters against stub servers: streamed tool-call parsing per provider, a round that is nothing but a tool call, prose arriving before a tool call, tools attached but never used still streaming, the Gemini thinking carve-out staying buffered, the no-sink gate staying buffered, partial tool-argument JSON split mid-key across frames and across network chunks, interleaved parallel tool blocks staying bound to their own indexes, a genuine second round asserting the tool result is named for the function that produced it, two id-less calls taking distinct synthesized ids, an Anthropic tool call with no stop reason behind it, and a stub that withholds the tail of its stream until the first token lands, so a buffered read fails the assertion instead of passing quietly. Fourteen surgical reverts of the changed code were each confirmed to fail it.What it cannot prove. A stub proves this parser against our own fixture — not Google's and Anthropic's actual emission. The two riskiest unknowns are precisely the ones a stub is structurally unable to answer: whether Gemini ever splits a single tool call across frames, and whether
?alt=sseplus tools survives a proxy path. Hence the live smokes.Two unrelated suite failures, so they are not a surprise. A full run of the regression suite comes back 207 of 209. Both failures reproduce identically with this PR's four files reverted to the parent commit: one gallery lane passes its own assertions and then dies removing a Windows temp directory at teardown, and the launcher format guard dies in a heap probe. Neither touches providers.
Docs and release impact
docs-i18nbranch updated to match, or a[docs-i18n]follow-up issue opened (see CONTRIBUTING.md § Translated documentation)What this PR actually does, for the reviewer to confirm against the boxes:
docs/prose changes, so nothing to mirror ondocs-i18n.UI evidence (if applicable)
The visible difference is timing, not layout, so a screenshot cannot show it. If the live smokes are recorded, one short clip per provider of a tools-attached turn filling in progressively is the evidence worth attaching — a before/after pair is better still, since the "before" is a bubble that sits empty for the whole generation.
Known limitations
{}rather than as an error. The old buffered path failed its response parse instead, and the OpenAI streaming path keeps the truncated string. Narrow (it needs a server that closes cleanly mid-fragment) and not fixed here; a warning when a non-empty accumulation fails to parse would be the smallest honest improvement.Summary by CodeRabbit
Bug Fixes
Documentation
Maintainer staging integration
Updated against current
staging; fixed both CodeRabbit findings inc61534831. Both adapters release their upstream stream on provider errors and rejected token callbacks and returnabortfor an interrupted tool turn. Six real local-HTTP cases cover those outcomes in both adapters; the cleanup case failed before the patch and the complete suite passes after it.pnpm check, provider compatibility, prompt and context-fit regressions pass locally. The old mobile-WebKit failure was the swipe theme assertion comparing retained Oklab transition serialization with the settled theme color. The fixture now disables only the input transition for its static color comparison, preserving all color/size/navigation assertions; the focused WebKit test passed three consecutive runs. No provider UI was changed for that fixture repair.The explicitly requested live Gemini and Anthropic smoke tests remain unverified; no paid-provider result is claimed here. The maintainer has been asked whether those have already been run or which test connections may be used. All human validation boxes remain unchecked.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.