Files
plugins/packages/kiro/cache-points.test.mjs
yetone 9f6c4e8861 kiro 0.1.16: a reply with no token counts says its cache is unknown, not 0 (zeewell on X)
zeewell on X: every Kiro request in magpie's Routing page shows cache 0%
(Claude Code through group/wf-coding-*, claude-sonnet-5.5 and
claude-opus-5.5, 150-180k tokens, consecutive calls seconds apart).

Cause, from real requests (Google sign-in through kiro-cli, 2026-10-24):
Kiro caches, but reports no token counts at all. A
generateAssistantResponse reply ends with metadataEvent {stopReason},
contextUsageEvent {contextUsagePercentage} and meteringEvent {credits};
no tokenUsage. The same 9k-token prompt sent twice was billed 0.091
then 0.048 credits, the same with or without cachePoints and with a
fresh or a fixed conversationId. kiro-cli 2.32's own trace log
(KIRO_LOG_LEVEL=trace) shows its MetadataEvent with total_tokens,
uncached_input_tokens, cache_read_input_tokens and
cache_write_input_tokens all None, and kiro-cli sends no cachePoint.
So nothing the plugin sends or drops keeps Kiro from caching (#110's
reading that only cachePoints make it cache was wrong); the plugin said
cache_read_input_tokens 0 for a number Kiro never gave.

Now, with no tokenUsage, the reply's usage leaves both cache fields out
and says magpie_cache_unknown: true, which magpie reads as no hit rate
instead of 0%. A tokenUsage, if Kiro sends one, is told as before.
cache-points.test.mjs's comment is corrected; the marks stay.

Run: cache-unknown.test.mjs (the real reply's events, streamed and not)
fails on 0.1.15's index.mjs (1 fail) and passes; bun test in
packages/kiro 57 pass under a scratch HOME/TMPDIR; scripts/check.mjs
kiro ok. The patched loader's fetch against real Kiro (source "kiro",
claude-sonnet-4.5, twice): 200, message_delta usage
{"output_tokens":1,"magpie_cache_unknown":true}. Not tried: an IDE or
Builder ID sign-in, other models' replies.
2026-10-24 17:03:48 +08:00

91 lines
5.1 KiB
JavaScript

// The caller's prompt-cache marks reach Kiro (#110: "kiro cli cache 率为 0").
// Kiro's generateAssistantResponse takes Bedrock's cachePoint
// ({"type":"default"}) on a userInputMessage, on an assistantResponseMessage
// and as an entry of the tools (the CodeWhisperer streaming model's
// UserInputMessage.cachePoint, AssistantResponseMessage.cachePoint and
// Tool::CachePoint; Kiro CLI 2.27's model list tells
// supportsPromptCaching and maximumCacheCheckpointsPerRequest). Tried
// with a real account on 2026-10-24, Kiro caches a repeated prompt without
// them too (the second call billed about half the credits, marked or not,
// and kiro-cli 2.32 sends none); the marks stay as the caller's intent.
// Why magpie showed 0% is cache-unknown.test.mjs: Kiro reports no tokens.
import { expect, test } from "bun:test"
import { _internal } from "./index.mjs"
const { buildKiro } = _internal
const sent = (r) => JSON.parse(buildKiro(r, "claude-sonnet-4.5", "", 0)).conversationState
const EPH = { type: "ephemeral" }
const POINT = { type: "default" }
// where Claude Code 2.1.296 put its marks, from a captured request (texts
// cut short): the last two system blocks, and the last message, the
// system-role message holding a hook's context after a tool result
const claudeCode = {
model: "claude-sonnet-4.5",
system: [
{ type: "text", text: "x-anthropic-billing-header: cc_version=2" },
{ type: "text", text: "You are a Claude agent, built on Anthropic's Claude Agent SDK.", cache_control: EPH },
{ type: "text", text: "You are an agent working with the user …", cache_control: EPH },
],
messages: [
{ role: "user", content: [{ type: "text", text: "<system-reminder>\nAttribution …</system-reminder>" }, { type: "text", text: "run echo hi" }] },
{ role: "system", content: "# Environment\nYou have been invoked in the following environment: …" },
{ role: "assistant", content: [{ type: "tool_use", id: "toolu_01", name: "Bash", input: { command: "sleep 6; echo hi", description: "say hi" } }] },
{ role: "user", content: [{ tool_use_id: "toolu_01", type: "tool_result", is_error: false, content: "hi" }] },
{ role: "system", content: [{ type: "text", text: "PreToolUse:Bash hook additional context: …", cache_control: EPH }] },
],
tools: [{ name: "Bash", description: "Run a command", input_schema: { type: "object", properties: { command: { type: "string" } } } }],
}
test("Claude Code's marks go on the messages they end up in", () => {
const s = sent(claudeCode)
// the instructions are at the head of the first message, so its mark
// covers them; the hook's context joins the tool result's message
expect(s.history[0].userInputMessage.cachePoint).toEqual(POINT)
expect(s.history[1].assistantResponseMessage.cachePoint).toBeUndefined()
expect(s.currentMessage.userInputMessage.cachePoint).toEqual(POINT)
expect(s.currentMessage.userInputMessage.userInputMessageContext.tools).toEqual([
{ toolSpecification: { name: "Bash", description: "Run a command", inputSchema: { json: claudeCode.tools[0].input_schema } } },
])
})
test("a marked tool is followed by a cachePoint, as magpie marks the last tool of a translated request", () => {
const r = {
system: "You are Codex.",
messages: [
{ role: "user", content: [{ type: "text", text: "hi", cache_control: EPH }] },
{ role: "assistant", content: [{ type: "text", text: "hello", cache_control: EPH }] },
{ role: "user", content: "and now?" },
],
tools: [
{ name: "a", input_schema: { type: "object", properties: {} } },
{ name: "b", input_schema: { type: "object", properties: {} }, cache_control: EPH },
],
}
const s = sent(r)
expect(s.currentMessage.userInputMessage.userInputMessageContext.tools.map((t) => Object.keys(t)[0])).toEqual(["toolSpecification", "toolSpecification", "cachePoint"])
expect(s.history[0].userInputMessage.cachePoint).toEqual(POINT)
expect(s.history[1].assistantResponseMessage.cachePoint).toEqual(POINT)
expect(s.currentMessage.userInputMessage.cachePoint).toBeUndefined()
})
test("nothing marked, nothing sent", () => {
const s = sent({ messages: [{ role: "user", content: "hi" }, { role: "assistant", content: "yo" }, { role: "user", content: "again" }] })
expect(JSON.stringify(s)).not.toContain("cachePoint")
})
test("at most 4, the last ones kept", () => {
const msgs = []
for (let i = 0; i < 6; i++) {
msgs.push({ role: "user", content: [{ type: "text", text: "q" + i, cache_control: EPH }] })
msgs.push({ role: "assistant", content: "a" + i })
}
msgs.push({ role: "user", content: [{ type: "text", text: "last", cache_control: EPH }] })
const s = sent({ messages: msgs, tools: [{ name: "t", input_schema: { type: "object", properties: {} }, cache_control: EPH }] })
const body = JSON.stringify(s)
expect(body.match(/cachePoint/g).length).toBe(4)
expect(s.currentMessage.userInputMessage.cachePoint).toEqual(POINT)
expect(s.currentMessage.userInputMessage.userInputMessageContext.tools.some((t) => t.cachePoint)).toBe(false)
expect(s.history.filter((e) => e.userInputMessage?.cachePoint).map((e) => e.userInputMessage.content)).toEqual(["q3", "q4", "q5"])
})