Skip to content

refactor(billing): separate ordinary and legacy rating - #5326

Merged
turip merged 3 commits into
mainfrom
feat/separate-legacy-rating-contract
Oct 21, 2026
Merged

turip merged 3 commits into
mainfrom
feat/separate-legacy-rating-contract

Conversation

@turip

@turip turip commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

Overview

Ordinary charge rating currently has to implement legacy progressive-billing accessors. Split the contract into StandardLineAccessor / GenerateDetailedLines for charges and LegacyProgressivelyBillableLineAccessor / GenerateLegacyProgressiveBilledLines for legacy invoice lines, while retaining one pricing pipeline.

Remove the unused credit mutator and credit inputs/outputs from rating. Charge engines continue allocating credits and applying them to detailed lines after rating.

Related to #4750; this implements the rating-contract separation only. Hierarchy representation, persistence, and loading remain unchanged.

Notes for reviewer

  • Preserve pricing, discounts, commitments, unit conversion, currency rounding, repeated-rating identities, and validation issues returned alongside usable results.
  • Retain missing-hierarchy errors and the existing zero-history behavior of header-only hierarchies.
  • Retain main’s new invoice sanity checks in the rating fixtures.
  • Regression tests cover progressive prior-billed commitments, deleted history, detail/discount identity, clone isolation, warning/error behavior, and amounts before credit application.

Validation:

  • Rating, legacy line engine, and usage-based/flat-fee charge tests passed in the root test run.
  • make test-go-sdk passed.
  • make lint-go-fast scoped to the affected packages passed with zero issues.
  • Full make test GO_TEST_PACKAGE_PARALLELISM=8 reached the default 10-minute timeout in tools/migrate; CI will verify the full suite. The notification integration test passed after starting its local Svix dependency.

The billing domain README documents the revised rating and credit-application boundary.

Summary by CodeRabbit

  • Billing
    • Improved support for progressive billing commitments, including prior-period usage and previously billed amounts.
    • Standard rating now uses independent usage snapshots and does not rely on legacy split-line history.
    • Rating results are calculated before credit application, with credit allocations handled separately for detailed charge lines.
    • Negative usage quantities are safely clamped to zero with validation warnings.

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; no actionable issues were found.

Summary

Separates ordinary charge rating from legacy progressive rating while keeping one pricing pipeline.

  • Ordinary charges and legacy invoice lines use separate rating inputs.
  • Rating returns gross detailed lines and leaves credit application to charge flows.

Intentional change: turip explicitly describes removing rating-owned credit inputs, outputs, and the unused mutator. Hierarchy storage and loading remain out of scope.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  C[Charge snapshots] --> O[GenerateDetailedLines]
  O --> A[Adapter supplies zero history]
  L[Legacy invoice snapshots] --> G[GenerateLegacyProgressiveBilledLines]
  A --> P[Shared pricing pipeline]
  G --> P
  P --> R[Details and totals before credits]
  R --> E[Charge services apply credits]
  R --> I[Legacy engine merges rated details]
Loading

Reviews (1) · Last reviewed commit: "test(billing): adapt rating sanity fixtu..." · Reviewed by Greptile

@turip
turip requested a review from a team as a code owner October 21, 2026 12:44
@coderabbitai

coderabbitai Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

📝 Walkthrough

The rating API now separates ordinary line snapshots from legacy progressive-billing inputs. Rating returns amounts before credit application. The line engine uses the legacy entry point, and tests cover progressive commitment history.

Changes: Rating and credit allocation

Layer / File(s) Summary
Separate ordinary and legacy rating inputs
openmeter/billing/rating/line.go, openmeter/billing/rating/service.go, openmeter/billing/rating/service/..., openmeter/billing/rating/clampedstandardline.go
The rating API adds a legacy progressive-billing accessor and generation method. Ordinary rating adapts standard accessors to that path, while validation and quantity clamping use the legacy accessor.
Return amounts before credit allocation
openmeter/billing/rating/detailedline.go, openmeter/billing/rating/service/..., openmeter/billing/stdinvoiceline.go, openmeter/billing/charges/usagebased/detailedline.go, openmeter/billing/lineengine/rating.go, openmeter/billing/README.md
Detailed-line totals no longer subtract applied credits. The credits mutator, its disable option, and credit fields or accessors in the affected rating paths are removed. The README describes credit allocation outside legacy rating.
Use legacy inputs for progressive line rating
openmeter/billing/lineengine/..., openmeter/billing/rating/service/testutil/ubptest.go
The line engine calls the legacy rating entry point. Tests cover progressive commitments, prior billing history, input preservation, and ordinary or header-only lines.
Update charge rating integrations
openmeter/billing/charges/flatfee/..., openmeter/billing/charges/usagebased/...
Flat-fee and usage-based rating integrations stop passing the credit-mutator override. Their rating accessors, service calls, and test stubs use the updated rating contract.

Priority: ➖ Normal

Estimated code review effort: 4 (Complex) | ~45 minutes

Change: Refactor

Sequence Diagram(s)

sequenceDiagram
  participant RateStandardLines
  participant RatingService
  participant ClampedLegacyLineAccessor
  participant Pricer
  RateStandardLines->>RatingService: GenerateLegacyProgressiveBilledLines(accessor)
  RatingService->>ClampedLegacyLineAccessor: validate and clamp quantities
  RatingService->>Pricer: calculate with legacy line accessor
  Pricer-->>RatingService: detailed amounts
  RatingService-->>RateStandardLines: generated detailed lines
Loading

Suggested reviewers: galexihu, rolosp

Merge Risk: ⚪ Minimal · up to c28e8

The rating and charge-allocation boundary shows no actionable merge-blocking issue; the change is ready for 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: rating is split into an ordinary path and a legacy path. The raw summary confirms this. Separate legacy progressive accessors and a new GenerateLegacyProgressiveBilled…
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: rating is split into an ordinary path and a legacy path. The raw summary confirms this. Separate legacy progressive accessors and a new GenerateLegacyProgressiveBilledLines entry point are added, and credit mutation is removed from rating. The title is short, specific, and follows the conventional commit style. It does not mention the credit-removal part, but it does not need to cover every detail.

✨ Finishing Touches: 💡 1
🛠️ Fix failing CI checks: 💡
  • Commit to this branch
  • Create a new PR
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • 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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔇 Additional comments: 25
openmeter/billing/charges/flatfee/service/lineengine_test.go: 1

228-230: LGTM!

openmeter/billing/charges/flatfee/service/realizations/rating.go: 1

21-21: LGTM!

openmeter/billing/charges/usagebased/rating.go: 1

19-20: LGTM!

openmeter/billing/charges/usagebased/service/lineengine_test.go: 1

601-603: LGTM!

openmeter/billing/charges/usagebased/service/rating/delta/engine.go: 1

79-79: LGTM!

openmeter/billing/charges/usagebased/service/rating/delta/engine_test.go: 1

404-407: LGTM!

openmeter/billing/charges/usagebased/service/rating/periodpreserving/engine.go: 1

218-218: LGTM!

openmeter/billing/charges/usagebased/service/rating/service_test.go: 1

584-584: LGTM!

Also applies to: 606-609

openmeter/billing/charges/usagebased/service/rating/totals.go: 1

69-69: LGTM!

openmeter/billing/lineengine/engine_test.go: 1

515-518: LGTM!

openmeter/billing/lineengine/rating_test.go: 2

21-21: LGTM!

Also applies to: 124-124, 286-292, 487-487, 517-517, 550-550, 587-591


339-477: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

⚠️ Unverified finding
Verification ran but could not confirm this finding. It is shown for review, not as a verified issue.

Nice coverage. One small readability tweak is worth a look.

The table pins good contracts: prior billing, deleted history, identity survival, and input immutability. The expected values (40, 60, 80, 100, 10, 20) are bare numbers, though. A short comment per case would show how each one is derived. For example, the minimum-spend case is 20 usage plus a 40 prior charge, which gives a 60 total against the 100 minimum.

Also, expectedCharges, ordinaryCharges, and the totals mix several scenarios in one table. This is fine today. If more cases arrive, consider splitting the ordinary and header-only checks into their own subtests.

Based on learnings, expected values in table-driven tests should be computed or explained, not bare magic numbers.

Source: Learnings

openmeter/billing/rating/line.go: 1

44-57: LGTM!

