Repository navigation
Description
Objective
Both-mode semantic search must never fail because the merged candidate set exceeds the reranker's per-batch token budget.
Context
Found in a read-only audit of the MCP surface on 2026-11-11 (no code changed). vector_source="both" over a large git range sends every merged candidate to one rerank request. At page_size=100 the candidate pool is 500 per source, so up to 1000 documents are submitted in a single call. Roughly 873 tokens per document puts that over the cap of the configured reranker.
Reproduction (deterministic, 2/2):
search(type="semantic", query="compaction atomic swap crash recovery",
commit_range="HEAD~1058..HEAD", page_size=100, vector_source="both")
Observed:
RuntimeError: Reranking failed: Request to model 'rerank-3-lite' failed.
The max allowed tokens per submitted batch is 600000.
Your batch has 872637 tokens after truncation.
page_size=50 on the same range and query succeeds. vector_source="diff" at page_size=100 succeeds because that path never reranks.
Requirements
- A rerank request must never exceed the target model's declared per-batch token budget. Batch by tokens and by count, then merge by score.
- Every candidate receives exactly one score, and all scores share one scale (no cosine/rerank mixing).
- If the provider still rejects a batch, log a warning and return cosine order. Do not raise.
- Apply the same rules to the SDK and HTTP rerank paths.
- Declare the rerank token budget in one place for all providers — the existing
get_max_tokens_per_batchis the embedding budget and must not be reused for rerank.
Acceptance criteria
- The reproduction call returns 100 results and exits 0; no
RuntimeError. -
page_size=50andvector_source="diff"produce byte-identical results to before the change. - New unit test: 1000 documents against a 600k-token budget produce only requests within the budget, every candidate scored once, one score scale.
-
uv run pytest tests/unit/test_transient_search_service.py tests/unit/test_openai_provider_rerank_retry.py tests/test_multi_hop_semantic_search.py -qis green. - The fallback is observable: a provider that always rejects logs one warning and returns vector-ordered results.
Implementation anchors
chunkhound/mcp_server/tools.py:734(page_size cap),:755(_inject_diff_service).chunkhound/services/transient_search_service.py:754-755(snapshot size),:884-885(documents=[...],rerank(top_k=len(documents))),:44(_candidate_pool_size).chunkhound/providers/embeddings/voyageai_provider.py:942,:965,:1025-1041,:1111.- Fallback precedent:
chunkhound/services/search/multi_hop_strategy.py:170.
Boundaries
- Always: run the tests named above before proposing a commit.
- Ask first: changing the embedding-provider protocol signature, adding a dependency.
- Never: delete or weaken rerank tests to make them pass; commit without explicit approval.
Out of scope
Pagination window semantics (#513) and diff chunk caching (#517). Sequencing only — land this before #513 to avoid conflicting edits in the same module.
Activity