René's URL Explorer Experiment


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

direct link

Domain: github.com

react-profiling0
react-import-mapreact,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-controllerpull_requests
route-actionfiles
fetch-noncev2:8ba5be10-fe65-a7e5-5156-ee77806d4bfb
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-idB466:37056B:1A08B9:234037:6AFB9B74
html-safe-noncebc8c95acbe70cf46f0862d0b939c73a5add3847206d3501826b56736d3105403
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNDY2OjM3MDU2QjoxQTA4Qjk6MjM0MDM3OjZBRkI5Qjc0IiwidmlzaXRvcl9pZCI6IjU2OTYzNjUyNTc2MTQ0MDAzNzIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmacc357c877473e5b8c35dc0255a5fe432b6e8564b6e5a60bc83f1dcb68e5c3d4a7
hovercard-subject-tagpull_request:4698269400
github-keyboard-shortcutsrepository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///pull_requests/show/files
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/simstudioai/sim/pull/8505/files
twitter:imagehttps://avatars.githubusercontent.com/u/40672544?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/40672544?s=400&v=4
og:image:altSummary 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_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
None3cefd19a936819d82cfb5279d50fb56fe79cb9ec30f0f807e04e89b0c58ffae1
turbo-cache-controlno-preview
diff-viewunified
go-importgithub.com/simstudioai/sim git https://github.com/simstudioai/sim.git
octolytics-dimension-user_id199344406
octolytics-dimension-user_loginsimstudioai
octolytics-dimension-repository_id912559512
octolytics-dimension-repository_nwosimstudioai/sim
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id912559512
octolytics-dimension-repository_network_root_nwosimstudioai/sim
turbo-body-classeslogged-out env-production page-responsive full-width
disable-turbotrue
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release606800bdb1b81d125fc845bdcc262c9fe063fc8f
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/simstudioai/sim/pull/8505/files#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fsimstudioai%2Fsim%2Fpull%2F8505%2Ffiles
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fsimstudioai%2Fsim%2Fpull%2F8505%2Ffiles
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fpull_requests%2Fshow%2Ffiles&source=header-repo&source_repo=simstudioai%2Fsim
Reloadhttps://github.com/simstudioai/sim/pull/8505/files
Reloadhttps://github.com/simstudioai/sim/pull/8505/files
Reloadhttps://github.com/simstudioai/sim/pull/8505/files
simstudioaihttps://github.com/simstudioai
simhttps://github.com/simstudioai/sim
Codehttps://github.com/simstudioai/sim
Issues129 (129)https://github.com/simstudioai/sim/issues
Pull requests199 (199)https://github.com/simstudioai/sim/pulls
Actionshttps://github.com/simstudioai/sim/actions
Projectshttps://github.com/simstudioai/sim/projects
Security and qualityhttps://github.com/simstudioai/sim/security
Insightshttps://github.com/simstudioai/sim/pulse
Sign up for GitHub https://github.com/signup?return_to=%2Fsimstudioai%2Fsim%2Fissues%2Fnew%2Fchoose
terms of servicehttps://docs.github.com/terms
privacy statementhttps://docs.github.com/privacy
Sign inhttps://github.com/login?return_to=%2Fsimstudioai%2Fsim%2Fissues%2Fnew%2Fchoose
waleedlatif1https://github.com/waleedlatif1
staginghttps://github.com/simstudioai/sim/tree/staging
fix/billing-no-topup-after-terminal-settlementhttps://github.com/simstudioai/sim/tree/fix/billing-no-topup-after-terminal-settlement
Conversation 12 https://github.com/simstudioai/sim/pull/8505
Commits 2 https://github.com/simstudioai/sim/pull/8505/commits
Checks 31 https://github.com/simstudioai/sim/pull/8505/checks
Files changed https://github.com/simstudioai/sim/pull/8505/files
Please reload this pagehttps://github.com/simstudioai/sim/pull/8505/files
fix(billing): refuse charges into a period the terminal settlement already invoiced https://github.com/simstudioai/sim/pull/8505/files#top
Show all changes 2 commits https://github.com/simstudioai/sim/pull/8505/files
577323f fix(billing): refuse charges into a period the terminal settlement al… waleedlatif1 Oct 1, 2026 https://github.com/simstudioai/sim/pull/8505/commits/577323f61383b9fa813c366a971b612b6cfd4efb
37a2eea test(billing): cover a terminal claim overlapping an in-flight charge waleedlatif1 Oct 1, 2026 https://github.com/simstudioai/sim/pull/8505/commits/37a2eeabc536cd1f3185cca7b51ad853343489a1
Clear filters https://github.com/simstudioai/sim/pull/8505/files
Please reload this pagehttps://github.com/simstudioai/sim/pull/8505/files
Please reload this pagehttps://github.com/simstudioai/sim/pull/8505/files
route.integration.ts https://github.com/simstudioai/sim/pull/8505/files#diff-4f0c196bfeb7b8fe42baaf013f4a8327fb66616fefc383aef30803f913b5d898
route.ts https://github.com/simstudioai/sim/pull/8505/files#diff-f3ec9e3eecbe194c2377f2e873f10be432af241ae9be88b693e9fec6699f335b
usage-log.integration.ts https://github.com/simstudioai/sim/pull/8505/files#diff-d2e514f1318ddfc455458154faa0cc2a1a07d587a0568b5adf6e5a1aef4f328f
usage-log.ts https://github.com/simstudioai/sim/pull/8505/files#diff-61e17bbb06140b8753417feb8b60df7a9170f3216b76d314f14d5210cb9e8431
cycle-close.ts https://github.com/simstudioai/sim/pull/8505/files#diff-1573d069352d91221b13ba135abb1eb1cb348fe2c573cd534338339f452ed27b
subscription.ts https://github.com/simstudioai/sim/pull/8505/files#diff-0914e886f10da17ee71ab4583530b28155ab3040cbd9a440c1949adf9e62bd9e
schema.ts https://github.com/simstudioai/sim/pull/8505/files#diff-9a959e2ce3cb4c86957bbae021d06eae364f7b0cb18964f5873bab53220af928
billing-usage-log.mock.ts https://github.com/simstudioai/sim/pull/8505/files#diff-c21fb49f8a439b03ac7bf8fa55a203d3c1ea76a0f7d00d5f062b7a9e1378a6d9
index.ts https://github.com/simstudioai/sim/pull/8505/files#diff-38963b775b9a08da9c40dbdf25380b1bd038aa3f9f93e9c657ef549fb39f512c
apps/sim/app/api/billing/update-cost/route.integration.tshttps://github.com/simstudioai/sim/pull/8505/files#diff-4f0c196bfeb7b8fe42baaf013f4a8327fb66616fefc383aef30803f913b5d898
View file https://github.com/simstudioai/sim/blob/37a2eeabc536cd1f3185cca7b51ad853343489a1/apps/sim/app/api/billing/update-cost/route.integration.ts
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/simstudioai/sim/pull/8505/{{ revealButtonHref }}
https://github.com/simstudioai/sim/pull/8505/files#diff-4f0c196bfeb7b8fe42baaf013f4a8327fb66616fefc383aef30803f913b5d898
https://github.com/simstudioai/sim/pull/8505/files#diff-4f0c196bfeb7b8fe42baaf013f4a8327fb66616fefc383aef30803f913b5d898
https://github.com/simstudioai/sim/pull/8505/files#diff-4f0c196bfeb7b8fe42baaf013f4a8327fb66616fefc383aef30803f913b5d898
Please reload this pagehttps://github.com/simstudioai/sim/pull/8505/files
apps/sim/app/api/billing/update-cost/route.tshttps://github.com/simstudioai/sim/pull/8505/files#diff-f3ec9e3eecbe194c2377f2e873f10be432af241ae9be88b693e9fec6699f335b
View file https://github.com/simstudioai/sim/blob/37a2eeabc536cd1f3185cca7b51ad853343489a1/apps/sim/app/api/billing/update-cost/route.ts
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/simstudioai/sim/pull/8505/{{ revealButtonHref }}
https://github.com/simstudioai/sim/pull/8505/files#diff-f3ec9e3eecbe194c2377f2e873f10be432af241ae9be88b693e9fec6699f335b
https://github.com/simstudioai/sim/pull/8505/files#diff-f3ec9e3eecbe194c2377f2e873f10be432af241ae9be88b693e9fec6699f335b
https://github.com/simstudioai/sim/pull/8505/files#diff-f3ec9e3eecbe194c2377f2e873f10be432af241ae9be88b693e9fec6699f335b
https://github.com/simstudioai/sim/pull/8505/files#diff-f3ec9e3eecbe194c2377f2e873f10be432af241ae9be88b693e9fec6699f335b
apps/sim/lib/billing/core/usage-log.integration.tshttps://github.com/simstudioai/sim/pull/8505/files#diff-d2e514f1318ddfc455458154faa0cc2a1a07d587a0568b5adf6e5a1aef4f328f
View file https://github.com/simstudioai/sim/blob/37a2eeabc536cd1f3185cca7b51ad853343489a1/apps/sim/lib/billing/core/usage-log.integration.ts
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/simstudioai/sim/pull/8505/{{ revealButtonHref }}
https://github.com/simstudioai/sim/pull/8505/files#diff-d2e514f1318ddfc455458154faa0cc2a1a07d587a0568b5adf6e5a1aef4f328f
https://github.com/simstudioai/sim/pull/8505/files#diff-d2e514f1318ddfc455458154faa0cc2a1a07d587a0568b5adf6e5a1aef4f328f
https://github.com/simstudioai/sim/pull/8505/files#diff-d2e514f1318ddfc455458154faa0cc2a1a07d587a0568b5adf6e5a1aef4f328f
https://github.com/simstudioai/sim/pull/8505/files#diff-d2e514f1318ddfc455458154faa0cc2a1a07d587a0568b5adf6e5a1aef4f328f
https://github.com/simstudioai/sim/pull/8505/files#diff-d2e514f1318ddfc455458154faa0cc2a1a07d587a0568b5adf6e5a1aef4f328f
https://github.com/simstudioai/sim/pull/8505/files#diff-d2e514f1318ddfc455458154faa0cc2a1a07d587a0568b5adf6e5a1aef4f328f
https://github.com/simstudioai/sim/pull/8505/files#diff-d2e514f1318ddfc455458154faa0cc2a1a07d587a0568b5adf6e5a1aef4f328f
https://github.com/simstudioai/sim/pull/8505/files#diff-d2e514f1318ddfc455458154faa0cc2a1a07d587a0568b5adf6e5a1aef4f328f
https://github.com/simstudioai/sim/pull/8505/files#diff-d2e514f1318ddfc455458154faa0cc2a1a07d587a0568b5adf6e5a1aef4f328f
Please reload this pagehttps://github.com/simstudioai/sim/pull/8505/files
Please reload this pagehttps://github.com/simstudioai/sim/pull/8505/files
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.