[Bug] Auto-compaction trigger ignores the system prompt and tool schemas when the provider omits cached-token counts

未关闭
#5,064 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
72/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
typescript
领域
backend, devtools

调研方向

Start with packages/agent-core-v2/src/agent/fullCompaction/fullCompactionService.ts, especially tokenCountWithPending() and requestTokens(), then read packages/agent-core-v2/src/session/tokenCounting/tokenCountingAgentModel.ts and its token counting tests. Reproduce the missing cached-token case described in the issue and add a regression test for the compaction gate with a system prompt and tool schemas. Done when the fallback trigger and block checks count the request-level estimate and the regression test passes.

由索引模型根据 Issue 内容生成。

描述

What version of Kimi Code is running?

2.6.3 (source main snapshot, 2026-11-18). Confirmed by reading the code paths below and running packages/agent-core-v2/test/agent/tokenCounting/tokenCounting.test.ts (19 tests pass).

Which open platform/subscription were you using?

None involved (code-level audit).

Which model were you using?

Any provider whose streaming usage does not report cached prompt tokens (for example Gemini without context caching, or an OpenAI-compatible proxy that omits cached_tokens).

What platform is your computer?

Windows 11 x64.

What issue are you seeing?

For providers that do not report cached prompt tokens, automatic compaction is triggered from a count that includes only the conversation messages and silently omits the system prompt and tool schemas. Compaction therefore fires later than the configured threshold, and the real request can exceed the model window before compaction runs.

The chain:

  1. packages/core/src/llm/usage.ts:13-16: inputTotal() returns undefined when inputCacheRead === undefined, so grandTotal() is also undefined.
  2. packages/core/src/llm/builtin/protocol/openai/decode.ts:192-196: when cached_tokens is absent, inputCacheRead stays undefined. For google-genai, decode.ts:155-160 leaves it undefined whenever cachedContentTokenCount is absent (no explicit context caching).
  3. packages/agent-core-v2/src/session/tokenCounting/tokenCountingAgentModel.ts:110-118: measured() computes grandTotal(usage) and returns early when it is undefined, so no measured anchor is ever recorded.
  4. packages/agent-core-v2/src/agent/fullCompaction/fullCompactionService.ts:287-295: usageAnchoredRequestTokens() also requires grandTotal(usage) and returns undefined for every assistant message.
  5. fullCompactionService.ts:951-955: with both sources unavailable, tokenCountWithPending() falls back to this.tokenCounting.get().size.

TokenCountingAgentModel.get() (tokenCountingAgentModel.ts:89-102) estimates readHistory() only. The system prompt and the tools[] block are not part of that array, so they are never counted by the fallback.

By contrast, requestTokens() / historyRequestTokens() (fullCompactionService.ts:281-303, used for tokensBefore and for validateCompactionStart) do include the system prompt and tools. So the trigger condition and the reported pre-compaction size are computed from different bases.

Concrete impact: with a 256K window and compaction_trigger_ratio = 0.85, a typical MCP tool set can add tens of thousands of tokens of schema. The gate sees messages only and compacts around 0.85 * 256K of messages, while the actual request is already beyond the model window. The turn then goes through the context-overflow recovery path (or fails), and the reported tokensBefore/context size under-reports.

What steps can reproduce the bug?
  1. Use a provider that does not return cached prompt tokens (e.g. Gemini without context caching, or an OpenAI-compatible proxy that omits cached_tokens).
  2. Configure a large tool set (many MCP tools) so the system prompt plus tool schemas are a significant fraction of the window.
  3. Run a multi-step tool session until the context approaches the window.
  4. Compare tokenCounting.get().size / the compaction trigger count with historyRequestTokens(history): the trigger count does not include the system prompt or tools.
  5. Observe that compaction starts at the wrong threshold and that the real request can hit a provider context-overflow error first.

A unit-level observation is already pinned by packages/agent-core-v2/test/agent/tokenCounting/tokenCounting.test.ts:201-207: calling measured() with inputCacheRead: undefined leaves the anchor ledger empty. No test covers the downstream effect on the compaction gate.

What is the expected behavior?

When neither a measured anchor nor a usage-anchored count is available, the compaction trigger and block checks should fall back to a request-level estimate that includes the system prompt and tool schemas (the same basis as historyRequestTokens), instead of a message-only estimate.

Additional information

Suggested fix (does not require changing the conservative inputTotal semantics): in tokenCountWithPending(), when usageAnchored === undefined and no measured anchor covers the current length, return Math.max(counted, this.requestTokens(this.loopService.history())) or add this.requestTokens([]) (prompt + tools) to counted. A regression test could construct an agent with a large system prompt/tool list and assert that the auto-compaction threshold is crossed at the expected size for a provider that omits cached tokens.

Contribution
  • I am willing to submit a PR for this bug fix myself (please wait for maintainer approval in this issue first)
主要语言
TypeScript
星标
8.3k
派生
1.4k
平均合并
12 小时 6 分钟
30 天内合并 PR
596

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

MoonshotAI/kimi-code 的其他 Issue

查看 MoonshotAI/kimi-code 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。