Skip to content

Commit 13cb03a

Browse files
easonLiangWorldedtecheasonliang28easonLiangWorldedtech
authored
fix(openrouter): profile moonshotai/kimi-k3 with truthful max_tokens and reasoning effort (Zoo-Code-Org#1325)
* fix(openrouter): profile moonshotai/kimi-k3 with truthful max_tokens and reasoning effort OpenRouter reports max_completion_tokens: null for moonshotai/kimi-k3, so the generic fallback fabricated max_tokens = ceil(context_length * 0.2) (209,716 for a 1M window), and no reasoning effort was ever populated so the reasoning field was dropped from requests. Long turns then burned the upstream 32,768-token output cap on invisible thinking billed as completion tokens. Add a Moonshot K3 capability profile (maxTokens 32768, supportsReasoningEffort [low, high, max] with model default high, supportsTemperature false) applied in parseOpenRouterModel at fetch time and re-applied in OpenRouterHandler.getModel() at consumption time, so users with stale cached model info are fixed without a re-fetch. The default request body becomes {max_tokens: 32768, reasoning: {effort: high}} with temperature omitted (K3 is fixed at 1.0 server-side). Adds parse-time, stale-cache, and createMessage wire tests, plus shouldUseReasoningEffort cases for array capability with a model default effort. * fix(openrouter): address PR 1325 review feedback for Kimi K3 - Send explicit temperature: 1.0 for Moonshot K3 via OpenRouter instead of omitting it: issue Zoo-Code-Org#1316's expected result requires the request to carry temperature: 1.0, matching the direct Moonshot provider profile (defaultTemperature: 1.0, "temperature is fixed at 1.0"). - Add a handler-level regression test for the openRouterSpecificProvider endpoint branch: a stale endpoint record (fabricated max_tokens, boolean supportsReasoningEffort) is corrected at consumption time (CodeRabbit pre-merge check: regression evidence for the endpoint path). - Restore OpenAI.prototype.chat in the Kimi K3 wire test via try/finally so the stub cannot leak into later tests; clearAllMocks does not undo prototype assignment (CodeRabbit actionable comment). * Keep Moonshot K3 profile literals inside applyOpenRouterMoonshotK3Profile The mutation-diff gate (Stryker 10 + Vitest 4.1) never activates static mutants, so module-scope literals report as surviving mutants. Move the K3 model ids and profile values into the function body where the existing tests kill every resulting mutant, and drop the OPENROUTER_MOONSHOT_K3_MODELS export that had no consumers. * fix(openrouter): match the tilde-prefixed OpenRouter Kimi alias id The OpenRouter catalogue identifier for the rolling Kimi alias is ~moonshotai/kimi-latest - the ~ prefix is part of the id and is what reaches applyOpenRouterMoonshotK3Profile as modelId. The previous bare moonshotai/kimi-latest entry never matched, so the alias case from issue Zoo-Code-Org#1316 was left unprofiled. * fix(openrouter): use a direct comparison for the K3 profile guard Replace the per-call Set allocation in applyOpenRouterMoonshotK3Profile with the same direct-comparison shape as the gpt-6-astra guard in parseOpenRouterModel (review feedback: the Set was allocated on every non-K3 call before the early return). Add direct unit tests covering both catalogue ids (including the tilde-prefixed rolling alias ~moonshotai/kimi-latest) and the non-K3 pass-through, so the guard's mutants stay killed at the lowest layer. * test(shared): cover a user-selected effort outside the K3 ladder Review feedback: a mutation dropping the .includes() ladder check in shouldUseReasoningEffort would survive the existing suite (which only covered the default-effort -> true and enableReasoningEffort: false -> false directions). Assert that a user-selected effort ("medium") not present in the K3 capability ladder (["low", "high", "max"]) returns false, both with and with an explicit enableReasoningEffort: true. --------- Co-authored-by: Eason Liang <[email protected]> Co-authored-by: easonLiangWorldedtech <[email protected]>
1 parent 169d141 commit 13cb03a

5 files changed

Lines changed: 402 additions & 4 deletions

File tree

‎src/api/providers/__tests__/openrouter.spec.ts‎

Lines changed: 183 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ import { Anthropic } from "@anthropic-ai/sdk"
1818
import OpenAI from "openai"
1919

2020
import { providerIdentifiers } from "@roo-code/types"
21+
import type { ModelInfo } from "@roo-code/types"
22+
23+
import { applyOpenRouterMoonshotK3Profile } from "../fetchers/openrouter"
2124

2225
import { OpenRouterHandler } from "../openrouter"
2326
import { Package } from "../../../shared/package"
@@ -43,6 +46,14 @@ vitest.mock("@roo-code/telemetry", () => ({
4346
},
4447
}))
4548

