Skip to content
Closed
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
15 changes: 7 additions & 8 deletions src/adapters/kiro-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 3 additions & 1 deletion structure/providers/kiro.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
39 changes: 38 additions & 1 deletion tests/providers/kiro/kiro-tool-catalog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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<typeof convertKiroToolContext>;
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();
Expand Down
Loading