Skip to content

fix(timeutil): keep recurrence iterator index aligned - #5562

Merged
GAlexIHU merged 1 commit into
mainfrom
codex/fix-recurrence-iterator-index
Nov 19, 2026
Merged

GAlexIHU merged 1 commit into
mainfrom
codex/fix-recurrence-iterator-index

Conversation

@GAlexIHU

@GAlexIHU GAlexIHU commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Overview

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.

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, 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.

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

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

@GAlexIHU
GAlexIHU requested a review from a team as a code owner November 17, 2026 16:03
@GAlexIHU GAlexIHU added the release-note/bug-fix Release note: Bug Fixes label Nov 17, 2026 — with ChatGPT Codex Connector
@coderabbitai

coderabbitai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

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.

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

Change: Bug fix · Severity of issue fixed: Low

Possibly related PRs

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

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

Share

Recent review info

Run configuration

Commits

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.

@GAlexIHU
GAlexIHU merged commit 55759ef into main Nov 19, 2026
33 of 35 checks passed
@GAlexIHU
GAlexIHU deleted the codex/fix-recurrence-iterator-index branch November 19, 2026 14:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note/bug-fix Release note: Bug Fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RecurrenceIterator: iteration and At name different instants when the anchor is in the future, so Next() repeats or skips

2 participants