Skip to content

fix(billing): bill the exact package count just under a boundary - #5372

Merged
turip merged 2 commits into
openmeterio:mainfrom
arian-gogani:fix/package-count-exact-division
Oct 27, 2026
Merged

turip merged 2 commits into
openmeterio:mainfrom
arian-gogani:fix/package-count-exact-division

Conversation

@arian-gogani

@arian-gogani arian-gogani commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Package.GetNumberOfPackages reads one quantity through two accessors that disagree.

requiredPackages := qty.Div(packageSize).Floor()
if qty.Mod(packageSize).IsZero() {
    return requiredPackages
}
return requiredPackages.Add(DecimalOne)

Div is not exact. In alpacadecimal the fast path checks that the division came out exact and otherwise falls through to DivRound at DivisionPrecision (16), which rounds. Mod has no fast path and is always exact. When the true quotient sits just below a whole number, Div rounds up to it, Floor keeps it, and Mod still reports a remainder, so the function adds a package that is not owed.

MeterQueryRow.Value is a float64, and both quantity snapshots accumulate sum.Add(alpacadecimal.NewFromFloat(row.Value)). Two ordinary values, 9.9 and 0.09999999999999999, sum to 9.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.

TestPackagePriceBelowOnePackageBillsOnePackage prices 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. TestGetNumberOfPackagesExactBoundary pins 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). packageSize is 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

  • Bug Fixes
    • Improved package-based billing for fractional usage near package boundaries. Values just below a package multiple now consistently count toward the next package, preventing underbilling caused by decimal precision.
    • Zero usage, exact package multiples, and values above a package boundary continue to produce the expected package counts.
    • For example, usage just below 10 units with a package size of 10 is billed as one package.

RetriggerView in GreptileConfidence 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.

  • Package billing counts usage just below a boundary without adding an extra package.

Reviews (2) · Last reviewed commit: "fix(billing): document division rounding..." · Reviewed by Greptile

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.
@arian-gogani
arian-gogani requested a review from a team as a code owner October 27, 2026 05:02
@coderabbitai

coderabbitai Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 9c9d153 and f3ef63e.

Files selected for processing: 3
  • AGENTS.md
  • openmeter/billing/rating/service/rate/package.go
  • openmeter/billing/rating/service/rate/package_test.go
Files skipped from review as they are similar to previous changes: 2
  • openmeter/billing/rating/service/rate/package_test.go
  • openmeter/billing/rating/service/rate/package.go

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

Walkthrough

GetNumberOfPackages now uses QuoRem to calculate the quotient and remainder. New tests cover high-precision quantities near package boundaries and a rating-service case.

Changes: Package count calculation

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: Normal

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: Passed 4
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 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 …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@turip turip added the kind/bug Something isn't working label Oct 27, 2026

@turip turip left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@turip
turip merged commit 8239676 into openmeterio:main Oct 27, 2026
27 of 29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants