Skip to content

fix(agent-core-v2): dedupe historical tool schemas - #4861

Closed
sailist wants to merge 7 commits into
mainfrom
bug-356-11-12-duplicate-system-message-tools
Closed

sailist wants to merge 7 commits into
mainfrom
bug-356-11-12-duplicate-system-message-tools

Conversation

@sailist

@sailist sailist commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

Requirement or Bug

启用动态工具加载的 TUI、desktop/web、ACP 与 SDK 会话在同一个工具被移除并重新加载后,可能在多条历史 system message 中携带同名工具定义,导致 Kimi 协议请求被 duplicate tool name ... between message tools 拒绝,后续 turn 也无法继续。

本 PR 在 tool-select resolver 的历史投影阶段去除重复定义,使每次请求只保留第一次消息级声明;完全相同的后续重复定义会被过滤,schema 不同的后新增 message 会转换为提醒,说明当前会话暂不支持更新并指引新会话。没有单独关联 issue,问题、复现和回归验证均记录在本 PR 中。

Bug Reproduction Steps

  1. 启用动态工具加载,通过 select_tools 加载一个 deferred MCP 工具,使历史写入一条 content: []、tools: [schema] 的 system message。
  2. 移除并以同名定义替换该工具,再次加载,使历史同时保留旧、新两条同名 schema message。
  3. 发起下一次模型请求。修改前 resolver 将两条消息级定义同时投影到请求中,Kimi 服务端在校验阶段以 HTTP 400 duplicate tool name ... between message tools 拒绝。

回归测试使用中性构造工具和真实 resolver 调用复现该请求结构,不依赖线上模型或真实用户会话。

Root Cause

AgentToolSelectService.shapeActiveHistory() 只根据顶层 inline 工具名过滤历史 schema,没有处理不同 system message 之间的同名定义。每次 select_tools 加载都会追加一条 schema message,因此重载后旧、新定义会一起进入出站请求。

这是 resolver 请求投影层的修复,而不是在 serializer 中掩盖问题或改写持久化历史。为避免改变已有消息前缀并维护缓存,投影保留第一次出现的旧定义;完全相同的后续重复定义直接过滤,schema 不同的后新增 message 转换为更新提醒,明确当前会话暂不支持应用新 schema 并指引新会话。提醒只针对最新仍有效的 schema 更新,A→B→A 或工具移除后不提示重启。既有顶层 inline 优先规则保持不变。

Code Changes

 shapeActiveHistory
+  记录每个工具名第一次出现的 system message、tool 位置及首次 schema
 shapeActiveMessage
-  保留所有不与顶层 inline 重名的历史 schema
+  仅保留第一次消息级 schema,继续过滤顶层 inline 同名项
+  相同 schema 的重复项过滤后没有正文和剩余 tool 时,移除整条 system message
+  不同 schema 的后新增 message 转换为更新 reminder,保留正文并指引新会话
 toolExecutor
+  新增 validating 预检钩子,在参数校验前拒绝 pending schema 更新调用

带正文的重复消息只移除后出现的重复 tool,正文继续保留;不同 schema 的后新增 message 转换为更新提醒,说明当前会话不支持更新并指引新会话。原始 context history、registry、wire/state 契约和 Kimi serializer 均未修改。

主要阅读位置:packages/agent-core-v2/src/agent/toolSelect/toolSelectService.ts;回归位于现有 toolSelectService.test.ts 同名移除/替换用例,测试声明总数净零。

Behavior Changes and Affected Users

行为 修改前 修改后 依赖旧行为的人群 退路
多条 system message 中的同名工具定义 旧、新定义全部进入请求,可能被 provider 拒绝 只保留第一次消息级定义 开启动态工具的 TUI、desktop/web、ACP、SDK 会话,含旧版本已写入重复历史的会话 无需保留无法通过校验的重复请求
后出现的空重复 system message 空消息仍进入请求 相同 schema 时整条移除;不同 schema 时转换为更新提醒 同上 无需;消息正文和其他 tool 不受影响
带正文的重复 system message 正文和旧 tool 一起保留 保留正文,移除后出现的重复 tool 依赖历史正文上下文的会话 正文原样保留
pending schema 更新工具调用 旧 schema 可能进入新版参数校验或执行 预检返回当前会话不支持更新并指引新会话;回退到原 schema 后恢复 需要避免旧 schema 调用新实现的会话 新会话使用更新后的 schema
顶层 inline 去重、非重复定义和持久化历史 按既有规则处理 保持不变 所有 tool-select 会话及旧会话恢复 既有测试继续覆盖

