Skip to content

fix(credit): reuse the caller transaction for metered grant reads - #5595

Merged
turip merged 1 commit into
mainfrom
codex/metered-grant-read-transaction
Nov 19, 2026
Merged

turip merged 1 commit into
mainfrom
codex/metered-grant-read-transaction

Conversation

@GAlexIHU

@GAlexIHU GAlexIHU commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Metered entitlement balance calculations can run inside a transaction that has just issued a grant. Looking up active grants on a separate connection misses that grant and can time out if the transaction already holds the last available connection.

Use the caller’s transaction for active-grant reads in balance, history, and reset calculations, including the starting snapshot. Standalone reads continue to use the ordinary grant repository.

The PostgreSQL regression test issues 10 units, reads the balance before commit with a one-connection pool, and checks that 3 units of usage leave 7 available.

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; no actionable issues were found.

Summary

Active-grant reads now reuse the caller’s transaction across balance, history, reset, and starting-snapshot calculations.

  • Credit balance reads now see grants created in the caller’s transaction.

No actionable issues found. Tests were not run.

Reviews (1) · Last reviewed commit: "fix(credit): reuse the caller transactio..." · Reviewed by Greptile

Summary by CodeRabbit

  • Bug Fixes
    • Balance calculations now consistently include grants created within the current transaction, so entitlement balances reflect those grants before the transaction commits.

@GAlexIHU GAlexIHU added the release-note/bug-fix Release note: Bug Fixes label Nov 19, 2026
@coderabbitai

coderabbitai Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Balance grant lookups now use a transaction-aware helper. A new test verifies that an entitlement balance read inside a transaction includes a grant created in that transaction.

Changes: Balance grant lookup

Layer / File(s) Summary
Transaction-aware balance lookups
openmeter/credit/helper.go, openmeter/credit/balance.go, openmeter/entitlement/metered/balance_test.go
listActiveGrantsBetween runs grant listing through entutils.TransactingRepo. Balance calculations and snapshot lookup use this helper. The test reads the balance before the transaction commits and asserts a result of 7.

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

Change: Bug fix

Suggested reviewers: tothandras, turip

Merge Risk: Minimal · up to c372d

Balance calculations inside a transaction now include grants created in that same transaction. They also avoid waiting for a second database connection when the pool has only one. No concrete merge-blocking risk was found.

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 main change: metered grant reads reuse the caller's transaction. The raw summary shows listActiveGrantsBetween now runs through entutils.TransactingRepo, and the balance calculatio…
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 main change: metered grant reads reuse the caller's transaction. The raw summary shows listActiveGrantsBetween now runs through entutils.TransactingRepo, and the balance calculations call that helper. The added regression test covers the one-connection, uncommitted-grant case. The title is specific and matches the changeset and the PR objectives. It uses the conventional fix(scope) format and is 65 characters long, within a typical limit.

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 6856c74 and c372d87.

Files selected for processing: 3

  • openmeter/credit/balance.go
  • openmeter/credit/helper.go
  • openmeter/entitlement/metered/balance_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 marked this pull request as ready for review November 19, 2026 17:22
@GAlexIHU
GAlexIHU requested a review from a team as a code owner November 19, 2026 17:22
@turip
turip merged commit 4d945dc into main Nov 19, 2026
34 of 36 checks passed
@turip
turip deleted the codex/metered-grant-read-transaction branch November 19, 2026 17:30
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