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.
This commit is contained in:
yetone
2026-10-14 12:54:44 +08:00
parent 643ef5018e
commit 386a2ba5ad
3 changed files with 111 additions and 1 deletions
+87
View File
@@ -0,0 +1,87 @@
// 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"])
})
+23
View File
@@ -646,6 +646,12 @@ function thinking(req, model) {
// buildKiro is the body of a generateAssistantResponse call.
function buildKiro(req, model, profile, budget) {
const entries = []
// the messages the caller marked for its prompt cache (cache_control),
// which go to Kiro as a cachePoint on the message they end up in (#110)
const marked = new WeakSet()
const mark = (msg, parts) => {
if (parts.some((p) => p?.cache_control)) marked.add(msg)
}
const user = () => ({ content: "", modelId: model, origin: "KIRO_CLI" })
for (const m of req.messages ?? []) {
const parts = typeof m.content === "string" ? [{ type: "text", text: m.content }] : m.content ?? []
@@ -666,6 +672,7 @@ function buildKiro(req, model, profile, budget) {
prev.content = joinNonEmpty(prev.content, a.content)
prev.toolUses.push(...a.toolUses)
} else entries.push({ assistantResponseMessage: a })
mark(prev ?? a, parts)
continue
}
const u = user()
@@ -693,6 +700,7 @@ function buildKiro(req, model, profile, budget) {
prev.images.push(...u.images)
if (results.length) (prev.userInputMessageContext ??= { toolResults: [] }).toolResults.push(...results)
} else entries.push({ userInputMessage: u })
mark(prev ?? u, parts)
}
// the conversation opens with something the user said: tool results with
// no call before them go, and a reply the caller began with
@@ -748,6 +756,7 @@ function buildKiro(req, model, profile, budget) {
let system = typeof req.system === "string" ? req.system : textOf(req.system)
if (budget) system = `<thinking_mode>enabled</thinking_mode><max_thinking_length>${budget}</max_thinking_length>` + (system ? "\n" + system : "")
if (system) entries[0].userInputMessage.content = system + "\n\n" + entries[0].userInputMessage.content
if (Array.isArray(req.system)) mark(entries[0].userInputMessage, req.system)
// the caller's tools, and any the conversation used that it no longer
// offers — Kiro rejects a history naming a tool it wasn't given
@@ -760,6 +769,7 @@ function buildKiro(req, model, profile, budget) {
const name = toolName(t.name)
offered.add(name)
tools.push({ toolSpecification: { name, description: t.description || t.name, inputSchema: { json: t.input_schema ?? EMPTY } } })
if (t.cache_control) tools.push({ cachePoint: { type: "default" } })
}
for (const e of entries)
for (const c of e.assistantResponseMessage?.toolUses ?? [])
@@ -768,6 +778,19 @@ function buildKiro(req, model, profile, budget) {
tools.push({ toolSpecification: { name: c.name, description: "Tool", inputSchema: { json: EMPTY } } })
}
if (tools.length) (current.userInputMessageContext ??= {}).tools = tools
// Kiro's cachePoint is Bedrock's: it caches the prompt up to the point,
// and a request may hold 4 at most, as Anthropic's cache_control; past
// that the last ones are kept, the most of the prompt they cover
const points = []
for (const t of tools) if (t.cachePoint) points.push(() => tools.splice(tools.indexOf(t), 1))
for (const e of entries) {
const msg = e.userInputMessage ?? e.assistantResponseMessage
if (marked.has(msg)) {
msg.cachePoint = { type: "default" }
points.push(() => delete msg.cachePoint)
}
}
for (const drop of points.slice(0, Math.max(0, points.length - 4))) drop()
const state = { chatTriggerType: "MANUAL", agentTaskType: "vibe", conversationId: randomUUID(), currentMessage: { userInputMessage: current } }
if (entries.length > 1) state.history = entries.slice(0, -1)
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-kiro-auth",
"version": "0.1.9",
"version": "0.1.10",
"description": "Kiro subscriptions (Free, Pro, Pro+, Power) in OpenCode and magpie",
"type": "module",
"main": "./index.mjs",