Repository navigation
fix(ai): keep Gemini requests from ending on a model turn - #64641
opencode-agent[bot] wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
The change looks sound. I ran the PR's Gemini tests against the base, and the trailing-assistant cases there still produce contents ending on a model turn. With this PR they end on a user turn. The Gemini test file passes (63 tests), the rest of the packages/ai suite passes, and tsc is clean. I couldn't check it against the real Gemini API.
Public API: unchanged.
Handling the tail inside the protocol matches what mistral-chat.ts already does for a trailing assistant message. Dropping a thought-only tail and adding Continue. at the end both keep the cached prefix intact, and a tail with function calls is correctly left alone. One gap remains: since the cause in #64628 isn't confirmed, a tail that ends on provider-executed function calls with no results would still fail. The session export should show whether that's the case there, so I've listed #64628 as related rather than fixed.
Fixes the user-visible error in #64628. The root cause in that session isn't confirmed yet.
Problem
Gemini rejects any request whose
contentsend on amodelturn:lowerMessagesinpackages/ai/src/protocols/gemini.tslowers whatever history it gets. When that history ends on an assistant message, the request goes out ending onmodel, and the session stops until the user prompts again. In #64628 it happened three times in one session, each time on the request right after a tool-calling step.Unanswered local tool calls are already answered with
Tool result missingbynormalizeToolHistory. So the tail that reaches Gemini is an assistant message with text or reasoning only.Fix
Before the request is sent,
lowerMessagesnow makes sure the contents end on a user turn:Continue., appended. It isn't persisted, and adding it at the end keeps the cached prefix intact.This only changes Gemini requests. Other protocols are unchanged.
Testing
bun test test/provider/gemini.test.ts(packages/ai): 63 pass. Adds tests for both tail shapes and updates two that ended on a model turn.bun test(packages/ai): all pass.bun run typecheck(packages/ai): no errors.oxlinton the changed files is clean.Requested by: Filip via Slack
Added by the opencode bot (AI-generated).
Related: #64628, #55694