Files
plugins/packages/kiro/refresh.test.mjs
T
yetone 9b41df1507 kiro 0.1.33: an account in Kiro IDE's CreateResponse experiment is asked at /v1/responses, as the IDE asks it (#842, #848)
ThirdTimes (#842, #848): on one Builder ID account the plugin (0.1.32)
was answered 429 USER_REQUEST_RATE_EXCEEDED on generateAssistantResponse,
and Kiro IDE 1.4.121, 11s later, was answered. The IDE's log says
"api=CreateResponse armSource=experiment callPath=main". The version
strings the plugin sends already match the IDE (1.4.121, the update
feed's latest, 2026-11-06). The difference is the call. Kiro puts accounts
in a create_response_api experiment, and an account in its arm is asked
at POST runtime.<region>.kiro.dev/v1/responses. The IDE's own
generateAssistantResponse title call on that account was 429 too.

All of this is read from the IDE's extension.js (kiro.kiro-agent, in the
1.4.121 linux tarball, which was not run):
- The experiment is read as ExperimentConfigService reads it:
  GET /featureConfiguration?origin=AI_EDITOR&version=1.4.121 with
  x-amzn-kiro-profile-arn. The key is
  sha256hex("kiro-feature-config-key-salt-3e9b1d7a" + "create_response_api"),
  and the arm's value is "create_response". The result is kept 15 minutes
  per sign-in. A failed read is asked again after 60s and keeps the last
  good config, and the read has a 5s timeout. With no config the account
  stays on generateAssistantResponse, as the IDE's default does, so an
  account outside the arm is sent what 0.1.32 sent.
- CreateResponse is sent with the IDE's headers: the plain IDE user agent
  (no aws-sdk prefix, no amz-sdk-*), x-amzn-kiro-origin AI_EDITOR,
  x-amzn-codewhisperer-optout, x-amzn-kiro-profile, TokenType and
  x-amzn-kiro-attempt "n;max=3". The body is {model, input, tools, stream,
  origin, profileArn, metadata kiro:agent_mode/kiro:conversation_id,
  reasoning}, made from the request built for generateAssistantResponse
  (history, tool calls and results, images as data: URLs). The system text
  stays at the head of the first message, as with the IDE's
  system_field_injection off.
- The SSE reply is read into the same events the old frames gave: text,
  reasoning and its signature, refusals, function calls (pieces or
  whole), stop reason, context %, and credits. An error before any answer
  becomes the status generateAssistantResponse's would have, mapped through
  the IDE's reason table (rate_limit_exceeded is USER_REQUEST_RATE_EXCEEDED,
  context_length_exceeded is CONTENT_LENGTH_EXCEEDS_THRESHOLD, and so on).
  The 0.1.32 429 retry applies to both paths.

Not done: the auxiliary path (create_response_api_auxiliary; the plugin
has no auxiliary calls), the x-amzn-kiro-fallback header, documents as
input_file, and reasoning items in history.

Run on mote at base 966cf717: packages/kiro bun test 168 pass 0 fail
(HOME a temp dir). bun scripts/check.mjs kiro ok. Root bun test 6892 pass,
20 skip, 0 fail. Rebased onto e7af7856 (inkstone only) before pushing.
Broken by hand: with the arm's value changed, 8 of the 11 new tests go
red. image-size and refresh tests now leave the featureConfiguration GET
out of what they count as a chat send.

Not tried: a real Kiro account. No account in the CreateResponse arm was
available, so the /v1/responses body and SSE are as the IDE's source
writes and reads them, not as captured from Kiro.
2026-11-08 20:31:34 +08:00

114 lines
5.5 KiB
JavaScript

