Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/cache-tool-batch-lookups.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@moonshot-ai/agent-core": patch
"@moonshot-ai/kimi-code": patch
---

Cache tool lookups during each tool-call batch.
18 changes: 15 additions & 3 deletions packages/agent-core/src/loop/tool-call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ export async function runToolCallBatch(
response: LLMChatResponse,
): Promise<ToolCallBatchResult> {
if (response.toolCalls.length === 0) return { stopTurn: false };
const calls = response.toolCalls.map((toolCall) => preflightToolCall(step.tools, toolCall));
const toolsByName = buildToolsByName(step.tools);
const calls = response.toolCalls.map((toolCall) => preflightToolCall(toolsByName, toolCall));
const scheduler = new ToolScheduler<PendingToolResult>();
const pendingResults: Array<Promise<PendingToolResult>> = [];
let stopTurn = false;
Expand Down Expand Up @@ -163,18 +164,29 @@ export async function runToolCallBatch(
return { stopTurn };
}

function buildToolsByName(
tools: readonly ExecutableTool[] | undefined,
): ReadonlyMap<string, ExecutableTool> | undefined {
if (tools === undefined || tools.length === 0) return undefined;
const byName = new Map<string, ExecutableTool>();
for (const tool of tools) {
if (!byName.has(tool.name)) byName.set(tool.name, tool);
}
return byName;
}

/**
* Provider-order validation pass. It does not run hooks, spawn tools, or write
* events. Validator compilation may populate the local cache.
*/
function preflightToolCall(
tools: readonly ExecutableTool[] | undefined,
toolsByName: ReadonlyMap<string, ExecutableTool> | undefined,
toolCall: ToolCall,
): PreflightedToolCall {
const toolName = toolCall.name;
const parsedArgs = parseToolCallArguments(toolCall.arguments);
const args = parsedArgs.success ? parsedArgs.data : {};
const tool = tools?.find((candidate) => candidate.name === toolName);
const tool = toolsByName?.get(toolName);
if (tool === undefined) {
return {
kind: 'rejected',
Expand Down
41 changes: 41 additions & 0 deletions packages/agent-core/test/loop/tool-call.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,47 @@ describe('runTurn — tool-call behaviour', () => {
});
});

it('uses the first registered tool when duplicate names are present', async () => {
const firstCalls: string[] = [];
const secondCalls: string[] = [];
const first: ExecutableTool = {
name: 'dupe',
description: 'first duplicate tool',
parameters: { type: 'object', additionalProperties: true },
resolveExecution: () => ({
approvalRule: 'dupe',
execute: async (ctx) => {
firstCalls.push(ctx.toolCallId);
return { output: 'first' };
},
}),
};
const second: ExecutableTool = {
name: 'dupe',
description: 'second duplicate tool',
parameters: { type: 'object', additionalProperties: true },
resolveExecution: () => ({
approvalRule: 'dupe',
execute: async (ctx) => {
secondCalls.push(ctx.toolCallId);
return { output: 'second' };
},
}),
};

const { context } = await runTurn({
tools: [first, second],
responses: [
makeToolUseResponse([makeToolCall('dupe', {}, 'tc-1')]),
makeEndTurnResponse('done'),
],
});

expect(firstCalls).toEqual(['tc-1']);
expect(secondCalls).toEqual([]);
expect(context.toolResults()[0]?.result.output).toBe('first');
});

it('records an error tool.result when the tool name is unknown', async () => {
const { sink, context } = await runTurn({
tools: [], // no tools at all
Expand Down