feat(context): gate reranking behind retrieval config#436
Conversation
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
📝 WalkthroughWalkthroughAdds an optional rerank stage to hybrid context retrieval, gated by ChangesConfig-gated retrieval reranking
Estimated code review effort: 3 (Moderate) | ~25 minutes Sequence Diagram(s)sequenceDiagram
participant context as context._retrieve
participant config as _configured_rerank
participant cache as _default_reranker_cached
participant rerank as embeddings.rerank.rerank
context->>config: read retrieval.rerank.enabled/top_k
config-->>context: enabled flag, top_k
context->>cache: load default reranker
cache-->>context: cached reranker
context->>rerank: rerank(query, hits, reranker, top_k)
rerank-->>context: reordered hits
Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 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 |
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 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 `@src/vouch/context.py`:
- Around line 131-145: The reranking logic in do_rerank() is incorrectly reusing
the tuples returned by do_rerank(), which swaps in cross-encoder scores instead
of preserving the original hybrid scores. Update the ordering/reconciliation
step to use the original window tuples when building ordered results, and only
use reranked output for relative ordering via the hit keys (kind, artifact_id),
so items[*].score and explain[*].score stay unchanged for hybrid results.
- Around line 100-101: The rerank top_k normalization in the context setup is
incorrectly accepting boolean values because bool is a subclass of int, so True
can slip through as 1 instead of falling back to limit. Update the top_k
handling in the rerank logic to explicitly reject booleans before the integer
check, keeping the existing fallback behavior to limit when the value is
missing, non-numeric, or non-positive.
- Around line 119-126: The reranker is being recreated on every call to
build_context_pack(), which adds unnecessary CrossEncoder initialization cost.
Update the build_context_pack flow in context.py to reuse a single cached
reranker instance instead of calling default_reranker() inline each time, and
pass that cached instance into do_rerank so repeated retrievals avoid
reinitialization.
🪄 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: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 63e86fa2-916b-4536-8ebc-e8039204c7c2
📒 Files selected for processing (2)
src/vouch/context.pytests/test_retrieval_backend.py
There was a problem hiding this comment.
🧹 Nitpick comments (1)
tests/test_retrieval_backend.py (1)
178-179: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winReset
_RERANKER_CACHEviamonkeypatchinstead of direct mutation.All four rerank tests set
context._RERANKER_CACHE = Nonedirectly rather than throughmonkeypatch.setattr. This bypasses pytest's automatic teardown, so if a test fails before completion (or tests run in a different order/under xdist), the cache can leak a mocked rerankerobject()into unrelated tests instead of being restored to its original state.♻️ Suggested fix (repeat for each occurrence)
- context._RERANKER_CACHE = None + monkeypatch.setattr(context, "_RERANKER_CACHE", None)Also applies to: 224-224, 259-259, 294-294
🤖 Prompt for 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. In `@tests/test_retrieval_backend.py` around lines 178 - 179, The rerank tests are mutating context._RERANKER_CACHE directly, which can leak the mocked reranker across tests if teardown is skipped. Update each rerank test to reset context._RERANKER_CACHE using monkeypatch.setattr instead of direct assignment, alongside the existing monkeypatch on rerank_mod.default_reranker, so pytest restores the cache automatically after each test.
🤖 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.
Nitpick comments:
In `@tests/test_retrieval_backend.py`:
- Around line 178-179: The rerank tests are mutating context._RERANKER_CACHE
directly, which can leak the mocked reranker across tests if teardown is
skipped. Update each rerank test to reset context._RERANKER_CACHE using
monkeypatch.setattr instead of direct assignment, alongside the existing
monkeypatch on rerank_mod.default_reranker, so pytest restores the cache
automatically after each test.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 131c6b76-25e4-4371-98b6-7278ae4fc4da
📒 Files selected for processing (2)
src/vouch/context.pytests/test_retrieval_backend.py
🚧 Files skipped from review as they are similar to previous changes (1)
- src/vouch/context.py
|
@Steve-too , it should be targeting 'test' not 'main', otherwise it won't get merged |
Try checking the contributing.MD |
b5228ca to
6d236de
Compare
|
Retargeted to |
Summary
retrieval.rerank.enabled/retrieval.rerank.top_kconfig forkb.contextTests
PYTHONPATH=src python3 -m pytest tests/test_retrieval_backend.py tests/embeddings/test_rerank.py tests/test_context.py -quv run --with ruff python -m ruff check src testsuv run --with mypy --with types-PyYAML --with jinja2 python -m mypy srcCloses #429
Summary by CodeRabbit
New Features
Bug Fixes
top_k, and repeated-call caching behavior.