// magpie renews a sign-in kept here ahead of its end through auth.refresh,
// under the same lock as a request's own refresh, so one refresh token is
// never spent twice; OpenCode, which doesn't call it, still refreshes
// before a request.
import { test, expect, afterAll, beforeAll, afterEach } from "bun:test"
import { homedir, tmpdir } from "node:os"
import { realpathSync } from "node:fs"
let plugin
beforeAll(async () => {
// Bun reads HOME once, at start: run as HOME=$(mktemp -d) bun test, so
// no real sign-in (~/.kiro, ~/.aws, kiro-cli's) is ever read or written
if (![tmpdir(), realpathSync(tmpdir())].some((t) => homedir().startsWith(t))) throw new Error("run with HOME=$(mktemp -d) bun test")
;({ KiroAuthPlugin: plugin } = await import("./index.mjs"))
})
const offline = async () => { throw new Error("no network in tests") }
const real = globalThis.fetch
globalThis.fetch = offline
afterAll(() => (globalThis.fetch = real))
afterEach(() => (globalThis.fetch = offline))
const PROFILE = "arn:aws:codewhisperer:us-east-1:111111111111:profile/TEST"
const REFRESH = "https://prod.us-east-1.auth.desktop.kiro.dev/refreshToken"
const signIn = (over = {}) => ({ type: "oauth", access: "old", refresh: "r1", expires: Date.now() + 5 * 60_000, method: "social",
region: "us-east-1", profileArn: PROFILE, accountId: "me@example.com", ...over })
// kiro answers the refresh with the given status (and a new token when
// 200), and every request to Kiro's API with a 500; it notes both.
function kiro(status = 200, wait = 0) {
const seen = { refreshes: [], sent: [] }
globalThis.fetch = async (url, init) => {
if (String(url) === REFRESH) {
seen.refreshes.push(JSON.parse(init.body))
if (wait) await new Promise((r) => setTimeout(r, wait))
if (status !== 200) return new Response("{}", { status })
return Response.json({ accessToken: "new", refreshToken: "r2", expiresIn: 3600 })
}
// the experiment read before a chat (plugins#842) is no request's send
if (new URL(String(url)).pathname !== "/featureConfiguration") seen.sent.push(new Headers(init.headers).get("Authorization"))
return new Response(JSON.stringify({ message: "boom" }), { status: 500 })
}
return seen
}
const hooksWith = async () => {
const saves = []
const hooks = await plugin({ client: { auth: { set: async (r) => void saves.push(r) } } })
return { hooks, saves }
}
test("magpie renews a sign-in ahead of its end; magpie saves what it gives", async () => {
const seen = kiro()
const { hooks, saves } = await hooksWith()
expect(hooks.auth.refreshLead).toBeGreaterThanOrEqual(2 * 60_000)
const got = await hooks.auth.refresh(signIn())
expect(got).toEqual({ access: "new", refresh: "r2", expires: expect.any(Number) })
expect(got.expires).toBeGreaterThan(Date.now() + 50 * 60_000)
expect(seen.refreshes).toEqual([{ refreshToken: "r1" }])
expect(saves).toEqual([])
})
test("a request's refresh and magpie's renewal spend the refresh token once", async () => {
const seen = kiro(200, 50)
const { hooks } = await hooksWith()
const stored = signIn({ expires: Date.now() - 1000 }) // the store keeps the old one throughout
const l = await hooks.auth.loader(async () => stored)
const ask = () => l.fetch("https://kiro.invalid/v1/messages", { method: "POST", body: JSON.stringify({ model: "auto", messages: [{ role: "user", content: "hi" }] }) })
const [res, got] = await Promise.all([ask(), hooks.auth.refresh(stored)])
expect(res.status).toBe(500)
expect(got).toMatchObject({ access: "new", refresh: "r2" })
expect(seen.refreshes.length).toBe(1)
// the renewal not yet saved, a request goes on with its token
await ask()
expect(seen.refreshes.length).toBe(1)
expect(seen.sent).toEqual(["Bearer new", "Bearer new"])
// and asked again before magpie saved it, the same is given
expect(await hooks.auth.refresh(stored)).toMatchObject({ access: "new", refresh: "r2" })
expect(seen.refreshes.length).toBe(1)
})
// as the built-in, a refusal doesn't mark the account lapsed either
test("a refresh token Kiro refused is thrown plainly, the account not marked", async () => {
for (const status of [400, 401, 403]) {
kiro(status)
const { hooks } = await hooksWith()
const e = await hooks.auth.refresh(signIn()).catch((e) => e)
expect(e.message).toBe("Kiro's sign-in has expired; sign in again with `kiro-cli login` or the Kiro IDE")
expect(e.signIn).toBeUndefined()
}
})
test("any other failure is thrown plainly, to be tried again", async () => {
kiro(503)
let { hooks } = await hooksWith()
let e = await hooks.auth.refresh(signIn()).catch((e) => e)
expect(e.message).toBe("refreshing Kiro's sign-in: 503 Service Unavailable")
expect(e.signIn).toBeUndefined()
;({ hooks } = await hooksWith())
globalThis.fetch = offline
e = await hooks.auth.refresh(signIn()).catch((e) => e)
expect(e.message).toBe("refreshing Kiro's sign-in: no network in tests")
expect(e.signIn).toBeUndefined()
})
test("nothing to renew with is nothing renewed", async () => {
const seen = kiro()
const { hooks } = await hooksWith()
expect(await hooks.auth.refresh(signIn({ refresh: "" }))).toBeUndefined()
expect(await hooks.auth.refresh({ type: "api", key: "ksk_x" })).toBeUndefined()
// kiro-cli's and the IDE's sign-ins are their owners' to refresh
expect(await hooks.auth.refresh({ type: "oauth", access: "", refresh: "", expires: 0, source: "kiro-cli" })).toBeUndefined()
expect(await hooks.auth.refresh({ type: "oauth", access: "a", refresh: "r", expires: 1, source: "kiro-ide" })).toBeUndefined()
expect(seen.refreshes).toEqual([])
})