kimi-code icon indicating copy to clipboard operation
kimi-code copied to clipboard

[Bug] Compaction forgets dynamically loaded (select_tools) MCP tools

Open vastStage opened this issue 2 weeks ago • 1 comments

What version of Kimi Code is running?

2.6.3 (source main snapshot, 2026-11-18). Confirmed by reading the compaction and tool-selection code.

Which open platform/subscription were you using?

None involved (code-level audit).

Which model were you using?

A model with dynamically_loaded_tools capability, with at least one deferred MCP server configured.

What platform is your computer?

Windows 11 x64.

What issue are you seeing?

After a full compaction, tools that were loaded through select_tools are forgotten, even though they were active before the compaction.

How the two sides work:

  • Loading a deferred tool writes a system message with origin { kind: 'injection', variant: 'dynamic_tool_schema' } (packages/agent-core-v2/src/agent/toolSelect/dynamicTools.ts:9).
  • The tool-selection service derives the currently loaded tools only from history: requestTools() (packages/agent-core-v2/src/agent/toolSelect/toolSelectService.ts:153-172) and shapeToolsForDisclosure() (:470-491) both call collectLoadedDynamicTools(messages) (dynamicTools.ts:109-127), which scans for those system messages.
  • The compaction rebuild (packages/agent-core-v2/src/agent/contextMemory/compactionHandoff.ts:126-131) keeps only: user messages selected by the head/tail policy, the summary, the continuation anchor, and injections appended after compactedCount. System messages are not kept, and appendedInjections only covers messages after the compaction snapshot, so every dynamic_tool_schema message is dropped.

After compaction, collectLoadedDynamicTools(history) returns an empty map. For deferred tools this means:

  • requestTools() no longer adds the previously loaded tools to the request.
  • shapeToolsForDisclosure() no longer marks them as loaded.
  • The model has to call select_tools again to re-load tools it already loaded, and may make tool calls that were valid moments earlier.

The loadable-tools announcement (isLoadableToolsAnnouncement) lives in the same history-based path and is dropped too.

What steps can reproduce the bug?

  1. Configure an MCP server with deferred tools and use a model with the dynamically_loaded_tools capability.
  2. Load one of those tools via select_tools and verify it can be called.
  3. Run /compact (or let auto-compaction run).
  4. Inspect the next request: the loaded tool is gone from tools[], and the model must re-run select_tools.
  5. In a unit test: build a context containing a role: 'system' message with origin: { kind: 'injection', variant: 'dynamic_tool_schema' }, apply buildContextCompactionShape, and assert that collectLoadedDynamicTools(compacted.messages) is non-empty (it currently returns an empty map).

What is the expected behavior?

Tools that were loaded before compaction should remain loaded after compaction. Either the dynamic_tool_schema messages (and the announcement) should be carried into the rebuilt history, or the loaded-tool set should live in a state that compaction does not reset.

Additional information

Suggested fix: in buildContextCompactionShape, carry forward the tool-selection system/injection messages that describe loaded dynamic tools (they are small compared with the summary and are behavior, not history), or teach createCompactionSummaryMessage/the continuation message to re-emit them from tool-selection state. A regression test would pin collectLoadedDynamicTools across a compaction round trip.

Related but distinct: #4389 described the compaction request losing the prompt cache because it strips the announcement; this issue is about the rebuilt context losing loaded tools.

Contribution

  • [x] I am willing to submit a PR for this bug fix myself (please wait for maintainer approval in this issue first)

vastStage avatar Nov 18 '26 18:11 vastStage

Reproduced with a direct compaction round trip (temporary test, reverted afterwards):

const schemaMessage = {
  role: 'system',
  content: [{ type: 'text', text: 'loaded tool schema' }],
  tools: [{ name: 'mcp__srv__foo', description: 'foo', parameters: { type: 'object' } }],
  origin: { kind: 'injection', variant: 'dynamic_tool_schema' },
};
const userMessage = {
  role: 'user',
  content: [{ type: 'text', text: 'hello' }],
  origin: { kind: 'user' },
};
const history = [schemaMessage, userMessage];
const shape = buildContextCompactionShape(history, {
  summary: 'summary',
  contextSummary: 'summary',
  compactedCount: history.length,
  tokensBefore: 10,
});
collectLoadedDynamicTools(shape.messages);

Observed: LOADED_AFTER_COMPACTION []; expected ['mcp__srv__foo'].

buildContextCompactionShape keeps only user messages plus the summary/continuation, so the role: 'system' schema message is gone. Both toolSelectService.requestTools() and shapeToolsForDisclosure() derive the loaded set from collectLoadedDynamicTools(history), so on the next request the tool is treated as never loaded.

I will include this round-trip test in the PR.

vastStage avatar Nov 18 '26 19:11 vastStage