agy-cli: no stub descriptions; a low level's room goes to tools and contracts (#1140)
tungloong's real run of agy-cli 0.1.49 with 131 tools: the schemas alone
took ~37 KB, the level stopped at 10, and 373 of 382 property texts went
as 9-10 byte stubs ("The fil…") that spent ~10 KB on their keys, while
the tools' own descriptions got 25 bytes and show_view's contract 110.
With more tools the level stopped at 1-6 and never reached the bare
branch, which would have zeroed the contracts too.
A cut description now goes whole or, cut, with at least MIN_TEXT (24)
bytes; anything shorter is dropped. When the highest level leaves an
ordinary property under that, ordinary properties keep only texts short
enough to go whole, and a second search gives the room to the tools'
own descriptions and the contracts' (the old bare(top) branch is gone).
Run (mote): packages/agy-cli bun test 21 pass (each test file in its
own bun process); bun scripts/check.mjs agy-cli ok. On a 131-tool
fixture shaped like the report (~285 B of schema per tool), 0.1.49
gave field "field…", tool "tool_0 does the t…"; now no field stubs, tool
"tool_0 does the thing it says.…" and a longer contract. Broken by
hand: the new tests on 0.1.49's tool-budget.mjs go red (field_0:
"field…" at 131 tools, stubs at 180).
Not tried: real agy with the reporter's 131 tools.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@magpie-community/opencode-agy-cli-auth",
|
||||
"version": "0.1.49",
|
||||
"version": "0.1.50",
|
||||
"description": "Magpie/OpenCode model provider that connects your local agy CLI through the Agent Client Protocol (agy acp), so Antigravity subscription models run through the real CLI",
|
||||
"type": "module",
|
||||
"main": "./opencode.mjs",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import assert from "node:assert/strict"
|
||||
import test from "node:test"
|
||||
import { TOOL_TOKEN_BUDGET, agyTokens, cut, fitTools } from "../tool-budget.mjs"
|
||||
import { MIN_TEXT, TOOL_TOKEN_BUDGET, agyTokens, cut, fitTools } from "../tool-budget.mjs"
|
||||
|
||||
const total = (tools) => tools.reduce((n, t) => n + agyTokens(JSON.stringify(t)), 0)
|
||||
|
||||
@@ -91,6 +91,47 @@ test("a catalog too large for any description keeps every tool", () => {
|
||||
for (const t of fitted) assert.equal(t.inputSchema.properties.title.description, undefined)
|
||||
})
|
||||
|
||||
// plugins#1140's 131 tools after 0.1.46: the schemas alone took ~37 KB of
|
||||
// the budget, the level stopped at 10, and 373 of 382 property texts went
|
||||
// as 9-byte stubs ("The fil…") that spent ~10 KB on their keys
|
||||
function heavy(n, props = 3) {
|
||||
return Array.from({ length: n }, (_, i) => ({
|
||||
name: `tool_${i}`,
|
||||
description: long(`tool_${i}`, 3),
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
...Object.fromEntries(Array.from({ length: props }, (_, j) => [`field_${j}`, { type: "string", description: long(`field_${j}`, 2) }])),
|
||||
view: { type: "object", description: long("view", 4) },
|
||||
flag: { type: "boolean", description: "Short." },
|
||||
},
|
||||
},
|
||||
}))
|
||||
}
|
||||
const bytes = (s) => new TextEncoder().encode(s).length
|
||||
|
||||
for (const n of [131, 180]) {
|
||||
test(`with ${n} heavy tools, no property text is a stub and the room goes to the tools' own (plugins#1140)`, () => {
|
||||
const tools = heavy(n)
|
||||
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++) {
|
||||
assert.deepEqual(structure(fitted[i].inputSchema), structure(tools[i].inputSchema))
|
||||
const p = fitted[i].inputSchema.properties
|
||||
for (const [k, v] of Object.entries(p)) {
|
||||
const was = tools[i].inputSchema.properties[k].description
|
||||
if (v.description !== undefined && v.description !== was) assert.ok(bytes(v.description) >= MIN_TEXT, `${k}: ${v.description}`)
|
||||
}
|
||||
assert.equal(p.flag.description, "Short.", "a text that fits whole stays")
|
||||
const own = fitted[i].description
|
||||
if (own !== undefined) assert.ok(bytes(own) >= MIN_TEXT, `the tool's own: ${own}`)
|
||||
if (n === 131) assert.ok(own, "with room, every tool says what it is for")
|
||||
assert.ok(bytes(p.view.description ?? "") >= MIN_TEXT, `the contract keeps its text: ${p.view.description}`)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
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]) {
|
||||
|
||||
@@ -61,6 +61,18 @@ export function cut(text, max) {
|
||||
return out.trimEnd() + "…"
|
||||
}
|
||||
|
||||
// A cut text shorter than this says nothing ("The fil…") and only spends
|
||||
// the budget on its key: such a description goes whole or not at all
|
||||
// (plugins#1140).
|
||||
export const MIN_TEXT = 24
|
||||
|
||||
// trim is text within max bytes: whole when it fits, else cut, else null
|
||||
// when what a cut would leave is under MIN_TEXT
|
||||
function trim(text, max) {
|
||||
if (bytes(text) <= max) return text
|
||||
return max < MIN_TEXT ? null : cut(text, max)
|
||||
}
|
||||
|
||||
// walk copies one schema node with every description capped: cap(node)
|
||||
// gives the byte cap for a node's description, and Infinity leaves it.
|
||||
// strip takes the decorative keywords out too.
|
||||
@@ -72,7 +84,7 @@ function walk(node, cap, strip) {
|
||||
if (strip && DECOR.has(key)) continue
|
||||
if (key === "description" && typeof value === "string") {
|
||||
const max = cap(node)
|
||||
const text = max === Infinity ? value : cut(value, max)
|
||||
const text = max === Infinity ? value : trim(value, max)
|
||||
if (text) out.description = text
|
||||
continue
|
||||
}
|
||||
@@ -90,14 +102,17 @@ function walk(node, cap, strip) {
|
||||
return out
|
||||
}
|
||||
|
||||
function shaped(tools, { strip, L, top }) {
|
||||
const prop = L === undefined ? () => Infinity : (node) => (isContract(node) ? CONTRACT_WEIGHT * L : L)
|
||||
function shaped(tools, { strip, L, lean }) {
|
||||
// lean: an ordinary property keeps only a description short enough to
|
||||
// go whole, and the room goes to the tools' own and the contracts'
|
||||
const plain = lean ? MIN_TEXT - 1 : L
|
||||
const prop = L === undefined ? () => Infinity : (node) => (isContract(node) ? CONTRACT_WEIGHT * L : plain)
|
||||
return tools.map((tool) => {
|
||||
const out = { ...tool }
|
||||
if (tool.inputSchema && typeof tool.inputSchema === "object") out.inputSchema = walk(tool.inputSchema, prop, strip)
|
||||
if (typeof tool.description === "string") {
|
||||
const max = top ?? (L === undefined ? Infinity : TOOL_WEIGHT * L)
|
||||
const text = max === Infinity ? tool.description : cut(tool.description, max)
|
||||
const max = L === undefined ? Infinity : TOOL_WEIGHT * L
|
||||
const text = max === Infinity ? tool.description : trim(tool.description, max)
|
||||
if (text) out.description = text
|
||||
else delete out.description
|
||||
}
|
||||
@@ -119,21 +134,22 @@ function highest(lo, hi, fits) {
|
||||
// fitTools gives the tools as agy should list them: unchanged when they fit
|
||||
// the budget, else with the decorative keywords gone, then with every
|
||||
// description capped at the highest level that fits (a tool's own
|
||||
// description gets 2.5 times a property's, a contract property's 11 times),
|
||||
// and when that level is nothing, with the properties' descriptions gone and
|
||||
// the tools' own capped alone. A set too large even with no descriptions at
|
||||
// all goes as small as it can, and agy drops what is left over.
|
||||
// description gets 2.5 times a property's, a contract property's 11 times).
|
||||
// When that level leaves an ordinary property less than MIN_TEXT, those
|
||||
// properties keep only what goes whole, and the highest level that fits
|
||||
// then goes to the tools' own descriptions and the contracts', which say
|
||||
// what each tool is for and what goes in an open object. A set too large
|
||||
// even with no descriptions at all goes as small as it can, and agy drops
|
||||
// what is left over.
|
||||
export function fitTools(tools, budget = TOOL_TOKEN_BUDGET) {
|
||||
if (tokensOf(tools) <= budget) return tools
|
||||
const stripped = shaped(tools, { strip: true })
|
||||
if (tokensOf(stripped) <= budget) return stripped
|
||||
const at = (L) => shaped(tools, { strip: true, L })
|
||||
const fits = (L) => tokensOf(at(L)) <= budget
|
||||
if (!fits(0)) return at(0)
|
||||
const L = highest(0, 4000, fits)
|
||||
if (L > 0) return at(L)
|
||||
// the properties' descriptions are gone; what room is left goes to the
|
||||
// tools' own, which say what each tool is for
|
||||
const bare = (top) => shaped(tools, { strip: true, L: 0, top })
|
||||
return bare(highest(0, 4000, (top) => tokensOf(bare(top)) <= budget))
|
||||
if (tokensOf(at(0)) > budget) return at(0)
|
||||
const L = highest(0, 4000, (n) => tokensOf(at(n)) <= budget)
|
||||
if (L >= MIN_TEXT) return at(L)
|
||||
const lean = (n) => shaped(tools, { strip: true, L: n, lean: true })
|
||||
if (tokensOf(lean(0)) > budget) return at(L)
|
||||
return lean(highest(0, 4000, (n) => tokensOf(lean(n)) <= budget))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user