Files
plugins/packages/kiro/reasoning-signature.test.mjs
Chanyeong Limandasp345 a82a2e3558 kiro: preserve scoped reasoning across tool continuations (#1156)
Kiro keeps reasoning signatures across tool continuations, sealed as
kiro:<scope>:<payload> where scope hashes the account, region and model, so
a signature is replayed only to the account and model that made it. A 400
from Kiro is returned as is (no strip-and-retry). Effort levels come from a
per-account model list read at request time, shared by concurrent requests
and kept 10 minutes; a failed read is swallowed and the request still goes
out. kiro 0.1.39.

Reviewed at d60a5d001bd7f7b62574798885714d446eb48e9c (mica, temp HOME):
bun test in packages/kiro 203 pass; bun scripts/check.mjs kiro ok. The two
probes from the last review (a failed model list made the request a 502;
an unlisted model was re-listed on every request) failed at 6af97c7 and
pass at this head.
Not tried: a real Kiro account.

Co-authored-by: asp345 <10955734+asp345@users.noreply.github.com>
2026-11-15 01:25:15 +08:00

66 lines
2.3 KiB
JavaScript

import { test, expect } from "bun:test"
import { _internal } from "./index.mjs"
const { events, reply, sealReasoning } = _internal
function frame(headers, payload) {
const enc = new TextEncoder()
const hs = []
for (const [k, v] of Object.entries(headers)) {
const name = enc.encode(k)
const val = enc.encode(v)
const h = new Uint8Array(1 + name.length + 1 + 2 + val.length)
h[0] = name.length
h.set(name, 1)
h[1 + name.length] = 7
new DataView(h.buffer).setUint16(2 + name.length, val.length)
h.set(val, 4 + name.length)
hs.push(h)
}
const hlen = hs.reduce((n, h) => n + h.length, 0)
const body = enc.encode(JSON.stringify(payload))
const total = 12 + hlen + body.length + 4
const out = new Uint8Array(total)
const v = new DataView(out.buffer)
v.setUint32(0, total)
v.setUint32(4, hlen)
let i = 12
for (const h of hs) {
out.set(h, i)
i += h.length
}
out.set(body, i)
return out
}
const stream = (...fs) =>
new ReadableStream({
start(c) {
for (const f of fs) c.enqueue(f)
c.close()
},
})
const ev = (type, payload) => frame({ ":message-type": "event", ":event-type": type }, payload)
const SIG = ".KTR~~eyJlbmNyeXB0aW9uUmVnaW9uIjpudWxsLCJtaW50ZWRBdE1zIjoxNzk0MDU1MjcyODg0LCJtb2RlbEhhc2giOiJ5RUxWOXhiY2Z3dlUiLCJzbG90cyI6W10sInYiOjF9"
const SCOPE = "account-region-model"
const signed = () => sealReasoning({ reasoningText: { text: "...", signature: SIG } }, SCOPE)
const body = () => stream(
ev("assistantResponseEvent", { content: "OK", modelId: "auto" }),
ev("reasoningContentEvent", { signature: SIG, text: "..." }),
ev("metadataEvent", { stopReason: "END_TURN" }),
)
test("reasoning sent with its signature in one event keeps the signature", async () => {
const res = await reply(events(body(), "auto", 0, false, SCOPE), "auto", false)
const j = await res.json()
expect(j.content).toEqual([{ type: "text", text: "OK" }, { type: "thinking", thinking: "...", signature: signed() }])
})
test("streamed, the signature follows the reasoning in its block", async () => {
const res = await reply(events(body(), "auto", 0, false, SCOPE), "auto", true)
const out = await res.text()
expect(out).toContain(JSON.stringify({ type: "thinking_delta", thinking: "..." }))
expect(out).toContain(JSON.stringify({ type: "signature_delta", signature: signed() }))
})