agy-cli 0.1.6: every prompt's own cache reads set the ratio the next tool steps report (baranwang on X)

Tool steps estimate their cache reads from the last ratio the proc learned,
and only a final, uncancelled response taught it, so a cancelled prompt left
the next one's tool steps reporting every token as a cache miss. agy reports
each prompt's cached reads, so recordPromptUsage now sets the ratio from them.
Test: a cancelled prompt (390k of 520k cached) then a tool step reports 0.75.
Red with the line off, green with it. Not captured from a real agy: the
owner's agy is signed out on this machine and mote was down. A fresh
conversation's first prompt still has no ratio until it ends.
Ran node --test packages/agy-cli/test (2 reds on base too: cp-hook,
version/report hangs), bun test packages/antigravity, scripts/check.mjs.
This commit is contained in:
yetone
2026-11-11 11:55:10 +08:00
parent 5623d7123f
commit c27ba5f700
3 changed files with 51 additions and 1 deletions
+7
View File
@@ -906,6 +906,13 @@ class Turn {
this.accountedPrompts.add(number)
this.rawAcpUsages.push(usage)
this.rawUsage = addUsage(this.rawUsage, usage)
// agy reports what each prompt read from the cache, cancelled or not.
// Tool steps of the next prompt (or of a late one in this response) have
// no usage of their own until it ends, so they take this ratio. Only a
// final, uncancelled response used to teach it, so a fresh or cancelled
// conversation reported every tool step as a full cache miss.
const input = tokenCount(usage?.inputTokens)
if (input > 0) this.proc.lastCacheRatio = Math.min(1, Math.min(input, tokenCount(usage.cachedReadTokens)) / input)
}
return this.rawUsage
}
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-agy-cli-auth",
"version": "0.1.5",
"version": "0.1.6",
"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",
@@ -604,3 +604,46 @@ test("SSE 与 stream:false 经同一流生成/消费路径得到相同内容与
assert.equal(results[0].length, 2)
assert.deepEqual(results[1], results[0])
})
test("取消的 prompt 也教会缓存占比:下一回合的工具步沿用 agy 报告的读缓存比例", async () => {
await withKit({}, async (kit) => {
// 形状取自 agy 的 session/prompt 结果:inputTokens 含缓存读,一次取消后无校准。
const cancelled = { inputTokens: 520_000, cachedReadTokens: 390_000, cachedWriteTokens: 0, outputTokens: 900, thoughtTokens: 40 }
kit.prompts.push(async (agent) => {
await agent.text("partial answer")
await agent.waitCancel()
return { stopReason: "cancelled", usage: cancelled }
})
kit.prompts.push(async (agent) => {
await agent.tool("Read", { path: "/a" })
await agent.text("done")
return { stopReason: "end_turn", usage: { inputTokens: 9000, cachedReadTokens: 6000, outputTokens: 5 } }
})
const body = baseBody({ tools: [{ name: "Read", description: "read", input_schema: { type: "object", properties: { path: { type: "string" } } } }] })
const controller = new AbortController()
const response = await kit.request(body, { signal: controller.signal })
const reader = response.body.getReader()
const decoder = new TextDecoder()
let seen = ""
while (!seen.includes("text_delta")) {
const { value, done } = await reader.read()
if (done) break
seen += decoder.decode(value, { stream: true })
}
controller.abort()
await reader.read().then(() => {}, () => {})
for (let i = 0; i < 200 && !kit.turns[0]?.finalized; i++) await sleep(5)
assert.ok(kit.turns[0].finalized)
const next = {
...body,
messages: [...body.messages, { role: "assistant", content: [{ type: "text", text: "partial answer" }] }, { role: "user", content: "go on" }],
}
const { responses } = await runTurn(kit, next)
assert.equal(kit.acpSpawns.length, 1, "同一会话没有复用 Proc")
const toolStep = responses[0].usage
assert.equal(responses[0].stopReason, "tool_use")
const ratio = toolStep.cache_read_input_tokens / usageTotal(toolStep)
assert.ok(Math.abs(ratio - 0.75) < 0.02, `工具步缓存占比 ${ratio},期望沿用取消 prompt 的 0.75`)
})
})