受影响模块仅为 agent-core-v2 的 tool-select resolver 及其既有测试。回归覆盖首次定义和首条消息对象保留、相同 schema 的空重复消息删除、不同 schema 转更新提醒、正文保留、原始历史不变,以及既有 inline 去重行为;还覆盖 pending 调用预检拦截、A→B→A 后提醒消失与调用恢复。

未请求真实线上 provider,也未运行全仓测试;已执行本地修改测试、m0-dev 聚焦 tool-select 测试(含 E2E)、Core typecheck 和局部 type-aware lint。

验证命令与结果
  • 本地:corepack pnpm -C packages/agent-core-v2 exec vitest run test/agent/toolSelect/toolSelectService.test.ts --config vitest.config.ts,96/96 通过。
  • m0-dev:test/agent/toolSelect 三个文件,121/121 通过,包含 E2E。
  • m0-dev:corepack pnpm -C packages/agent-core-v2 typecheck,通过。
  • 两个实现/测试文件运行 type-aware oxlint:0 errors;7 条 warnings 均位于未修改代码行。
  • 交付 diff 只包含实现、既有测试和一份 patch changeset;安全审计未发现敏感信息或无关文件。

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve).(无单独 issue,问题、复现与验证见上文。)
  • 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 12, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 41cc602

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 12, 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-12T05:37:27.387590Z 41cc602 New commits
ℹ️ 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 12, 2026 •

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

commit: 41cc602

@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: ed43f5c2b2

ℹ️ 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 +619 to 621
if (!inlineNames.has(tool.name) && isFirstDeclaration) {
if (kept !== undefined) kept.push(tool);
continue;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Avoid advertising replacement tools with their original schema

When a same-name replacement changes its parameters, these lines retain the first definition even though the registry and executor now contain the replacement and the persistent history makes it count as loaded. In the added test, the model is therefore shown the original permissive schema and can call the tool with {}, while toolArgsError validates that call against the replacement schema requiring payload and rejects it. Project a schema compatible with the active implementation, or mark the replacement unavailable until a new session instead of leaving it callable with a stale contract.

Useful? React with 👍 / 👎.

Comment on lines +627 to +629
toolSelectSchemaSignature(firstDeclaration.tool) !== toolSelectSchemaSignature(tool)
) {
schemaUpdates.push(tool.name);

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 Only announce schema updates that are still pending

When a tool changes schema and is subsequently removed or reverts to its original schema, this comparison still treats the intermediate historical declaration as a pending update because every duplicate is compared only with the first declaration. Every later request then tells the model to start a new session for an update that is either unavailable or already matches the retained schema, potentially producing unnecessary restart guidance. Determine the reminder from the effective latest declaration and current availability rather than from every historical difference.

Useful? React with 👍 / 👎.

@sailist sailist closed this Nov 12, 2026

@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: 41cc6022dc

ℹ️ 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 +140 to +143
if (validating !== undefined && 'args' in validating) {
const rewritten = await prepare(tool);
if (rewritten !== undefined) return control.returned(rewritten);
}

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 Skip duplicate preparation after validation rewrites

When a validating hook returns { args: ... }, this branch calls prepare(tool) and then falls through to the unconditional prepare(tool) on line 144. Any non-synthetic tool is therefore prepared twice, discarding the first prepared value and potentially repeating side effects in prepare/legacy resolveExecution; reuse the first result or skip the second preparation after a rewrite.

Useful? React with 👍 / 👎.

Comment on lines +168 to +170
if (
toolSelectSchemaSignature(latestTool) === toolSelectSchemaSignature(currentSchema) &&
toolSelectSchemaSignature(firstTool) !== toolSelectSchemaSignature(currentSchema)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Keep blocking after another live schema update

When a loaded tool has historical schemas A→B and the live registry advances again to C before another declaration is appended, latestTool no longer equals currentSchema, so this predicate stops treating the tool as pending. The request still retains only schema A, while isLoadedToolActive can regard the pinned B declaration as loaded and the executor invokes implementation C, allowing a call under a contract the model never saw. Fresh evidence beyond the prior stale-schema thread is this third-schema transition, which bypasses the newly added rejection guard; keep the tool pending whenever the live schema differs from the retained first schema, except when it is absent or has reverted to that first schema.

Useful? React with 👍 / 👎.

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.

1 participant