Repository navigation
refactor(subscriptionsync): simplify reconciliation scheduling - #5561
Conversation
Decide the recorded state and the next reconciliation deadline in one place in sync, and compute the deadline with a single pure function instead of two helpers split across the target state and sync. - Remove NextBillingPeriod from the target state and the plan; sync resolves the billing period at the generation limit itself. Only BillingEndsAt still flows from the target. - Drop the "jitter point already passed, use the period end" fallback. It only applied when AsOf lags the worker clock; such a sync now leaves a past deadline that stays due, like any historical AsOf. - Drop the past-deadline warning and validation of values sync produces itself. - Share the deadline rule between SyncStateV2 and RecordSyncResultV2Input validation. - Trim the sync state tests with shared helpers and remove cases that duplicate TestEndedSubscriptionSyncResult. Co-Authored-By: Claude Fable 5.2 <[email protected]>
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
857ab68
into
feat/subscription-sync-active-reconciliation
Stacked on #5555. Simplifies how a successful sync decides its recorded state and next reconciliation deadline, without changing the v2 outcomes that PR introduces.
What changes
nextReconciliationAt(period, billingEndsAt, fraction)insync.goreplacescalculateNextBillingPeriodandgetNextReconciliationAt, together with their input structs andValidatemethods. If the period reaches the effective billing end, the deadline is exactly that end; otherwise it is a point 80–95% through the period.switchafter the dry-run return picksended,no_billables, oractiveplus its deadline.NextBillingPeriodremoved fromtargetstate.Stateandreconciler.Plan. Sync resolves the billing period at the generation limit itself; onlyBillingEndsAtstill flows from the target. A nil period no longer has three meanings.SyncStateV2.ValidateandRecordSyncResultV2Input.Validatecall onevalidateNextReconciliationAtmethod. Error messages are unchanged.Behavior change
The "jitter point already passed, use the period end" fallback is gone, along with the past-deadline warning. The fallback only applied when
AsOflags the worker clock, and every production caller passes the current time or the futureActiveTo. A sync with a staleAsOfnow leaves a deadline in the past, which keeps the subscription due until a sync with a currentAsOfcatches up. This is the same behavior the README already documents for a historicalAsOf.Tests
TestNextReconciliationAtis a pure table on the new function and absorbs the cases fromTestNextBillingPeriodForMidPeriodCustomerDeletion.sync_state_test.goshrinks from 1,054 to 766 lines throughforEachBackend,useNewCustomer,requireUnscheduled, and a mergedrequireFees.TestCustomerDeletionReconciliationkeeps 5 of 8 cases per backend. The three removed cases checked how the worker clock andAsOfdecide between active and ended, whichTestEndedSubscriptionSyncResultalready covers forActiveTothrough the sameBillingEndsAtvalue.TestNilPlanPreservesPreviousSyncResult(the nil-plan guard stays) andTestNextReconciliationAtRequiresPeriodOrEnd(the period is no longer optional).createSubscriptionForRevisionChangemoves back tosync_test.go, and the adapter's per-state "clears the deadline" loop becomes one case because the upsert does not branch on state.Validation
Before trimming any tests, the original integration matrix from #5555 ran unchanged against the simplified code and passed. PostgreSQL-backed
./openmeter/billing/worker/subscriptionsync/...tests pass, the root module builds, andgolangci-lintreports no issues for the package.🤖 Generated with Claude Code


Confidence Score: 5/5
This PR appears safe to merge; no actionable issues were found.Summary
Simplifies how subscription sync records its result and next deadline.
Past deadlines after historical syncs: turip explicitly describes removing the fallback as intentional so historical work stays due.
Diagram
%%{init: {'theme': 'neutral'}}%% flowchart TD A[Apply billing changes] --> B{Dry run?} B -->|Yes| C[Return without saving sync state] B -->|No| D[Update legacy scheduling] D --> E{Choose result} E -->|Deletion cleanup| F[Keep cleanup result] E -->|Clock and horizon reach billing end| G[Record ended] E -->|No billables| H[Record no_billables] E -->|Otherwise| I[Resolve billing period] I --> J{Period reaches billing end?} J -->|Yes| K[Use exact billing end] J -->|No| L[Choose deadline 80–95% through period] K --> M[Record active with deadline] L --> MReviews (1) · Last reviewed commit: "refactor(subscriptionsync): simplify rec..." · Reviewed by Greptile
Summary by CodeRabbit