Repository navigation
Conversation
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. |
commit: |
liruifengv
approved these changes
Nov 26, 2026
sailist
deleted the
bug-470-11-26-google-genai-abort-usage-release-11-26
branch
November 26, 2026 09:27
Merged
kimi-agent-bot
added a commit
that referenced
this pull request
Nov 26, 2026
kimi-agent-bot
added a commit
that referenced
this pull request
Nov 26, 2026
6 tasks done
sailist
pushed a commit
that referenced
this pull request
Nov 26, 2026
* chore(changeset): add product notes for #5515 and clarify #5516 * ci: release packages (#5519) Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --------- Co-authored-by: kimi-agent-bot <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Requirement or Bug
Resolve #5063
将已合入
main的 #5377(原始提交0d8816b8/ squash87011ba4)零冲突移植到release/2026-11-26,随 11-26 版本发布。使用 Gemini / Vertex(
google-genai协议)时,用户按 Esc 或调用loop.cancel()取消生成后,底层 HTTP 请求不会真正中断,仍在后台继续生成和计费;若流式响应卡住,请求会一直挂起到网络超时。同时,取消前已经收到的 token 用量因为没有和finishReason出现在同一个数据块而被丢弃。本 PR 使取消能立即关闭底层连接,并保留取消前已收到的用量。Bug Reproduction Steps
See linked issue.
补充的回归测试在修复前可稳定复现:
usageMetadata(无finishReason)的数据块后卡住,收到首个文本片段时触发取消,修复前测试超时且用量事件缺失;Root Cause
这是针对根因的修复(fundamental fix),不是绕过:
google-genai的send/sendStream只用Promise.race包裹 SDK 调用,没有设置params.config.abortSignal。Promise.race只让调用方停止等待;abortableStream只在收到下一个数据块后才检查signal.aborted,所以底层@google/genai的fetch从未收到取消信号。decode.ts要求同一数据块同时带有非空finishReason才调用sink.onUsage。Gemini 流式响应在每个数据块都返回累计usageMetadata,但只有最后一个数据块才带finishReason,中途取消时最后一个数据块收不到,此前的用量就全部丢失。Code Changes
以
release/2026-11-26基线(46d9085f,即 11-25 发版提交)移植0d8816b8,无冲突,patch-id 与 #5377 完全一致。传输层把取消信号透传给
@google/genaiSDK(SDK 会将其桥接到fetch的RequestInit.signal,且不会序列化进请求体);解码层在收到用量元数据时立即上报,与同目录 OpenAI / Anthropic 协议保持一致。Promise.race与abortableStream:@google/[email protected]在挂载abort监听前没有检查abortSignal.aborted,如果信号在调用前就已取消,SDK 仍会发出 HTTP 请求。保留Promise.race可以在预取消或 SDK 前置异步阶段(如 Vertex ADC 凭证解析)直接短路;abortableStream继续作为兜底,for await提前退出时会沿生成器链自动调用return()释放 reader 锁。usageMetadata是累计值,core 的mergeUsagePatch用最新值覆盖旧值,正常结束时最终结果不变,取消时则保留取消前最后一次收到的用量。.changeset/google-genai-abort-usage-core.md(@moonshot-ai/core: patch);fix(core): abort Google GenAI requests and forward streamed usage #5377 未进入 11-25 发版,release 分支上无重复条目。阅读顺序:先看
packages/core/src/llm/builtin/protocol/google-genai/index.ts(传输层取消透传),再看decode.ts(用量上报条件);abort.test.ts与usage-without-finish.ts为回归测试。Behavior Changes and Affected Users
aborted结束llm.delta.usage事件时机finishReason时才发出,中途取消时丢弃全部已收用量usageMetadata的数据块到达即发出(累计覆盖),取消结果保留已收用量TokenUsage数值不变)send/sendStream前signal已 aborted)Promise.race直接抛出AbortError受影响模块与测试覆盖:
packages/core/src/llm/builtin/protocol/google-genai/index.ts:由abort.test.ts覆盖首包后取消并保留用量、首包前卡住取消、非流式卡住取消三种真实 HTTP 场景,均断言服务端连接被关闭且请求体不含abortSignal。packages/core/src/llm/builtin/protocol/google-genai/decode.ts:由usage-without-finish.ts夹具覆盖无finishReason的多数据块累计用量上报,原有 decode / encode / transport 夹具全部保持通过。验证记录
usage-without-finish缺少 2 个usage事件)。corepack pnpm exec tsc -p packages/core/tsconfig.json --noEmit通过;corepack pnpm exec vitest run --reporter=agent packages/core/test:37 个测试文件、402 个用例全部通过。46d9085f):pnpm lint与pnpm run check:duplication全部通过,0 警告 0 错误。Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.