Skip to content

fix(entitlement): fix grant recurrence on period start - #4278

Merged
GAlexIHU merged 1 commit into
mainfrom
fix/recurrence-at-reset
May 4, 2026
Merged

GAlexIHU merged 1 commit into
mainfrom
fix/recurrence-at-reset

Conversation

@GAlexIHU

@GAlexIHU GAlexIHU commented May 4, 2026 •

Copy link
Copy Markdown
Contributor

Overview

Grants recurring at the period boundary didn't contribute to the starting balance in case it was resolved for the exact period start (at = period.From)

Summary by CodeRabbit

  • Refactor

    • Improved handling of recurring grant balances during period transitions
    • Optimized recurrence processing for more accurate balance updates at period boundaries
  • Tests

    • Added test coverage for recurring grant balance verification during resets

@GAlexIHU
GAlexIHU requested a review from a team as a code owner May 4, 2026 10:25
@GAlexIHU GAlexIHU added the release-note/bug-fix Release note: Bug Fixes label May 4, 2026
@coderabbitai

coderabbitai Bot commented May 4, 2026 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

This PR refactors grant recurrence handling to properly capture grants recurring at period boundaries. It introduces a phasePlan struct to separate recurrence events at the period start, refines the boundary condition logic for zero-length periods, and updates balance application to use the captured recurrence data.

Changes

Grant Recurrence at Period Boundaries

Layer / File(s) Summary
Data Structure
openmeter/credit/engine/burnphase.go
Introduces phasePlan struct with phases []burnPhase and grantsRecurredAtStart []string. getPhases now returns phasePlan instead of raw []burnPhase and pre-processes recurrence times occurring at period.From.
Boundary Condition Logic
openmeter/credit/engine/grant.go
Adds inPeriod predicate that treats zero-length periods ([T,T]) as including T, while non-zero periods treat period.To as exclusive. Updates recurrence iteration condition to use this new predicate.
Core Integration
openmeter/credit/engine/run.go
runBetweenResets unpacks phasePlan.phases from the refactored getPhases return. Adds applyRecurrences helper that applies RecurrenceBalance for both grantsRecurredAtStart and later recurring grants, with centralized error handling for missing grant IDs.
Test Coverage
openmeter/credit/engine/reset_test.go
New test verifies that grant recurrence at period start is included in starting balance, checking that snapshot and history segment balances reflect the recurring grant amount.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • tothandras
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically identifies the main fix: grant recurrence handling at period start boundaries, which aligns perfectly with the changeset's core purpose.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/recurrence-at-reset

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
Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
openmeter/credit/engine/reset_test.go (1)

272-272: Swap context.Background() to t.Context() here.

This keeps the test lifecycle tied to the test harness instead of spinning up a background context. Since you've got t *testing.T available in the test function, you get better cancellation semantics and cleaner test teardown this way—it's a small change with nice benefits.

Suggested change
-		res, err := eng.Run(
-			context.Background(),
+		res, err := eng.Run(
+			t.Context(),
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@openmeter/credit/engine/reset_test.go` at line 272, Replace the call to
context.Background() used in the test with the test's cancellable context
t.Context(): locate the invocation that currently passes context.Background()
(e.g., as an argument to the function under test in reset_test.go) and change it
to use t.Context() so the test lifecycle and cancellation are tied to the
testing.T instance rather than a background context.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@openmeter/credit/engine/reset_test.go`:
- Line 272: Replace the call to context.Background() used in the test with the
test's cancellable context t.Context(): locate the invocation that currently
passes context.Background() (e.g., as an argument to the function under test in
reset_test.go) and change it to use t.Context() so the test lifecycle and
cancellation are tied to the testing.T instance rather than a background
context.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 25533cdc-16a2-42d2-9c84-df58533eaf3f

📥 Commits

Reviewing files that changed from the base of the PR and between 3f41436 and 7bad4f2.

📒 Files selected for processing (4)
  • openmeter/credit/engine/burnphase.go
  • openmeter/credit/engine/grant.go
  • openmeter/credit/engine/reset_test.go
  • openmeter/credit/engine/run.go

@GAlexIHU
GAlexIHU enabled auto-merge (squash) May 4, 2026 10:34
@GAlexIHU
GAlexIHU merged commit 4a22fd9 into main May 4, 2026
28 of 30 checks passed
@GAlexIHU
GAlexIHU deleted the fix/recurrence-at-reset branch May 4, 2026 11:39

This branch was previously deployed

1 inactive deployment
dev — 7bad4f2c Deployed May 4, 2026 by GAlexIHU via Migration Checks #14038
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.

2 participants