Skip to content

fix(agent-core-v2): wait for MCP discovery before tool announcements - #4364

Merged
7Sageer merged 2 commits into
MoonshotAI:mainfrom
pvzheroes125:fix/mcp-first-turn-announcement
Oct 23, 2026
Merged

7Sageer merged 2 commits into
MoonshotAI:mainfrom
pvzheroes125:fix/mcp-first-turn-announcement

Conversation

@pvzheroes125

@pvzheroes125 pvzheroes125 commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Requirement or Bug

修复新会话首轮遗漏动态加载的 MCP 工具公告,导致模型到第二轮才能发现这些工具的问题。

Bug Reproduction Steps

  1. 启用动态工具加载,并使用支持该能力的模型和 deferred MCP 工具。
  2. MCP 初始发现尚未完成时发送新会话的第一条消息。
  3. MCP 工具在首次模型请求前已经注册,但该请求中的动态工具公告仍缺少它们;下一轮才补上。

Root Cause

Reminder 在 MCP 的 before-step 等待之前生成工具公告,读到尚未完成初始化的工具注册表。公告只在新 turn 或压缩后的重新注入时计算,随后 MCP 初始化完成不会重算当前公告。

本次在生成公告前等待现有的 MCP initial-load promise,修正执行顺序;不新增初始化流程、轮询或重试。

Code Changes

 首轮 / 压缩后生成动态工具公告
+  动态工具加载关闭时直接返回
+  等待现有 MCP 初始发现完成,传入当前 step 的 AbortSignal
   从工具注册表生成公告

 Reminder provider
+  接收可选 AbortSignal
+  取消时向现有 loop 传播中止,不作为普通 provider 错误吞掉

在现有 tool-select 测试中增加延迟发现和等待期间取消两条回归,并添加 patch changeset。

补齐现有 toolSelectService.test.ts 的 MCP 依赖注册:该单元测试使用手动组装的 strict harness,需要显式提供已经就绪的 MCP stub。此次 CI 修正只增加 4 行测试代码,不修改生产逻辑。

Behavior Changes and Affected Users

行为 修改前 修改后 受影响人群 退路
首轮动态 MCP 工具公告 发现较慢时可能漏掉工具,第二轮才出现 现有 MCP 初始发现完成后再生成公告 使用 v2、启用动态工具加载且模型支持该能力的 TUI、print、SDK 及集成客户端用户 保留现有动态工具加载开关;本次不改变默认值
等待 MCP 期间停止 turn Reminder provider 未取得 step 的取消信号 复用现有 MCP 等待的取消能力,中止向 loop 传播 上述用户在工具发现期间点击停止或取消 原有停止入口,无新增配置
已取消 step 的 provider 同时抛错 作为 provider 错误记录并跳过 按 step 已取消处理,不继续注入 所有使用 v2 Reminder 的客户端 普通未取消的 provider 错误仍按原逻辑记录并跳过

动态工具加载关闭或模型不支持时,在新增等待之前返回;后续 step 的公告时机、压缩后的重算与去重保持原有规则。MCP 连接、授权、代理和工具调用路径不变。

验证:

  • 延迟发现回归在修复前失败:首次请求缺少 MCP 工具公告;修复后通过。
  • tool-select、MCP、Reminder 三组定向测试:77 项通过;调整测试 promise 辅助函数后重跑 tool-select:11 项通过。
  • @moonshot-ai/agent-core-v2 typecheck 通过。
  • 改动 TS 文件的 type-aware lint:0 errors,8 条 warning 均已核对为基线已有。
  • comment-free 检查与 git diff --check 通过。
  • 上述为受控 MCP readiness 的自动化回归,未声称覆盖真实 OAuth 或所有客户端端到端联调。

CI 修正后的补充验证:

  • 原失败文件 toolSelectService.test.ts:91 项全部通过。
  • 本机 macOS 跑完整 agent-core-v2:374 文件通过、1 文件失败;7372 项通过、1 项跳过、1 项失败。唯一失败是 tool/tool.test.ts 中 returns the spawned agent id when a foreground subagent times out 的 30 秒超时。
  • 将 PR 基线 087db850e02d3e487ac834c4867e90b3334a63d6 的 Core 源码单独提取到临时目录,同一超时用例仍失败;本次不修改该基线问题,也不将本机全量结果报告为全绿。
  • Core typecheck、仓库完整 lint(0 errors)及 sherif 均通过。提交 d684d6d16 的远端完整 CI 已通过,五个测试分片、typecheck、lint、build 均成功;Windows job 沿用仓库既有禁用。Nix Build、pkg.pr.new 和 PR Title Checker 也已通过。

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.

未关联 issue;问题和复现步骤已直接写在上文。本次无新增配置或使用方式,文档无需更新。

@changeset-bot

changeset-bot Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: d684d6d

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 Oct 23, 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-10-23T11:59:28.362395Z d684d6d Manual request
ℹ️ 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 Oct 23, 2026 •

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

commit: d684d6d

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

ℹ️ 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".

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

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Copy link
Copy Markdown
Contributor Author

@codex review

请复审最新提交 d684d6d。上一轮 P1 指出的测试 MCP 依赖注册已补齐,原失败的 91 项测试及最新远端完整 CI 均已通过。

@7Sageer
7Sageer merged commit 7ccac2b into MoonshotAI:main Oct 23, 2026
18 checks passed
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: d684d6d162

ℹ️ 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".

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

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

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