49+
const { mockGetModelEndpoints } = vitest.hoisted(() => ({
50+
mockGetModelEndpoints: vitest.fn(),
51+
}))
52+
53+
vitest.mock("../fetchers/modelEndpointCache", () => ({
54+
getModelEndpoints: mockGetModelEndpoints,
55+
}))
56+
4657
vitest.mock("../fetchers/modelCache", () => ({
4758
getModels: vitest.fn().mockImplementation(function () {
4859
return Promise.resolve({
@@ -101,6 +112,19 @@ vitest.mock("../fetchers/modelCache", () => ({
101112
excludedTools: ["existing_excluded"],
102113
includedTools: ["existing_included"],
103114
},
115+
// Stale cache record simulating what users cached before the Moonshot K3
116+
// profile existed: the fabricated 0.2 context-window max_tokens and a
117+
// boolean supportsReasoningEffort with no default effort.
118+
"moonshotai/kimi-k3": {
119+
maxTokens: 209716,
120+
contextWindow: 1000000,
121+
supportsImages: true,
122+
supportsPromptCache: true,
123+
inputPrice: 0.6,
124+
outputPrice: 3,
125+
description: "Kimi K3",
126+
supportsReasoningEffort: true,
127+
},
104128
})
105129
}),
106130
refreshModels: vitest.fn(async (options, handler) => {
@@ -115,7 +139,13 @@ describe("OpenRouterHandler", () => {
115139
openRouterModelId: "anthropic/claude-sonnet-4",
116140
})
117141

118-
beforeEach(() => clearAllMocks())
142+
beforeEach(() => {
143+
clearAllMocks()
144+
// Endpoint records default to "not fetched" (same as the real guard for
145+
// missing options); tests that exercise the specific-provider branch
146+
// override this per test.
147+
mockGetModelEndpoints.mockResolvedValue({})
148+
})
119149

120150
it("initializes with correct options", () => {
121151
const handler = new OpenRouterHandler(mockOptions)
@@ -237,8 +267,122 @@ describe("OpenRouterHandler", () => {
237267
expect(result.info.excludedTools).toBeUndefined()
238268
expect(result.info.includedTools).toBeUndefined()
239269
})
270+
271+
it("applies the Moonshot K3 profile to stale cached model info", async () => {
272+
const handler = new OpenRouterHandler(
273+
makeApiHandlerOptions({
274+
openRouterApiKey: "test-key",
275+
openRouterModelId: "moonshotai/kimi-k3",
276+
}),
277+
)
278+
279+
const result = await handler.fetchModel()
280+
281+
// The stale cache record carried a fabricated max_tokens (209716) and a
282+
// boolean supportsReasoningEffort with no default effort; the profile must
283+
// correct all of that before any request parameters are derived.
284+
expect(result.id).toBe("moonshotai/kimi-k3")
285+
expect(result.maxTokens).toBe(32768)
286+
expect(result.temperature).toBe(1)
287+
expect(result.reasoningEffort).toBe("high")
288+
expect(result.reasoning).toEqual({ effort: "high" })
289+
expect(result.info.maxTokens).toBe(32768)
290+
expect(result.info.supportsReasoningEffort).toEqual(["low", "high", "max"])
291+
expect(result.info.reasoningEffort).toBe("high")
292+
expect(result.info.supportsTemperature).toBe(true)
293+
expect(result.info.defaultTemperature).toBe(1)
294+
})
295+
296+
it("applies the Moonshot K3 profile to a stale specific-provider endpoint record", async () => {
297+
// Endpoint records are cached separately from the parent model cache and
298+
// are selected before the profile is applied. A stale endpoint record
299+
// (fabricated max_tokens, boolean supportsReasoningEffort, no reasoning
300+
// default, no temperature default) must be corrected for the selected
301+
// endpoint.
302+
mockGetModelEndpoints.mockResolvedValue({
303+
moonshotai: {
304+
maxTokens: 209716,
305+
contextWindow: 1000000,
306+
supportsImages: true,
307+
supportsPromptCache: true,
308+
inputPrice: 0.6,
309+
outputPrice: 3,
310+
description: "Kimi K3 (Moonshot endpoint)",
311+
supportsReasoningEffort: true,
312+
},
313+
})
314+
315+
const handler = new OpenRouterHandler(
316+
makeApiHandlerOptions({
317+
openRouterApiKey: "test-key",
318+
openRouterModelId: "moonshotai/kimi-k3",
319+
openRouterSpecificProvider: "moonshotai",
320+
}),
321+
)
322+
323+
const result = await handler.fetchModel()
324+
325+
// The selected endpoint record (not the parent model cache entry) must be
326+
// profiled: a regression that applied the profile before endpoint
327+
// selection would send the stale endpoint values.
328+
expect(result.id).toBe("moonshotai/kimi-k3")
329+
expect(result.info.maxTokens).toBe(32768)
330+
expect(result.info.supportsReasoningEffort).toEqual(["low", "high", "max"])
331+
expect(result.info.reasoningEffort).toBe("high")
332+
expect(result.info.supportsTemperature).toBe(true)
333+
expect(result.info.defaultTemperature).toBe(1)
334+
expect(result.maxTokens).toBe(32768)
335+
expect(result.temperature).toBe(1)
336+
expect(result.reasoningEffort).toBe("high")
337+
expect(result.reasoning).toEqual({ effort: "high" })
338+
})
240339
})
241340

341+
describe("applyOpenRouterMoonshotK3Profile", () => {
342+
// Stale cache record shape: fabricated 0.2 context-window max_tokens and a
343+
// boolean supportsReasoningEffort with no default effort (pre-profile).
344+
const staleK3Record: ModelInfo = {
345+
maxTokens: 209_716,
346+
contextWindow: 1_000_000,
347+
supportsImages: true,
348+
supportsPromptCache: true,
349+
inputPrice: 0.6,
350+
outputPrice: 3,
351+
description: "Kimi K3 (stale cache)",
352+
supportsReasoningEffort: true,
353+
}
354+
355+
it("applies the profile to the exact moonshotai/kimi-k3 id", () => {
356+
const result = applyOpenRouterMoonshotK3Profile("moonshotai/kimi-k3", { ...staleK3Record })
357+
358+
expect(result.maxTokens).toBe(32_768)
359+
expect(result.supportsReasoningEffort).toEqual(["low", "high", "max"])
360+
expect(result.reasoningEffort).toBe("high")
361+
expect(result.supportsTemperature).toBe(true)
362+
expect(result.defaultTemperature).toBe(1.0)
363+
// Unprofiled fields pass through from the (stale) record
364+
expect(result.contextWindow).toBe(1_000_000)
365+
expect(result.description).toBe("Kimi K3 (stale cache)")
366+
})
367+
368+
it("applies the profile to the tilde-prefixed rolling alias", () => {
369+
const result = applyOpenRouterMoonshotK3Profile("~moonshotai/kimi-latest", { ...staleK3Record })
370+
371+
// The catalogue keeps the ~ prefix on the rolling alias id, so the profile
372+
// must recognize the exact string rather than a stripped or bare alias
373+
expect(result.maxTokens).toBe(32_768)
374+
expect(result.supportsReasoningEffort).toEqual(["low", "high", "max"])
375+
expect(result.reasoningEffort).toBe("high")
376+
expect(result.supportsTemperature).toBe(true)
377+
expect(result.defaultTemperature).toBe(1.0)
378+
})
379+
380+
it("leaves non-K3 model records untouched", () => {
381+
const record: ModelInfo = { ...staleK3Record }
382+
383+
expect(applyOpenRouterMoonshotK3Profile("moonshotai/kimi-k2-thinking", record)).toBe(record)
384+
})
385+
})
242386
describe("createMessage", () => {
243387
it("generates correct stream chunks", async () => {
244388
const handler = new OpenRouterHandler(mockOptions)
@@ -546,6 +690,44 @@ describe("OpenRouterHandler", () => {
546690
expect(endChunks[0].id).toBe("call_openrouter_test")
547691
})
548692

693+
it("sends profiled max_tokens, explicit temperature 1.0, and reasoning effort for moonshotai/kimi-k3", async () => {
694+
const handler = new OpenRouterHandler(
695+
makeApiHandlerOptions({
696+
openRouterApiKey: "test-key",
697+
openRouterModelId: "moonshotai/kimi-k3",
698+
}),
699+
)
700+
701+
const mockStream = asyncStreamFrom([{ id: "test-id", choices: [{ delta: { content: "ok" } }] }])
702+
const mockCreate = vitest.fn().mockResolvedValue(mockStream)
703+
const chatStub = { completions: { create: mockCreate } }
704+
// The vitest-mocked OpenAI class is structurally incompatible with the narrow
705+
// chat stub; the double assertion routes through unknown (instead of any) to
706+
// keep this file's no-explicit-any budget flat.
707+
const openAiPrototype = OpenAI as unknown as { prototype: { chat?: unknown } }
708+
const originalChat = openAiPrototype.prototype.chat
709+
openAiPrototype.prototype.chat = chatStub
710+
711+
try {
712+
const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "test message" }]
713+
await collectStream(handler.createMessage("test system prompt", messages))
714+
715+
const [requestParams] = mockCreate.mock.calls[0] as [Record<string, unknown>]
716+
expect(requestParams).toMatchObject({
717+
model: "moonshotai/kimi-k3",
718+
max_tokens: 32768,
719+
temperature: 1,
720+
reasoning: { effort: "high" },
721+
})
722+
// K3 is fixed at temperature 1.0 upstream (issue #1316); the request
723+
// must carry it explicitly.
724+
} finally {
725+
// Restore the original prototype property so this stub cannot leak
726+
// into later tests; clearAllMocks does not undo prototype assignment.
727+
openAiPrototype.prototype.chat = originalChat
728+
}
729+
})
730+
549731
it("emits completion only for identified calls and clears completed IDs", async () => {
550732
const toolCall = (id?: string) => ({
551733
id: "stream",

‎src/api/providers/fetchers/__tests__/openrouter.spec.ts‎

Lines changed: 120 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@ import * as path from "path"
44

55
import { back as nockBack } from "nock"
66

7-
import { getOpenRouterModelEndpoints, getOpenRouterModels, parseOpenRouterModel } from "../openrouter"
7+
import type { ModelInfo } from "@roo-code/types"
8+
9+
import {
10+
applyOpenRouterMoonshotK3Profile,
11+
getOpenRouterModelEndpoints,
12+
getOpenRouterModels,
13+
parseOpenRouterModel,
14+
} from "../openrouter"
815

916
nockBack.fixtures = path.join(__dirname, "fixtures")
1017
nockBack.setMode("lockdown")
@@ -550,6 +557,63 @@ describe("OpenRouter API", () => {
550557
expect(result.contextWindow).toBe(128000)
551558
})
552559

560+
it("applies the Moonshot K3 profile for moonshotai/kimi-k3", () => {
561+
const mockModel = {
562+
name: "Kimi K3",
563+
description: "Test model",
564+
context_length: 1000000,
565+
max_completion_tokens: null,
566+
pricing: {
567+
prompt: "0.0000006",
568+
completion: "0.000003",
569+
},
570+
}
571+
572+
const result = parseOpenRouterModel({
573+
id: "moonshotai/kimi-k3",
574+
model: mockModel,
575+
inputModality: ["text", "image"],
576+
outputModality: ["text"],
577+
maxTokens: null,
578+
supportedParameters: ["reasoning"],
579+
})
580+
581+
expect(result.maxTokens).toBe(32768)
582+
expect(result.contextWindow).toBe(1000000)
583+
expect(result.supportsReasoningEffort).toEqual(["low", "high", "max"])
584+
expect(result.reasoningEffort).toBe("high")
585+
expect(result.supportsTemperature).toBe(true)
586+
expect(result.defaultTemperature).toBe(1)
587+
})
588+
589+
it("applies the Moonshot K3 profile for ~moonshotai/kimi-latest", () => {
590+
const mockModel = {
591+
name: "Kimi Latest",
592+
description: "Test model",
593+
context_length: 1000000,
594+
max_completion_tokens: null,
595+
pricing: {
596+
prompt: "0.0000006",
597+
completion: "0.000003",
598+
},
599+
}
600+
601+
const result = parseOpenRouterModel({
602+
id: "~moonshotai/kimi-latest",
603+
model: mockModel,
604+
inputModality: ["text", "image"],
605+
outputModality: ["text"],
606+
maxTokens: null,
607+
supportedParameters: ["reasoning"],
608+
})
609+
610+
expect(result.maxTokens).toBe(32768)
611+
expect(result.supportsReasoningEffort).toEqual(["low", "high", "max"])
612+
expect(result.reasoningEffort).toBe("high")
613+
expect(result.supportsTemperature).toBe(true)
614+
expect(result.defaultTemperature).toBe(1)
615+
})
616+
553617
it("does not override max tokens for other models", () => {
554618
const mockModel = {
555619
name: "Other Model",
@@ -668,4 +732,59 @@ describe("OpenRouter API", () => {
668732
expect(resultWithoutTools.supportedParameters).toContain("max_tokens")
669733
})
670734
})
735+
736+
describe("applyOpenRouterMoonshotK3Profile", () => {
737+
it("overrides stale cached values for moonshotai/kimi-k3", () => {
738+
const stale: ModelInfo = {
739+
maxTokens: 209716,
740+
contextWindow: 1000000,
741+
supportsImages: true,
742+
supportsPromptCache: true,
743+
inputPrice: 0.6,
744+
outputPrice: 3,
745+
supportsReasoningEffort: true,
746+
}
747+
748+
const result = applyOpenRouterMoonshotK3Profile("moonshotai/kimi-k3", stale)
749+
750+
expect(result).toEqual({
751+
...stale,
752+
maxTokens: 32768,
753+
supportsReasoningEffort: ["low", "high", "max"],
754+
reasoningEffort: "high",
755+
supportsTemperature: true,
756+
defaultTemperature: 1,
757+
})
758+
// The original record (e.g. a shared cache entry) must not be mutated.
759+
expect(stale.maxTokens).toBe(209716)
760+
expect(stale.supportsReasoningEffort).toBe(true)
761+
})
762+
763+
it("applies the profile to ~moonshotai/kimi-latest", () => {
764+
const stale: ModelInfo = {
765+
maxTokens: 209716,
766+
contextWindow: 1000000,
767+
supportsPromptCache: true,
768+
supportsReasoningEffort: true,
769+
}
770+
771+
const result = applyOpenRouterMoonshotK3Profile("~moonshotai/kimi-latest", stale)
772+
773+
expect(result.maxTokens).toBe(32768)
774+
expect(result.supportsReasoningEffort).toEqual(["low", "high", "max"])
775+
expect(result.reasoningEffort).toBe("high")
776+
expect(result.supportsTemperature).toBe(true)
777+
expect(result.defaultTemperature).toBe(1)
778+
})
779+
780+
it("returns other models unchanged", () => {
781+
const info: ModelInfo = {
782+
maxTokens: 8192,
783+
contextWindow: 200000,
784+
supportsPromptCache: true,
785+
}
786+
787+
expect(applyOpenRouterMoonshotK3Profile("openai/gpt-4o", info)).toBe(info)
788+
})
789+
})
671790
})

0 commit comments

Comments
 (0)