fix(billing): refuse charges into a period the terminal settlement already invoiced - #8505
Merged
waleedlatif1 merged 2 commits intoOct 1, 2026
Merged
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Collaborator
Author
Collaborator
Author
|
@cubic-dev-ai review this PR |
Contributor
@waleedlatif1 I have started the AI code review. It will take a few minutes to complete. |
Contributor
|
…ready 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.
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.
waleedlatif1
force-pushed
the
fix/billing-no-topup-after-terminal-settlement
branch
from
October 1, 2026 06:27
8af8bf8 to
37a2eea
Compare
Collaborator
Author
Collaborator
Author
|
@cubic-dev-ai review this PR |
Contributor
@waleedlatif1 I have started the AI code review. It will take a few minutes to complete. |
waleedlatif1
deleted the
fix/billing-no-topup-after-terminal-settlement
branch
October 1, 2026 06:43
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This addresses the release review's finding "Closed periods receive later charges" (
apps/sim/lib/billing/core/usage-log.ts). A cost callback that commits after a subscription's terminal settlement no longer tops up a period the final invoice has already summed.Root cause
When a subscription is deleted,
handleSubscriptionDeletedsettles its terminal period straight away: it claims the period, sums overage, sends the final invoice and writes the bookkeeping. A deleted subscription'speriodStartnever moves again, sorecordCumulativeUsagenever sees a rolled period. A request that was still running therefore kept topping up the terminal period's row after the invoice had summed it, and no invoice ever read that spend. The terminal claim wrote no durable "settled" marker that a later callback could check: it only moved the close marker toperiodStart, which every reader treats as "current period still open". This is pre-existing behaviour, not a regression in the release; prod tops up the frozen row the same way.Fix
claimTerminalPeriod(lib/billing/cycle-close.ts) now moves the existing close marker (last_closed_period_start) to the terminal period's end (or toperiodStartwhen there is no end). It does this under theFOR UPDATElock it already held, and does it whether or not the marker was current.recordCumulativeUsagereads that marker in its existingFOR SHAREsubscription read. If the marker is at or past the end of the charge's target period, it throwsCumulativeUsagePeriodClosedError. The two locks put every charge in a strict order against the settlement: a charge either commits before the claim, so the final invoice sums it, or it sees the marker and is refused.update-costmaps that error to the existing non-retryableBILLING_PERIOD_ELAPSED(409) outcome, which the worker already treats as non-retryable.>= periodStart, so a marker atperiodEndstill reads as "current". Rollovers are unaffected: the sweep sets the marker to the newperiodStart, which is always earlier than the end of any period a charge can target.Behaviour changes
BILLING_PERIOD_ELAPSED(retryable: false). Before, it was silently written to a row no invoice would read.Not changed
periodStartnever rolls, the sweep never closes it, andclaimTerminalPeriodreturns early for it. No invoiced period is being topped up there, so there is nothing to refuse against. Topping up the latest row is the correct behaviour for an active payer with unsynced bounds. Adding a wall-clock heuristic would refuse legitimate charges, so this PR leaves that path alone.Test plan
usage-log.integration.ts: a charge that would roll into a period whose marker the terminal settlement has passed is refused, and the ledger is unchanged. Fails with the guard removed.update-cost/route.integration.ts: a callback, thenclaimTerminalPeriod, then a later callback returns 409BILLING_PERIOD_ELAPSEDand leaves the original row's cost unchanged. Fails with the guard removed, and also fails with the claim reverted toperiodStart.bun run test:integration(filtered to the billing usage-log and update-cost suites, on throwaway containers)lib/billing/cycle-close,lib/billing/webhooks,lib/billing/core,update-cost/route.test.ts,threshold-billing)bun run lint,bun run type-check(apps/sim, packages/testing),bun run check:audits