Skip to content

fix(entitlement): invalidate snapshots after reset - #4845

Merged
GAlexIHU merged 1 commit into
mainfrom
codex/fix-entitlement-reset-snapshots
Aug 3, 2026
Merged

fix(entitlement): invalidate snapshots after reset#4845
GAlexIHU merged 1 commit into
mainfrom
codex/fix-entitlement-reset-snapshots

Conversation

@GAlexIHU

@GAlexIHU GAlexIHU commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Overview

Backdated entitlement resets can land before an already-persisted balance snapshot. Because that snapshot represents derived state from before the reset, reusing it would skip the reset and return a stale balance.

This change invalidates snapshots after the reset's effective time before saving the replacement reset snapshot. The reset test now verifies that subsequent balance calculations resume from the reset snapshot instead of a stale later one.

Notes for reviewer

  • Invalidation runs after the owner lock and in the same transaction as saving the reset snapshot and ending the usage period.
  • The existing test already claimed this lifecycle behavior; it now asserts the resulting snapshot selection.

Validation

  • nix develop --impure .#ci -c env POSTGRES_HOST=127.0.0.1 go test -tags=dynamic ./openmeter/entitlement/metered -run 'TestResetEntitlementUsage/Should_invalidate_snapshots_after_the_reset_time$' -count=1
  • nix develop --impure .#ci -c env POSTGRES_HOST=127.0.0.1 go test -tags=dynamic ./openmeter/entitlement/metered -run '^TestResetEntitlementUsage$' -count=1
  • nix develop --impure .#ci -c env POSTGRES_HOST=127.0.0.1 go test -tags=dynamic ./openmeter/entitlement/metered -count=1

Summary by CodeRabbit

  • Bug Fixes
    • Fixed usage resets so outdated balance snapshots are invalidated.
    • Ensured recalculated balances correctly reflect backdated resets and subsequent usage.

@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

ResetUsageForOwner now invalidates later balance snapshots before saving the reset snapshot. The metered entitlement test documents the backdated reset case and verifies the resulting snapshot state.

Changes

Reset snapshot consistency

Layer / File(s) Summary
Reset snapshot invalidation and validation
openmeter/credit/balance.go, openmeter/entitlement/metered/reset_test.go
ResetUsageForOwner invalidates snapshots after the reset time within the transaction. The test verifies reset success and confirms that the reset-time snapshot is the latest valid snapshot.

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

Possibly related PRs

Suggested reviewers: tothandras

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the snapshot invalidation fix after entitlement resets.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/fix-entitlement-reset-snapshots

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

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

@GAlexIHU GAlexIHU added the release-note/bug-fix Release note: Bug Fixes label Aug 3, 2026
@GAlexIHU
GAlexIHU marked this pull request as ready for review August 3, 2026 16:31
@GAlexIHU
GAlexIHU requested a review from a team as a code owner August 3, 2026 16:31

@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/balance.go (1)

319-321: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Document the backdated-reset ordering.

Add a short comment before Line 319. This call invalidates later snapshots so the replacement snapshot saved at Line 324 becomes the latest valid state. The ordering and stale-snapshot consequence are not obvious from InvalidateAfter alone.

Suggested comment
+		// Invalidate later snapshots before saving the reset snapshot so a backdated reset
+		// cannot leave stale balance data as the latest valid snapshot.
 		if err := m.BalanceSnapshotService.InvalidateAfter(ctx, ownerID, at); err != nil {

As per coding guidelines, comments should explain non-obvious intent, domain constraints, lifecycle state, and failure consequences.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@openmeter/credit/balance.go` around lines 319 - 321, Add a concise comment
immediately before the BalanceSnapshotService.InvalidateAfter call explaining
that later snapshots are invalidated before saving the replacement snapshot,
ensuring the replacement becomes the latest valid state and preventing stale
backdated snapshots from remaining active.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@openmeter/credit/balance.go`:
- Around line 319-321: Add a concise comment immediately before the
BalanceSnapshotService.InvalidateAfter call explaining that later snapshots are
invalidated before saving the replacement snapshot, ensuring the replacement
becomes the latest valid state and preventing stale backdated snapshots from
remaining active.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 0517c35c-c995-4c7f-ae58-fd471732dcf8

📥 Commits

Reviewing files that changed from the base of the PR and between e395c7d and 0d1eac6.

📒 Files selected for processing (2)
  • openmeter/credit/balance.go
  • openmeter/entitlement/metered/reset_test.go

@GAlexIHU
GAlexIHU merged commit c348100 into main Aug 3, 2026
29 of 30 checks passed
@GAlexIHU
GAlexIHU deleted the codex/fix-entitlement-reset-snapshots branch August 3, 2026 16:39
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