You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(timeutil): keep recurrence iterator index in sync with At - #5585
The iterators returned by Recurrence.IterateFromNextAfter and Recurrence.IterateFromPrevBefore now keep their internal iteration index in sync with the exposed At time. Advancing such an iterator with Next() (or Prev()) now yields the actual next (or previous) recurrence instead of repeating or skipping one.
Root Cause & Problem
Both search helpers walk backwards or forwards from the anchor and stop early when a step crosses t:
iterateFromNextAfterInclusive, anchor in the future: each loop iteration decrements ic, computes v = anchor + ic*interval, and breaks when v is before t, leaving At at the previous (correct) occurrence but ic one step behind it.
iterateFromPrevBeforeInclusive, anchor in the past: each loop iteration increments ic, computes v = anchor + ic*interval, and breaks when v is after t, leaving At at the previous (correct) occurrence but ic one step ahead of it.
RecurrenceIterator.Next() and Prev() recompute At from iteration (addIntervalNTimes(anchor, iteration+delta)), so the stale index produced wrong results. This is observable in openmeter/credit/engine/grant.go, which repeatedly calls Next() to enumerate recurrence times: the first step could repeat the starting time (future-anchor case) or skip the next one (past-anchor case), leading to duplicate or missing credit-grant recurrence points.
NextAfter/PrevBefore only return At, so existing tests did not cover the index and passed.
Technical Solution
Undo the index step before breaking out of each early-break branch, so the returned iteration describes the At that is actually returned. No signature or API changes; the loop's At behavior is unchanged, only the index carried by the iterator is corrected.
Test Plan
Added pkg/timeutil/recurrence_iterator_test.go (package timeutil_test) with two cases:
Daily recurrence anchored in the future, querying a time between two occurrences: IterateFromNextAfter returns the correct At, and Next() returns the following day.
Daily recurrence anchored in the past, querying a time between two occurrences: IterateFromPrevBefore returns the correct At, and Next() returns the following day.
Both cases fail against the original code (Next() returned Jan 7 instead of Jan 8, and Jan 8 instead of Jan 7 respectively) and pass with the fix.
Validation executed locally (Windows, Go 1.27.3):
go test ./pkg/timeutil/ -run TestRecurrenceIteratorIndexStaysInSync -count=1 -v -> fails before fix (2 subtests), passes after fix
go test ./pkg/timeutil/ -count=1 -> ok (all package tests)
go vet ./pkg/timeutil/ -> exit 0
gofmt -l pkg/timeutil/recurrence.go pkg/timeutil/recurrence_iterator_test.go -> no output
No Docker or full-repository builds were run. The wider suite is left to GitHub Actions.
Confidence Score: 5/5
This PR appears safe to merge; no actionable issues were found.
Summary
This PR fixes recurrence iterators so advancing them does not repeat or skip an occurrence after a search.
Recurrence iterators advance from the time they return.
Corrected recurrence search results so the iterator advances by the expected daily interval from the selected occurrence, including when searching from a future or past anchor.
No actionable comments were generated in the recent review.
Walkthrough
The backward and forward recurrence searches adjust the iterator index when a candidate crosses the target. New tests check that Next() advances by one daily interval from the selected occurrence in both search directions.
Changes: Recurrence iterator
Layer / File(s)
Summary
Index correction and iterator tests pkg/timeutil/recurrence.go, pkg/timeutil/recurrence_iterator_test.go
Both searches correct the index after crossing the target. Tests verify inclusive starting occurrences and the following daily occurrence for future and past anchors.
Issue 5521 describes the same recurrence-index mismatch addressed by these changes.
Possibly related PRs
openmeterio/openmeter#5562: Changes the same recurrence search loops to keep the iteration index aligned with At.
openmeterio/openmeter#5556: Restores the index when a candidate is rejected in the same recurrence search paths.
Suggested reviewers:galexihu, turip, tothandras
Merge Risk: Minimal · up to de43f
This fixes the recurrence iterator so Next() returns the actual following occurrence instead of repeating or skipping one. The change is small and tested in both directions, and no merge-blocking risk is identified.
Pre-merge checks: 4
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
The title names the fix and its scope: the recurrence iterator index is kept in sync with At in pkg/timeutil. The raw summary confirms the change adjusts the iteration index when the search crosses th…
Linked Issues check
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
Check skipped because no linked issues were found for this pull request.
Full details: Title check
Explanation
The title names the fix and its scope: the recurrence iterator index is kept in sync with At in pkg/timeutil. The raw summary confirms the change adjusts the iteration index when the search crosses the target, and the objectives describe the same undo step. The title is specific and matches the main change.
Finishing Touches
Generate unit tests (beta)
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.
Reviewing files that changed from the base of the PR and between d457312 and de43fe7.
Files selected for processing: 2
pkg/timeutil/recurrence.go
pkg/timeutil/recurrence_iterator_test.go
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The iterators returned by
Recurrence.IterateFromNextAfterandRecurrence.IterateFromPrevBeforenow keep their internaliterationindex in sync with the exposedAttime. Advancing such an iterator withNext()(orPrev()) now yields the actual next (or previous) recurrence instead of repeating or skipping one.Root Cause & Problem
Both search helpers walk backwards or forwards from the anchor and stop early when a step crosses
t:iterateFromNextAfterInclusive, anchor in the future: each loop iteration decrementsic, computesv = anchor + ic*interval, and breaks whenvis beforet, leavingAtat the previous (correct) occurrence buticone step behind it.iterateFromPrevBeforeInclusive, anchor in the past: each loop iteration incrementsic, computesv = anchor + ic*interval, and breaks whenvis aftert, leavingAtat the previous (correct) occurrence buticone step ahead of it.RecurrenceIterator.Next()andPrev()recomputeAtfromiteration(addIntervalNTimes(anchor, iteration+delta)), so the stale index produced wrong results. This is observable inopenmeter/credit/engine/grant.go, which repeatedly callsNext()to enumerate recurrence times: the first step could repeat the starting time (future-anchor case) or skip the next one (past-anchor case), leading to duplicate or missing credit-grant recurrence points.NextAfter/PrevBeforeonly returnAt, so existing tests did not cover the index and passed.Technical Solution
Undo the index step before breaking out of each early-break branch, so the returned
iterationdescribes theAtthat is actually returned. No signature or API changes; the loop'sAtbehavior is unchanged, only the index carried by the iterator is corrected.Test Plan
Added
pkg/timeutil/recurrence_iterator_test.go(packagetimeutil_test) with two cases:IterateFromNextAfterreturns the correctAt, andNext()returns the following day.IterateFromPrevBeforereturns the correctAt, andNext()returns the following day.Both cases fail against the original code (
Next()returned Jan 7 instead of Jan 8, and Jan 8 instead of Jan 7 respectively) and pass with the fix.Validation executed locally (Windows, Go 1.27.3):
go test ./pkg/timeutil/ -run TestRecurrenceIteratorIndexStaysInSync -count=1 -v-> fails before fix (2 subtests), passes after fixgo test ./pkg/timeutil/ -count=1-> ok (all package tests)go vet ./pkg/timeutil/-> exit 0gofmt -l pkg/timeutil/recurrence.go pkg/timeutil/recurrence_iterator_test.go-> no outputNo Docker or full-repository builds were run. The wider suite is left to GitHub Actions.


Confidence Score: 5/5
This PR appears safe to merge; no actionable issues were found.Summary
This PR fixes recurrence iterators so advancing them does not repeat or skip an occurrence after a search.
No actionable issues found.
Reviews (1) · Last reviewed commit: "fix(timeutil): keep recurrence iterator ..." · Reviewed by Greptile
Summary by CodeRabbit