Files
plugins/packages/agy-cli/test/tool-budget.mjs
T
yetone 6646797e15 agy-cli 0.1.46: a catalog over agy's tool budget keeps every tool (#1140)
Since #989 every client tool is listed always-loaded, so all of them go
into agy's customization budget: 20,000 tokens counted as
ceil(UTF-8 bytes / 3). Past it, agy drops whole tools, the largest first,
and with tools: [] the model has no other way to reach them. tungloong's
131-tool catalog (~25,000 tokens) lost bash and 12 Paper tools.

Proc.listTools() now hands agy the catalog through fitTools
(tool-budget.mjs), aiming at 18,000 tokens to leave agy room for its name
prefix and serialization:
- within the budget, the catalog goes as it is (the same array);
- else $schema, title and examples come out where they are keywords (a
  property named "title" is the client's and stays);
- else every description is capped at the highest level L that fits,
  found by binary search: a property's at L bytes, a tool's own at 2.5L,
  a contract property's (an object with no properties, or tool-schema's
  Variants/Accepts anyOf text) at 11L. A cut drops a trailing Examples
  block first, ends on a sentence or a whole code point, and adds "…";
- at L = 0 the properties' descriptions go and the room left goes to the
  tools' own.
Names, property names, types, enum, const, required, items, anyOf and
bounds are never touched, and this.tools / toolsSignature stay as the
client sent them. The trimmed list is kept per catalog.

Run (WSL magpie-desktop, temp HOME; mote was at load 178 and mica's disk
is full): bun test 21 pass; node --test conversation-key, step-usage,
usage-settlement, repeat-batch, compat, tool-schema, catalog,
tool-budget, steer-in-session, windows-nc: 198 pass, 0 fail;
bun scripts/check.mjs agy-cli ok; npm pack --dry-run ships
tool-budget.mjs. With main's index.mjs the new bridge test fails (31,690
tokens reach agy). Not tried with a real agy: the budget and its
counting are the reporter's reading of agy 1.5.8, not checked here.
2026-11-14 18:34:29 +08:00

106 lines
4.8 KiB
JavaScript

import assert from "node:assert/strict"
import test from "node:test"
import { TOOL_TOKEN_BUDGET, agyTokens, cut, fitTools } from "../tool-budget.mjs"
const total = (tools) => tools.reduce((n, t) => n + agyTokens(JSON.stringify(t)), 0)
// a schema with its prose taken out, so two shapes compare by what the model
// may send
function structure(node) {
if (Array.isArray(node)) return node.map(structure)
if (!node || typeof node !== "object") return node
const out = {}
for (const [k, v] of Object.entries(node)) {
if (k === "description") continue
out[k] = k === "properties" ? Object.fromEntries(Object.entries(v).map(([n, s]) => [n, structure(s)])) : structure(v)
}
return out
}
const undecorated = (node) => {
if (Array.isArray(node)) return node.map(undecorated)
if (!node || typeof node !== "object") return node
const out = {}
for (const [k, v] of Object.entries(node)) {
if (["$schema", "title", "examples"].includes(k)) continue
out[k] = k === "properties" ? Object.fromEntries(Object.entries(v).map(([n, s]) => [n, undecorated(s)])) : undecorated(v)
}
return out
}
const long = (what, n) => `${what} does the thing it says. `.repeat(n) + "Examples: " + `${what}("a"), ${what}("b"). `.repeat(n)
// a client's catalog about plugins#1140's size: 80 tools, each with a
// long description, long property texts, an enum, a property named "title"
// and one contract property whose text is all there is to it
function catalog(n = 80, size = 2) {
return Array.from({ length: n }, (_, i) => ({
name: `tool_${i}`,
description: long(`tool_${i}`, size),
inputSchema: {
$schema: "https://json-schema.org/draft/2020-12/schema",
type: "object",
title: `Tool ${i}`,
properties: {
title: { type: "string", description: long("title", size), examples: ["x"] },
mode: { type: "string", enum: ["a", "b"], description: "Short." },
style: { type: "object", description: "Variants: " + long("style", size) },
list: { type: "array", items: { type: "string", description: long("item", size) } },
},
required: ["title"],
},
_meta: { "antigravity/alwaysLoad": true },
}))
}
test("a catalog within the budget goes as it is", () => {
const tools = catalog(10, 1)
assert.ok(total(tools) <= TOOL_TOKEN_BUDGET)
assert.equal(fitTools(tools), tools)
})
test("a catalog over the budget keeps every tool and its whole schema, and fits", () => {
const tools = catalog()
// about plugins#1140's 131 tools (~76 KB, ~25,000 agy tokens)
assert.ok(total(tools) > 20000 && total(tools) < 30000, `the fixture is over agy's budget (${total(tools)})`)
const fitted = fitTools(tools)
assert.ok(total(fitted) <= TOOL_TOKEN_BUDGET, `fits (${total(fitted)})`)
assert.deepEqual(fitted.map((t) => t.name), tools.map((t) => t.name))
for (let i = 0; i < tools.length; i++) {
// nothing that decides a valid argument changes: names, types, enum,
// required, items; a property named "title" is the client's and stays
assert.deepEqual(structure(fitted[i].inputSchema), structure(undecorated(tools[i].inputSchema)))
assert.ok(fitted[i].inputSchema.properties.title, "the property named title stays")
assert.equal(fitted[i].inputSchema.$schema, undefined)
assert.equal(fitted[i].inputSchema.title, undefined)
assert.deepEqual(fitted[i]._meta, tools[i]._meta)
const p = fitted[i].inputSchema.properties
// a short description is left whole; long ones are cut, a contract's least
assert.equal(p.mode.description, "Short.")
assert.ok(p.title.description.length < tools[i].inputSchema.properties.title.description.length)
assert.ok(p.style.description.length > p.title.description.length, "a contract keeps more")
assert.ok(fitted[i].description.length >= p.title.description.length, "a tool's own keeps more")
}
// the client's catalog itself is untouched
assert.equal(tools[0].inputSchema.title, "Tool 0")
})
test("a catalog too large for any description keeps every tool", () => {
const tools = catalog(400, 6)
const fitted = fitTools(tools)
assert.deepEqual(fitted.map((t) => t.name), tools.map((t) => t.name))
for (const t of fitted) assert.equal(t.inputSchema.properties.title.description, undefined)
})
test("cut stops on a whole character, within the bytes, and drops the examples first", () => {
const zh = "这是一个很长的中文说明,每个字是三个字节。".repeat(10)
for (const max of [10, 11, 12, 40, 100]) {
const out = cut(zh, max)
assert.ok(new TextEncoder().encode(out).length <= max, `${max}: ${out}`)
assert.ok(!out.includes("�"))
assert.ok(out.endsWith("…"))
}
assert.equal(cut("Reads a file. Examples: read('a'), read('b')", 20), "Reads a file.")
assert.equal(cut("short", 100), "short")
assert.equal(cut("anything longer", 3), null)
})