Skip to content

The agent that grows with you

fix(usage-window): Billing says credits cover a used-up Large allowance only when credits_pay

Teknium4eaf8fe1cec0
diff --git a/apps/desktop/src/app/settings/billing/use-billing-state.test.ts b/apps/desktop/src/app/settings/billing/use-billing-state.test.ts
index b15723c4cb8..5b5e7840c65 100644
--- a/apps/desktop/src/app/settings/billing/use-billing-state.test.ts
+++ b/apps/desktop/src/app/settings/billing/use-billing-state.test.ts
@@ -61,6 +61,24 @@ function monthlyCapRowForSpent(spent: string) {
return view.usageRows.find(row => row.id === 'monthly_cap')
}
+// The account setting rides on billing.state; `credits_pay` (setting on AND a top-up balance left) rides on
+// plan_usage, which the page reads from subscription.state first.
+function withKeepGoing(
+ { billing, subscription }: (typeof windowFixtures)['window-low'],
+ { credits_pay, keep_going_on_credits }: { credits_pay: boolean; keep_going_on_credits: boolean }
+) {
+ const planUsage = subscription.ok ? subscription.data.plan_usage : null
+
+ if (!billing.ok || !subscription.ok || !planUsage) {
+ throw new Error('window fixtures are ok and carry plan_usage')
+ }
+
+ return [
+ okBilling({ ...billing.data, keep_going_on_credits, plan_usage: { ...planUsage, credits_pay } }),
+ okSubscription({ ...subscription.data, plan_usage: { ...planUsage, credits_pay } })
+ ] as const
+}
+
describe('deriveBillingView (usage-window plans)', () => {
it('replaces the subscription credits row with today / week / month / large-model gauges', () => {
const fixture = windowFixtures['window-healthy']
@@ -98,10 +116,8 @@ describe('deriveBillingView (usage-window plans)', () => {
it('colours a running-down window yellow then red, and names an exhausted large-model allowance', () => {
const fixture = windowFixtures['window-low']
- const billing = fixture.billing?.ok
- ? { ...fixture.billing, data: { ...fixture.billing.data, keep_going_on_credits: true } }
- : fixture.billing
- const view = deriveBillingView(billing, fixture.subscription)
+ // What the server sends with Keep going on credits on and a top-up balance left.
+ const view = deriveBillingView(...withKeepGoing(fixture, { credits_pay: true, keep_going_on_credits: true }))
expect(view.usageRows[0]).toMatchObject({ bar: { state: 'danger' }, value: '88% used' })
expect(view.usageRows[1]).toMatchObject({ bar: { state: 'warn' }, value: '59% used' })
@@ -120,12 +136,21 @@ describe('deriveBillingView (usage-window plans)', () => {
const billing = fixture.billing?.ok
? { ...fixture.billing, data: { ...fixture.billing.data, keep_going_on_credits } }
: fixture.billing
+
const view = deriveBillingView(billing, fixture.subscription)
expect(view.usageRows[2]?.caption).toBe('Monthly allowance used up, other included models still work')
}
})
+ it('never says credits cover a used-up large-model allowance when no top-up balance is left', () => {
+ const fixture = windowFixtures['window-low']
+ // Keep going on credits is on, but the balance is $0: the server answers credits_pay false.
+ const view = deriveBillingView(...withKeepGoing(fixture, { credits_pay: false, keep_going_on_credits: true }))
+
+ expect(view.usageRows[2]?.caption).toBe('Monthly allowance used up, other included models still work')
+ })
+
it('captions every gauge as paused when the renewal payment failed', () => {
const fixture = windowFixtures['window-paused']
const view = deriveBillingView(fixture.billing, fixture.subscription)
diff --git a/apps/desktop/src/app/settings/billing/use-billing-state.ts b/apps/desktop/src/app/settings/billing/use-billing-state.ts
index dbd2c8fc8f9..12cc1ea2460 100644
--- a/apps/desktop/src/app/settings/billing/use-billing-state.ts
+++ b/apps/desktop/src/app/settings/billing/use-billing-state.ts
@@ -781,10 +781,7 @@ function deriveUsageRows(
// A usage-window plan shows each allowance as percent used. Those gauges replace
// the subscription-credits row; the top-up balance still follows below.
if (planUsage?.kind === 'usage_window') {
- // "Credits cover the rest" only when the setting is known to be on: members
- // (and older portals) don't get it, and "other included models still work"
- // is true either way.
- rows.push(...deriveWindowRows(planUsage, b, billing.keep_going_on_credits !== true))
+ rows.push(...deriveWindowRows(planUsage, b))
} else {
const remaining = parseAmount(current?.credits_remaining)
const monthly = parseAmount(current?.monthly_credits)
@@ -875,14 +872,13 @@ function deriveUsageRows(
* top-tier today; a new allowance just appears). Known ids keep their translated titles; any other id
* shows the server's title. Mirrors the CLI/TUI `/usage` block and the portal: `NN% used` +
* `resets in …`, the bar fills as the allowance is used, coloured by the gateway's `style`
- * (green → yellow → red). `keepGoingOff`: the account's "Keep going on
- * credits" is off, so a used-up top-tier allowance is not covered by credits (the other included
- * models still work).
+ * (green → yellow → red). A used-up top-tier allowance says credits cover the rest only when the
+ * server says they pay (`credits_pay`: Keep going on credits is on AND a top-up balance is left; with
+ * the setting on and $0 the next request is refused too); otherwise the other included models still work.
*/
function deriveWindowRows(
planUsage: PlanUsage,
- b: Translations['settings']['billing'],
- keepGoingOff: boolean
+ b: Translations['settings']['billing']
): BillingUsageRowView[] {
const copy = b.state.usage.window
const words = { day: copy.today, month: copy.month, top_tier: copy.topTier, week: copy.week }
@@ -913,9 +909,9 @@ function deriveWindowRows(
? copy.captionPaused
: copy.captionPausedOther
: gaugeScope(gauge) === 'top_tier' && exhausted
- ? keepGoingOff
- ? copy.captionTopTierUsedUpKeepGoingOff
- : copy.captionTopTierUsedUp
+ ? planUsage.credits_pay === true
+ ? copy.captionTopTierUsedUp
+ : copy.captionTopTierUsedUpKeepGoingOff
: resets
? copy.captionResetsIn(resets)
: copy.captionIncluded,