Skip to content

fix(captain): prevent clamp ArgumentError on inbox creation#242

Merged
gabrieljablonski merged 1 commit intomainfrom
fix/captain-limits-clamp-error
Mar 19, 2026
Merged

fix(captain): prevent clamp ArgumentError on inbox creation#242
gabrieljablonski merged 1 commit intomainfrom
fix/captain-limits-clamp-error

Conversation

@gabrieljablonski
Copy link
Member

@gabrieljablonski gabrieljablonski commented Mar 19, 2026

Summary

  • Bug: Creating an inbox triggers a 500 error (ArgumentError: min argument must be less than or equal to max argument) when captain limits resolve to a negative total_count.
  • Root cause: usage_limits eagerly computes captain limits (even when only :inboxes is needed). In get_captain_limits, total_count.clamp(0, total_count) fails when total_count < 0 because min (0) > max (negative).
  • Fix: Floor total_count at 0 with [..., 0].max so the clamp range is always valid.

Test plan

  • Create an inbox on an account with limits: { "inboxes" => 20 } and no captain-specific limits — should succeed without 500
  • Verify captain usage limits still work correctly for accounts with valid captain configs

🤖 Generated with Claude Code


This change is Reviewable

Summary by CodeRabbit

Bug Fixes

  • Fixed a calculation issue where plan usage limits could display as negative values. Plan limits are now properly calculated with enhanced validation to ensure non-negative amounts. This improves the accuracy of usage information displayed in account management and billing interfaces, providing clearer visibility into plan capacity and consumption.

…ative

When creating an inbox, `usage_limits` eagerly computes captain limits
even though only the `:inboxes` value is needed. If `total_count` in
`get_captain_limits` resolves to a negative number, `clamp(0, negative)`
raises `ArgumentError: min argument must be less than or equal to max
argument`, causing a 500 on inbox creation.

Ensure `total_count` is floored at 0 so the clamp range is always valid.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@coderabbitai
Copy link

coderabbitai bot commented Mar 19, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: a584c406-3bdc-45cc-94b5-984cf5454a87

📥 Commits

Reviewing files that changed from the base of the PR and between 521ce90 and 4c5067c.

📒 Files selected for processing (1)
  • enterprise/app/models/enterprise/account/plan_usage_and_limits.rb

📝 Walkthrough

Walkthrough

A single-line modification to the get_captain_limits(type) method that adds non-negative clamping to the monthly limit conversion, ensuring the total_count cannot be negative when computed.

Changes

Cohort / File(s) Summary
Plan Limits Clamping
enterprise/app/models/enterprise/account/plan_usage_and_limits.rb
Added max(..., 0) to the monthly limit conversion to prevent negative total_count values from propagating downstream.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐰 A limit once negative, now clamped with care,
No more minus values floating in the air,
With max we ensure the total stays right,
Hopping forward keeps everything bright!

🚥 Pre-merge checks | ✅ 2 | ❌ 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 (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and accurately summarizes the main change: fixing an ArgumentError in the captain limits clamp logic that occurs during inbox creation.

✏️ 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/captain-limits-clamp-error
📝 Coding Plan
  • Generate coding plan for human review comments

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

@gabrieljablonski
Copy link
Member Author

@coderabbitai review

@coderabbitai
Copy link

coderabbitai bot commented Mar 19, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@gabrieljablonski gabrieljablonski merged commit 05e0d35 into main Mar 19, 2026
2 checks passed
@gabrieljablonski gabrieljablonski deleted the fix/captain-limits-clamp-error branch March 19, 2026 23:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant