Skip to content

fix(agent-core-v2): reconcile tool definitions after updates - #4719

Merged
7Sageer merged 1 commit into
MoonshotAI:mainfrom
pvzheroes125:fix/tool-definition-updates
Nov 5, 2026
Merged

7Sageer merged 1 commit into
MoonshotAI:mainfrom
pvzheroes125:fix/tool-definition-updates

Conversation

@pvzheroes125

Copy link
Copy Markdown
Contributor

Requirement or Bug

启用动态工具加载的会话在工具更新后,可能向模型同时声明同名工具的不同版本;恢复已保存的会话后,冲突还会继续出现。内联工具更新也会注入没有实际工具增删的提醒。本 PR 修复请求中的定义冲突,并跳过这类空公告。

Bug Reproduction Steps

  1. 启用动态工具加载,注册一个内联工具并开始会话,保存其初始工具快照。
  2. 更新该工具的描述或参数,保持名称不变,然后继续或恢复会话。
  3. 旧实现保留顶层旧定义,同时把当前版本当成 runtime 工具供 select_tools 加载;加载后,新定义出现在历史 system.tools 中。
  4. 检查 Kimi 请求编码,可以看到同名旧顶层定义和新历史定义并存。仅更新内联工具时,还会注入 added、removed 均为空的公告。

回归测试使用构造的工具和保存状态复现,无需模型网络请求或真实用户日志。

Root Cause

初始工具快照用于稳定工具列表,但按名称排除 runtime 工具时,也排除了初始工具的新版定义。动态加载从当前定义取 schema,历史投影又没有收到实际顶层工具列表,因此两个版本会一起进入请求。旧加载状态还会把重新归为内联的工具判为不可执行。

公告按变化记录逐条生成;过滤掉内联替换后,即使没有剩余增删,旧实现仍返回一条提醒。

这是 Core 内部的根因修复:保留原始快照和持久化历史,只统一当前请求使用的定义、加载判断和公告。

Code Changes

 工具更新或恢复会话
- 顶层始终使用同名工具的初始定义
- 当前新版作为 runtime 工具再次加载,历史定义与顶层分别保留
+ 初始工具的有效定义取自 current;原本内联且现在非 deferred 的工具保持内联
+ 根据实际顶层工具列表过滤同名历史 schema,保留正文、调用和结果
+ 当前有效 deferred MCP 定义加载后,过滤历史旧版本
 工具公告
- 过滤后没有增删也立即返回
+ 顺序跳过空变化,以实际索引推进 revision;最后仍检查权限变化

主要逻辑在 toolSelectService.ts;toolSelect.ts 为历史投影增加可选工具列表参数,toolSelectPolicy.ts 传入本次请求实际使用的列表。真正新增的 runtime 工具和原本 deferred 的工具继续按已有流程加载。

Behavior Changes and Affected Users

行为 修改前 修改后 受影响用户 退路
初始内联工具同名更新 顶层使用旧定义,新定义需要再次加载 顶层使用当前定义,直接可调用,无需再次 select_tools 启用动态工具加载的 CLI、SDK、desktop/web 和 ACP 会话,含旧版本保存的状态 无新增开关;修复定义冲突
已保存加载记录的内联工具执行 可能被旧加载状态判为不可用 当前工具有效且策略允许时正常执行 恢复包含历史 schema 的旧会话的用户 不重建或迁移会话
顶层与历史工具定义 同名定义可能同时保留 历史移除与实际非 deferred 顶层同名的 schema;正文、调用、结果保留 Kimi 协议用户及显式指定工具列表的调用方 deferred 和空显式列表继续保留所需历史声明
deferred MCP 更新后重新加载 历史可能同时保留旧版和新版 仅在已有当前有效加载定义时过滤旧版;未加载新版或断连时保持原行为 MCP 和插件工具用户 不改变加载门槛
仅内联工具更新的公告 没有实际增删也注入提醒 跳过空变化,后续动态增删和权限变化仍正常公告 运行中更新工具的动态工具会话 无新增开关;只去掉无实际增删的提醒
工具提示缓存 同名更新后顶层旧内容不变 同名内联定义变化会改变顶层工具内容,可能使该会话对应的提示缓存失效;未变化定义、顺序及 deferred 行为保留 使用工具提示缓存的动态工具会话 保留现有动态工具开关,无新增配置

106 项聚焦测试全部通过,覆盖构造旧状态并恢复、实际 Kimi 请求编码、本地 executor 执行、保留未变化工具和 deferred 工具、显式工具列表、MCP 新旧 schema、跳过空公告后的动态增删及权限变化。Core 类型检查及注释检查通过;局部类型感知 lint 为 0 错误,7 条警告均位于未修改的现有语句。

验证边界:没有请求线上模型,也没有读取或复测真实用户的原始会话。真实原会话仍需在包含修复的版本中继续对话验证。

验证命令
  • pnpm -C packages/agent-core-v2 exec vitest run test/agent/toolSelect/toolSelectService.test.ts test/agent/toolSelect/dynamicTools.test.ts --config vitest.config.ts
  • pnpm -C packages/agent-core-v2 exec tsc --noEmit -p tsconfig.json
  • 对三个 Core 源文件及现有测试文件运行 oxlint --type-aware
  • node scripts/check-no-comments.mjs
  • git diff --check

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.

目前未关联已获维护者 /approve 的 issue;本 PR 提供独立复现、修复和回归验证。没有新增命令、配置或需要同步的公开文档。

@changeset-bot

changeset-bot Bot commented Nov 5, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 5f204ef

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 5, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-05T14:27:28.880268Z 5f204ef 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 5, 2026

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

commit: 5f204ef

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5f204eff14

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +214 to +218
const added = change.added.filter((tool) => this.isDynamicallyLoadable(tool));
const removed = change.removed.filter((tool) =>
this.isDynamicallyLoadable(tool) || !change.added.some(
(next) => next.name === tool.name && !this.isDynamicallyLoadable(next),
));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve unsupported-model warnings for inline replacements

When the flag is enabled but a request resolves with dynamically_loaded_tools: false (for example after switching to a model without that capability), a same-name inline schema replacement makes both filtered lists empty here, so no change announcement is persisted. defaultToolsForDisclosure(false) still builds from initialTools(), while shapeUnsupportedDynamicToolContext uses these announcements to warn that replaced definitions may fail; the model therefore silently receives the obsolete parameter schema even though the executor validates the replacement, so calls whose parameters changed can fail. Preserve this replacement in the unsupported-model projection or expose its current schema there.

AGENTS.md reference: AGENTS.md:L110-L112

Useful? React with 👍 / 👎.

@7Sageer
7Sageer merged commit bae3211 into MoonshotAI:main Nov 5, 2026
17 checks passed
@pvzheroes125
pvzheroes125 deleted the fix/tool-definition-updates branch November 5, 2026 15:52
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