Skip to content

fix(kimi-code): correct maxTokens defaults and add server-side override - #1216

Closed
myk1yt wants to merge 33 commits into
Zoo-Code-Org:mainfrom
myk1yt:fix/kimi-code
Closed

fix(kimi-code): correct maxTokens defaults and add server-side override#1216
myk1yt wants to merge 33 commits into
Zoo-Code-Org:mainfrom
myk1yt:fix/kimi-code

Conversation

@myk1yt

@myk1yt myk1yt commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #1215

Kimi Code OAuth provider displayed incorrect Max Output values (32,768) for all models. According to Kimi's official documentation, the correct default for kimi-k3 is 131,072.

Root Cause

Three issues in the codebase:

  1. kimiCodeDefaultModelInfo.maxTokens was hardcoded to 32_768 instead of 131_072
  2. The Zod schema for /models API response didn't include max_tokens field (silently stripped)
  3. mapKimiCodeModel() overrode contextWindow from server but not maxTokens

Changes (3 files, +47/-1)

File Change
packages/types/src/providers/kimi-code.ts Update maxTokens to 131_072, add kimiCodeModelDefaults for per-model fallback
src/api/providers/fetchers/kimi-code.ts Add max_tokens to Zod schema, override in mapKimiCodeModel()
src/api/providers/fetchers/__tests__/kimi-code.spec.ts Add 4 tests for maxTokens override and fallback

Fallback Chain

maxTokens = server.max_tokens
         ?? kimiCodeModelDefaults[model.id].maxTokens
         ?? kimiCodeDefaultModelInfo.maxTokens (131,072)

This ensures automatic updates when the server provides max_tokens, with per-model and global fallbacks.

Verification

  • Vitest: 11/11 passed (7 existing + 4 new)
  • ESLint: clean
  • TypeScript: clean

Summary by CodeRabbit

  • New Features

    • Added smarter tool-call handling based on model and provider capabilities.
    • Improved support for single-call and parallel-call workflows, including safer handling of incomplete or duplicate calls.
    • Expanded MiMo compatibility, including flexible endpoint configuration and accurate usage-based cost reporting.
    • Increased Kimi Code model token limits with model-specific defaults.
  • Bug Fixes

    • Improved recovery from unsupported request options and malformed tool-call responses.
    • Fixed Mistral streaming cost calculations and usage reporting.
  • Tests

    • Added comprehensive coverage for tool-call behavior, provider compatibility, and cost tracking.

Zoo (VP) and others added 30 commits August 7, 2026 04:11
# Conflicts:
#	src/core/tools/error-interception/StructuralValidator.ts
# Conflicts:
#	src/core/assistant-message/__tests__/NativeToolCallParser.spec.ts
#	src/core/assistant-message/__tests__/presentAssistantMessage-parser-dedup.integration.spec.ts
#	src/core/assistant-message/presentAssistantMessage.ts
# Conflicts:
#	src/core/assistant-message/__tests__/presentAssistantMessage-parser-dedup.integration.spec.ts
MimoHandler was passing raw tool schemas to the API without the
strict mode conversion that all other OpenAI-compatible providers use.
This caused tool call errors due to missing required/strict fields.

- Call this.convertToolsForOpenAI(tools) instead of raw assignment
- Adds strict: true, required properties, additionalProperties: false
An id-less argument-continuation chunk belongs to the most recent id chunk
seen at its index. When a provider reuses index 0 with a NEW id (a disguised
second parallel call), the new call's id chunk was dropped but its id-less
argument fragments were still kept and concatenated into the FIRST call's
accumulator, corrupting its JSON.

Track dropped indexes in filterToFirstToolCall state and drop subsequent
id-less fragments for those indexes. Also rewrite the function docblock,
which referenced a non-existent error-interception retry loop.
The parseErrors/parseFailures docblocks claimed presentAssistantMessage
routes recorded failures to an INVALID_JSON_ARGUMENTS error-interception
pattern. No such routing exists on this codebase; describe the actual
lifecycle (consumed via the consume* APIs, cleared on new API request).
Comment-only change, no behavior difference.
parseErrors/parseFailures static maps accumulated an entry per malformed
tool call and were never cleared in production (the consume* APIs have no
production callers), slowly leaking for the extension-host lifetime.

Add NativeToolCallParser.clearParseFailures() and call it in
Task.recursivelyMakeClineRequests alongside clearAllStreamingToolCalls()/
clearRawChunkState(), where other per-stream state is reset. The consume*
APIs keep working for tests.
MiMo sends tools through convertToolsForOpenAI(), which attaches a strict
flag to every function tool. An OpenAI-compatible endpoint that doesn't
support structured outputs rejects the request with a 400 and the turn
fails outright.

Mirror the existing parallel_tool_calls fallback: detect schema-rejection
errors narrowly (400 status plus a mention of strict/additionalProperties
in a tools context, so unrelated 400s like MiMo's missing-reasoning_content
rejection are not retried) and retry once with the original schemas and no
strict flag.
…1132)

CI failure: E2E Tests (Mocked) failed with '404 No fixture matched' because
provider-cost.test.ts calls startNewTask with probe tag 'provider-cost-e2e'
but no fixture existed.
Zoo (VP) added 2 commits August 8, 2026 16:52
Fixes Zoo-Code-Org#1215

- Update kimiCodeDefaultModelInfo.maxTokens from 32768 to 131072

- Add kimiCodeModelDefaults for per-model fallback values

- Add max_tokens to kimiCodeModelSchema for server response parsing

- Override maxTokens in mapKimiCodeModel: server > model-default > global-default

- Add 4 new tests for maxTokens override and fallback behavior
@coderabbitai

