From cf36ae1f9f979a1d06854a4bffca505a9c10af32 Mon Sep 17 00:00:00 2001 From: Epinephrine Date: Mon, 16 Nov 2026 12:06:44 +0900 Subject: [PATCH] fix(kiro): keep unbounded tool catalog admission linear --- src/adapters/kiro-tools.ts | 15 ++++--- structure/providers/kiro.md | 4 +- .../providers/kiro/kiro-tool-catalog.test.ts | 39 ++++++++++++++++++- 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/src/adapters/kiro-tools.ts b/src/adapters/kiro-tools.ts index 9beaec58bd6..810e6c359f1 100644 --- a/src/adapters/kiro-tools.ts +++ b/src/adapters/kiro-tools.ts @@ -282,14 +282,13 @@ export function convertKiroToolContext( const filled: unknown[] = []; for (const entry of candidates) { if (entry === reserved) continue; - // Measure the projected FINAL array: the byte budget is computed over the serialized array, so - // subtracting a standalone size would misjudge it by the separators JSON adds between entries. - const projected = reserved ? [...filled, entry.converted, reserved.converted] : [...filled, entry.converted]; - if ( - !unbounded - && (projected.length > MAX_KIRO_TOOL_COUNT - || serializedToolCatalogBytes(projected) > MAX_KIRO_TOOL_CATALOG_BYTES) - ) break; + if (!unbounded) { + // Measure the projected FINAL array only while enforcing a budget. Repeatedly copying + // the admitted prefix would make an unbounded catalog quadratic even without measuring it. + const projected = reserved ? [...filled, entry.converted, reserved.converted] : [...filled, entry.converted]; + if (projected.length > MAX_KIRO_TOOL_COUNT + || serializedToolCatalogBytes(projected) > MAX_KIRO_TOOL_CATALOG_BYTES) break; + } filled.push(entry.converted); admitted.add(entry.index); } diff --git a/structure/providers/kiro.md b/structure/providers/kiro.md index 2dcd1714ce0..9fd7d8717f2 100644 --- a/structure/providers/kiro.md +++ b/structure/providers/kiro.md @@ -154,7 +154,9 @@ chooses entries, so the byte limit measures the truncated strings. `OCX_KIRO_TOOL_DESCRIPTION_LIMIT=off` (`KIRO_TOOL_DESCRIPTION_LIMIT_ENV`) are operator opt-outs read at request-conversion time. Turning the budget off removes both limits, so the priority sort and the `exec` reservation have nothing to exclude: every declared tool goes on the wire in -declaration order and no omission notice is emitted. An oversized catalog therefore reaches Kiro +declaration order and no omission notice is emitted. Admission does not build or measure projected +catalogs when the budget is off, so it performs linear work in the number of converted entries. +An oversized catalog therefore reaches Kiro intact and the upstream `ValidationException` becomes the failure mode instead of a trimmed request. The default remains bounded because that is the shape a fresh Kiro account is measured to accept. diff --git a/tests/providers/kiro/kiro-tool-catalog.test.ts b/tests/providers/kiro/kiro-tool-catalog.test.ts index e239307ff49..9e8715e5823 100644 --- a/tests/providers/kiro/kiro-tool-catalog.test.ts +++ b/tests/providers/kiro/kiro-tool-catalog.test.ts @@ -3,7 +3,7 @@ import { mkdtempSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { createKiroAdapter } from "../../../src/adapters/kiro"; -import { KIRO_TOOL_BUDGET_ENV, KIRO_TOOL_DESCRIPTION_LIMIT_ENV, MAX_KIRO_TOOL_CATALOG_BYTES, MAX_KIRO_TOOL_COUNT } from "../../../src/adapters/kiro-tools"; +import { convertKiroToolContext, KIRO_TOOL_BUDGET_ENV, KIRO_TOOL_DESCRIPTION_LIMIT_ENV, MAX_KIRO_TOOL_CATALOG_BYTES, MAX_KIRO_TOOL_COUNT } from "../../../src/adapters/kiro-tools"; import type { OcxParsedRequest, OcxProviderConfig } from "../../../src/types"; import { removeTreeWithRetry } from "../../helpers/remove-tree"; @@ -87,6 +87,43 @@ describe("kiro catalog budget opt-out", () => { expect(content).not.toContain("Kiro's outbound catalog budget allows"); }); + for (const reserveExec of [false, true]) { + test(`off visits converted tools linearly (reserved exec: ${reserveExec})`, () => { + process.env[KIRO_TOOL_BUDGET_ENV] = "off"; + const tools = Array.from({ length: 2_048 }, (_, index) => ({ + name: `tool_${index}`, description: "Tool", parameters: { type: "object" }, + })); + const catalog = reserveExec + ? [...tools, { name: "exec", description: "Execute code", freeform: true, parameters: { type: "object" } }] + : tools; + // Count actual array traversal rather than wall time. Abort repeated copying early, so + // the regression remains bounded even if the quadratic fill loop returns. + const iterate = Array.prototype[Symbol.iterator]; + let convertedVisits = 0; + let result: ReturnType; + try { + Array.prototype[Symbol.iterator] = function () { + const iterator = iterate.call(this); + const next = iterator.next.bind(iterator); + iterator.next = () => { + const step = next(); + if (!step.done && step.value && typeof step.value === "object" && Object.hasOwn(step.value, "toolSpecification")) { + if (++convertedVisits > catalog.length * 4) throw new Error("unbounded catalog repeatedly traversed converted tools"); + } + return step; + }; + return iterator; + }; + result = convertKiroToolContext(parsedWith([{ role: "user", content: "hi" }], catalog)); + } finally { + Array.prototype[Symbol.iterator] = iterate; + } + expect(convertedVisits).toBeLessThanOrEqual(catalog.length * 4); + expect(result.tools.map(tool => (tool as { toolSpecification: { name: string } }).toolSpecification.name)).toEqual(catalog.map(tool => tool.name)); + expect(result.systemAdditions).toEqual([]); + }); + } + test("any other value keeps the budget", async () => { process.env[KIRO_TOOL_BUDGET_ENV] = "0"; const { names } = await emitted();