Skip to content

fix: add deterministic tiebreaker to RR host selection algorithm#28783

Open
SinghaAnirban005 wants to merge 4 commits intocalcom:mainfrom
SinghaAnirban005:itg-test
Open

fix: add deterministic tiebreaker to RR host selection algorithm#28783
SinghaAnirban005 wants to merge 4 commits intocalcom:mainfrom
SinghaAnirban005:itg-test

Conversation

@SinghaAnirban005
Copy link
Copy Markdown
Contributor

@SinghaAnirban005 SinghaAnirban005 commented Apr 7, 2026

What does this PR do?

This PR addresses the TODO item in the RR host selection system by implementing deterministic ordering

image

When isRRWeightsEnabled: false and all hosts have the same priority and no bookings, the system was preserving the input order from availableUsers array instead of providing deterministic ordering.This led to non deterministic behavior where the same inputs could produce different outputs depending on array order.

Image Demo (if applicable):

image

Mandatory Tasks (DO NOT REMOVE)

  • I have self-reviewed the code (A decent size PR without self-review might be rejected).
  • I have updated the developer docs in /docs if this PR makes changes that would require a documentation change. N/A
  • I confirm automated tests are in place that prove my fix is effective or that my feature works.

Open with Devin

@SinghaAnirban005 SinghaAnirban005 requested a review from a team as a code owner April 7, 2026 19:24
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 7, 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: 258d6b40-358f-4184-9bfd-18193d015e19

📥 Commits

Reviewing files that changed from the base of the PR and between 3167248 and 24e51a2.

📒 Files selected for processing (1)
  • packages/features/bookings/lib/getLuckyUser.integration-test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/features/bookings/lib/getLuckyUser.integration-test.ts

📝 Walkthrough

Walkthrough

The change adds eventType to the leastRecentlyBookedUser input destructuring and modifies the availableUsers.sort(...) comparator: when eventType.isRRWeightsEnabled is true the comparator returns 0 for ties, otherwise it returns a.id - b.id. The integration test was updated to compute a single expectedOrder by sorting users by id and assert that getOrderedListOfLuckyUsers returns that order for different availableUsers permutations, replacing permutation-specific hard-coded expectations.

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: adding deterministic tiebreaking to the RR host selection algorithm, which directly matches the core purpose of the PR.
Description check ✅ Passed The description is clearly related to the changeset, explaining the deterministic ordering implementation for the RR host selection system and addressing the TODO item mentioned in the code.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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 and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (2)
packages/features/bookings/lib/getLuckyUser.integration-test.ts (2)

449-449: Test name is now misleading after the fix.

The test description says "should sort as per availableUsers" but the implementation now sorts by user ID, making it independent of availableUsers order. Consider updating the description to reflect the new deterministic behavior.

📝 Suggested test name update
-  it("should sort as per availableUsers if no other criteria like weight/priority/calibration", async () => {
+  it("should sort deterministically by user ID when no other criteria like weight/priority/calibration", async () => {
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/features/bookings/lib/getLuckyUser.integration-test.ts` at line 449,
The test description for the case starting with it("should sort as per
availableUsers...") is now inaccurate because the implementation sorts
deterministically by user ID rather than following availableUsers order; update
the test name string to reflect the new behavior (e.g., "should sort
deterministically by user ID when no weight/priority/calibration criteria") so
the description matches the implementation in this test case.

540-542: Remove commented-out code.

This appears to be leftover exploration/debug code. Consider removing these lines to keep the test file clean.

🧹 Remove commented-out code
-      // const [firstTiedUser, secondTiedUser] = [user1WithWeight100, user2WithWeight100].sort((a, b) => a.id - b.id);
-      // // const expectedOrder = [userWithHighestWeight, firstTiedUser, secondTiedUser];
-
       expectLuckyUsers(luckyUsers, [
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/features/bookings/lib/getLuckyUser.integration-test.ts` around lines
540 - 542, Remove the leftover commented-out debug/exploration lines that define
firstTiedUser, secondTiedUser, and the commented expectedOrder
([userWithHighestWeight, firstTiedUser, secondTiedUser]) in the
getLuckyUser.integration-test.ts test; simply delete those commented lines
(references: firstTiedUser, secondTiedUser, user1WithWeight100,
user2WithWeight100, userWithHighestWeight, expectedOrder) so the test file
contains only active test code and no vestigial comments.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@packages/features/bookings/lib/getLuckyUser.integration-test.ts`:
- Line 449: The test description for the case starting with it("should sort as
per availableUsers...") is now inaccurate because the implementation sorts
deterministically by user ID rather than following availableUsers order; update
the test name string to reflect the new behavior (e.g., "should sort
deterministically by user ID when no weight/priority/calibration criteria") so
the description matches the implementation in this test case.
- Around line 540-542: Remove the leftover commented-out debug/exploration lines
that define firstTiedUser, secondTiedUser, and the commented expectedOrder
([userWithHighestWeight, firstTiedUser, secondTiedUser]) in the
getLuckyUser.integration-test.ts test; simply delete those commented lines
(references: firstTiedUser, secondTiedUser, user1WithWeight100,
user2WithWeight100, userWithHighestWeight, expectedOrder) so the test file
contains only active test code and no vestigial comments.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 47c84777-4520-4025-b121-8894515d4329

📥 Commits

Reviewing files that changed from the base of the PR and between 3c52f57 and 0e73128.

📒 Files selected for processing (2)
  • packages/features/bookings/lib/getLuckyUser.integration-test.ts
  • packages/features/bookings/lib/getLuckyUser.ts

devin-ai-integration[bot]

This comment was marked as resolved.

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@sahitya-chandra sahitya-chandra added ready-for-e2e run-ci Approve CI to run for external contributors labels Apr 8, 2026
@sahitya-chandra sahitya-chandra added run-ci Approve CI to run for external contributors and removed run-ci Approve CI to run for external contributors labels Apr 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-e2e run-ci Approve CI to run for external contributors size/S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants