Skip to content

Commit c57c905

Browse files
authored
fix(billing): refuse charges into a period the terminal settlement already invoiced (#8505)
* fix(billing): refuse charges into a period the terminal settlement already invoiced A subscription's deletion settles its terminal period at once (claim, overage, final invoice, bookkeeping), but recordCumulativeUsage kept topping up that period's row for a still-running request because the subscription's period never rolls after deletion. That spend was never billed. The terminal claim now advances the close marker to the period's end under its FOR UPDATE lock, and recordCumulativeUsage reads the marker in its existing FOR SHARE read: a charge whose target period ends at or before the marker throws CumulativeUsagePeriodClosedError, which update-cost answers with the existing non-retryable BILLING_PERIOD_ELAPSED outcome, so the worker quarantines the leg for reconciliation instead of the spend disappearing into an invoiced period. No migration: the marker is an existing column, and every reader treats a marker at or past periodStart as current, so v0.9.6 behaves unchanged. * test(billing): cover a terminal claim overlapping an in-flight charge Both lock orders against real PostgreSQL: a claim waits for an in-flight charge so the final sum includes it, and a charge that waited on an in-flight claim is refused.
1 parent a0b859b commit c57c905

9 files changed

Lines changed: 260 additions & 71 deletions

File tree

Lines changed: 87 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
/**
22
* Cost callbacks against real PostgreSQL: a direct-v1 run that outlives its admitted Stripe period
33
* records its later spend in the payer's current period, so the closed period is never topped up
4-
* after its invoice. Only the internal-key check is stubbed.
4+
* after its invoice, and spend after the payer's terminal settlement is refused. Only the
5+
* internal-key check is stubbed.
56
*/
67
import { db } from '@sim/db'
78
import { subscription, usageLog, user, userStats } from '@sim/db/schema'
89
import { envFlagsMock } from '@sim/testing/mocks/env-flags.mock'
910
import { generateId } from '@sim/utils/id'
10-
import { eq } from 'drizzle-orm'
11+
import { eq, inArray } from 'drizzle-orm'
1112
import { NextRequest } from 'next/server'
1213
import { afterAll, describe, expect, it, vi } from 'vitest'
1314

@@ -25,31 +26,86 @@ import {
2526
BILLING_ACCOUNT_DECISION_HEADER,
2627
serializeAccountBillingDecisionHeader,
2728
} from '@/lib/billing/core/billing-attribution'
29+
import { claimTerminalPeriod } from '@/lib/billing/cycle-close'
2830
import { POST } from '@/app/api/billing/update-cost/route'
2931

3032
const DAY_MS = 24 * 60 * 60 * 1000
31-
const userId = `update-cost-user-${generateId()}`
32-
const subscriptionId = generateId()
33+
const userIds: string[] = []
3334

3435
afterAll(async () => {
35-
await db.delete(usageLog).where(eq(usageLog.userId, userId))
36-
await db.delete(subscription).where(eq(subscription.id, subscriptionId))
37-
await db.delete(userStats).where(eq(userStats.userId, userId))
38-
await db.delete(user).where(eq(user.id, userId))
36+
if (userIds.length === 0) return
37+
await db.delete(usageLog).where(inArray(usageLog.userId, userIds))
38+
await db.delete(subscription).where(inArray(subscription.referenceId, userIds))
39+
await db.delete(userStats).where(inArray(userStats.userId, userIds))
40+
await db.delete(user).where(inArray(user.id, userIds))
3941
})
4042

41-
function callback(requestKey: string, cost: number, decision: string): NextRequest {
43+
interface Payer {
44+
userId: string
45+
subscriptionId: string
46+
/** The direct-v1 decision of a run admitted in the subscription's period. */
47+
decision: string
48+
}
49+
50+
/** A user on a pro subscription for `period`, whose close marker has caught up to it. */
51+
async function createPayer(period: { start: Date; end: Date }): Promise<Payer> {
52+
const userId = `update-cost-user-${generateId()}`
53+
const subscriptionId = generateId()
54+
userIds.push(userId)
55+
await db.insert(user).values({
56+
id: userId,
57+
name: 'Update Cost Test',
58+
email: `${userId}@update-cost.test`,
59+
emailVerified: true,
60+
createdAt: new Date(),
61+
updatedAt: new Date(),
62+
})
63+
await db.insert(userStats).values({ id: generateId(), userId })
64+
await db.insert(subscription).values({
65+
id: subscriptionId,
66+
plan: 'pro',
67+
referenceId: userId,
68+
status: 'active',
69+
periodStart: period.start,
70+
periodEnd: period.end,
71+
lastClosedPeriodStart: period.start,
72+
})
73+
const decision = serializeAccountBillingDecisionHeader({
74+
userId,
75+
billingEntity: { type: 'user', id: userId },
76+
billingPeriod: {
77+
start: period.start.toISOString(),
78+
end: period.end.toISOString(),
79+
source: 'stripe',
80+
},
81+
payerSubscriptionId: subscriptionId,
82+
})
83+
return { userId, subscriptionId, decision }
84+
}
85+
86+
function requestRows(requestKey: string) {
87+
return db
88+
.select({
89+
eventKey: usageLog.eventKey,
90+
cost: usageLog.cost,
91+
billingPeriodStart: usageLog.billingPeriodStart,
92+
})
93+
.from(usageLog)
94+
.where(inArray(usageLog.eventKey, [`update-cost:${requestKey}`, `update-cost:${requestKey}@1`]))
95+
}
96+
97+
function callback(payer: Payer, requestKey: string, cost: number): NextRequest {
4298
return new NextRequest('http://localhost:3000/api/billing/update-cost', {
4399
method: 'POST',
44100
headers: {
45101
'content-type': 'application/json',
46102
'x-api-key': 'internal',
47103
'x-sim-billing-protocol': 'direct-v1',
48104
'x-sim-billing-request-id': requestKey,
49-
[BILLING_ACCOUNT_DECISION_HEADER]: decision,
105+
[BILLING_ACCOUNT_DECISION_HEADER]: payer.decision,
50106
},
51107
body: JSON.stringify({
52-
userId,
108+
userId: payer.userId,
53109
cost,
54110
model: 'test-model',
55111
source: 'copilot',
@@ -63,50 +119,17 @@ describe('direct-v1 cost callbacks in PostgreSQL', () => {
63119
const now = Date.now()
64120
const admitted = { start: new Date(now - 10 * DAY_MS), end: new Date(now + 20 * DAY_MS) }
65121
const rolled = { start: new Date(now - 60 * 60 * 1000), end: new Date(now + 30 * DAY_MS) }
66-
await db.insert(user).values({
67-
id: userId,
68-
name: 'Update Cost Test',
69-
email: `${userId}@update-cost.test`,
70-
emailVerified: true,
71-
createdAt: new Date(now),
72-
updatedAt: new Date(now),
73-
})
74-
await db.insert(userStats).values({ id: generateId(), userId })
75-
await db.insert(subscription).values({
76-
id: subscriptionId,
77-
plan: 'pro',
78-
referenceId: userId,
79-
status: 'active',
80-
periodStart: admitted.start,
81-
periodEnd: admitted.end,
82-
})
83-
const decision = serializeAccountBillingDecisionHeader({
84-
userId,
85-
billingEntity: { type: 'user', id: userId },
86-
billingPeriod: {
87-
start: admitted.start.toISOString(),
88-
end: admitted.end.toISOString(),
89-
source: 'stripe',
90-
},
91-
payerSubscriptionId: subscriptionId,
92-
})
122+
const payer = await createPayer(admitted)
93123
const requestKey = generateId()
94124

95-
expect((await POST(callback(requestKey, 0.5, decision), {})).status).toBe(200)
125+
expect((await POST(callback(payer, requestKey, 0.5), {})).status).toBe(200)
96126
await db
97127
.update(subscription)
98128
.set({ periodStart: rolled.start, periodEnd: rolled.end })
99-
.where(eq(subscription.id, subscriptionId))
100-
expect((await POST(callback(requestKey, 0.8, decision), {})).status).toBe(200)
129+
.where(eq(subscription.id, payer.subscriptionId))
130+
expect((await POST(callback(payer, requestKey, 0.8), {})).status).toBe(200)
101131

102-
const rows = await db
103-
.select({
104-
eventKey: usageLog.eventKey,
105-
cost: usageLog.cost,
106-
billingPeriodStart: usageLog.billingPeriodStart,
107-
})
108-
.from(usageLog)
109-
.where(eq(usageLog.userId, userId))
132+
const rows = await requestRows(requestKey)
110133
const byKey = new Map(rows.map((row) => [row.eventKey, row]))
111134
expect(rows).toHaveLength(2)
112135
expect(Number(byKey.get(`update-cost:${requestKey}`)?.cost)).toBeCloseTo(0.5)
@@ -118,4 +141,19 @@ describe('direct-v1 cost callbacks in PostgreSQL', () => {
118141
rolled.start.getTime()
119142
)
120143
})
144+
145+
it("refuses spend that lands after the payer's terminal settlement", async () => {
146+
const now = Date.now()
147+
const period = { start: new Date(now - 10 * DAY_MS), end: new Date(now + 20 * DAY_MS) }
148+
const payer = await createPayer(period)
149+
const requestKey = generateId()
150+
151+
expect((await POST(callback(payer, requestKey, 0.5), {})).status).toBe(200)
152+
await claimTerminalPeriod(payer.subscriptionId)
153+
const late = await POST(callback(payer, requestKey, 0.8), {})
154+
155+
expect(late.status).toBe(409)
156+
expect(await late.json()).toMatchObject({ code: 'BILLING_PERIOD_ELAPSED', retryable: false })
157+
expect((await requestRows(requestKey)).map((row) => Number(row.cost))).toEqual([0.5])
158+
})
121159
})

‎apps/sim/app/api/billing/update-cost/route.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
import {
3131
type CumulativeUsageContextField,
3232
CumulativeUsageContextMismatchError,
33+
CumulativeUsagePeriodClosedError,
3334
recordCumulativeUsage,
3435
} from '@/lib/billing/core/usage-log'
3536
import {
@@ -512,7 +513,8 @@ async function updateCostInner(req: NextRequest, span: Span): Promise<NextRespon
512513
const pgCode = getPostgresErrorCode(error)
513514
const pgConstraint = getPostgresConstraintName(error)
514515
const reconciliationOutcome =
515-
error instanceof ThresholdSettlementError && !error.retryable
516+
(error instanceof ThresholdSettlementError && !error.retryable) ||
517+
error instanceof CumulativeUsagePeriodClosedError
516518
? BILLING_CALLBACK_OUTCOME.billingPeriodElapsed
517519
: pgCode === '23503' && pgConstraint === 'usage_log_user_id_user_id_fk'
518520
? BILLING_CALLBACK_OUTCOME.billingUserNotFound

‎apps/sim/lib/billing/core/usage-log.integration.ts‎

Lines changed: 95 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { db } from '@sim/db'
99
import * as schema from '@sim/db/schema'
1010
import { readTestDatabaseUrl } from '@sim/db/testing/test-infrastructure'
1111
import { getPostgresErrorCode } from '@sim/utils/errors'
12+
import { sleep } from '@sim/utils/helpers'
1213
import { generateId } from '@sim/utils/id'
1314
import { eq, sql } from 'drizzle-orm'
1415
import { drizzle } from 'drizzle-orm/postgres-js'
@@ -20,16 +21,21 @@ const databaseUrl = readTestDatabaseUrl()
2021

2122
vi.mock('@sim/db', () => ({ db: { transaction }, dbReplica: {} }))
2223
vi.mock('@/lib/billing/core/plan', () => ({ getHighestPrioritySubscription: vi.fn() }))
23-
vi.mock('@/lib/billing/subscriptions/utils', () => ({ isOrgScopedSubscription: vi.fn() }))
24+
vi.mock('@/lib/billing/subscriptions/utils', async (importOriginal) => ({
25+
...(await importOriginal<typeof import('@/lib/billing/subscriptions/utils')>()),
26+
isOrgScopedSubscription: vi.fn(),
27+
}))
2428

2529
import {
2630
CumulativeUsageContextMismatchError,
31+
CumulativeUsagePeriodClosedError,
2732
getBillingPeriodUsageCost,
2833
getBillingPeriodUsageCostByUser,
2934
getStampedPeriodRangeUsageCostByUser,
3035
type RecordCumulativeUsageParams,
3136
recordCumulativeUsage,
3237
} from '@/lib/billing/core/usage-log'
38+
import { claimTerminalPeriod } from '@/lib/billing/cycle-close'
3339

3440
const require = createRequire(import.meta.url)
3541
const commonJsPostgres = require('postgres') as typeof postgres
@@ -121,7 +127,10 @@ describe('Cumulative billing with PostgreSQL', () => {
121127
CREATE UNIQUE INDEX usage_log_event_key_unique ON usage_log(event_key)
122128
WHERE event_key IS NOT NULL;
123129
CREATE TABLE driver_probe (id text PRIMARY KEY);
124-
CREATE TABLE subscription (id text PRIMARY KEY, period_start timestamp, period_end timestamp)
130+
CREATE TABLE subscription (
131+
id text PRIMARY KEY, period_start timestamp, period_end timestamp,
132+
last_closed_period_start timestamp
133+
)
125134
`)
126135
transaction.mockImplementation(async (callback: (tx: Transaction) => Promise<unknown>) => {
127136
const pause = nextPause
@@ -362,12 +371,16 @@ describe('Cumulative billing with PostgreSQL', () => {
362371
]
363372
const payer = { type: 'organization', id: 'payer' } as const
364373

374+
/** Moves the subscription to a window whose predecessor the cycle close has settled. */
365375
async function setSubscriptionWindow(start: Date, end: Date) {
366376
await connection`
367-
insert into subscription (id, period_start, period_end)
368-
values ('sub-1', ${start.toISOString()}::timestamptz at time zone 'UTC', ${end.toISOString()}::timestamptz at time zone 'UTC')
377+
insert into subscription (id, period_start, period_end, last_closed_period_start)
378+
values ('sub-1', ${start.toISOString()}::timestamptz at time zone 'UTC', ${end.toISOString()}::timestamptz at time zone 'UTC', ${start.toISOString()}::timestamptz at time zone 'UTC')
369379
on conflict (id) do update
370-
set period_start = excluded.period_start, period_end = excluded.period_end
380+
set period_start = excluded.period_start, period_end = excluded.period_end,
381+
last_closed_period_start = greatest(
382+
subscription.last_closed_period_start, excluded.last_closed_period_start
383+
)
371384
`
372385
}
373386

@@ -528,6 +541,83 @@ describe('Cumulative billing with PostgreSQL', () => {
528541
expect(await ledgerRows()).toEqual([{ event_key: usage(0).eventKey, cost: '0.4' }])
529542
})
530543

544+
it('refuses a charge that would roll into a period the terminal settlement already summed', async () => {
545+
await setSubscriptionPeriod(0)
546+
await charge(0.4)
547+
await setSubscriptionPeriod(1)
548+
await connection`
549+
update subscription
550+
set last_closed_period_start = ${periods[2].toISOString()}::timestamptz at time zone 'UTC'
551+
`
552+
553+
await expect(charge(1)).rejects.toBeInstanceOf(CumulativeUsagePeriodClosedError)
554+
expect(await ledgerRows()).toEqual([{ event_key: usage(0).eventKey, cost: '0.4' }])
555+
})
556+
557+
/**
558+
* Resolves true once a session waits on a row lock of the subscription table, or false once
559+
* `work` settles without anyone waiting, so a missing lock fails instead of hanging.
560+
*/
561+
async function waitsOnSubscriptionRow(work: Promise<unknown>) {
562+
let settled = false
563+
work.then(
564+
() => {
565+
settled = true
566+
},
567+
() => {
568+
settled = true
569+
}
570+
)
571+
while (!settled) {
572+
const [row] = await connection<{ waiting: boolean }[]>`
573+
select exists (
574+
select 1 from pg_locks
575+
where locktype = 'tuple' and relation = 'subscription'::regclass
576+
) as waiting
577+
`
578+
if (row.waiting) return true
579+
await sleep(10)
580+
}
581+
return false
582+
}
583+
584+
it('makes the terminal claim wait for an in-flight charge, so the final sum includes it', async () => {
585+
await setSubscriptionPeriod(0)
586+
await charge(0.4)
587+
const pause = pauseNextTransaction()
588+
const inFlight = charge(0.6)
589+
let claim: Promise<unknown> = Promise.resolve()
590+
try {
591+
await pause.reached.promise
592+
claim = claimTerminalPeriod('sub-1')
593+
expect(await waitsOnSubscriptionRow(claim)).toBe(true)
594+
} finally {
595+
pause.release.resolve()
596+
await inFlight
597+
await claim
598+
}
599+
expect(await stampedTotal(0)).toBeCloseTo(0.6, 9)
600+
await expect(charge(0.8)).rejects.toBeInstanceOf(CumulativeUsagePeriodClosedError)
601+
})
602+
603+
it('refuses a charge that waited on an in-flight terminal claim', async () => {
604+
await setSubscriptionPeriod(0)
605+
await charge(0.4)
606+
const pause = pauseNextTransaction()
607+
const claim = claimTerminalPeriod('sub-1')
608+
let late: Promise<unknown> = Promise.resolve()
609+
try {
610+
await pause.reached.promise
611+
late = charge(0.6)
612+
expect(await waitsOnSubscriptionRow(late)).toBe(true)
613+
} finally {
614+
pause.release.resolve()
615+
await claim
616+
}
617+
await expect(late).rejects.toBeInstanceOf(CumulativeUsagePeriodClosedError)
618+
expect(await stampedTotal(0)).toBeCloseTo(0.4, 9)
619+
})
620+
531621
it('holds an early period-start move until an in-flight top-up commits', async () => {
532622
const start = new Date(Date.now() - 24 * 60 * 60 * 1000)
533623
const end = new Date(Date.now() + 30 * 24 * 60 * 60 * 1000)

0 commit comments

Comments
 (0)