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 aligned - #5562
Advance the recurrence index only when accepting the candidate timestamp. This keeps iteration and At aligned when a search overshoots the query, preventing subsequent Next() or Prev() calls from repeating or skipping an occurrence.
Limited to the two search loops and regression coverage. Tests exercise past, matching, and future anchors; exact and between-occurrence queries; inclusive/exclusive boundaries; two steps in both directions; and NextAfter, PrevBefore, and GetPeriodAt.
Validation:
Regression tests fail before the fix and pass afterward.
Full pkg/timeutil, pkg/datetime, and openmeter/credit/engine tests pass with -tags=dynamic.
Timeutil lint and formatting checks pass.
Summary by CodeRabbit
Bug Fixes
Fixed monthly recurrence iteration when the recurrence anchor falls before or after the queried date. Iterator positions now remain consistent with the recurrence periods returned, including when a candidate crosses the query date.
Improved behavior at inclusive and exclusive date boundaries, helping iterator results align with the corresponding recurrence lookup results.
Confidence Score: 5/5
This PR appears safe to merge.
Summary
Keeps iteration and At aligned when a recurrence search overshoots the query.
Recurrence searches keep iterator steps aligned with their timestamps.
No actionable comments were generated in the recent review.
Walkthrough
The recurrence iterator search loops now keep their iteration indexes aligned with the selected timestamps. New tests cover monthly iterators with past, matching, and future anchors, including forward and backward steps.
Changes: Recurrence iterator
Layer / File(s)
Summary
Align search indexes with candidates pkg/timeutil/recurrence.go, pkg/timeutil/recurrence_test.go
The future-anchor and past-anchor loops update their indexes only after accepting a candidate. Tests check iterator starts and steps, compare starts with NextAfter and PrevBefore, and verify the containing period.
openmeterio/openmeter#5556: Updates the same recurrence search loops to keep candidate timestamps and iteration indexes aligned.
Suggested reviewers:turip, tothandras
Merge Risk: Minimal · up to 1271d
This change keeps the recurrence iterator's index aligned with its timestamp after a search overshoots, which prevents repeated or skipped occurrences. It is narrowly scoped and covered by new tests, so merge risk is minimal.
Pre-merge checks: 4
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
The title "fix(timeutil): keep recurrence iterator index aligned" matches the main change. The fix changes the two search loops in recurrence.go so the iteration index stays aligned with the retained …
Linked Issues check
Issue #5521 asks that the recurrence index and At stay aligned on both backward-loop paths. The diff at head changes iterateFromNextAfterInclusive (future-anchor loop) and `iterateFromPrevBeforeIn…
Out of Scope Changes check
The whole-PR diff against the merge base touches only pkg/timeutil/recurrence.go and pkg/timeutil/recurrence_test.go. The source change is limited to the two search loops named in the PR and issue…
Full details: Title check
Explanation
The title "fix(timeutil): keep recurrence iterator index aligned" matches the main change. The fix changes the two search loops in recurrence.go so the iteration index stays aligned with the retained timestamp. The linked issue asks for the same alignment. The added test covers the regression, so the title is specific and accurate.
Full details: Linked Issues check
Explanation
Issue #5521 asks that the recurrence index and At stay aligned on both backward-loop paths. The diff at head changes iterateFromNextAfterInclusive (future-anchor loop) and iterateFromPrevBeforeInclusive (past-anchor loop) in pkg/timeutil/recurrence.go. Each loop now computes the candidate at ic-1 or ic+1 and advances ic only when it accepts the candidate, so ic always matches res. The breaking pass no longer leaves ic ahead of res. This addresses both the repeat case (Next from anchor 2024-06-01, query 2024-03-15) and the skip case (anchor 2024-01-01, query 2024-03-15) described in the issue. The past-anchor control path is unchanged in outcome. The GetPeriodAt path uses these iterators, so it now receives a consistent index. The summary reports regression tests (TestRecurrenceIterator) that check iterator starts, two Next/Prev steps, inclusive and exclusive boundaries, and NextAfter, PrevBefore, and GetPeriodAt. The issue's coding requirement is met in source. I did not read the test file directly, so test details rest on the summary.
Full details: Out of Scope Changes check
Explanation
The whole-PR diff against the merge base touches only pkg/timeutil/recurrence.go and pkg/timeutil/recurrence_test.go. The source change is limited to the two search loops named in the PR and issue. The test changes add regression coverage for those loops. No exported declarations changed. No unrelated behavior change appears in the reviewed hunks. The grant-engine deduplication behavior mentioned in the issue is background only and is not changed here.
Finishing Touches
Generate unit tests (beta)
Commit to this branch
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 f2da409 and 1271d69.
Files selected for processing: 2
pkg/timeutil/recurrence.go
pkg/timeutil/recurrence_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.
Overview
Advance the recurrence index only when accepting the candidate timestamp. This keeps
iterationandAtaligned when a search overshoots the query, preventing subsequentNext()orPrev()calls from repeating or skipping an occurrence.Fixes #5521.
Notes for reviewer
Limited to the two search loops and regression coverage. Tests exercise past, matching, and future anchors; exact and between-occurrence queries; inclusive/exclusive boundaries; two steps in both directions; and
NextAfter,PrevBefore, andGetPeriodAt.Validation:
pkg/timeutil,pkg/datetime, andopenmeter/credit/enginetests pass with-tags=dynamic.Summary by CodeRabbit


Confidence Score: 5/5
This PR appears safe to merge.Summary
Keeps
iterationandAtaligned when a recurrence search overshoots the query.Reviews (1) · Last reviewed commit: "fix(timeutil): keep recurrence iterator ..." · Reviewed by Greptile