Skip to content

fix(search): bound both-mode rerank by the provider token budget #511

Open

Description

@ofriw

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

  1. 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.
  2. Every candidate receives exactly one score, and all scores share one scale (no cosine/rerank mixing).
  3. If the provider still rejects a batch, log a warning and return cosine order. Do not raise.
  4. Apply the same rules to the SDK and HTTP rerank paths.
  5. Declare the rerank token budget in one place for all providers — the existing get_max_tokens_per_batch is 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=50 and vector_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 -q is 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.

Related: #513, #517

Activity

  1. added
    bug
    on Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions