Skip to content

fix: validate charge patches by service period - #4868

Merged
turip merged 2 commits into
mainfrom
agent/reproduce-shrink-extend-validation
Aug 6, 2026
Merged

turip merged 2 commits into
mainfrom
agent/reproduce-shrink-extend-validation

Conversation

@turip

@turip turip commented Aug 6, 2026 •

Copy link
Copy Markdown
Member

What changed

  • Make shrink and extend validation derive direction exclusively from ServicePeriod.To.
  • Allow full-service and billing-period ends to reconcile independently while preserving their lower-bound validity.
  • Add unit coverage for mixed period directions and invalid period boundaries.
  • Add a table-driven PostgreSQL subscription-sync regression covering flat-fee and usage-based charges with both credits_only and credit_then_invoice settlement modes.
  • Document the charge patch direction contract in the billing domain documentation.

Why

Subscription sync selects shrink or extend from the service-period change. Other charge timing fields are target metadata and can legitimately move in the opposite direction.

In particular, canceling an annual subscription with a one-time in-advance flat fee extends the charge's initially zero-length service and full-service periods to the cancellation boundary while shortening its annual billing period to that same boundary. The previous extend validator rejected the shorter billing period, causing the entire synchronization transaction to roll back.

Usage-based cancellation follows the complementary shrink path, where full-service metadata can remain later than the shortened service period. Requiring every period to move in the patch direction was therefore stricter than the charge lifecycle semantics.

Impact

Subscription cancellation can now reconcile mixed charge-period directions without replacing the charge or rolling back synchronization. Retrying the synchronization remains idempotent.

Validation

  • nix develop --impure .#ci -c make lint-go-fast — passes with 0 issues
  • nix develop --impure .#ci -c make test-nocache — passes: 6,800 tests, 9 skipped

Greptile Summary

The PR changes charge-period patch validation so service direction determines whether a patch is an extend or shrink while full-service and billing endpoints reconcile independently.

  • Relaxes extend and shrink validation for mixed period-direction updates.
  • Adds unit coverage for independently moving period endpoints.
  • Adds a PostgreSQL-backed cancellation regression test across flat-fee and usage-based settlement modes.
  • Documents the revised period-reconciliation semantics.

Confidence Score: 5/5

The PR appears safe to merge because no blocking failure remains.

No blocking failure remains.

Important Files Changed

Filename Overview
openmeter/billing/charges/meta/patchextend.go Allows full-service and billing endpoints to shorten during a service-period extension while preserving period-start bounds.
openmeter/billing/charges/meta/patchshrink.go Allows full-service and billing endpoints to extend during a service-period shrink while preserving valid non-empty periods.
openmeter/billing/worker/subscriptionsync/service/sync_regression_test.go Covers cancellation reconciliation and retry idempotency for flat-fee and usage-based charges under both settlement modes.
openmeter/billing/charges/README.md Documents service-period direction as the patch discriminator and independent reconciliation of other timing fields.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  Subscription[Canceled subscription] --> Sync[Subscription synchronization]
  Sync --> Direction{Service-period direction}
  Direction -->|Later end| Extend[Extend patch]
  Direction -->|Earlier end| Shrink[Shrink patch]
  Extend --> Reconcile[Reconcile service, full-service, billing, and invoice timing independently]
  Shrink --> Reconcile
  Reconcile --> Charge[Updated existing charge]
Loading

Reviews (2): Last reviewed commit: "fix: validate charge patches by service ..." | Re-trigger Greptile

Context used:

Summary by CodeRabbit

  • Bug Fixes

    • Improved subscription cancellation handling when service periods are extended or shortened.
    • Charge and invoice timing now reconcile correctly across supported settlement modes.
    • Repeated synchronization remains consistent without duplicating or replacing valid charges.
  • Documentation

    • Clarified how service periods, billing periods, invoices, and charge realizations behave when periods change.

@coderabbitai

coderabbitai Bot commented Aug 6, 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: Pro Plus

Run ID: 738acc16-5c83-44e9-8478-7e4e9e89e314

📥 Commits

Reviewing files that changed from the base of the PR and between 5aa70a1 and 7e83ee8.

📒 Files selected for processing (6)
  • openmeter/billing/charges/README.md
  • openmeter/billing/charges/meta/patchextend.go
  • openmeter/billing/charges/meta/patchextend_test.go
  • openmeter/billing/charges/meta/patchshrink.go
  • openmeter/billing/charges/meta/patchshrink_test.go
  • openmeter/billing/worker/subscriptionsync/service/sync_regression_test.go
💤 Files with no reviewable changes (1)
  • openmeter/billing/charges/meta/patchshrink.go

📝 Walkthrough

Walkthrough

Charge patch validation now supports independent movement of full-service and billing period ends. Regression tests cover cancellation reconciliation for flat-fee and usage-based charges across settlement modes.

Changes

Charge reconciliation

Layer / File(s) Summary
Period-end validation semantics
openmeter/billing/charges/meta/patchextend.go, openmeter/billing/charges/meta/patchshrink.go, openmeter/billing/charges/meta/*_test.go, openmeter/billing/charges/README.md
Patch validation now checks period ends against their respective starts. Tests and documentation describe shrink, extend, timing, and realization behavior.
Cancellation reconciliation regression coverage
openmeter/billing/worker/subscriptionsync/service/sync_regression_test.go
Regression tests cover flat-fee and usage-based charges in credit-only and credit-then-invoice modes. Tests verify period reconciliation, invoice timing, charge identity, and idempotent synchronization.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Possibly related PRs

Suggested reviewers: tothandras

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 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: validating charge patches by service period direction.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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 agent/reproduce-shrink-extend-validation

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 changed the title test: reproduce mixed charge period cancellation fix: validate charge patches by service period Aug 6, 2026
@turip turip added area/billing release-note/bug-fix Release note: Bug Fixes labels Aug 6, 2026
@turip
turip marked this pull request as ready for review August 6, 2026 13:27
@turip
turip requested a review from a team as a code owner August 6, 2026 13:27
@turip
turip merged commit 9d0f659 into main Aug 6, 2026
29 of 32 checks passed
@turip
turip deleted the agent/reproduce-shrink-extend-validation branch August 6, 2026 13:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants