Repository navigation
Description
What version of Kimi Code is running?
2.6.3 (source main snapshot, 2026-11-18). Confirmed by reading the code paths below and by checking the installed @google/[email protected] package.
Which open platform/subscription were you using?
None involved (code-level audit).
Which model were you using?
None involved (code-level audit). The issue is specific to the google-genai protocol (Gemini and Vertex).
What platform is your computer?
Windows 11 x64.
What issue are you seeing?
The google-genai protocol never forwards the request abort signal to the SDK, so cancelling a Gemini/Vertex request does not abort the underlying HTTP request. Streaming is worse: a stalled stream cannot be cancelled until the next chunk arrives.
packages/core/src/llm/builtin/protocol/google-genai/index.ts:111-124:
send: async (client, request, signal) => {
const response = await Promise.race([
modelsOf(client).generateContent(request.params),
abortPromise(signal),
]);
...
},
sendStream: async (client, request, signal) => {
const stream = await Promise.race([
modelsOf(client).generateContentStream(request.params),
abortPromise(signal),
]);
return { body: abortableStream(stream, signal) as AsyncIterable<unknown> };
},Promise.race only stops waiting. The params built in lower.ts:339 never set config.abortSignal, and abortableStream (index.ts:94-104) checks signal.aborted only after the next chunk is pulled:
for await (const chunk of stream) {
if (signal.aborted) throw createAbortException();
yield chunk;
}The installed SDK already supports cancellation:
GenerateContentConfig.abortSignal?: AbortSignalin@google/genai/dist/genai.d.ts:4345.generateContentInternal/generateContentStreamInternalforward it torequest()/requestStream()in@google/genai/dist/node/index.cjs(around lines 14950 and 15017).
So after Esc / loop.cancel(), the request keeps streaming and billing in the background, and a stalled response cannot be interrupted at all.
A second, related gap: streamed usage is only forwarded when the same chunk also carries a finish reason. packages/core/src/llm/builtin/protocol/google-genai/decode.ts:118-121:
const usage = parseUsage(response);
if (usage !== undefined && rawFinish !== undefined && rawFinish !== null) {
sink.onUsage(usage);
}The other protocols forward usage as soon as it appears (openai decode.ts:323-326, anthropic decode.ts:166-173). With the gate, an aborted Gemini stream (exactly the cancellation case above) drops every usageMetadata it did receive, so the request contributes no usage and no measured token anchor.
What steps can reproduce the bug?
- Configure a
google-genaimodel. - Start a long streaming response and press Esc while it is generating.
- Observe that the UI turn ends after the abort grace period, but the HTTP request keeps running (and billing) instead of being closed.
- For a stalled provider, start a stream that stops sending chunks and then cancel:
abortableStreamnever runs, so the request is held until a network timeout. - Inspect the request usage for the cancelled turn: no usage is recorded from any
usageMetadatareceived before the cancellation.
What is the expected behavior?
Cancellation should close the HTTP request, not just stop the caller from waiting. The abort signal should be passed to the SDK (config.abortSignal) so both generateContent and generateContentStream abort the transport, and the stream should be closed on abort. Usage received before an abort should still be forwarded to the caller.
Additional information
Suggested fixes:
- In both
sendandsendStream, pass{ ...request.params, config: { ...request.params.config, abortSignal: signal } }to the SDK.abortableStreamcan stay as a belt-and-braces check, but the SDK is what actually cancels the transport. - On abort, close the stream (
stream.return()) so the response body is released deterministically. - In
decode.ts, callsink.onUsage(usage)wheneverparseUsagereturns a value. Gemini'susageMetadatais cumulative, andmergeUsagePatchkeeps the latest value, so the finish-reason gate is not needed. - Optionally treat a normalized
NO_FINISHfinish (nullfinishReasonand nullrawFinishReason) as missing for streaming insettleLlmRequest, so the existing "stream ended without a finish event" check is not bypassed bysink.onFinish(NO_FINISH).
Contribution
- I am willing to submit a PR for this bug fix myself (please wait for maintainer approval in this issue first)
Activity
vastStage commentedon Nov 18, 2026
I confirmed both points with temporary failing tests against this snapshot (2.6.3 source,
main), then reverted them. No network or credentials are needed.Abort signal not forwarded
Observed:
AssertionError: expected undefined to be AbortSignal { aborted: false }.The SDK receives
configwithoutabortSignal, while@google/[email protected]forwardsconfig.abortSignaltorequestStreamingenerateContentStreamInternal(dist/node/index.cjs). So the race only abandons the caller; the HTTP request is never told to stop.Usage dropped without a finish reason
Observed:
DECODER_EVENTS_WITHOUT_FINISH []— neitherusagenorfinishis emitted. For contrast, the openai decoder emits usage as soon asparseUsagereturns a value, so an aborted request still contributes the input tokens it received.I will include both regression tests in the PR.
vastStage commentedon Nov 18, 2026
Live stream observation from a real Google GenAI endpoint (
gemini-4-argon, 1M context). Request: "Count from 1 to 40, one number per line",maxOutputTokens: 200, then parse the SSE chunks:finishReasonusageMetadatapromptTokenCount=18,candidatesTokenCount=4promptTokenCount=18,candidatesTokenCount=20promptTokenCount=18,candidatesTokenCount=44promptTokenCount=18,candidatesTokenCount=68promptTokenCount=18,candidatesTokenCount=92promptTokenCount=18,candidatesTokenCount=110STOPpromptTokenCount=49,candidatesTokenCount=114Summary:
usageMetadatawas present on 7/7 chunks;finishReasononly on 1/7. The decoder atpackages/core/src/llm/builtin/protocol/google-genai/decode.ts:118-121requires both on the same chunk before callingsink.onUsage, so:This makes the gate consequential in real traffic, not just a theoretical asymmetry.
For the abort-forwarding part, the SDK evidence is in the issue body (
GenerateContentConfig.abortSignalis forwarded torequestStream), and the failing unit test in the previous comment shows Kimi never sets it. A transport-level live observation would need a proxy or a local mock server to watch when the HTTP request actually closes; I can add that as an integration test in the PR if you prefer.I also confirmed a real
kimi -pturn works end-to-end over thisgoogle-genaiendpoint (Reply with exactly OK->OK), so the protocol path itself is live-verified.