coderabbitai Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 397c164d-f08d-4950-9b04-dc7b0a2e1f67

📥 Commits

Reviewing files that changed from the base of the PR and between fcc507c and dc31fca.

📒 Files selected for processing (29)
  • apps/vscode-e2e/fixtures/provider-cost.json
  • apps/vscode-e2e/src/suite/provider-cost.test.ts
  • codecov.yml
  • packages/telemetry/src/TelemetryService.ts
  • packages/types/src/model.ts
  • packages/types/src/provider-settings.ts
  • packages/types/src/providers/kimi-code.ts
  • packages/types/src/providers/mimo.ts
  • packages/types/src/telemetry.ts
  • progress.txt
  • src/api/index.ts
  • src/api/providers/__tests__/mimo.spec.ts
  • src/api/providers/__tests__/mistral.spec.ts
  • src/api/providers/fetchers/__tests__/kimi-code.spec.ts
  • src/api/providers/fetchers/kimi-code.ts
  • src/api/providers/mimo.ts
  • src/api/providers/mistral.ts
  • src/core/assistant-message/NativeToolCallParser.ts
  • src/core/assistant-message/ToolCallRetentionPolicy.ts
  • src/core/assistant-message/__tests__/NativeToolCallParser.spec.ts
  • src/core/assistant-message/__tests__/ToolCallRetentionPolicy-telemetry.spec.ts
  • src/core/assistant-message/__tests__/ToolCallRetentionPolicy.spec.ts
  • src/core/prompts/tools/native-tools/execute_command.ts
  • src/core/task/Task.ts
  • src/core/task/__tests__/ghost-quarantine.spec.ts
  • src/core/task/__tests__/tool-call-policy.spec.ts
  • src/core/tools/ExecuteCommandTool.ts
  • src/eslint-suppressions.json
  • src/shared/tools.ts

📝 Walkthrough

Walkthrough

The change adds model-aware tool-call policies, MiMo single-call handling, ghost-call quarantine, classified parser failures, metadata-only telemetry, Kimi Code token defaults, provider cost validation, and related type and configuration updates.

Changes

Tool-call control

Layer / File(s) Summary
Policy and telemetry contracts
packages/types/src/model.ts, packages/types/src/providers/mimo.ts, packages/types/src/telemetry.ts, src/api/index.ts, packages/telemetry/src/TelemetryService.ts
Model capabilities and provider metadata now resolve tool-call generation and enforcement policies. New telemetry events record policy metadata and enforcement counts without raw tool data.
Parser failures and call retention
src/core/assistant-message/NativeToolCallParser.ts, src/core/assistant-message/ToolCallRetentionPolicy.ts, src/core/assistant-message/__tests__/*
Parser failures are classified and consumable through typed and legacy APIs. Retention helpers classify empty ghosts, select executable calls, and emit metadata-only enforcement telemetry.
MiMo tool-call handling
src/api/providers/mimo.ts, src/api/providers/__tests__/mimo.spec.ts
MiMo requests use typed parameters, retry unsupported fields, and filter streamed responses to the first tool call. Tests cover request forwarding, retries, streaming, malformed calls, and duplicate IDs.
Task policy integration and ghost quarantine
src/core/task/Task.ts, src/core/task/__tests__/*, src/core/prompts/tools/native-tools/execute_command.ts
Task requests use resolved parallel-call settings. Streaming, legacy, and unterminated calls discard provably empty ghosts, clean state, and emit telemetry. Tool documentation reflects the cwd contract.

Provider metadata and cost handling

Layer / File(s) Summary
Kimi Code model limits
packages/types/src/providers/kimi-code.ts, src/api/providers/fetchers/kimi-code.ts, src/api/providers/fetchers/__tests__/kimi-code.spec.ts
Kimi Code model mapping now prioritizes server max_tokens, then per-model defaults, then the global default. The global default is 131,072 tokens.
Provider usage cost validation
src/api/providers/mistral.ts, src/api/providers/__tests__/mistral.spec.ts, apps/vscode-e2e/...
Mistral usage events now include calculated cost. The MiMo E2E test validates streamed usage and persisted pricing using a local SSE fixture.
Provider and tool contracts
packages/types/src/provider-settings.ts, src/core/tools/ExecuteCommandTool.ts, src/shared/tools.ts, codecov.yml
MiMo base URLs accept valid URLs. Execute-command timeouts no longer accept null. Default patch coverage is informational.

Estimated code review effort: 5 (Critical) | ~120 minutes

Possibly related issues

Possibly related PRs

Suggested labels: awaiting-review

Suggested reviewers: taltas

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

apps/vscode-e2e/src/suite/provider-cost.test.ts

ESLint skipped: missing config or dependency (missing-dependency). The ESLint configuration references a package that is not available in the sandbox.

packages/telemetry/src/TelemetryService.ts

ESLint skipped: missing config or dependency (missing-dependency). The ESLint configuration references a package that is not available in the sandbox.

packages/types/src/model.ts

ESLint skipped: missing config or dependency (missing-dependency). The ESLint configuration references a package that is not available in the sandbox.

  • 22 others

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Warning

⚠️ This pull request shows signs of AI-generated slop (description_diff_mismatch). It has been flagged by CodeRabbit slop detection and should be reviewed carefully.

@myk1yt

myk1yt commented Aug 10, 2026

Copy link
Copy Markdown
Contributor Author

Closing this PR because it includes unrelated changes from the fork. Reopening a clean PR with only kimi-code changes.

@myk1yt myk1yt closed this Aug 10, 2026
@codecov

codecov Bot commented Aug 10, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: Kimi Code OAuth shows incorrect Max Output (32K instead of 131K)

1 participant