Skip to content

fix(rag/raptor): handle max_cluster edge case in GMM cluster selection - #16199

Merged
yingfeng merged 4 commits into
infiniflow:mainfrom
Harsh23Kashyap:Harsh23Kashyap/fix/raptor-optimal-clusters
Jun 23, 2026
Merged

yingfeng merged 4 commits into
infiniflow:mainfrom
Harsh23Kashyap:Harsh23Kashyap/fix/raptor-optimal-clusters

Conversation

@Harsh23Kashyap

Copy link
Copy Markdown
Collaborator

What problem does this PR solve?

_get_optimal_clusters in rag/raptor.py had two edge-case issues in GMM cluster-count selection:

  1. It used np.arange(1, max_clusters), which never evaluates the upper-bound candidate (max_clusters).
  2. When effective max_clusters becomes 1, the candidate list was empty and argmin crashed.

This PR makes candidate evaluation inclusive (1..max_clusters) and guards the single-cluster case by returning 1 directly.

Type of change

  • Bug Fix (non-breaking change which fixes an issue)

Validation

  • pytest test/unit_test/rag/test_raptor_psi_tree_builder.py --config-file pyproject.toml -q
  • ruff check rag/raptor.py test/unit_test/rag/test_raptor_psi_tree_builder.py

Tests added

  • Regression test for max_cluster == 1 path (no crash, returns 1)
  • Regression test verifying upper-bound candidate is evaluated and can be selected

AI-assistance disclosure: parts of this change (bug triage and test scaffolding) were drafted with AI assistance and fully reviewed and verified by me.

Evaluate the full candidate range up to max_cluster and guard the single-cluster edge case so _get_optimal_clusters does not crash when max_cluster resolves to 1. Add regression tests to cover the max_cluster=1 path and verify the upper-bound candidate is evaluated.

Co-authored-by: Cursor <[email protected]>
@dosubot dosubot Bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Jun 18, 2026
@Harsh23Kashyap

Copy link
Copy Markdown
Collaborator Author

Thanks for reviewing! Could a maintainer please add the ci label so the full test workflow runs on this non-draft PR?

@coderabbitai

coderabbitai Bot commented Jun 18, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

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: 5fabcd1b-d86f-4e27-948a-ea842a7b1a85

📥 Commits

Reviewing files that changed from the base of the PR and between ddc11d8 and 3dfe387.

📒 Files selected for processing (1)
  • rag/raptor.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • rag/raptor.py

📝 Walkthrough

Walkthrough

_get_optimal_clusters in rag/raptor.py gains an early-return guard for max_clusters <= 1, an inclusive upper bound in the candidate cluster range, and an explicit int cast on the return value. Two new unit tests cover the max_clusters == 1 edge case and verify that all candidates including the upper bound are evaluated via a monkeypatched GaussianMixture stub.

Changes

Optimal Cluster Selection Fix

Layer / File(s) Summary
_get_optimal_clusters fixes and tests
rag/raptor.py, test/unit_test/rag/test_raptor_psi_tree_builder.py
Adds early exit when max_clusters <= 1, changes the candidate range to range(1, max_clusters + 1) so the upper bound is included, casts the result to int, and adds two pytest tests: one asserting the early-return path and one monkeypatching GaussianMixture to confirm candidates [1, 2, 3] are evaluated and the BIC-optimal count (3) is returned.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐇 A cluster of one? Return right away!
The upper bound was lost — now it's found today.
Numpy's scalar sneaked in, but int() caught the trick,
BIC scores recorded, the stub did the trick.
Three is the answer when scores say so clear —
The rabbit fixed the range, and the tests give a cheer! 🎉

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 22.22% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The PR title clearly describes the main fix: handling a max_cluster edge case in GMM cluster selection, directly corresponding to the primary changes in the code.
Description check ✅ Passed The PR description comprehensively addresses the required template sections, clearly explaining the problems solved, type of change, validation steps, and tests added.
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.

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


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.

@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.

Actionable comments posted: 1

🤖 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.

Inline comments:
In `@rag/raptor.py`:
- Around line 238-239: The early-return path when max_clusters <= 1 lacks
logging, making single-cluster selection invisible in runtime diagnostics. Add a
log statement before the return statement inside the if max_clusters <= 1
condition to record when this branch is taken, including the max_clusters value
and the input size for observability and debugging purposes.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: d0365571-3936-4b78-906e-57add3b04c77

📥 Commits

Reviewing files that changed from the base of the PR and between b409cfc and ddc11d8.

📒 Files selected for processing (2)
  • rag/raptor.py
  • test/unit_test/rag/test_raptor_psi_tree_builder.py

Comment thread rag/raptor.py Resolved
@Harsh23Kashyap

Copy link
Copy Markdown
Collaborator Author

Friendly follow-up: could a maintainer please add the ci label so the full test workflows run on this PR? Thank you!

@Harsh23Kashyap

Copy link
Copy Markdown
Collaborator Author

Hi maintainers, quick follow-up on this one. The PR is non-draft and mergeable, but the test workflow is still skipped without the ci label. Could you please add the label when you get a chance?

Harsh23Kashyap and others added 2 commits June 21, 2026 01:29
Log the max cluster and embedding count when RAPTOR skips GMM model selection because only one cluster is available.
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. and removed size:XS This PR changes 0-9 lines, ignoring generated files. labels Jun 21, 2026
@Harsh23Kashyap

Copy link
Copy Markdown
Collaborator Author

Addressed the CodeRabbit feedback by adding logging for the single-cluster RAPTOR GMM fallback path.

Validated locally with:

.v/bin/pytest test/unit_test/rag/test_raptor_psi_tree_builder.py -q

Result: 18 passed.

The PR is still non-draft and mergeable. Could a maintainer please add the ci label when possible so the full workflow can run?

@yingfeng
yingfeng requested a review from KevinHuSh June 22, 2026 08:49
@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Jun 23, 2026
@KevinHuSh KevinHuSh added the ci Continue Integration label Jun 23, 2026
@KevinHuSh
KevinHuSh marked this pull request as draft June 23, 2026 07:19
@KevinHuSh
KevinHuSh marked this pull request as ready for review June 23, 2026 07:19
@Harsh23Kashyap

Copy link
Copy Markdown
Collaborator Author

Friendly follow-up: this PR is ready for review with all checks passing. When you have a moment, could you please take a look? Thanks for your time!

@codecov

codecov Bot commented Jun 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.16%. Comparing base (11e14a8) to head (a7c4c8b).

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #16199   +/-   ##
=======================================
  Coverage   93.16%   93.16%           
=======================================
  Files          10       10           
  Lines         717      717           
  Branches      118      118           
=======================================
  Hits          668      668           
  Misses         29       29           
  Partials       20       20           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@yingfeng
yingfeng merged commit b4a8a90 into infiniflow:main Jun 23, 2026
3 checks passed
@Harsh23Kashyap
Harsh23Kashyap deleted the Harsh23Kashyap/fix/raptor-optimal-clusters branch July 1, 2026 02:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Continue Integration lgtm This PR has been approved by a maintainer size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants