Repository navigation
fix(agent-core-v2): preserve Gemini context across turns - #4827
Conversation
🦋 Changeset detectedLatest commit: 7a9a01b The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 445fc543d3
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| function isBlankText(part: ContentPart): boolean { | ||
| return part.type === 'text' && part.text.trim().length === 0; | ||
| return part.type === 'text' && | ||
| part.meta?.['thought_signature_b64'] === undefined && |
There was a problem hiding this comment.
Drop Gemini-only blank parts before non-Gemini lowering
When a user switches from Gemini to an Anthropic-backed model after a Gemini tool turn containing the now-preserved { text: '', thoughtSignature: ... } part, this provider-agnostic exemption (also added in message.ts:165 and structure.ts:15-18) keeps that empty block in the projected history. anthropic/lower.ts:183-184 then serializes it as { type: 'text', text: '' }, which Anthropic rejects, whereas this history worked before because blank text was removed. Keep the signature carrier in stored history for Gemini replay, but omit it when lowering to protocols that cannot represent it.
Useful? React with 👍 / 👎.
commit: |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 445fc543d3
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| parts.push(thinkPart); | ||
| } else if (p['text']) { | ||
| parts.push({ type: 'text', text: p['text'] as string }); | ||
| } else if (typeof p['text'] === 'string') { |
There was a problem hiding this comment.
Exclude blank text carriers from success detection
When Gemini returns only empty text part(s), with or without a signature, this branch leaves message.content nonempty. emptyResponseError() then skips its empty-array case, and because there is no think-only content, settleLlmRequest() reports done with no visible output instead of returning an empty_response; keep signed carriers in replay history but exclude blank text when checking for usable output.
AGENTS.md reference: AGENTS.md:L112-L112
Useful? React with 👍 / 👎.
…on responses Skip blank text when lowering for Anthropic so signed Gemini parts do not produce empty text blocks after a model switch.
|
@codex review |
|
Codex Review: Didn't find any major issues. What shall we delve into next? Reviewed commit: ℹ️ About Codex in GitHubCodex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback". |
Requirement or Bug
使用 Gemini 进行多轮工具调用时,Kimi Code 没有完整保留模型返回的思考签名、原生调用 ID 和多模态工具响应结构。历史再次发送给 Gemini 时会丢失这些上下文。
本 PR 让新产生的 Gemini 历史可以跨请求、进程重启和会话回放保留这些信息,并按 Gemini 3 及更新版本支持的响应结构回传工具图片。
Bug Reproduction Steps
thoughtSignature的普通文本或空文本,再返回带原生 ID 的functionCall。{ text: "", thoughtSignature: "..." }。functionCall.id与对应functionResponse.id相同。functionResponse.parts中。修复前,文本和空文本签名被丢弃,调用 ID 不回传,工具图片放在函数响应外部。持久化工具事件还会丢失已解析的原生 ID。
Root Cause
这是根本修复,覆盖从协议解码、消息保存到再次编码的完整链路:
functionResponse.parts。Code Changes
核心思路是保留服务端上下文的归属和位置,内部工具执行 ID 与 Gemini 原生 ID 分别保存。无需新增公开消息类型或配置。
三个关键决定:
rawId只用于 Gemini 出站协议;内部工具执行、排序和本地结果关联继续使用已有 ID。parts结构。阅读顺序:
packages/agent-core-v2/src/human/llm/builtin/protocol/google-genai/decode.ts:签名与原生 ID 的接收。packages/agent-core-v2/src/human/llm/message.ts:签名 part 的流式边界与空内容判断。packages/agent-core-v2/src/agent/loop/journal.ts及回放相关文件:工具原生 ID 的保存与恢复。packages/agent-core-v2/src/human/llm/builtin/protocol/google-genai/lower.ts:下一轮请求中的签名、ID 和媒体布局。最小示例:
{ "role": "model", "parts": [ - { "text": "ORCHID-426" }, - { "functionCall": { "name": "get_weather", "args": { "city": "sf" } } } + { "text": "ORCHID-426", "thoughtSignature": "sig-text" }, + { "text": "", "thoughtSignature": "sig-end" }, + { + "functionCall": { "id": "call_weather", "name": "get_weather", "args": { "city": "sf" } }, + "thoughtSignature": "sig-tool" + } ] }{ "functionResponse": { "id": "call_weather", "name": "get_weather", "response": { "output": "sunny" }, "parts": [{ "inlineData": { "mimeType": "image/png", "data": "..." } }] } }Behavior Changes and Affected Users
functionCall.id的 Gemini 端点functionResponse.parts;其他类型(GIF、音频、视频、URL 引用)和 2.5 保持外置parts测试覆盖:
验证结果与范围
agent-core-v2全量:374 个测试文件、7830 个测试通过。agent-core-v2类型检查与包构建通过。lint:imports有 4 个既有违规,位于fullCompactionService.ts、completionRestore.ts、taskOutput.ts、human/agent/origin.ts,均不在本 PR 的 diff 中。Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.