Skip to content

Commit 0906201

Browse files
authored
fix(kiro): prioritize tool search results within catalog budget (#2475)
1 parent 0f30b39 commit 0906201

2 files changed

Lines changed: 55 additions & 9 deletions

File tree

‎src/adapters/kiro-tools.ts‎

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,12 @@ function omittedToolCatalogNotice(kept: number, omitted: readonly OcxTool[], reg
172172
return `[opencodex] Kiro's outbound catalog budget allows ${kept} of ${kept + omitted.length} client tools this turn. Omitted and unavailable this turn: ${summary}.`;
173173
}
174174

175+
function boundedCatalogPriority(tool: OcxTool): number {
176+
if (tool.loadedFromToolSearch) return 0;
177+
if (tool.toolSearch) return 1;
178+
return 2;
179+
}
180+
175181
export function convertKiroToolContext(
176182
parsed: OcxParsedRequest,
177183
registry: KiroToolNameRegistry = createKiroToolNameRegistry(),
@@ -181,9 +187,7 @@ export function convertKiroToolContext(
181187
// Validate every listed name even when tool_choice:none emulates a tool-free turn.
182188
for (const tool of tools) registry.alias(namespacedToolName(tool.namespace, tool.name));
183189
const effectiveTools = parsed.options.toolChoice === "none" ? [] : tools;
184-
const convertedTools: unknown[] = [];
185-
let omittedAt = effectiveTools.length;
186-
for (const [index, tool] of effectiveTools.entries()) {
190+
const convertedEntries = effectiveTools.map((tool, index) => {
187191
const description = tool.description || `Tool: ${tool.name}`;
188192
// Send the full namespaced wire name (e.g. mcp__chrome-devtools__navigate_page) so Kiro echoes
189193
// it back; the bridge's toolNsMap is keyed by this name and restores the MCP namespace Codex
@@ -198,19 +202,26 @@ export function convertKiroToolContext(
198202
inputSchema: { json: ensureRootObjectType(sanitizeKiroSchema(tool.parameters ?? {})) },
199203
},
200204
};
201-
// Preserve declaration order and only omit a suffix. Ranking tools would make a catalog change
202-
// silently alter which capability disappears; this deterministic policy is paired with a
203-
// model-visible omission notice so unavailable tools are explicit rather than assumed absent.
205+
return { tool, index, converted };
206+
});
207+
const exceedsBudget = convertedEntries.length > MAX_KIRO_TOOL_COUNT
208+
|| serializedToolCatalogBytes(convertedEntries.map(entry => entry.converted)) > MAX_KIRO_TOOL_CATALOG_BYTES;
209+
const candidates = exceedsBudget
210+
? convertedEntries.toSorted((a, b) => boundedCatalogPriority(a.tool) - boundedCatalogPriority(b.tool) || a.index - b.index)
211+
: convertedEntries;
212+
const convertedTools: unknown[] = [];
213+
let omittedAt = candidates.length;
214+
for (const [index, entry] of candidates.entries()) {
204215
if (
205216
convertedTools.length >= MAX_KIRO_TOOL_COUNT
206-
|| serializedToolCatalogBytes([...convertedTools, converted]) > MAX_KIRO_TOOL_CATALOG_BYTES
217+
|| serializedToolCatalogBytes([...convertedTools, entry.converted]) > MAX_KIRO_TOOL_CATALOG_BYTES
207218
) {
208219
omittedAt = index;
209220
break;
210221
}
211-
convertedTools.push(converted);
222+
convertedTools.push(entry.converted);
212223
}
213-
const omittedTools = effectiveTools.slice(omittedAt);
224+
const omittedTools = candidates.slice(omittedAt).map(entry => entry.tool);
214225
return {
215226
tools: convertedTools,
216227
systemAdditions: omittedTools.length > 0 ? [omittedToolCatalogNotice(convertedTools.length, omittedTools, registry)] : [],

‎tests/kiro-adapter.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,41 @@ describe("kiro adapter — buildRequest", () => {
724724
expect(current.content).toContain("Omitted and unavailable this turn");
725725
});
726726

727+
test("large catalogs prioritize tool-search discoveries and the search gateway", async () => {
728+
const ordinaryTools = Array.from({ length: MAX_KIRO_TOOL_COUNT + 20 }, (_, index) => ({
729+
name: `ordinary_tool_${String(index).padStart(3, "0")}`,
730+
description: `Ordinary tool ${index}`,
731+
parameters: { type: "object" },
732+
}));
733+
const searchGateway = {
734+
name: "tool_search",
735+
description: "Search deferred tools",
736+
parameters: { type: "object" },
737+
toolSearch: true,
738+
};
739+
const loadedTool = {
740+
name: "codex_app__send_message_to_thread",
741+
description: "Send a message to a task",
742+
parameters: { type: "object" },
743+
loadedFromToolSearch: true,
744+
};
745+
const tools = [...ordinaryTools, searchGateway, loadedTool];
746+
747+
const current = JSON.parse((await createKiroAdapter(provider).buildRequest(
748+
parsedWith([{ role: "user", content: "hi" }], tools),
749+
)).body).conversationState.currentMessage.userInputMessage;
750+
const ordinary = current.userInputMessageContext.tools.slice(0, -1);
751+
const names = ordinary.map((tool: { toolSpecification: { name: string } }) => tool.toolSpecification.name);
752+
const omissionNotice = current.content.split("\n\n", 1)[0];
753+
754+
expect(ordinary).toHaveLength(MAX_KIRO_TOOL_COUNT);
755+
expect(names.slice(0, 2)).toEqual([loadedTool.name, searchGateway.name]);
756+
expect(names.slice(2)).toEqual(ordinaryTools.slice(0, MAX_KIRO_TOOL_COUNT - 2).map(tool => tool.name));
757+
expect(omissionNotice).toContain("ordinary_tool_046");
758+
expect(omissionNotice).not.toContain(loadedTool.name);
759+
expect(omissionNotice).not.toContain(searchGateway.name);
760+
});
761+
727762
test("large catalogs retain the declared prefix within Kiro's serialized byte budget", async () => {
728763
// Top-level descriptions stay small, so existing description truncation cannot make this pass.
729764
// The repeated schema descriptions instead make the aggregate converted catalog exceed 96 KiB.

0 commit comments

Comments
 (0)