Repository navigation
fix(agent-core-v2): pin the initial tool baseline after startup MCP discovery - #5240
Conversation
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
commit: |
7Sageer
left a comment
There was a problem hiding this comment.
LGTM。等待点收敛在基线固定之前,显式继承的 fork/btw 基线保持一致,MCP 工具可见性与取消路径都有回归覆盖。已在本机运行该 PR 涉及的定向用例,相关测试通过。
ea19860 to
007a9d9
Compare
🦋 Changeset detectedLatest commit: b81d3ca The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
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 |
💡 Codex Reviewkimi-code/packages/agent-core-v2/src/agent/toolSelect/toolSelectService.ts Lines 107 to 108 in 007a9d9 When a deployment leaves the experimental flag at its default-off value or explicitly sets AGENTS.md reference: AGENTS.md:L195-L197 kimi-code/packages/agent-core-v2/src/agent/toolSelect/toolSelectService.ts Lines 150 to 152 in 007a9d9 When a deferred or runtime-added tool has been loaded through AGENTS.md reference: AGENTS.md:L207-L209 When a prompt is submitted before MCP readiness settles, this await leaves it in AGENTS.md reference: AGENTS.md:L203-L205 When a contentless system message contains only tool declarations that duplicate a later message or ℹ️ About Codex in GitHubCodex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
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". |
007a9d9 to
b81d3ca
Compare
Loop initialization now waits for the session MCP handle's ready before pinning the tool baseline, and mcpService drops its per-step and per-tool-call waits, so tools from slow-starting servers stay visible for the session (MoonshotAI#5240). The obsolete waits are covered by the loop suites (mcp 44, loop+toolSelect 176).
Part 7/7 of the loop stack onto
main. Identical diff to #5217, previously reviewed and merged into the loop integration branch. Stacked on #5239; the shown diff contains only this PR's own changes.Requirement or Bug
Resolve #4904。
2.6.0 引入的回归:会话在初始 MCP 发现完成前就固定了工具基线,启动较慢的 MCP 服务器发现的工具对整个会话不可见——
tool_select.initialized包含全部工具,但llm.tools_snapshot缺失这些工具,后续 turn 也不会恢复。本 PR 之后,启动期发现的 MCP 工具从首个请求起就在请求工具列表里。Bug Reproduction Steps
See linked issue(慢速 stdio MCP 服务器 + 新会话立即提交 prompt,观察首个请求的
llm.tools_snapshot)。Root Cause
根本修复,非绕过。
agent 的请求工具列表来自 loop 启动时钉死的基线(
profile.tools),而不是 toolSelect 的工具目录。initializeLoop()捕获基线时不等待 MCP 就绪,钉入的是启动瞬间的注册表快照;之后没有任何路径重建基线。启动期稍晚发现的 MCP 工具只进入 toolSelect 目录和公告,永远到不了请求——这就是 issue 里"目录有、请求没有"的分层错位。修复把"等待 MCP 首轮发现 settle"放到钉基线之前:等待之后注册表必然完整(connected 事件同步注册工具,且先于连接尝试 resolve),此刻钉入的基线天然包含启动期发现的全部 MCP 工具。
Code Changes
核心思路:把 MCP 就绪等待收敛到 loop 启动这唯一一处,钉基线发生在等待之后;会话过程中不再有任何对 MCP 连接的 await,工具增删改全部走既有的事件驱动 reconcile。
关键决策:
initializeLoop钉基线前prepareInitialTools钩子(issue 评论区的方案)packages/core,不新增钩子机制,改动面最小unprovideUnit('reminder')会级联 disposeAgentLoopService),留作后续项阅读指南:先看
packages/agent-core-v2/src/agent/loop/loopService.ts的initializeLoop(全部修复就这一处等待),再看mcpService.ts与toolSelectService.ts的删除(纯收敛),测试看toolSelect.e2e.test.ts的启动发现三例与forkParity.test.ts的基线一致两例。可略读:changeset 与测试桩清理。Behavior Changes and Affected Users
POST /sessions、ACPsession/new、desktop 打开会话startupTimeoutMs;不配置 MCP 无影响turn.endedcancelled)受影响模块与测试覆盖:
loopService.initializeLoop(等待 + 钉基线):toolSelect.e2e.test.ts启动发现公告用例、disclosure 形态矩阵(inline/deferred × 开关)、"启动等待期间按 id 取消排队 prompt"用例;forkParity.test.tsstartup/runtime 两用例断言 fork 基线与父 loop 基线完全一致。mcpService钩子删除:mcp.test.ts+ 上述 e2e(原 "holds the LLM step" 用例随钩子删除,契约由 e2e 在真实入口覆盖)。reconcileForRequest等待删除:toolSelectService.test.ts+ e2e。forkParity.test.ts新增 2 用例。验证记录
tsc -p packages/agent-core-v2/tsconfig.json --noEmit通过。check-comments.mjs与check:duplication在 jj-only 工作区因 git 根解析上溯无法整体运行(预存在的工具问题),已改为对全部改动文件显式验证无注释。testkitAgentFixtures+model.script,fork 子代理走subagentlane);toolSelect.e2e 的 legacymockNextResponse计数 44 → 42(净减少,基线已随 PR 下调),forkParity 保持 14 零新增。forkParity 新用例将 btw fork 安排在父代理任何 turn 之前:harness 的PersistenceAppendLogStore.read不按 scope 过滤,父代理已有 turn 时子代理 restore 会 replay 到父记录、触发initializeHistory的 BugIndicatingError(基线同样复现的 harness 限制),父历史为空时 fork 不调用 initializeHistory,语义不变。Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.