Repository navigation
fix(agent-core-v2): unload only previously declared dynamic tools - #5438
Merged
Merged
Conversation
Pair tool_lifecycle.removed with earlier system tool-schema declarations when shaping request history so catalog-only removals and repeated deltas do not emit native unload_tools, while preserving persisted history and loaded MCP unload/reload ordering.
🦋 Changeset detectedLatest commit: 44136f6 The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 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 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
approved these changes
Nov 26, 2026
6 tasks done
6 tasks done
This was referenced Nov 27, 2026
7723qqq
pushed a commit
to 7723qqq/kimi-code
that referenced
this pull request
Dec 2, 2026
`bun scripts/upstream-ledger.mjs --refresh` at upstream/main 4eb2919 added 126 rows, so every open upstream commit and every PR it references now has a ledger row: 0/596, down from 125 at 8bf8ba3 and 126 at the fetched tip. The refresh only reads `upstream/main ^ main`, so the three release-2.6.2 commits named by the mission (MoonshotAI#4875/MoonshotAI#4905/MoonshotAI#4922) gained rows with the refresh's row shape. Settled with live probes in this tree: - ported: MoonshotAI#5533 (docs 2.8.1 changelog), MoonshotAI#4875 (registry search_url and feedback_url plus the feedback service), MoonshotAI#4905 (Kimi upload status validation in packages/core), MoonshotAI#4922 (swarm task events in sessionTaskFeed), MoonshotAI#5438 (unload only declared dynamic tools), MoonshotAI#5592 (dynamic tool replacement order), MoonshotAI#5595 (append-log known length), MoonshotAI#5573 (skill roots for every work dir), MoonshotAI#5576 (date reminder cwd comparison), MoonshotAI#5618 (session titles for skill openers), MoonshotAI#5638 (frozen loop tool baseline), MoonshotAI#5626 (legacy turn-input origins) - partial: MoonshotAI#5505 (processLiveness source and consumers present; the upstream unit test file was never carried), MoonshotAI#5571 (program-resume and OSC 7577 present; the upstream test helper was never carried), MoonshotAI#5532 (gitService half present; fsService still leaks the environment lease at fsService.ts:1133 and :1267), MoonshotAI#5557 (app-client icon registry kept; the move to app-ui was never carried) - not-applicable: MoonshotAI#4655 (upstream reverted auto review in MoonshotAI#5649), MoonshotAI#5333 (PLAYBOOK:111 deliberate flat experimental flags) M39's "likely present" list was checked, not trusted: MoonshotAI#5438 is present (upstream test passes), MoonshotAI#5487 is not (nativeStartSettleMs/leg.nativeSettled absent), MoonshotAI#5635 is not (ToolsSettled absent), MoonshotAI#5429 needs a visual probe. The remaining 194 pending rows stay open with their reasons; no feature was ported and no upstream-drift allowlist entry was added. Verification: verify -> 332 closed rows, 332 carry every file of their PR; 317 probes pass, 2 foreign-runner skips, 0 fail; a second verify is byte-identical (ledger sha256 cbf53638...). Probe tests 29 pass; bun run lint and node scripts/checks/ratchet.mjs green.
7723qqq
pushed a commit
to 7723qqq/kimi-code
that referenced
this pull request
Dec 2, 2026
The upstream ledger reads `upstream/main ^main`, so a commit that only ever existed on a `@moonshot-ai/kimi-code@<version>` release branch was never enumerated: 89 commits reachable from 2.6.0-2.8.1 are absent from upstream/main, 20 of them are owned by an existing main-ledger row (by sha or PR), and the other 69 had no row anywhere and no path into the ledger at all. `.upstream/release-ledger.tsv` now carries one row per release-only commit the main ledger does not own, in the main ledger's 11 columns plus a `tags` column recording every version that reaches the commit. `scripts/upstream-ledger.mjs` gains two entries that share `scripts/lib/upstream-ledger-probes.mjs` with the existing tooling: - `--release-refresh` fetches upstream main, enumerates the tag union, refreshes each row's tag list, and adds a pending row for every commit neither ledger owns. Rows whose commit is no longer release-only are reported, never deleted. - `--release-verify` fails by name when a release tag commit has no row in either ledger, runs each release row's probe with the same runner-aware skip semantics as the main verify, holds `ported` rows to the shared per-commit file-completeness check, and re-runs the live probes of release commits the main ledger owns, so MoonshotAI#4875/MoonshotAI#4905/MoonshotAI#4922 stay on their probes instead of getting duplicate rows. The initial classification is evidence-based: 15 release-metadata commits are not-applicable with a written reason, 1 is ported (its patch reverse-applies), 16 are equivalent with live grep/test probes, 13 are partial with the present and absent halves named, and 24 are pending with file and added-line evidence. M39's "confirmed absent" list was partly stale: MoonshotAI#4995's Stop-hook code is in the tree, MoonshotAI#5354's one-line change is fully present, MoonshotAI#5516 is the twin of the ported MoonshotAI#5438, and MoonshotAI#5096/MoonshotAI#5191/MoonshotAI#5303 are present at source level, so those rows carry the evidence that says so rather than an absence claim. Verified: release verify -> 44 probes pass, 0 skipped, 0 fail, completeness 1/1, 0 missing rows, 0 hidden failures; main ledger verify -> 317 probes pass, 2 skipped, 0 fail, 332/332 closed rows complete; upstream-ledger-release fixture test 16 pass; upstream-ledger-probes 29 pass; bun run lint 0 warnings 0 errors and ratchet green on 6995 files.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Requirement or Bug
使用支持动态工具加载(
dynamically_loaded_tools)的 Kimi 模型时,如果某个工具只在<tools_added>目录公告里出现过名字、从未通过select_tools向模型声明过 schema,之后该工具从注册表消失(例如宿主在创建会话后收窄内置工具名单再重启恢复、未加载的 deferred MCP 被停用/删除、或/tower off),Core 仍会在请求里发送unload_tools。严格校验的网关会拒绝“卸载未声明工具”,导致恢复后的会话无法继续对话。本 PR 让unload_tools只与请求内此前同名的systemschema 声明消息配对,旧会话无需迁移即可恢复。Bug Reproduction Steps
dynamically_loaded_tools的 Kimi 协议模型,并注册一个 deferred MCP 服务(或先全量注册内置工具、再通过策略禁用后重启恢复会话)。<tools_added>中看到工具名,不调用select_tools加载其 schema。{ "role": "system", "unload_tools": ["<从未声明的工具名>"] }。Root Cause
根因是把“注册表/可加载目录变化”直接等同于“模型侧 schema 卸载”:
AgentToolSelectService.reconcile()对比持久化账本与实时toolRegistry.list(),把消失的工具记入removed;renderDeltaAnnouncement()不区分这些工具是否曾注入system.tools,全部写入tool_lifecyclemeta 的removed。shapeActiveHistory()虽然已经在按顺序维护declaredDynamicTools(本次请求里实际保留的system.tools声明),但只用它做同名 schema 去重,没有过滤公告上的meta.removed。kimiOpenAIProtocol.message()看到非空meta.removed就无条件追加unload_tools,而后端按单次无状态请求判断工具是否声明过。这是对“卸载声明生成规则”的根本修复(不针对特定工具加名单);宿主分步创建会话导致的注册表与账本不一致不在本 PR 重做。
Code Changes
只改请求出站视图整形的一处边界,不改 loop 持久化历史、不改
reconcile()的目录公告逻辑,下游 Kimi 协议层与legalize-tools无需改动:shapeActiveHistory(messages, tools) for message of messages if isLoadableToolsAnnouncement(message) - delete meta.removed names from declaredDynamicTools + keep only names deleted from declaredDynamicTools in outbound meta.removed next = shapeActiveMessage(message, inlineNames, declaredDynamicTools)system.tools(自动排除当前请求顶层inlineNames),而不是<tools_added>。<tools_removed>公告之后,保证已加载工具同名更新的旧 system.tools→unload_tools→新 system.tools时序,并避免改动历史前缀破坏缓存。<tools_removed>正文保持不变,模型仍能得知哪些名字不再可select_tools;未发生过滤的消息保持原引用,落盘历史完全不改写。阅读顺序:先看
packages/agent-core-v2/src/agent/toolSelect/toolSelectService.ts的shapeActiveHistory/shapeLifecycleAnnouncement,再看packages/agent-core-v2/test/agent/toolSelect/toolSelectService.test.ts的恢复与未加载更新回归用例;docs/en/llm.md与 changeset 可略读。Behavior Changes and Affected Users
removed)unload_toolsmeta.removed剔除,不发unload_tools;<tools_removed>正文与落盘历史保留unload_toolssystem.tools声明profile.tools声明的 inline 工具出现在历史removed中unload_toolssystem.tools加载的 MCP / deferred 工具被移除或同名 schema 更新unload_tools,同名更新按“旧定义 → unload → 新定义”发送unload_toolsdynamically_loaded_tools的模型及其他 provider受影响模块与测试覆盖:
packages/agent-core-v2/src/agent/toolSelect/toolSelectService.ts:新增出站公告的removed过滤。packages/agent-core-v2/test/agent/toolSelect/toolSelectService.test.ts:新增恢复会话只卸载已声明system.tools工具的回归用例,并补强未加载同名更新不产出unload_tools的断言(修复前两条均失败,修复后通过)。验证记录
vitest run packages/agent-core-v2/test/agent/toolSelect packages/agent-core-v2/test/agent/loop/coreLoopService.test.ts packages/agent-core-v2/test/session/subagent/forkParity.test.ts:5 文件 162/162 通过。@moonshot-ai/core全量测试:37 文件 402/402 通过。@moonshot-ai/agent-core-v2全量测试:8230 通过 / 13 跳过 / 2 失败——失败均位于test/app/git/gitService.test.ts(git sparse-checkout读取config.worktree报ENOENT,为测试机 git 版本行为差异,与本 PR 改动无交集)。tsc -p packages/agent-core-v2/tsconfig.json --noEmit:通过。pnpm lint(含 check-comments、type-aware oxlint--deny-warnings、import-boundaries、guards、ratchet、instructions 等)0 warning / 0 error;pnpm run check:duplication通过。unload_tools的协议校验属于独立配套项);Windows 专项(不涉及平台路径/进程行为)。Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.