Repository navigation
fix(billing): bill the exact package count just under a boundary - #5372
Conversation
GetNumberOfPackages read the quantity through Div().Floor() and through Mod. Div rounds, Mod does not, so a quantity just under a whole package billed one package too many. QuoRem returns both from one exact division.
|
No actionable comments were generated in the recent review.
|
| Layer / File(s) | Summary |
|---|---|
Package rounding and validation openmeter/billing/rating/service/rate/package.go, openmeter/billing/rating/service/rate/package_test.go, AGENTS.md |
GetNumberOfPackages derives the quotient and remainder from one QuoRem call with precision zero. Tests cover high-precision quantities near package boundaries and rating-service output. AGENTS.md describes the rounding behavior of Div and recommends QuoRem for exact quotient and remainder calculations. |
Priority:
Estimated code review effort: 2 (Simple) | ~10 minutes
Change: Bug fix
Suggested labels: kind/bug, release-note/bug-fix
Suggested reviewers: turip
Merge Risk: 
Minimal · up to f3ef6
-
The package-rounding change is ready to merge, subject to normal checks.
Pre-merge checks: 
4
-
Full details: Title check
-
Explanation
The title names the main change: it fixes billing so a quantity just under a package boundary is billed as the correct package count. The raw summary shows the GetNumberOfPackages change now uses one QuoRem call, and the PR objectives confirm the boundary rounding bug is the target. The title is specific and matches the changeset, with a conventional commit prefix. It does not mention the docs update in AGENTS.md, which is expected for a title.
-
Finishing Touches
-
🧪 Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands.
turip
left a comment
There was a problem hiding this comment.
Thanks @arian-gogani for tracking this down and adding regression coverage! Using QuoRem keeps the quotient and remainder consistent and fixes the package-boundary overcount.
Package.GetNumberOfPackagesreads one quantity through two accessors that disagree.Divis not exact. In alpacadecimal the fast path checks that the division came out exact and otherwise falls through toDivRoundatDivisionPrecision(16), which rounds.Modhas no fast path and is always exact. When the true quotient sits just below a whole number,Divrounds up to it,Floorkeeps it, andModstill reports a remainder, so the function adds a package that is not owed.MeterQueryRow.Valueis afloat64, and both quantity snapshots accumulatesum.Add(alpacadecimal.NewFromFloat(row.Value)). Two ordinary values,9.9and0.09999999999999999, sum to9.99999999999999999.That quantity only shows up from an accumulated sum. I ran 520,000 single-float64 quantities across 13 package sizes and found no disagreement between the shipped count and an exact ceiling. A reviewer who tries one usage value will not hit this.
TestPackagePriceBelowOnePackageBillsOnePackageprices that quantity through the real rating service. Before this change, 9.99999999999999999 units of a 10-unit, $15 package invoice as quantity 2, amount 30, total 30. One package, $15, is owed.TestGetNumberOfPackagesExactBoundarypins the same boundary on the function. Both fail before the change and pass after. The existing package price tests still pass.The fix is one
qty.QuoRem(packageSize, 0).packageSizeis already validated non-zero and non-negative, and the quantity is clamped non-negative, so the truncating quotient is the floor.I did not ingest the two events through the HTTP API or ClickHouse. The quantity in the test is the decimal that production accumulation produces from those two float64 values. The rating of that quantity is executed. The path from ingest to the meter row is traced by symbol, not executed.
Made with Cursor
Summary by CodeRabbit


Confidence Score: 5/5
The PR appears safe to merge; no actionable issues were found.Summary
Replaces rounded division with
QuoRem(packageSize, 0)so usage just below a package boundary does not incur an extra package.Reviews (2) · Last reviewed commit: "fix(billing): document division rounding..." · Reviewed by Greptile