Repository navigation
fix: preserve invoice backend for zero-amount artifacts - #4863
Conversation
📝 WalkthroughWalkthroughThe reconciliation planner now passes persisted items to scope filtering. Existing targets use their persisted backend collection, while new targets resolve the default collection. An integration test covers zero-proration invoice-line retirement during migration to charge-backed provisioning. ChangesSubscription reconciliation
Estimated code review effort: 3 (Moderate) | ~20 minutes Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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 |
4c8fb94 to
b1985e6
Compare
There was a problem hiding this comment.
🧹 Nitpick comments (1)
openmeter/billing/worker/subscriptionsync/service/sync_credittheninvoice_test.go (1)
2479-2489: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winSmall robustness nit: use
s.Require()where a failure cascades into a panic. Line 2489 usess.NoError(err), which only marks the test failed. Execution continues to line 2530 and calls a method oninvoiceBackedService. The same applies at line 2562: ifEditRunningreturns an error,editedSubViewis a zero value, andeditedSubView.Phases[0]at line 2564 panics with an index-out-of-range instead of reporting the real cause.🧰 Suggested tightening
- s.NoError(err) + s.Require().NoError(err) subView := s.createSubscriptionFromPlan(plan.CreatePlanInput{}, s.timingImmediate()) - s.NoError(err) + s.Require().NoError(err) usersVersions := editedSubView.Phases[0].ItemsByKey["users"]Also applies to: 2547-2567
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@openmeter/billing/worker/subscriptionsync/service/sync_credittheninvoice_test.go` around lines 2479 - 2489, Replace the non-fatal assertions in this test with required assertions wherever execution depends on success: use s.Require() for the New(...) error before calling methods on invoiceBackedService, and for the EditRunning result before indexing editedSubView.Phases. Preserve the existing test flow while ensuring failures stop before dependent operations can panic.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In
`@openmeter/billing/worker/subscriptionsync/service/sync_credittheninvoice_test.go`:
- Around line 2479-2489: Replace the non-fatal assertions in this test with
required assertions wherever execution depends on success: use s.Require() for
the New(...) error before calling methods on invoiceBackedService, and for the
EditRunning result before indexing editedSubView.Phases. Preserve the existing
test flow while ensuring failures stop before dependent operations can panic.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: 01c8dac5-d38d-4644-bc05-81c123a85a57
📒 Files selected for processing (2)
openmeter/billing/worker/subscriptionsync/service/reconciler/reconciler.goopenmeter/billing/worker/subscriptionsync/service/sync_credittheninvoice_test.go
b1985e6 to
fb76334
Compare
|
Note GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer. |
|
Addressed the CodeRabbit robustness feedback in fb76334: service construction and subscription edit failures now use fatal assertions before dependent calls/indexing. The focused subscription-sync regression and reconciler tests pass after rebasing onto the latest origin/main. |
What changed
Subscription reconciliation now determines zero-amount filtering from the backend that already owns the persisted artifact. A target backed by a legacy invoice line must still pass invoice-line realization; when proration rounds that target to zero, it behaves as absent and the legacy artifact is retired instead of being implicitly migrated to a charge.
Default charge routing remains unchanged for genuinely new subscription-sync identities.
Root cause
filterInScopeLinesresolved every target through the current default backend before diff planning. If charge provisioning became available after an invoice-backed artifact had already been created, the target was classified as charge-backed and bypassed invoice-line realization. A prorated amount that rounded to zero could therefore survive filtering, causing reconciliation to delete the legacy line and create a zero-amount charge for the same identity.Regression coverage
The integration test reproduces the transition with the production billing, subscription, charge, ledger, and PostgreSQL fixture stack:
Impact
This preserves stable backend ownership for existing subscription-sync artifacts and prevents an implicit invoice-to-charge migration when a legacy artifact recalculates to zero.
Validation
make test-nocache: 6791 tests passed, 9 skippedgit diff --checkNo Jira ticket: maintenance/reliability fix.
Summary by CodeRabbit
Greptile Summary
Subscription reconciliation now selects the backend already associated with a persisted artifact while retaining default routing for new identities.
Confidence Score: 5/5
The PR appears safe to merge.
No blocking failure remains.
Important Files Changed
Flowchart
%%{init: {'theme': 'neutral'}}%% flowchart TD T[Subscription target] --> P{Persisted artifact exists?} P -->|Yes| E[Use persisted backend] P -->|No| D[Resolve default backend] E --> B{Backend type} D --> B B -->|Invoice| R[Realize expected invoice line] B -->|Charge| C[Keep billable target] R -->|Zero or absent| X[Treat target as absent and retire legacy line] R -->|Present| I[Reconcile invoice line] C --> Q[Reconcile charge]Reviews (4): Last reviewed commit: "test: require dependent subscription syn..." | Re-trigger Greptile
Context used: