Skip to content

fix(agent-core-v2): complete startup MCP tool baseline - #5011

Open
crlcrl1 wants to merge 7 commits into
MoonshotAI:mainfrom
crlcrl1:fix/issue-4904-mcp-startup-tools
Open

crlcrl1 wants to merge 7 commits into
MoonshotAI:mainfrom
crlcrl1:fix/issue-4904-mcp-startup-tools

Conversation

@crlcrl1

@crlcrl1 crlcrl1 commented Nov 17, 2026 •

Copy link
Copy Markdown

Requirement or Bug

Resolve #4904.

用户在 MCP 服务还在启动时提交首条消息,可能在整段会话中都看不到随后发现的工具。使用 /btw 或触发上下文压缩时,还可能遇到相同历史对应不同工具列表的问题,影响模型可见的工具和提示缓存前缀。

Bug Reproduction Steps

配置一个需要稍晚才能完成发现的 MCP 服务,开启新会话并立即提问。比较 tool_select.initialized 和首个 llm.tools_snapshot 中的工具名。关联 issue 提供了完整步骤和日志。

要复现评审指出的分叉问题,可以关闭渐进式工具加载,让父会话先看到一个延迟加载的 MCP 工具,再断开该服务并启动 /btw。检查父子会话实际发给模型的工具定义。相关评审讨论

Root Cause

创建 agent 时,Loop 提前捕获了启动工具清单。后续请求虽然会等待 MCP 发现结束,仍从早先捕获的清单构造模型请求。分叉又有自己的注册表,如果用子 agent 当前能查到的名字过滤继承清单,就会丢掉父 agent 已经捕获的定义。压缩请求也有独立的准备入口,过去会在等待 MCP 前筛选工具。

Code Changes

每个 agent 保存启动时的工具定义及注册来源。普通首轮、同配置分叉和压缩共用这份清单,最先需要它的路径等待初始发现、补入缺少的 MCP 定义并固定结果。后续请求按当前策略决定哪些延迟工具可以内联。工具来源只在引擎内部使用。

flowchart TD
    A["创建 agent,捕获初始定义与来源"] --> B["首次普通请求、同配置分叉或压缩"]
    B --> C["等待初始 MCP 目录,补入缺失定义并固定清单"]
    C --> D["普通请求和压缩按当前策略构造工具列表"]
    C --> E["同配置子 agent 继承清单"]
    E --> F["持有会话锁时,原子保存继承清单"]
    F --> G["恢复子 agent,应用当前策略和工具选择开关"]
    C --> H["后续发现走运行时工具目录"]
Loading

packages/core/src/loop/loop-actor.ts 负责首轮准备、取消和重试,沿用现有的 tools.settled 事件。packages/agent-core-v2/src/agent/loop/loopService.ts 在调用模型前固定基线。toolSelectService.ts 按来源匹配策略,并在需要内联时优先使用历史中已加载的同名定义。agentLifecycleService.ts 负责复制和恢复继承清单。这样,子 agent 即使从未注册过该工具,也能复用父 agent 的定义。

Behavior Changes and Affected Users

行为 修改前 修改后 受影响用户 可用控制
MCP 发现晚于首条消息 工具进入目录,但首轮及后续请求可能一直遗漏它 创建会话仍可立即返回,首个需要工具的操作等待初始发现并补齐清单 接入慢启动 MCP 的 CLI、桌面、Web、VS Code、ACP 和 SDK 用户 首轮可取消,后续提交可重试
关闭渐进加载,或模型不支持动态加载 当前 main 可内联已捕获的延迟工具。策略变更或存在同名历史定义时,请求可能使用错误的清单或定义 按真实来源执行当前策略,对获准的延迟项内联,优先使用历史中已加载的定义 关闭 tool-select 的会话和仅支持内联工具的模型 原有工具启用和禁用策略
创建同配置子 agent 或 /btw 首轮前分叉、服务断开或运行时新增工具时,父子模型请求可能不同 父子共享同一次启动快照,后续发现的工具仍通过原有运行时机制处理 使用 /btw 和同配置分叉的用户 指定其他 profile 的子 agent 使用自己的工具清单
恢复子 agent 从当前目录重建时可能丢掉已继承的定义,或加入仅在分叉后出现的工具 新创建的同配置子 agent 保存继承清单,在首次提问前退出也能恢复。恢复会应用当前策略和 select_tools 开关 重开会话、按 ID 恢复子 agent,以及使用 SDK includeSubagents 的客户端 旧会话缺少继承文件时沿用已有恢复路径
自动或手动压缩 首轮触发自动压缩可能遗漏启动工具,手动压缩可能使用等待期间已撤销的权限 压缩与普通请求共用已固定的清单,并在等待结束后读取工具策略 首轮输入较长或使用 /compact 的用户 当前工具策略在压缩请求中同样生效

inherited-tools.json 仅用于具有显式继承清单的子 agent,由持有会话写锁的客户端写入。主 agent 和独立绑定 profile 的 agent 继续从各自的配置恢复。客户端继续使用原有 CLI、SDK、REST、ACP 和 wire 接口。

测试与验证
  • 将新增回归场景放到修复前的 PR 提交 dfffdc6d7 上运行,有 10 个用例失败。修复后全部通过,包括真实回环 HTTP MCP 的断连、运行时发现、按 ID 恢复未提问的子 agent、工具来源与策略,以及压缩时序。
  • 合入最新 main 后,8 个相关引擎测试文件共 355 项通过,core 全部 65 项通过。SDK 的分叉与恢复用例通过,两份 SDK 测试文件共 125 项通过,suggestFiles 对忽略文件的断言失败,在干净的 main 上也能独立复现。
  • Node 24.15.0 下,agent-core-v2 全量共 7,783 项通过、6 项跳过、15 项失败,失败集中在 MCP OAuth 与工作区凭据刷新的两份测试文件,Vitest 另记录了 2 个 OAuth 未处理异常。用同一 Node 版本对照干净的 main(1ccbe4127),OAuth 测试文件也有 7 项失败,工作区凭据刷新的同一用例独立运行也会超时。
  • core 与 agent-core-v2 类型检查、引擎导入边界检查、注释检查以及类型感知 lint 通过。
  • 本 PR 先前已用源码 CLI 发起一次真实模型调用。7 个 MCP 服务中有 4 个在首条消息后才完成发现,全部 169 个已发现工具均出现在请求快照和模型列出的清单里。

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

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: b4b710e

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 17, 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-18T03:23:30.708082Z b4b710e 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.

@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: 866797a07c

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

@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: 62a6afbfca

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

@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: 9c39c13f8d

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

@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: 200e2ff795

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

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

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

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

ℹ️ 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 +327 to +329
await this.toolSelect.prepareCatalog(signal);
signal?.throwIfAborted();
return this.settleInitialTools();

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 Persist the source baseline settled by an early fork

The fresh remaining case after the earlier fork-preparation fix is a restart: when a same-profile fork is created before the source's first request, this path runs prepareCatalog(false) (so ToolSelectInitialized stays pending) and caches the completed source baseline only in settledTools, while the new child path writes inherited-tools.json. If the session is reopened before the source sends a request and the MCP catalog changed in the meantime (for example, a server was disabled), main rebuilds from the new catalog but the restored child keeps the old file, silently breaking request and prompt-cache parity. Publish or persist the source settlement here as well.

AGENTS.md reference: AGENTS.md:L131-L133

Useful? React with 👍 / 👎.

brandly added a commit to meridianlabs-ai/inspect_swe that referenced this pull request Nov 19, 2026
…263)

The Kimi Code bridged-tools test has been failing in the nightly since
Kimi Code 2.6.0. Kimi 2.6 connects to our MCP server and registers its
tools, but never puts them in the tool list it sends the model. On 2.5.3
the tool shows up as before. It's a known upstream regression,
MoonshotAI/kimi-code#4904: the agent loop fixes its tool list before MCP
discovery finishes. A fix is in review in MoonshotAI/kimi-code#5011.

The test also passed on some nights while 2.6 was out.
`check_bridged_tools` only checked the name of the call the model made,
and the model can guess `mcp__secrets__secret_lookup` from the prompt.
Inspect's bridge then refuses the call because the tool was never
offered, so the tool doesn't run, but the name check passes anyway.

This PR:

- adds a second assertion to `check_bridged_tools`: a `secret_lookup`
`ToolEvent` with no error that returned `ALPHA-SECRET-12345`. Only a
real host tool run produces that. This applies to every agent's
bridged-tools test.
- marks `test_kimi_code_bridged_tools` as strict xfail and links the
upstream issue. The nightly goes green, keeps floating on the latest
Kimi Code, and fails with XPASS once upstream fixes it. That's the cue
to remove the marker. The xfail only accepts the two bridged-tool
assertion messages, so a Kimi Code eval that errors or never takes a
turn still fails the nightly.

No migration required. Test-only change.

### Validation

There are no API keys locally, so I replayed eval logs instead of
calling a real model. I ran the `bridged_tools` example with `mockllm`
scripted to call `mcp__secrets__secret_lookup`, then fed those logs to
`check_bridged_tools` in place of `run_example`:

| Log | main | this branch |
|---|---|---|
| Kimi 2.5.3, tool runs and returns the secret | pass | pass |
| Kimi 2.6.3, model emits the right name, bridge rejects it | pass |
fail (`was not proposed by the model in a bridged generation`) |

Through pytest, with the replayed logs also going through
`run_example`'s completion checks, the 2.6.3 log gives `XFAIL` and the
2.5.3 log gives `XPASS(strict)`, which fails. A log from an eval that
errored fails outright rather than counting as the expected failure.

`make check` passes. `pytest` (fast suite): 804 passed, 74 skipped, 2
failed. Neither failure touches this change:

- `test_bundled_catalog_tracks_live_latest`: the live Codex catalog's
latest model is now `gpt-6.2-sol`, and the bundled fallback still says
`gpt-6-astra`.
- `test_bundle_cached_under_pre_libc_name_when_npm_lacks_libc`: the mise
npm shim recurses on my machine.

### Slow tests

- Bridged tools, all five agents, against this branch's head
(`24b863b`): [Inspect SWE Nightly Tests run
44390840703](https://github.com/meridianlabs-ai/actions/actions/runs/44390840703),
`pytest_args=tests/test_bridged_tools.py -rA`. 5 passed, 1 xfailed, 0
failed, 0 skipped. Claude Code, Codex CLI, Gemini CLI and OpenCode pass
the new assertion with real models. Kimi Code is the expected failure.
- Not run: the rest of the slow suite, since this change only touches
`tests/test_bridged_tools.py`.

### Agent review

- Reviewer: Claude Opus via a subagent (fresh context), 1 pass. It
traced the `ToolEvent` in inspect_ai's bridge source and confirmed the
event is unprefixed `secret_lookup` with a plain string result for every
agent, and that a call to a name the agent never offered is recorded
with a permission error.
- Findings: 3, all fixed. The xfail swallowed any `AssertionError`,
including an errored eval, so it now matches only this test's two
messages. The failure message now prints each result next to its error.
The xfail's issue link was a placeholder; it now points at
MoonshotAI/kimi-code#4904.
- It also flagged a risk: the grant check matches tools by description,
so an agent that rewrites MCP tool descriptions would now fail. The
nightly run above shows none of the other four agents do.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

[Bug] 2.6.0 在 MCP 发现完成前固定会话工具基线,导致部分 MCP 工具对整个会话不可见

1 participant