fix(billing): clamp meter quota headroom so overflow is not double-counted (#3931)#3934
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (4)
👮 Files not reviewed due to content moderation or server errors (4)
Warning Walkthrough skippedFile diffs could not be summarized. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #3934 +/- ##
=======================================
Coverage 92.67% 92.67%
=======================================
Files 169 169
Lines 5546 5546
Branches 1782 1783 +1
=======================================
Hits 5140 5140
Misses 326 326
Partials 80 80
Flags with carried forward coverage won't be shown. Click here to find out more. Continue to review full report in Codecov by Harness.
🚀 New features to boost your workflow:
|
Problem
The meter exhaustion gate computed
remaining = (meterQuota - meterUsed) + extrasBalancewith no clamp on the plan-quota term.meterUsedis$inc'd uncapped past quota and the same overflow is also debited from extras, so oncemeterUsed > meterQuotathe overflow is subtracted twice → an org is denied402 METER_EXHAUSTEDwhile still holding a positive extras balance (free plans block at half a pack).Fix
Clamp the plan headroom at 0 (same clamp the reconcile service already uses):
Math.max(0, meterQuota - meterUsed) + extrasBalance. Applied at both call sites — the gate (billing.quota.service.js) and theX-Meter-Remainingheader (billing.attachUsageContext.js). Identical to the old formula whenevermeterUsed <= meterQuota(no normal-path regression).Test
Added the previously-uncovered case
meterUsed > meterQuota && extrasBalance > 0on both surfaces — asserts no 402 andremaining == extrasBalance.Closes #3931
https://claude.ai/code/session_01V1mhCDsSUnn3WGtevd3YFX