kiro 0.1.27: Kiro's wait before asking again goes on as Retry-After
A 429 Kiro answered before any of the reply (USER_REQUEST_RATE_EXCEEDED, "Too many requests, please wait before trying again.") reached magpie with no word of when, as the plugin built its error response from nothing. With one account magpie then asked it again after 1, 2 and 4s and on through its patience: onemezao's one small message took six asks, five of them 429. The plugin now reads the wait where Kiro's own clients read it and sends it on as Retry-After (whole seconds, rounded up): the Kiro IDE's x-amzn-kiro-ratelimit-retry-after header and the body's retryAfterMilliseconds (both ms; kiro.kiro-agent 1.1.499, decompiled in hj01857655/kiro-account-manager docs/Kiro-1.4.5/re/pfd-full.txt), the x-amz-retry-after header kiro-cli 2.35's AWS SDK honours (ms, found in the binary's strings), and HTTP's Retry-After. magpie's gateway already waits a Retry-After: past 8s it skips the quick retries and rests the account that long. With none said, nothing changes. Run (mote, HOME a temp dir): bun test packages/kiro, 127 pass; bun scripts/check.mjs, ok. Broken by hand: generate without the wait argument; the new test fails (Expected "13", Received null). Not tried: a real Kiro 429. Which of these headers Kiro sends on generateAssistantResponse's 429 is not seen; the IDE reads them on its CreateResponse path. Hammering a real account to get one was not done. Reported-by: onemezao
This commit is contained in:
@@ -225,3 +225,32 @@ test("an answer says the account is kept, streamed or not, renewed or not", asyn
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
// onemezao on Discord: Kiro's 429 USER_REQUEST_RATE_EXCEEDED ("Too many
|
||||
// requests, please wait before trying again.") went to magpie with no word
|
||||
// of when, so magpie asked the one account again after 1, 2 and 4s and on
|
||||
// through its patience, six asks of the same request where Kiro had said
|
||||
// how long to wait. Kiro's wait goes on as Retry-After, from wherever Kiro's
|
||||
// own clients read it.
|
||||
test("Kiro's wait before asking again goes on as Retry-After", async () => {
|
||||
const limited = JSON.stringify({ message: "Too many requests, please wait before trying again.", reason: "USER_REQUEST_RATE_EXCEEDED" })
|
||||
const cases = [
|
||||
[{ "x-amzn-kiro-ratelimit-retry-after": "12500" }, limited, "13"],
|
||||
[{ "x-amz-retry-after": "3000" }, limited, "3"],
|
||||
[{}, JSON.stringify({ message: "Too many requests", reason: "USER_REQUEST_RATE_EXCEEDED", retryAfterMilliseconds: 20000 }), "20"],
|
||||
[{ "retry-after": "7" }, limited, "7"],
|
||||
[{ "retry-after": new Date(Date.now() + 30_000).toUTCString() }, limited, null],
|
||||
[{}, limited, null],
|
||||
[{ "x-amzn-kiro-ratelimit-retry-after": "soon" }, limited, null],
|
||||
]
|
||||
for (const [headers, body, want] of cases) {
|
||||
await withFetch(() => new Response(body, { status: 429, headers }), async () => {
|
||||
const res = await generate(creds(), {}, msgReq)
|
||||
expect(res.status).toBe(429)
|
||||
expect((await res.json()).error.message).toBe(JSON.parse(body).message + " (USER_REQUEST_RATE_EXCEEDED)")
|
||||
const got = res.headers.get("Retry-After")
|
||||
if (headers["retry-after"]?.includes("GMT")) expect(Number(got)).toBeWithin(28, 32)
|
||||
else expect(got).toBe(want)
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
+33
-6
@@ -1202,13 +1202,39 @@ const ERROR_TYPES = { 400: "invalid_request_error", 401: "authentication_error",
|
||||
404: "not_found_error", 413: "request_too_large", 429: "rate_limit_error", 503: "overloaded_error", 529: "overloaded_error" }
|
||||
const errorType = (status) => ERROR_TYPES[status] ?? "api_error"
|
||||
// Each failure says X-Magpie-Sign-In: kept, as the built-in never marked
|
||||
// an account lapsed (see gone).
|
||||
const errorResponse = (status, message) =>
|
||||
// an account lapsed (see gone). One Kiro said when to ask again (waitOf)
|
||||
// says it as Retry-After, in whole seconds, for magpie to wait as long.
|
||||
const errorResponse = (status, message, wait) =>
|
||||
new Response(JSON.stringify({ type: "error", error: { type: errorType(status), message } }), {
|
||||
status,
|
||||
headers: { "Content-Type": "application/json", "X-Magpie-Sign-In": "kept" },
|
||||
headers: { "Content-Type": "application/json", "X-Magpie-Sign-In": "kept",
|
||||
...(wait > 0 ? { "Retry-After": String(Math.ceil(wait / 1000)) } : {}) },
|
||||
})
|
||||
|
||||
// waitOf is the milliseconds Kiro's refusal says to wait before asking
|
||||
// again, or undefined: what Kiro's own clients read. The Kiro IDE (agent
|
||||
// 1.1.499) reads the x-amzn-kiro-ratelimit-retry-after header and the
|
||||
// body's retryAfterMilliseconds, both in milliseconds; kiro-cli's AWS SDK
|
||||
// reads x-amz-retry-after, in milliseconds; and Retry-After is HTTP's, in
|
||||
// seconds or as a date. Unsaid, magpie keeps to its own pauses.
|
||||
function waitOf(headers, body) {
|
||||
const ms = (v) => {
|
||||
const n = v == null || v === "" ? NaN : Number(v)
|
||||
return Number.isFinite(n) && n > 0 ? n : undefined
|
||||
}
|
||||
let w = ms(headers.get("x-amzn-kiro-ratelimit-retry-after")) ?? ms(headers.get("x-amz-retry-after"))
|
||||
if (w === undefined)
|
||||
try {
|
||||
w = ms(JSON.parse(body)?.retryAfterMilliseconds)
|
||||
} catch {}
|
||||
const ra = headers.get("retry-after")
|
||||
if (w === undefined && ra) {
|
||||
const secs = ms(ra)
|
||||
w = secs !== undefined ? secs * 1000 : ms(Date.parse(ra) - Date.now())
|
||||
}
|
||||
return w
|
||||
}
|
||||
|
||||
// kept is res saying the account is kept: a success too, which the
|
||||
// built-in never took for a sign-in come back.
|
||||
function kept(res) {
|
||||
@@ -1842,7 +1868,7 @@ async function generate(creds, auth, req, signal) {
|
||||
const hosted = hostedSearch(req)
|
||||
if (hosted?.error) return errorResponse(400, hosted.error)
|
||||
const first = await ask(creds, auth, req, model, budget, signal)
|
||||
if (!first.res) return errorResponse(first.status, first.message)
|
||||
if (!first.res) return errorResponse(first.status, first.message, first.wait)
|
||||
const it = hosted ? searching(creds, auth, req, model, budget, signal, hosted, first) : events(first.res.body, model, budget)
|
||||
return kept(await reply(it, model, req.stream === true, toolNames(req)))
|
||||
}
|
||||
@@ -1879,7 +1905,8 @@ async function ask(creds, auth, req, model, budget, signal) {
|
||||
}
|
||||
if (!res.ok) {
|
||||
// Kiro's own 401 is passed on, as the built-in did, the sign-in kept
|
||||
return failure(res.status, (await res.text()).slice(0, 1 << 20))
|
||||
const text = (await res.text()).slice(0, 1 << 20)
|
||||
return { ...failure(res.status, text), wait: waitOf(res.headers, text) }
|
||||
}
|
||||
return { res, a }
|
||||
}
|
||||
@@ -2418,4 +2445,4 @@ export async function KiroAuthPlugin({ client } = {}) {
|
||||
}
|
||||
|
||||
// for tests
|
||||
export const _internal = { generate, refresh, usageOf, buildKiro, kiroBody, IMAGE_LIMIT, events, reply, failure, toolID, toolName, toolNames, thinking, nativeFields, listModels, contextFirst, configModel, runtimeModel, readCLI, readIDE, regionOf, planName, frames }
|
||||
export const _internal = { generate, waitOf, refresh, usageOf, buildKiro, kiroBody, IMAGE_LIMIT, events, reply, failure, toolID, toolName, toolNames, thinking, nativeFields, listModels, contextFirst, configModel, runtimeModel, readCLI, readIDE, regionOf, planName, frames }
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@magpie-community/opencode-kiro-auth",
|
||||
"version": "0.1.26",
|
||||
"version": "0.1.27",
|
||||
"description": "Kiro subscriptions (Free, Pro, Pro+, Power) in OpenCode and magpie",
|
||||
"type": "module",
|
||||
"main": "./opencode.mjs",
|
||||
|
||||
Reference in New Issue
Block a user