diff --git a/packages/agy-cli/index.mjs b/packages/agy-cli/index.mjs index a1f8766..98aa5b9 100644 --- a/packages/agy-cli/index.mjs +++ b/packages/agy-cli/index.mjs @@ -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 } diff --git a/packages/agy-cli/package.json b/packages/agy-cli/package.json index 76c644b..a9f2db0 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.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", diff --git a/packages/agy-cli/test/usage-settlement.mjs b/packages/agy-cli/test/usage-settlement.mjs index 9d10231..2c3b6de 100644 --- a/packages/agy-cli/test/usage-settlement.mjs +++ b/packages/agy-cli/test/usage-settlement.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`) + }) +})