openmeter/billing/rating/service.go: 1

19-19: LGTM!

openmeter/billing/rating/service/standardline.go: 1

1-33: LGTM!

openmeter/billing/rating/clampedstandardline.go: 1

14-98: LGTM!

openmeter/billing/rating/service/detailedline.go: 1

18-24: LGTM!

Also applies to: 47-117

openmeter/billing/rating/service/detailedline_test.go: 1

18-266: LGTM!

openmeter/billing/rating/clampedstandardline_test.go: 1

13-164: LGTM!

openmeter/billing/rating/service/mutator/forbidunitconfig_test.go: 1

20-20: LGTM!

openmeter/billing/rating/service/mutator/unitconfig_test.go: 1

51-51: LGTM!

Also applies to: 131-131, 151-151, 170-170

openmeter/billing/README.md: 1

131-138: LGTM!

openmeter/billing/lineengine/rating.go: 1

92-92: LGTM!

openmeter/billing/rating/service/pricer.go: 1

29-33: LGTM!

openmeter/billing/rating/service/testutil/ubptest.go: 1

60-74: LGTM!

Also applies to: 117-128, 182-182, 197-208


ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository: openmeterio/openmeter/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: c4226730-12c1-48ed-bfb1-504ec8e60593
📥 Commits

Reviewing files that changed from the base of the PR and between 1fdb9c2 and c28e8e3.

📒 Files selected for processing: 32
  • openmeter/billing/README.md
  • openmeter/billing/charges/flatfee/rating.go
  • openmeter/billing/charges/flatfee/service/lineengine_test.go
  • openmeter/billing/charges/flatfee/service/realizations/rating.go
  • openmeter/billing/charges/usagebased/detailedline.go
  • openmeter/billing/charges/usagebased/rating.go
  • openmeter/billing/charges/usagebased/service/lineengine_test.go
  • openmeter/billing/charges/usagebased/service/rating/delta/engine.go
  • openmeter/billing/charges/usagebased/service/rating/delta/engine_test.go
  • openmeter/billing/charges/usagebased/service/rating/periodpreserving/engine.go
  • openmeter/billing/charges/usagebased/service/rating/service_test.go
  • openmeter/billing/charges/usagebased/service/rating/totals.go
  • openmeter/billing/lineengine/engine_test.go
  • openmeter/billing/lineengine/rating.go
  • openmeter/billing/lineengine/rating_test.go
  • openmeter/billing/rating/clampedstandardline.go
  • openmeter/billing/rating/clampedstandardline_test.go
  • openmeter/billing/rating/detailedline.go
  • openmeter/billing/rating/line.go
  • openmeter/billing/rating/service.go
  • openmeter/billing/rating/service/detailedline.go
  • openmeter/billing/rating/service/detailedline_test.go
  • openmeter/billing/rating/service/mutator/credits.go
  • openmeter/billing/rating/service/mutator/credits_test.go
  • openmeter/billing/rating/service/mutator/forbidunitconfig_test.go
  • openmeter/billing/rating/service/mutator/unitconfig_test.go
  • openmeter/billing/rating/service/options_test.go
  • openmeter/billing/rating/service/pricer.go
  • openmeter/billing/rating/service/rate/types.go
  • openmeter/billing/rating/service/standardline.go
  • openmeter/billing/rating/service/testutil/ubptest.go
  • openmeter/billing/stdinvoiceline.go
💤 Files with no reviewable changes: 7
  • openmeter/billing/rating/service/mutator/credits_test.go
  • openmeter/billing/charges/usagebased/detailedline.go
  • openmeter/billing/rating/service/options_test.go
  • openmeter/billing/rating/service/mutator/credits.go
  • openmeter/billing/stdinvoiceline.go
  • openmeter/billing/rating/detailedline.go
  • openmeter/billing/charges/flatfee/rating.go

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

@turip turip added release-note/misc Miscellaneous changes area/billing kind/refactor Code refactor, cleanup or minor improvement labels Oct 21, 2026
@turip
turip merged commit 4309400 into main Oct 21, 2026
@turip
turip deleted the feat/separate-legacy-rating-contract branch October 21, 2026 13:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/billing kind/refactor Code refactor, cleanup or minor improvement release-note/misc Miscellaneous changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants