kiro 0.1.41: a month used up with trial credits left goes on on credits, so magpie spends the other accounts' plan credits first (#1352)

Kiro spends an account's plan credits first, then its trial credits
(kiro.dev/docs/billing: "Kiro uses your plan credits first, then your trial
credits"; trial credits last 12 months, plan credits end at the month's
reset). A month (or, with overage on, the overage) used up while a bonus has
credits left now says `credits: true` beside `aside: true`, so magpie asks
that account after the provider's accounts with plan credits left; an older
magpie routes it as before. A bonus Kiro marks EXHAUSTED is shown at 100%.
Identity Center per-region windows never carry `credits`.

Review follow-up (d947cdc): only an ACTIVE bonus counts as credits left (an
EXHAUSTED one whose count still said 499.5 / 500 made the account go on on
credits Kiro won't spend), and a bonus left that lapses before the month's
reset keeps the month set aside only, so that bonus isn't asked last and
lost.

Run at the reviewed head d947cdc48: HOME=$(mktemp -d) bun test in
packages/kiro on mote, 225 pass, 0 fail; bun scripts/check.mjs kiro ok; PR
CI (test, bumped, on-npm) green. Broken by hand: with the ACTIVE and lapse
checks removed, the new usage test goes red (13 pass, 1 fail).

Co-authored-by: yetone <yetoneful@gmail.com>
This commit is contained in:
stringke
2026-11-19 16:43:12 +08:00
committed by GitHub
co-authored by yetone
parent 140c513b81
commit 0ae116db1a
4 changed files with 95 additions and 21 deletions
+12 -4
View File
@@ -290,10 +290,18 @@ beside the model. Claude models and Auto have thinking budget variants. Models w
`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.
credits, a promotion's) while it is `ACTIVE` or `EXHAUSTED` (used up) 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, then
the bonus, so a month used up while a bonus has credits left goes on on
credits (`credits: true`): magpie asks that account after the provider's
accounts that still have plan credits, since the plan's credits are lost at
the month's reset and a trial's last a year. The window is also set aside
(`aside: true`), so a magpie that doesn't read `credits` doesn't take the
account for spent. Only an `ACTIVE` bonus counts as credits left (an
`EXHAUSTED` one is shown only), and when a bonus left lapses before the
month's reset the month is only set aside, so that bonus isn't asked last
and lost.
With overage on (`overageConfiguration.overageStatus` `ENABLED`), the month
is two windows, as the Kiro IDE shows it: the plan's credits, shown only
+31 -14
View File
@@ -542,11 +542,16 @@ function goG(x) {
// round.
//
// 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.
// a promotion's) are a window each while ACTIVE or EXHAUSTED (used up) 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 goes on on them (credits): magpie asks the
// account after those with plan credits left, as the plan's end at the
// month's reset and a trial's run a year. It is set aside too, for a
// magpie that doesn't know credits, so the account isn't taken for spent.
// Only an ACTIVE bonus counts as credits left, and a bonus left that
// lapses before the month's reset keeps the month set aside only.
//
// With overage on, Kiro goes on past the plan's credits, billed as post-paid
// credits, until those reach the overage cap. The cap counts the overage
@@ -559,6 +564,8 @@ function goG(x) {
// still sends means nothing, and the plan's credits are the month.
const count = (used, limit) => used.toFixed(2).replace(/0+$/, "").replace(/\.$/, "") + " / " + goG(limit)
const num = (x) => (typeof x === "number" && Number.isFinite(x) ? x : 0)
const BONUS_SHOWN = new Set(["ACTIVE", "EXHAUSTED"])
const ON_BONUS = { aside: true, credits: true }
const overageOn = (l) =>
String(l?.overageConfiguration?.overageStatus ?? "").toUpperCase() === "ENABLED" || l?.overageConfiguration?.overageEnabled === true
@@ -577,20 +584,28 @@ function readUsage(l, now = Date.now()) {
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 = []
const bonuses = [], spendable = []
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
if (!BONUS_SHOWN.has(String(b?.status ?? "").toUpperCase()) || 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 })
// only an ACTIVE bonus is spent: one Kiro marks EXHAUSTED has nothing
// to go on with, whatever its count says
if (left > 0 && String(b?.status ?? "").toUpperCase() === "ACTIVE") spendable.push(expires)
}
const used = num(u?.currentUsageWithPrecision), limit = num(u?.usageLimitWithPrecision)
const cap = overage ? num(u?.overageCapWithPrecision) || num(u?.overageCap) : 0
const reset = at(num(u?.nextDateReset) || num(l?.nextDateReset))
const month = { resetsAt: reset, unit: "credits", span: 30 * 24 * 3600 }
const name = u?.displayNamePlural || u?.displayName || "Credits"
const left = bonuses.some((b) => b.amount < b.limit)
const left = spendable.length > 0
// credits only when what is left outlasts the month: a bonus that
// lapses before the reset would be lost if asked last, so then the
// month is only set aside and the account keeps its listed place
const resetMs = Date.parse(reset ?? "")
const onBonus = spendable.some((e) => e > 0 && resetMs > 0 && e * 1000 < resetMs) ? { aside: true } : ON_BONUS
if (cap > 0) {
// what Kiro says of the overage, else what is past the plan
const said = typeof u?.currentOveragesWithPrecision === "number" ? u.currentOveragesWithPrecision : u?.currentOverages
@@ -598,12 +613,12 @@ function readUsage(l, now = Date.now()) {
const inPlan = Math.min(used, limit)
plan = limit > 0 ? { name, used: (100 * inPlan) / limit, display: count(inPlan, limit), amount: inPlan, limit, ...month, aside: true } : undefined
stop = { name: "Overage", used: (100 * over) / cap, display: count(over, cap), amount: over, limit: cap, ...month,
...(over >= cap && left ? { aside: true } : {}) }
...(over >= cap && left ? onBonus : {}) }
windows.push(...(plan ? [plan] : []), stop)
plan ??= stop
} else if (limit > 0) {
plan = stop = { name, used: (100 * used) / limit, display: count(used, limit), amount: used, limit, ...month,
...(used >= limit && left ? { aside: true } : {}) }
...(used >= limit && left ? onBonus : {}) }
windows.push(plan)
}
windows.push(...bonuses)
@@ -655,9 +670,11 @@ function regionsUsage(reads, now = Date.now(), only = "") {
allOver &&= over
}
monthName ??= plan.name
// a region's own windows are shown only: the sum below is weighed
const own = ({ credits, ...w }, n) => ({ ...w, name: `${n} (${r.region})`, aside: true })
if (!counted) {
shown.push({ ...plan, name: `${plan.name} (${r.region})`, aside: true })
if (over) shown.push({ ...stop, name: `${stop.name} (${r.region})`, aside: true })
shown.push(own(plan, plan.name))
if (over) shown.push(own(stop, stop.name))
regions.push({ region: r.region, full: stop.amount >= stop.limit, resetsAt: Date.parse(stop.resetsAt ?? "") > 0 ? Date.parse(stop.resetsAt) : 0 })
continue
}
@@ -667,8 +684,8 @@ function regionsUsage(reads, now = Date.now(), only = "") {
ofPlan += plan.limit
const at = Date.parse(stop.resetsAt ?? "")
if (at > 0 && (!resets || at < resets)) resets = at
shown.push({ ...plan, name: `${plan.name} (${r.region})`, aside: true })
if (over) shown.push({ ...stop, name: `${stop.name} (${r.region})`, aside: true })
shown.push(own(plan, plan.name))
if (over) shown.push(own(stop, stop.name))
regions.push({ region: r.region, full: stop.amount >= stop.limit, resetsAt: at > 0 ? at : 0 })
}
const windows = []
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-kiro-auth",
"version": "0.1.40",
"version": "0.1.41",
"description": "Kiro subscriptions (Free, Pro, Pro+, Power) in OpenCode and magpie",
"type": "module",
"main": "./opencode.mjs",
+51 -2
View File
@@ -136,18 +136,67 @@ test("a free trial and bonus credits together, and a spent month set aside while
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
// the month used up: Kiro goes on to the bonus, so the month goes on on
// credits (set aside for a magpie that doesn't know them)
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 === "Credits")).toMatchObject({ used: 100, aside: true, credits: true })
expect(spent.find((w) => w.name === "Free trial").aside).toBeUndefined()
expect(spent.find((w) => w.name === "Bonus credits").credits).toBeUndefined()
// below its end the month is the account's as before
expect(windows.find((w) => w.name === "Credits").credits).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 === "Credits").credits).toBeUndefined()
expect(all.find((w) => w.name === "Bonus credits")).toMatchObject({ used: 100, aside: true })
expect(all.find((w) => w.name === "Bonus credits").runsOutAt).toBeUndefined()
})
// Kiro marks trial credits used up EXHAUSTED (a Pro+ account's answer,
// details replaced): still shown, at 100%, so the card says the trial was
// spent rather than leaving it out; the month used up stops the account.
test("an exhausted trial is shown spent, and a month used up with it stops the account", () => {
const now = 1800000000_000
const l = withBreakdown(PRO, { currentUsageWithPrecision: 984, bonuses: [trialCredits({ status: "EXHAUSTED", currentUsage: 500 })] })
const { windows } = _internal.usageOf(l, now)
expect(windows).toEqual([{ ...MONTH, used: 100, display: "984 / 984", amount: 984 },
{ ...BONUS, used: 100, display: "500 / 500", amount: 500, runsOutAt: undefined, runsOut: undefined }])
expect(windows[0].aside).toBeUndefined()
expect(windows[0].credits).toBeUndefined()
})
// A bonus Kiro marks EXHAUSTED is nothing to go on with, even if its count
// says some is left: the month used up stops the account. A bonus left
// that lapses before the month's reset is not asked last: the month is
// only set aside, as before credits.
test("an exhausted bonus is no credits left, and one lapsing before the reset is not asked last", () => {
const now = 1790000000_000
const odd = _internal.usageOf(withBreakdown(PRO, { currentUsageWithPrecision: 984,
bonuses: [trialCredits({ status: "EXHAUSTED", currentUsage: 499.5 })] }), now).windows
expect(odd[0]).toMatchObject({ name: "Credits", used: 100 })
expect(odd[0].aside).toBeUndefined()
expect(odd[0].credits).toBeUndefined()
const soon = _internal.usageOf(withBreakdown(PRO, { currentUsageWithPrecision: 984,
bonuses: [trialCredits({ currentUsage: 10, expiresAt: 1796083200 - 86400 })] }), now).windows
expect(soon[0]).toMatchObject({ name: "Credits", used: 100, aside: true })
expect(soon[0].credits).toBeUndefined()
const later = _internal.usageOf(withBreakdown(PRO, { currentUsageWithPrecision: 984,
bonuses: [trialCredits({ currentUsage: 10, expiresAt: 1796083200 + 86400 })] }), now).windows
expect(later[0]).toMatchObject({ name: "Credits", used: 100, aside: true, credits: true })
})
// With overage on, the overage used up while a trial has credits left
// goes on on them; the plan's own window, shown only, never says credits.
test("the overage used up with a trial left goes on on credits", () => {
const now = 1800000000_000
const l = { ...withBreakdown(PRO, { currentUsageWithPrecision: 1084, overageCapWithPrecision: 100, currentOveragesWithPrecision: 100,
bonuses: [trialCredits({ currentUsage: 10 })] }), overageConfiguration: { overageStatus: "ENABLED" } }
const { windows } = _internal.usageOf(l, now)
expect(windows.map((w) => [w.name, w.aside, w.credits])).toEqual([
["Credits", true, undefined], ["Overage", true, true], ["Trial credits", true, undefined]])
})
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" })