Skip to content

Commit 76cad8a

Browse files
committed
fix(billing): bound free payers' fresh usage reads and shrink the test-types baseline
1 parent 4d55764 commit 76cad8a

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

‎apps/sim/lib/billing/core/mid-run-usage.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { isFree } from '@/lib/billing/plan-helpers'
2424
import type { UsageUpgradePayer } from '@/lib/billing/usage-upgrade'
2525
import { coalesceLocally } from '@/lib/concurrency/singleflight'
2626
import { isBillingEnabled, isHosted } from '@/lib/core/config/env-flags'
27+
import { withinDeadline } from '@/lib/core/utils/deadline'
2728

2829
const logger = createLogger('MidRunUsage')
2930

@@ -144,7 +145,10 @@ async function readGateVerdict(
144145
try {
145146
usage =
146147
options.uncachedForFreePayer && isFreeRunPayer(attribution)
147-
? await checkAttributedUsageLimits(attribution)
148+
? await withinDeadline(
149+
() => checkAttributedUsageLimits(attribution),
150+
Date.now() + USAGE_GATE_SETTLE_TIMEOUT_MS
151+
)
148152
: await checkExecutionUsageLimits(attribution)
149153
} catch (error) {
150154
logger.warn('Mid-run usage read failed', {
@@ -278,7 +282,7 @@ export async function readMidRunAccountUsageVerdict(
278282
checkUsageStatus(decision.userId, usageSubscription, { billingEntity: payer, billingPeriod })
279283
// A shared in-flight read may have started before this caller's charge was recorded.
280284
const usage = uncached
281-
? await readUsage()
285+
? await withinDeadline(readUsage, Date.now() + USAGE_GATE_SETTLE_TIMEOUT_MS)
282286
: await coalesceLocally(
283287
`mid-run-account-usage:${key}`,
284288
readUsage,

‎scripts/check-test-types.baseline.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
"TS2554": 1
7272
},
7373
"apps/sim/app/api/billing/update-cost/route.test.ts": {
74-
"TS2554": 51
74+
"TS2554": 50
7575
},
7676
"apps/sim/app/api/billing/update-cost/worker.live.test.ts": {
7777
"TS2554": 1
@@ -98,7 +98,7 @@
9898
"TS2554": 1
9999
},
100100
"apps/sim/app/api/copilot/api-keys/validate/route.test.ts": {
101-
"TS2554": 55
101+
"TS2554": 52
102102
},
103103
"apps/sim/app/api/copilot/chat/stop/route.test.ts": {
104104
"TS18046": 2,

0 commit comments

Comments
 (0)