Skip to content

fix(timeutil): keep recurrence iterator index in sync with At - #5585

Closed
Tyagiquamar wants to merge 1 commit into
openmeterio:mainfrom
Tyagiquamar:fix/recurrence-iterator-index
Closed

Tyagiquamar wants to merge 1 commit into
openmeterio:mainfrom
Tyagiquamar:fix/recurrence-iterator-index

Conversation

@Tyagiquamar

@Tyagiquamar Tyagiquamar commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

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.

RetriggerView in GreptileConfidence 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.

No actionable issues found.

Reviews (1) · Last reviewed commit: "fix(timeutil): keep recurrence iterator ..." · Reviewed by Greptile

Summary by CodeRabbit

  • Bug Fixes
    • 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.

@Tyagiquamar
Tyagiquamar requested a review from a team as a code owner November 19, 2026 11:04
@coderabbitai

coderabbitai Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

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.

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix

Possibly related issues

  • Issue 5521 describes the same recurrence-index mismatch addressed by these changes.

Possibly related PRs

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: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 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 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.
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.

Share

Recent review info

Run configuration

Commits

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.

@Tyagiquamar

Copy link
Copy Markdown
Contributor Author

Closing as superseded by #5562, which fixed the recurrence iterator index alignment for #5521.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant