kiro 0.1.25: bonus credits show on the Usage card, each with when it lapses (#602)

kiro 0.1.25: bonus credits (bonuses in Get-Usage-Limits) show on the Usage card as windows of their own, aside, with when they lapse; a month used up while a bonus has credits left is set aside too, since Kiro spends plan credits first.

Reviewed head fc9eb1e on main df98bf6, on macOS (Bun 1.4.2): packages/kiro bun test 126 pass 0 fail; root bun test 4533 pass 9 skip 0 fail; scripts/check.mjs ok. With main's index.mjs, 3 of the new usage tests fail; removing the carried aside, the expiry filter, the ACTIVE filter, the bonus aside or the spent-bonus runsOut guard each turns at least one red. Shape checked against kiro-cli's UsageBreakdown model (bonuses, FIRST_UPGRADE_TRIAL) and kiro.dev/docs/billing. Not tried with a real Kiro account by the reviewer.
This commit is contained in:
Giant_GKL
2026-11-01 22:14:27 +08:00
committed by GitHub
parent 0678f8613f
commit 605381174d
4 changed files with 105 additions and 6 deletions
+9
View File
@@ -240,6 +240,15 @@ the `provider.models` hook replaces it with the account's own list from
context window, output limit and whether it takes images. Claude models
and Auto have the thinking variants.
## Usage in magpie
`auth.usage` reads `Get-Usage-Limits`: the month's credits, the Free trial
while it is active, and each bonus (`bonuses`: a first upgrade's trial
credits, a promotion's) while it is `ACTIVE` and not expired, as a window of
its own with its count and when it lapses. Bonus windows are shown only.
Kiro spends the plan's credits first, so a month used up while a bonus has
credits left is shown only too, and the account isn't taken for spent.
## Not included
- Usage and quota (credits, the monthly window).
+23 -5
View File
@@ -419,7 +419,14 @@ function goG(x) {
// magpie can say what is left of it when the user shows what is left
// (#408): display alone is the vendor's text, which magpie can't turn
// round.
function usageOf(l) {
//
// Bonus credits (each allowance's bonuses: a first upgrade's trial credits,
// a promotion's) are a window each while ACTIVE and not expired, after the
// month's: their count, and when they lapse (runsOutAt), as they don't
// reset. They are shown only (aside). Kiro spends the plan's credits
// first, then these, so a month used up while a bonus has credits left
// is set aside too, and the account isn't taken for spent.
function usageOf(l, now = Date.now()) {
const at = (secs) => (secs > 0 ? new Date(Math.trunc(secs) * 1000).toISOString() : undefined)
const count = (used, limit) => used.toFixed(2).replace(/0+$/, "").replace(/\.$/, "") + " / " + goG(limit)
const num = (x) => (typeof x === "number" && Number.isFinite(x) ? x : 0)
@@ -431,11 +438,22 @@ function usageOf(l) {
windows.push({ name: "Free trial", used: (100 * used) / limit, resetsAt: at(num(t.freeTrialExpiry)), display: count(used, limit),
amount: used, limit, unit: "credits" })
}
const bonuses = []
for (const b of Array.isArray(u?.bonuses) ? u.bonuses : []) {
const used = num(b?.currentUsage), limit = num(b?.usageLimit), expires = num(b?.expiresAt)
if (String(b?.status ?? "").toUpperCase() !== "ACTIVE" || limit <= 0 || (expires > 0 && expires * 1000 <= now)) continue
const left = Math.max(0, limit - used)
bonuses.push({ name: b?.displayName || "Bonus credits", used: (100 * used) / limit, display: count(used, limit),
amount: used, limit, unit: "credits", ...(expires > 0 && left > 0 ? { runsOutAt: at(expires), runsOut: left } : {}), aside: true })
}
const used = num(u?.currentUsageWithPrecision), limit = num(u?.usageLimitWithPrecision)
if (limit <= 0) continue
const reset = num(u?.nextDateReset) || num(l?.nextDateReset)
windows.push({ name: u?.displayNamePlural || u?.displayName || "Credits", used: (100 * used) / limit, resetsAt: at(reset),
display: count(used, limit), amount: used, limit, unit: "credits", span: 30 * 24 * 3600 })
if (limit > 0) {
const reset = num(u?.nextDateReset) || num(l?.nextDateReset)
const carried = used >= limit && bonuses.some((b) => b.amount < b.limit)
windows.push({ name: u?.displayNamePlural || u?.displayName || "Credits", used: (100 * used) / limit, resetsAt: at(reset),
display: count(used, limit), amount: used, limit, unit: "credits", span: 30 * 24 * 3600, ...(carried ? { aside: true } : {}) })
}
windows.push(...bonuses)
}
const email = l?.userInfo?.email
return { plan: planName(l?.subscriptionInfo?.subscriptionTitle), ...(typeof email === "string" && email ? { user: email } : {}), windows }
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-kiro-auth",
"version": "0.1.24",
"version": "0.1.25",
"description": "Kiro subscriptions (Free, Pro, Pro+, Power) in OpenCode and magpie",
"type": "module",
"main": "./index.mjs",
+72
View File
@@ -76,6 +76,78 @@ test("names, resets and counts as Go says them", () => {
expect(windows.map((w) => [w.amount, w.limit, w.unit])).toEqual([[1.005, 1000000, "credits"], [150.1, 100, "credits"]])
})
// A Pro account's answer with bonus credits, as Get-Usage-Limits gave it
// (account details replaced): the first upgrade's trial credits in
// bonuses, and the Free trial before it expired.
const BONUS_EXPIRES = 4100000000.5 // far off: the first test reads the real clock
const trialCredits = (over = {}) => ({ bonusCode: "TESTBONUS#0", bonusType: "FIRST_UPGRADE_TRIAL", currentUsage: 0.0,
description: "Trial credits from your first upgrade", displayName: "Trial credits", expiresAt: BONUS_EXPIRES,
redeemedAt: 1793000000.25, status: "ACTIVE", usageLimit: 500.0, ...over })
const PRO = { nextDateReset: 1796083200.0, overageConfiguration: { overageStatus: "DISABLED" },
subscriptionInfo: { subscriptionTitle: "KIRO PRO", type: "Q_DEVELOPER_STANDALONE_PRO" },
userInfo: { email: "me@example.com", userId: "d-0000000000.test" },
usageBreakdownList: [{ addOnMetadata: null, bonuses: [trialCredits()], currency: "USD", currentUsage: 48, currentUsageWithPrecision: 48.97,
displayName: "Credit", displayNamePlural: "Credits",
freeTrialInfo: { currentUsage: 24, currentUsageWithPrecision: 24.26, freeTrialExpiry: 1769846399.708, freeTrialStatus: "EXPIRED",
usageLimit: 500, usageLimitWithPrecision: 500.0 },
nextDateReset: 1796083200.0, overageCredits: [], resourceType: "CREDIT", unit: "INVOCATIONS", usageLimit: 984, usageLimitWithPrecision: 984.0 }] }
const withBreakdown = (l, over) => ({ ...l, usageBreakdownList: [{ ...l.usageBreakdownList[0], ...over }] })
const MONTH = { name: "Credits", used: (100 * 48.97) / 984, resetsAt: new Date(1796083200_000).toISOString(), display: "48.97 / 984",
amount: 48.97, limit: 984, unit: "credits", span: 30 * 24 * 3600 }
const BONUS = { name: "Trial credits", used: 0, display: "0 / 500", amount: 0, limit: 500, unit: "credits",
runsOutAt: new Date(4100000000_000).toISOString(), runsOut: 500, aside: true }
test("bonus credits are a window of their own after the month's, with when they lapse", async () => {
answer(200, PRO)
expect(await usage()).toEqual({ plan: "Kiro Pro", user: "me@example.com", windows: [MONTH, BONUS], signIn: "renewed" })
// part spent: what is left lapses
const { windows } = _internal.usageOf(withBreakdown(PRO, { bonuses: [trialCredits({ currentUsage: 120.5 })] }), 1800000000_000)
expect(windows[1]).toEqual({ ...BONUS, used: 24.1, display: "120.5 / 500", amount: 120.5, runsOut: 379.5 })
})
test("no bonuses: the windows are what they were", () => {
const before = _internal.usageOf(withBreakdown(PRO, { bonuses: undefined }))
expect(before.windows).toEqual([MONTH])
for (const bonuses of [[], null, "none", [null, {}, { status: "ACTIVE" }]])
expect(_internal.usageOf(withBreakdown(PRO, { bonuses }))).toEqual(before)
// the free account's answer has none either
expect(_internal.usageOf(FREE).windows.map((w) => w.name)).toEqual(["Free trial", "Credits"])
})
test("a bonus expired, or not active, is no window and keeps nothing aside", () => {
const now = 1800000000_000
for (const b of [trialCredits({ expiresAt: 1799999999 }), trialCredits({ status: "EXPIRED" }), trialCredits({ usageLimit: 0 })]) {
expect(_internal.usageOf(withBreakdown(PRO, { bonuses: [b] }), now).windows).toEqual([MONTH])
// a month used up stops the account as before
const spent = _internal.usageOf(withBreakdown(PRO, { bonuses: [b], currentUsageWithPrecision: 984 }), now).windows
expect(spent).toHaveLength(1)
expect(spent[0].aside).toBeUndefined()
}
// one with no expiry is kept, with no lapse told
const open = _internal.usageOf(withBreakdown(PRO, { bonuses: [trialCredits({ expiresAt: undefined })] }), now).windows[1]
expect(open.runsOutAt).toBeUndefined()
expect(open.aside).toBe(true)
})
test("a free trial and bonus credits together, and a spent month set aside while a bonus has credits left", () => {
const now = 1790500000_000
const l = withBreakdown(FREE, { bonuses: [trialCredits({ displayName: "Bonus credits", currentUsage: 10, usageLimit: 100 })] })
const { windows } = _internal.usageOf(l, now)
expect(windows.map((w) => [w.name, w.display, w.aside])).toEqual([
["Free trial", "100 / 500", undefined], ["Credits", "12.5 / 50", undefined], ["Bonus credits", "10 / 100", true]])
expect(windows[2].runsOut).toBe(90)
// the month used up: Kiro goes on to the bonus, so the month is shown only
const spent = _internal.usageOf(withBreakdown(l, { currentUsageWithPrecision: 50 }), now).windows
expect(spent.find((w) => w.name === "Credits").aside).toBe(true)
expect(spent.find((w) => w.name === "Free trial").aside).toBeUndefined()
// and the bonus used up too: the month stops the account again
const all = _internal.usageOf(withBreakdown(l, { currentUsageWithPrecision: 50,
bonuses: [trialCredits({ displayName: "Bonus credits", currentUsage: 100, usageLimit: 100 })] }), now).windows
expect(all.find((w) => w.name === "Credits").aside).toBeUndefined()
expect(all.find((w) => w.name === "Bonus credits")).toMatchObject({ used: 100, aside: true })
expect(all.find((w) => w.name === "Bonus credits").runsOutAt).toBeUndefined()
})
test("Kiro's refusal is the card's error", async () => {
answer(403, { message: "The bearer token included in the request is invalid." })
expect(await usage()).toEqual({ error: "Kiro: The bearer token included in the request is invalid. (403)", signIn: "kept" })