agy-cli 0.1.12: a tool step's estimated cache read stops at the step before it, so a new tool result is billed as input (#971 review)
0.1.11 (#971) estimated an uncalibrated first prompt's tool steps by summing every earlier settled step's whole window as the cached prefix. Each window already holds the steps before it, so the prefix was counted once per step: from the third step on, the step's whole window, the tool result just added included, came back as a cache read. In the review's probe (four Read calls, ACP reporting cachedReadTokens 0), step 4 billed 0 input and 34,456 read, and the steps claimed 56,608 cache-read tokens that ACP never reported. The prefix is now the largest earlier settled window (the step before), so each step reads that from the cache and bills what was added since. The step test that pinned the 100% clamp now checks that step 3's uncached input covers what was added after step 2, and that its read does not exceed step 2's window. Still an estimate until ACP reports. Run (WSL2 Ubuntu 24.04, Bun 1.4.2, temp HOME): bun test packages/agy-cli 8 of 8 pass; bun scripts/check.mjs agy-cli passes. Broken by hand: origin/main's index.mjs with the new test, node --test test/usage-settlement.mjs: 25 pass, 1 fail ("第三步未缓存 input 0 应覆盖 第二步之后新增的 32"). Restored, green. Not tried: Windows (main's smoke red there is #973), macOS (mote refused ssh), a real agy.
This commit is contained in:
@@ -3830,15 +3830,16 @@ function responseUsage(proc, turn, step, content, result) {
|
||||
// ACP-reported usage. A finite ratio already learned from
|
||||
// recordPromptUsage, including a calibrated 0, is left unchanged.
|
||||
//
|
||||
// prior sums each earlier step's full window (input + cache_read +
|
||||
// cache_creation). Overlap already counted as cache_read on step 2 is
|
||||
// counted again in step 3's prior, so a long tool loop clamps to 100%
|
||||
// quickly. The bound is per-step, not a precise prefix length.
|
||||
const prior = turn.responseSteps.reduce((total, item) => {
|
||||
if (item === step || !item.settled || !item.usage) return total
|
||||
return total + tokenCount(item.usage.input_tokens) +
|
||||
// prior is the largest earlier settled step's full window (input +
|
||||
// cache_read + cache_creation): each step's window already holds every
|
||||
// step before it, so that is the prefix sent before. Summing the
|
||||
// windows counted the prefix once per step, and from the third step on
|
||||
// billed the tool result just added as cached too.
|
||||
const prior = turn.responseSteps.reduce((largest, item) => {
|
||||
if (item === step || !item.settled || !item.usage) return largest
|
||||
return Math.max(largest, tokenCount(item.usage.input_tokens) +
|
||||
tokenCount(item.usage.cache_read_input_tokens) +
|
||||
tokenCount(item.usage.cache_creation_input_tokens)
|
||||
tokenCount(item.usage.cache_creation_input_tokens))
|
||||
}, 0)
|
||||
if (prior > 0) cacheRatio = Math.min(1, prior / input)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@magpie-community/opencode-agy-cli-auth",
|
||||
"version": "0.1.11",
|
||||
"version": "0.1.12",
|
||||
"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",
|
||||
|
||||
@@ -705,7 +705,7 @@ test("取消的 prompt 也教会缓存占比:下一回合的工具步沿用 ag
|
||||
})
|
||||
|
||||
|
||||
test("同一会话未校准 Proc 上,工具环第二步起把已结算前缀计为读缓存(估算,非 ACP 回传)", async () => {
|
||||
test("同一会话未校准 Proc 上,工具环第二步起把上一步窗口计为读缓存、新增部分计为 input(估算,非 ACP 回传)", async () => {
|
||||
await withKit({}, async (kit) => {
|
||||
kit.prompts.push(async (agent) => {
|
||||
await agent.tool("Read", { path: "/one" })
|
||||
@@ -729,12 +729,15 @@ test("同一会话未校准 Proc 上,工具环第二步起把已结算前缀
|
||||
`第二步读缓存 ${second.cache_read_input_tokens} 应覆盖第一步窗口 ${firstTotal}`)
|
||||
assert.ok(second.input_tokens <= Math.max(1, secondTotal - firstTotal + 2),
|
||||
`第二步未缓存 input ${second.input_tokens} 不应再包含全部前缀`)
|
||||
// Step 3's prior sums step 1+2 full windows, so the estimate clamps
|
||||
// to 100% cache rather than a precise new-prefix length.
|
||||
// Step 3 reads step 2's window from the cache and bills what was added
|
||||
// since (the second tool result) as input, never its whole window.
|
||||
assert.ok(third.cache_read_input_tokens >= secondTotal - 2,
|
||||
`第三步读缓存 ${third.cache_read_input_tokens} 应至少覆盖第二步窗口 ${secondTotal}`)
|
||||
assert.ok(third.cache_read_input_tokens >= thirdTotal - 1,
|
||||
`第三步读缓存 ${third.cache_read_input_tokens} 应对该步窗口 ${thirdTotal} 夹到约 100%`)
|
||||
assert.ok(thirdTotal > secondTotal, "第三步窗口应比第二步大")
|
||||
assert.ok(third.input_tokens >= thirdTotal - secondTotal - 2,
|
||||
`第三步未缓存 input ${third.input_tokens} 应覆盖第二步之后新增的 ${thirdTotal - secondTotal}`)
|
||||
assert.ok(third.cache_read_input_tokens <= secondTotal + 2,
|
||||
`第三步读缓存 ${third.cache_read_input_tokens} 不应超过第二步窗口 ${secondTotal}`)
|
||||
assert.equal(kit.acpSpawns.length, 1)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user