onemezao on Discord: a "hi" answered, and the next message straight after was Kiro's 429 USER_REQUEST_RATE_EXCEEDED, while the Kiro IDE on the same Builder ID account and model (claude-haiku-5.5) took two messages in a row (plugins#842, plugins#848). The IDE gets the same throttles; it rides them out. Its chat client (kiro.kiro-agent 1.1.787, dist/extension.js) classifies a ThrottlingException with reason USER_REQUEST_RATE_EXCEEDED or CREDIT_CONSUMPTION_RATE_EXCEEDED (ClientThrottleError) or any other throttle (ServiceThrottleError) as retryErrorType THROTTLING, and its QClientRetryStrategy tries 3 times: Kiro's retryAfterMilliseconds when said, else 1s doubling (20% jitter, 20s max), counting each try in amz-sdk-request and x-amzn-kiro-attempt. It shows "Retrying in N seconds" over 2s. The plugin sent the first 429 back at once, and since 0.1.30 with Retry-After 30s, so magpie didn't retry either. Now paced() sends a chat call the same way: a 429 throttle (those three reasons, or a reasonless "too many requests"/throttling) is waited out and sent again, up to 3 tries, one invocation id, attempt=N; max=3. A wait Kiro says is over 20s isn't held: the 429 goes back with it as before. Usage limits (MONTHLY/DAILY/..., USAGE_LIMIT), model capacity and other statuses aren't retried. A cancelled request ends during the pause and isn't sent again. The last try's 429 keeps 0.1.30's words and Retry-After. The 47.3K input tokens of a "hi" are Kiro's own contextUsagePercentage times the model's window: the agent's system prompt and tools, not something the plugin adds. Run (mote, HOME and TMPDIR inside the run dir): - packages/kiro: bun test, 157 pass 0 fail. - repo root: bun test; bun scripts/check.mjs kiro. Root: 6867 pass, 0 fail, 6887 tests across 499 files; check ok. Broken by hand: paced() returning the first answer (n >= 1) turns 5 tests red (the four new throttle tests in errors.test.mjs and region.test.mjs's rate-limit case). Not tried: a real throttled Kiro account. No Builder ID account that Kiro throttles was at hand, so the retry is checked against the IDE's code and mocked answers only, not against Kiro. Reported-by: onemezao (Discord)
222 lines
11 KiB
JavaScript
222 lines
11 KiB
JavaScript
// An Identity Center account's two credit regions: a profile and a month's
|
|
// credits in each of us-east-1 and eu-central-1, each with its own overage
|
|
// cap. A request goes on in the other region once one is used up; only both
|
|
// used up is a 429 for magpie. A region with no profile is not enabled.
|
|
import { test, expect, afterAll, beforeAll, afterEach } from "bun:test"
|
|
import { homedir, tmpdir } from "node:os"
|
|
import { realpathSync } from "node:fs"
|
|
|
|
let plugin, _internal
|
|
beforeAll(async () => {
|
|
// Bun reads HOME once, at start: run as HOME=$(mktemp -d) bun test, so
|
|
// no real sign-in is ever read
|
|
if (![tmpdir(), realpathSync(tmpdir())].some((t) => homedir().startsWith(t))) throw new Error("run with HOME=$(mktemp -d) bun test")
|
|
;({ KiroAuthPlugin: plugin, _internal } = await import("./index.mjs"))
|
|
// a throttle's pauses (paced) are counted, not waited
|
|
sleep = _internal.timing.sleep
|
|
_internal.timing.sleep = async (ms) => void pauses.push(ms)
|
|
})
|
|
let sleep
|
|
const pauses = []
|
|
afterAll(() => (_internal.timing.sleep = sleep))
|
|
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 US = "arn:aws:codewhisperer:us-east-1:333333333333:profile/US"
|
|
const EU = "arn:aws:codewhisperer:eu-central-1:333333333333:profile/EU"
|
|
const RESET = 1796083200
|
|
const later = () => Date.now() + 60 * 60_000
|
|
const auth = () => ({ type: "oauth", access: "t", refresh: "r", expires: later(), method: "idc", loginProvider: "Enterprise",
|
|
region: "us-east-1", profileArn: "", clientId: "c", clientSecret: "s", accountId: "dev@corp.example (IAM Identity Center)" })
|
|
|
|
// limits is Get-Usage-Limits' answer for a month of used credits, a plan
|
|
// of 2000 with overage on up to 10000, as Kiro answers it
|
|
const limits = (used, { overage = true } = {}) => ({
|
|
subscriptionInfo: { subscriptionTitle: "KIRO POWER" }, userInfo: { email: "dev@corp.example" }, nextDateReset: RESET,
|
|
overageConfiguration: { overageStatus: overage ? "ENABLED" : "DISABLED" },
|
|
usageBreakdownList: [{ displayName: "Credit", displayNamePlural: "Credits", currentUsageWithPrecision: used, usageLimitWithPrecision: 2000,
|
|
usageLimit: 2000, overageCap: 10000, overageCapWithPrecision: 10000, currentOverages: Math.max(0, used - 2000), nextDateReset: RESET }],
|
|
})
|
|
const USED_UP = { status: 402, body: { message: "You have reached the limit.", reason: "MONTHLY_REQUEST_COUNT" } }
|
|
const THROTTLED = { status: 429, body: { message: "Too many requests, please wait." }, headers: { "x-amzn-kiro-ratelimit-retry-after": "12500" } }
|
|
const STOP = { status: 500, body: { message: "stop here" } }
|
|
const MAX_ASKS = 6
|
|
|
|
// kiro is Kiro's API with the profiles each region lists, each region's
|
|
// credits, and how each region's runtime answers a chat
|
|
function kiro({ profiles = { "us-east-1": [US], "eu-central-1": [EU] }, usage = {}, chat = {} } = {}) {
|
|
const seen = { profiles: 0, usage: [], chat: [] }
|
|
globalThis.fetch = async (input, init = {}) => {
|
|
const url = new URL(String(input))
|
|
const region = url.host.split(".")[1]
|
|
if (url.pathname === "/List-Available-Profiles") {
|
|
seen.profiles++
|
|
return Response.json({ profiles: (profiles[region] ?? []).map((arn) => ({ arn })) })
|
|
}
|
|
if (url.pathname === "/Get-Usage-Limits") {
|
|
seen.usage.push([region, url.searchParams.get("profileArn")])
|
|
return Response.json(usage[region] ?? limits(0))
|
|
}
|
|
if (url.pathname === "/generateAssistantResponse") {
|
|
// a region loop that doesn't end fails here, not on a timeout
|
|
if (seen.chat.length >= MAX_ASKS) throw new Error(`Kiro asked more than ${MAX_ASKS} times for one request`)
|
|
seen.chat.push([region, JSON.parse(init.body).profileArn])
|
|
const r = chat[region] ?? STOP
|
|
return new Response(JSON.stringify(r.body), { status: r.status, headers: r.headers })
|
|
}
|
|
throw new Error("unexpected " + url)
|
|
}
|
|
return seen
|
|
}
|
|
|
|
const hooks = (saves = []) => plugin({ client: { auth: { set: async (r) => void saves.push(r.body) } } })
|
|
const ask = async (h, a) => {
|
|
const l = await h.auth.loader(async () => a)
|
|
const res = await l.fetch("https://kiro.invalid/v1/messages", { method: "POST",
|
|
body: JSON.stringify({ model: "claude-sonnet-5.5", messages: [{ role: "user", content: "hi" }] }) })
|
|
return { status: res.status, wait: res.headers.get("Retry-After"), message: (await res.json())?.error?.message ?? "" }
|
|
}
|
|
|
|
test("overage on counts the month against the overage cap", () => {
|
|
const u = _internal.usageOf(limits(4104.73))
|
|
expect(u.windows[0]).toMatchObject({ name: "Credits", amount: 4104.73, limit: 10000, display: "4104.73 / 10000" })
|
|
expect(u.windows[0].used).toBeCloseTo(41.0473)
|
|
// and off, the plan's credits alone
|
|
expect(_internal.usageOf(limits(1500, { overage: false })).windows[0]).toMatchObject({ limit: 2000, used: 75 })
|
|
})
|
|
|
|
test("both regions' profiles are found and kept with the account", async () => {
|
|
const seen = kiro()
|
|
const saves = []
|
|
const h = await hooks(saves)
|
|
await h.auth.usage(async () => auth())
|
|
expect(seen.profiles).toBe(2)
|
|
expect(saves.at(-1)).toMatchObject({ profileArn: US, profiles: { "us-east-1": US, "eu-central-1": EU } })
|
|
// kept, they aren't asked for again
|
|
seen.profiles = 0
|
|
const kept = { ...auth(), profileArn: US, profiles: { "us-east-1": US, "eu-central-1": EU } }
|
|
await (await hooks()).auth.usage(async () => kept)
|
|
expect(seen.profiles).toBe(0)
|
|
})
|
|
|
|
test("each region's credits are shown, and their sum is the account's", async () => {
|
|
const seen = kiro({ usage: { "us-east-1": limits(10000), "eu-central-1": limits(2500) } })
|
|
const u = await (await hooks()).auth.usage(async () => auth())
|
|
expect(seen.usage).toEqual(expect.arrayContaining([["us-east-1", US], ["eu-central-1", EU]]))
|
|
expect(u.user).toBe("dev@corp.example (IAM Identity Center)")
|
|
const [sum, us, eu] = u.windows
|
|
expect(sum).toMatchObject({ name: "Credits", amount: 12500, limit: 20000, used: 62.5, resetsAt: new Date(RESET * 1000).toISOString() })
|
|
expect(sum.aside).toBeUndefined()
|
|
expect(us).toMatchObject({ name: "Credits (us-east-1)", used: 100, aside: true })
|
|
expect(eu).toMatchObject({ name: "Credits (eu-central-1)", used: 25, aside: true })
|
|
})
|
|
|
|
test("a region used up per the usage read is skipped by the next request", async () => {
|
|
const seen = kiro({ usage: { "us-east-1": limits(10000), "eu-central-1": limits(100) } })
|
|
const h = await hooks()
|
|
await h.auth.usage(async () => auth())
|
|
await ask(h, auth())
|
|
expect(seen.chat).toEqual([["eu-central-1", EU]])
|
|
})
|
|
|
|
test("a region Kiro says is used up goes on in the other, and stays there", async () => {
|
|
const seen = kiro({ chat: { "us-east-1": USED_UP } })
|
|
const h = await hooks()
|
|
const r = await ask(h, auth())
|
|
expect(r.status).toBe(500) // the other region's own answer
|
|
expect(seen.chat).toEqual([["us-east-1", US], ["eu-central-1", EU]])
|
|
seen.chat.length = 0
|
|
await ask(h, auth())
|
|
expect(seen.chat).toEqual([["eu-central-1", EU]])
|
|
})
|
|
|
|
test("both regions used up is a 429 that says so, with Kiro's wait", async () => {
|
|
const seen = kiro({ chat: { "us-east-1": USED_UP, "eu-central-1": { ...USED_UP, headers: { "retry-after": "60" } } } })
|
|
const r = await ask(await hooks(), auth())
|
|
expect(r.status).toBe(429)
|
|
expect(r.wait).toBe("60")
|
|
expect(r.message).toContain("Kiro credits used up in us-east-1, eu-central-1")
|
|
expect(seen.chat).toEqual([["us-east-1", US], ["eu-central-1", EU]])
|
|
})
|
|
|
|
test("every region used up ends the request at once, however often it is asked", async () => {
|
|
const seen = kiro({ chat: { "us-east-1": USED_UP, "eu-central-1": USED_UP } })
|
|
const h = await hooks()
|
|
for (let i = 0; i < 3; i++) {
|
|
seen.chat.length = 0
|
|
const r = await ask(h, auth())
|
|
expect(r.status).toBe(429)
|
|
expect(seen.chat.length).toBeLessThanOrEqual(2)
|
|
}
|
|
})
|
|
|
|
test("a region not enabled is asked for again only after PROBE_MS", async () => {
|
|
const seen = kiro({ profiles: { "us-east-1": [US] } })
|
|
const h = await hooks()
|
|
await ask(h, auth())
|
|
expect(seen.profiles).toBe(2)
|
|
await ask(h, auth())
|
|
await ask(h, auth())
|
|
expect(seen.profiles).toBe(2)
|
|
})
|
|
|
|
test("a rate limit isn't taken for credits used up: no other region is asked", async () => {
|
|
const seen = kiro({ chat: { "us-east-1": THROTTLED } })
|
|
pauses.length = 0
|
|
const r = await ask(await hooks(), auth())
|
|
expect(r.status).toBe(429)
|
|
expect(r.wait).toBe("13") // Kiro's wait goes on as Retry-After
|
|
// waited out and tried again in the same region, as the Kiro IDE does
|
|
expect(seen.chat).toEqual([["us-east-1", US], ["us-east-1", US], ["us-east-1", US]])
|
|
expect(pauses).toEqual([12500, 12500])
|
|
})
|
|
|
|
test("a region not enabled is shown so, pointed out near the end, and named when the other is used up", async () => {
|
|
kiro({ profiles: { "us-east-1": [US] }, usage: { "us-east-1": limits(8500) } })
|
|
const u = await (await hooks()).auth.usage(async () => auth())
|
|
expect(u.windows[0]).toMatchObject({ amount: 8500, limit: 10000 })
|
|
expect(u.windows[0].display).toContain("eu-central-1 not enabled: ask your admin to enable it")
|
|
expect(u.windows[2]).toMatchObject({ name: "Credits (eu-central-1)", display: "not enabled", aside: true })
|
|
|
|
const seen = kiro({ profiles: { "us-east-1": [US] }, chat: { "us-east-1": USED_UP } })
|
|
const r = await ask(await hooks(), auth())
|
|
expect(r.status).toBe(429)
|
|
expect(r.message).toContain("Kiro credits used up in us-east-1; eu-central-1 not enabled: ask your admin to enable it")
|
|
expect(seen.chat).toEqual([["us-east-1", US]])
|
|
})
|
|
|
|
test("the region enabled comes before the one not enabled, whichever it is", async () => {
|
|
kiro({ profiles: { "eu-central-1": [EU] }, usage: { "eu-central-1": limits(2744.89) } })
|
|
const u = await (await hooks()).auth.usage(async () => auth())
|
|
expect(u.windows.map((w) => [w.name, w.display])).toEqual([
|
|
["Credits", "2744.89 / 10000"],
|
|
["Credits (eu-central-1)", "2744.89 / 10000"],
|
|
["Credits (us-east-1)", "not enabled"],
|
|
])
|
|
})
|
|
|
|
test("a region enabled later is found and used", async () => {
|
|
const seen = kiro({ profiles: { "us-east-1": [US] } })
|
|
const saves = []
|
|
const h = await hooks(saves)
|
|
await h.auth.usage(async () => auth())
|
|
expect(saves.at(-1).profiles).toEqual({ "us-east-1": US })
|
|
// enabled by the admin; a new plugin asks again at once
|
|
kiro()
|
|
await (await hooks(saves)).auth.usage(async () => ({ ...auth(), profileArn: US, profiles: { "us-east-1": US } }))
|
|
expect(saves.at(-1).profiles).toEqual({ "us-east-1": US, "eu-central-1": EU })
|
|
})
|
|
|
|
test("a social sign-in keeps one region", async () => {
|
|
const seen = kiro()
|
|
const a = { type: "oauth", access: "t", refresh: "r", expires: later(), method: "social", loginProvider: "Github", region: "us-east-1",
|
|
profileArn: US, accountId: "me@example.com (GitHub)" }
|
|
const u = await (await hooks()).auth.usage(async () => a)
|
|
expect(seen.profiles).toBe(0)
|
|
expect(seen.usage).toEqual([["us-east-1", US]])
|
|
expect(u.windows.map((w) => w.name)).toEqual(["Credits"])
|
|
})
|