Skip to content

feat(billing): gate gathering line invoice assignment - #5090

Merged
turip merged 2 commits into
mainfrom
feat/billing-invoice-assignment-gate
Sep 7, 2026
Merged

turip merged 2 commits into
mainfrom
feat/billing-invoice-assignment-gate

Conversation

@turip

@turip turip commented Sep 7, 2026 •

Copy link
Copy Markdown
Member

Overview

Add a line-engine gate that can exclude otherwise billable gathering lines from invoice assignment based on external state.

  • introduce a sparse GateInvoiceAssignment response where omitted line IDs are allowed by default
  • invoke gates after regular billability selection and before line limits, splitting, and materialization
  • treat an all-excluded candidate set as a successful empty result, preserving gate side effects and avoiding ErrInvoiceCreateNoLines worker warnings
  • keep existing line engines as no-op gates until their domain-specific implementations are added
  • cover validation, engine grouping, sparse responses, exclusion, line limits, fatal errors, and ErrInvoiceCreateNoLines behavior
  • document the line-engine lifecycle contract

Notes for reviewer

  • Usage-based external-state gating and charge validation-issue persistence are intentionally outside this PR.
  • TypeSpec and API changes are intentionally outside this PR.
  • ErrInvoiceCreateNoLines remains unchanged when there were no otherwise billable candidates.

Validation

  • go test ./openmeter/billing ./openmeter/billing/service ./openmeter/billing/charges/creditpurchase/service ./openmeter/billing/charges/flatfee/service ./openmeter/billing/charges/usagebased/service
  • POSTGRES_HOST=127.0.0.1 go test -tags=dynamic ./test/billing -run '^TestLineEngine$'
  • make lint-go-fast

Greptile Summary

This PR introduces a sparse, engine-specific gate between billability selection and invoice materialization.

  • Groups otherwise billable gathering lines by their owning line engine and validates each gate response.
  • Leaves omitted lines eligible while excluding explicitly blocked lines before line limits and progressive splitting.
  • Treats a fully blocked candidate set as a successful empty collection so gate-owned writes can commit.
  • Adds no-op implementations for existing engines, lifecycle documentation, and focused unit and integration coverage.

Confidence Score: 5/5

The PR appears safe to merge, with no outstanding correctness, security, or repository-rule violations identified.

The gate is applied at the intended collection boundary, validates engine responses, preserves sparse allow-by-default behavior, and distinguishes fully excluded candidates from having no billable candidates. The earlier testing-convention thread was manually resolved without explanation.

Important Files Changed

Filename Overview
openmeter/billing/lineengine.go Defines and validates the sparse invoice-assignment gate contract on the line-engine interface.
openmeter/billing/service/gatheringinvoicependinglines.go Invokes gates after billability selection, filters excluded lines, and handles fully excluded collections successfully.
openmeter/billing/service/gatheringinvoicependinglines_test.go Covers engine grouping, sparse responses, exclusions, ordering, and gate failures.
test/billing/lineengine_test.go Exercises collection behavior for fully blocked lines, line limits, no-candidate errors, and fatal gate failures.
openmeter/billing/README.md Documents gate ordering, persistence semantics, and rollback behavior.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart LR
    G[Gathering lines] --> B[Billability selection]
    B --> E[Group candidates by line engine]
    E --> A[Gate invoice assignment]
    A -->|Excluded| P[Remain pending]
    A -->|Allowed| L[Apply invoice line limit]
    L --> S[Split progressive lines]
    S --> M[Materialize standard invoice]
    A -->|All excluded| N[Successful empty result]
Loading

Reviews (3): Last reviewed commit: "test(billing): address gate review feedb..." | Re-trigger Greptile

Context used:

Summary by CodeRabbit

  • New Features

    • Added invoice-assignment gating for billable lines before invoice creation.
    • Line engines can exclude selected lines from invoices while preserving them on the gathering invoice.
    • Excluded lines do not count toward invoice line limits.
    • Collections complete without creating an invoice when all candidates are excluded.
    • Gate failures stop collection and roll back related writes.
  • Documentation

    • Documented invoice-assignment gating behavior and outcomes in the billing guide.
  • Tests

    • Added coverage for exclusions, validation, line limits, empty results, and gate errors.

@coderabbitai

coderabbitai Bot commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Team

Run ID: e2c677f0-a2f7-405a-96c2-9678783dd9df

📥 Commits

Reviewing files that changed from the base of the PR and between d19fcbe and 7f32069.

📒 Files selected for processing (11)
  • openmeter/billing/README.md
  • openmeter/billing/charges/creditpurchase/service/lineengine.go
  • openmeter/billing/charges/flatfee/service/lineengine.go
  • openmeter/billing/charges/usagebased/service/lineengine.go
  • openmeter/billing/lineengine.go
  • openmeter/billing/lineengine/stdinvoice.go
  • openmeter/billing/lineengine_test.go
  • openmeter/billing/service/gatheringinvoicependinglines.go
  • openmeter/billing/service/gatheringinvoicependinglines_test.go
  • openmeter/billing/testutils/lineengine.go
  • test/billing/lineengine_test.go

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


📝 Walkthrough

Walkthrough

Billing adds invoice-assignment gating for gathering lines. Line engines can exclude lines before invoice creation. The service validates gate responses, preserves line limits, handles all-excluded candidates, and rolls back collection on gate errors.

Changes

Invoice assignment gating

Layer / File(s) Summary
Gating contract and implementations
openmeter/billing/lineengine.go, openmeter/billing/lineengine/stdinvoice.go, openmeter/billing/charges/*/service/lineengine.go, openmeter/billing/testutils/lineengine.go, openmeter/billing/lineengine_test.go
The billing package adds gate input, response, result validation, and the LineEngine method. Built-in and test engines implement no-op gates. Validation tests cover missing lines, duplicate IDs, sparse responses, and unknown IDs.
Line gating orchestration
openmeter/billing/service/gatheringinvoicependinglines.go, openmeter/billing/service/gatheringinvoicependinglines_test.go
The service groups candidate lines by engine, invokes each gate, validates results, filters excluded lines, preserves order, counts exclusions, and handles all-excluded candidates.
Collection behavior and integration coverage
test/billing/lineengine_test.go, openmeter/billing/README.md
Integration tests cover blocked lines, line limits, no billable lines, gate errors, and invoice creation. The README documents gating and collection behavior.

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

Merge Risk: ⚪ Minimal · up to 7f320

The gating behavior and its tested collection semantics are ready to merge, with no identified material risk.

Sequence Diagram(s)

sequenceDiagram
  participant Collection
  participant prepareBillableLines
  participant LineEngine
  Collection->>prepareBillableLines: prepare pending gathering lines
  prepareBillableLines->>LineEngine: GateInvoiceAssignment(candidate lines)
  LineEngine-->>prepareBillableLines: exclusion decisions or error
  prepareBillableLines-->>Collection: filtered lines or collection failure
Loading

Suggested reviewers: chrisgacsal, borbelyr-kong, borosr

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: adding invoice-assignment gating for gathering lines in billing.
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.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/billing-invoice-assignment-gate

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 kind/feature New feature or request release-note/feature Release note: Exciting New Features area/billing labels Sep 7, 2026
@turip
turip marked this pull request as ready for review September 7, 2026 11:10
@turip
turip requested a review from a team as a code owner September 7, 2026 11:10
@turip
turip force-pushed the feat/billing-invoice-assignment-gate branch from 0e2f11b to 7f32069 Compare September 7, 2026 11:12
@turip
turip enabled auto-merge (squash) September 7, 2026 12:59
@turip
turip merged commit 6096efb into main Sep 7, 2026
47 of 48 checks passed
@turip
turip deleted the feat/billing-invoice-assignment-gate branch September 7, 2026 13:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/billing kind/feature New feature or request release-note/feature Release note: Exciting New Features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants