diff --git a/packages/agy-cli/index.mjs b/packages/agy-cli/index.mjs index 89fdee9..dd58004 100644 --- a/packages/agy-cli/index.mjs +++ b/packages/agy-cli/index.mjs @@ -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) } diff --git a/packages/agy-cli/package.json b/packages/agy-cli/package.json index 0d3f9ac..ff10136 100644 --- a/packages/agy-cli/package.json +++ b/packages/agy-cli/package.json @@ -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", diff --git a/packages/agy-cli/test/usage-settlement.mjs b/packages/agy-cli/test/usage-settlement.mjs index 08bf327..1c9d584 100644 --- a/packages/agy-cli/test/usage-settlement.mjs +++ b/packages/agy-cli/test/usage-settlement.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) }) })