Skip to content

fix(agent-core-v2): keep token counting and compaction anchored to usage - #4907

Merged
wbxl2000 merged 2 commits into
release/2026-11-13from
bug-367-11-13-backport-token-counting-usage
Nov 13, 2026
Merged

wbxl2000 merged 2 commits into
release/2026-11-13from
bug-367-11-13-backport-token-counting-usage

Conversation

@sailist

@sailist sailist commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator

Requirement or Bug

将 #4883 的同一修复 backport 到 release/2026-11-13。

长会话中的工具结果一旦带分页 note、错误状态、空输出或 Wall time 头,Token 实测锚点就可能停止更新。后续请求退回偏低的本地字符估算,导致真实上下文已达到窗口上限时自动压缩仍不触发,可能出现长时间预填充后空回复,或在 Gemini 超限报错后才进入兜底压缩。本 PR 让 release 分支在这些场景下持续使用真实用量,并在首步请求前完成必要的自动压缩。

Bug Reproduction Steps

  1. 在大窗口模型会话中执行带分页 note、错误状态、空输出或耗时头的工具调用。
  2. 继续对话,使服务端真实输入超过模型窗口的 85%,而本地字符估算仍低于该阈值。
  3. 发送新消息。修复前实测锚点停更,自动压缩可能漏触发;修复后工具结果场景持续记录真实用量,存量会话也会参考历史 assistant usage 自动压缩。

Root Cause

根本修复,与 #4883 相同,按 release 当前接口适配:

  • 请求侧工具消息会渲染 note、错误状态、空输出和耗时头,而历史侧将这些信息保存在独立字段;原有逐字节 content 比较因此返回 false,停止写入 token_counting.measured。
  • FullCompaction 只读取本地 TokenCounting 估算,没有使用历史 assistant 消息中持久化的真实 usage,所以存量会话在实测锚点缺失或停更后仍会漏掉自动压缩与压缩预裁剪。

Code Changes

  • tokenCountingAgentModel.ts:复用 release 已有的工具结果渲染器,对齐原始、note 追加及完整投影后的工具消息,同时拒绝不一致的 note 和错误状态。
  • requestObservation.ts:普通请求使用原始请求消息;媒体降级或剥离恢复后使用实际出站消息,避免给未降级上下文写入错误锚点。
  • fullCompactionService.ts:以最近 assistant usage 加尾部估算作为真实用量下界,并将该口径用于自动压缩判定、tokensBefore、预裁剪和溢出缩减。按相邻 usage 锚点差值校准各段消息权重,避免字符估算偏低时漏掉预裁剪。
  • 扩展现有 close→resume 与 auto pre-shrink 用例,覆盖工具结果投影、恢复后的持续实测,以及无实测锚点但历史 usage 超窗时的首步前压缩。测试声明数净变化为零。
  • 添加与 fix(agent-core-v2): keep token counting and compaction anchored to usage #4883 相同的 patch changeset。

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
带附加信息的工具结果后的 Token 实测 消息形态不一致后停止写入实测锚点 持续记录真实用量,媒体恢复后使用实际出站消息 无合理依赖;修复 release 分支全端长会话的计数失真 无需
实测锚点缺失时的自动压缩 仅按本地估算判定,可能漏触发 同时参考历史 assistant usage,达到阈值即在首步前压缩 旧版本写入的存量长会话用户无合理依赖 无需
压缩请求预裁剪与溢出缩减 仅按字符估算分配预算 按历史 usage 校准总预算和消息权重 触发自动或手动压缩的用户无合理依赖 无需

未改变 wire、配置、SDK 或客户端 API 契约,也未改变默认阈值。

验证:

  • 本地修改过的两个测试文件:132/132 通过。
  • m0-dev:loop、llmRequester、session、wire 共 41 个测试文件,651/651 通过。
  • agent-core-v2 tsc --noEmit 通过。
  • 五个变更文件 oxlint:0 error;3 个 warning 均为 release 已有写法。
  • lint:imports 仍有 4 个 release 基线既有违规,本次 diff 未新增违规。
  • 测试声明数净变化为零。

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve).
  • I have added tests that prove my feature works.
  • The behavior-change table above is complete, and every removed behavior or flipped default is named in the changeset and either has an escape hatch or was explicitly approved by a maintainer in this PR.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

@changeset-bot

changeset-bot Bot commented Nov 13, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: dbfa963

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-13T05:27:31.013633Z dbfa963 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@pkg-pr-new

pkg-pr-new Bot commented Nov 13, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@dbfa963
npx https://pkg.pr.new/@moonshot-ai/kimi-code@dbfa963

commit: dbfa963

@wbxl2000
wbxl2000 merged commit 397248f into release/2026-11-13 Nov 13, 2026
15 checks passed
@wbxl2000
wbxl2000 deleted the bug-367-11-13-backport-token-counting-usage branch November 13, 2026 06:02
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.

2 participants