Title: fix(billing): refuse charges into a period the terminal settlement already invoiced by waleedlatif1 · Pull Request #8505 · simstudioai/sim · GitHub
Open Graph Title: fix(billing): refuse charges into a period the terminal settlement already invoiced by waleedlatif1 · Pull Request #8505 · simstudioai/sim
X Title: fix(billing): refuse charges into a period the terminal settlement already invoiced by waleedlatif1 · Pull Request #8505 · simstudioai/sim
Description: 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, handleSubscriptionDeleted settles its terminal period straight away: it claims the period, sums overage, sends the final invoice and writes the bookkeeping. A deleted subscription's periodStart never moves again, so recordCumulativeUsage never 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 to periodStart, 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 to periodStart when there is no end). It does this under the FOR UPDATE lock it already held, and does it whether or not the marker was current. recordCumulativeUsage reads that marker in its existing FOR SHARE subscription read. If the marker is at or past the end of the charge's target period, it throws CumulativeUsagePeriodClosedError. 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-cost maps that error to the existing non-retryable BILLING_PERIOD_ELAPSED (409) outcome, which the worker already treats as non-retryable. No migration. The marker is an existing column. Every other reader compares it with >= periodStart, so a marker at periodEnd still reads as "current". Rollovers are unaffected: the sweep sets the marker to the new periodStart, which is always earlier than the end of any period a charge can target. Behaviour changes Spend reported for a deleted subscription's terminal period after its final settlement now returns 409 BILLING_PERIOD_ELAPSED (retryable: false). Before, it was silently written to a row no invoice would read. Nothing changes for active subscriptions, rollovers, or callbacks that commit before the terminal claim. Not changed The "no usable current period bounds" half of the finding. A subscription row with a null periodStart never rolls, the sweep never closes it, and claimTerminalPeriod returns 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, then claimTerminalPeriod, then a later callback returns 409 BILLING_PERIOD_ELAPSED and leaves the original row's cost unchanged. Fails with the guard removed, and also fails with the claim reverted to periodStart. bun run test:integration (filtered to the billing usage-log and update-cost suites, on throwaway containers) Affected unit suites (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
Open Graph Description: 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&...
X Description: 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 s...
Opengraph URL: https://github.com/simstudioai/sim/pull/8505
X: @github
Domain: github.com
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:8ba5be10-fe65-a7e5-5156-ee77806d4bfb |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | B466:37056B:1A08B9:234037:6AFB9B74 |
| html-safe-nonce | bc8c95acbe70cf46f0862d0b939c73a5add3847206d3501826b56736d3105403 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNDY2OjM3MDU2QjoxQTA4Qjk6MjM0MDM3OjZBRkI5Qjc0IiwidmlzaXRvcl9pZCI6IjU2OTYzNjUyNTc2MTQ0MDAzNzIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | c357c877473e5b8c35dc0255a5fe432b6e8564b6e5a60bc83f1dcb68e5c3d4a7 |
| hovercard-subject-tag | pull_request:4698269400 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/simstudioai/sim/pull/8505/files |
| twitter:image | https://avatars.githubusercontent.com/u/40672544?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/40672544?s=400&v=4 |
| og:image:alt | 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&... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 3cefd19a936819d82cfb5279d50fb56fe79cb9ec30f0f807e04e89b0c58ffae1 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/simstudioai/sim git https://github.com/simstudioai/sim.git |
| octolytics-dimension-user_id | 199344406 |
| octolytics-dimension-user_login | simstudioai |
| octolytics-dimension-repository_id | 912559512 |
| octolytics-dimension-repository_nwo | simstudioai/sim |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 912559512 |
| octolytics-dimension-repository_network_root_nwo | simstudioai/sim |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 606800bdb1b81d125fc845bdcc262c9fe063fc8f |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width