Files
plugins/packages/kiro/cache-points.test.mjs
T
yetone 386a2ba5ad kiro 0.1.10: the caller's prompt-cache marks reach Kiro as cachePoints (#110)
What: buildKiro dropped every cache_control, so Kiro was never asked to
cache a prompt and its tokenUsage reported no cache reads ("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 (aws/amazon-q-developer-cli's generated client), and
Kiro CLI 2.27's model list carries supportsPromptCaching and
maximumCacheCheckpointsPerRequest. A message holding a marked block now
gets a cachePoint on the entry it ends up in (merged turns included; a
marked system prompt marks the first message, where the instructions
go), and a marked tool is followed by a cachePoint entry, as kirocc and
AIClient2API send. Four at most, the last kept, as Anthropic allows.
conversationId is left per request: a cachePoint caches the prompt's
prefix, not a conversation.

Run: TMPDIR=<scratch> HOME=<scratch> bun test (1191 pass, 0 fail) and bun
scripts/check.mjs kiro. cache-points.test.mjs's first fixture places the
marks where a captured Claude Code 2.1.296 request had them (two system
blocks and the trailing system-role hook message; texts cut short); the
second is the shape magpie's buildAnthropic hands a plugin for a
translated request. With the two cachePoint lines taken out, 3 of its 4
tests fail. Not tried against a real Kiro account (none signed in here),
so whether Kiro then reports cacheReadInputTokens is not seen yet.
2026-10-14 12:54:44 +08:00

88 lines
4.9 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). The plugin
// dropped every cache_control, so Kiro was never asked to cache.
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"])
})