Skip to content

fix(core): validate topN in cohere and voyage rerankers (#582) - #589

Merged
anurag629 merged 3 commits into
codercops:devfrom
bhushan4work:582
Nov 6, 2026
Merged

anurag629 merged 3 commits into
codercops:devfrom
bhushan4work:582

Conversation

@bhushan4work

Copy link
Copy Markdown
Collaborator

What does this change?

this pr adds validation for topN in the cohere and voyage rerankers. invalid or non finite values such as NaN, Infinity and -Infinity now return an empty result without sending an outbound request.

closes #582

How I tested this

  • ran pnpm vitest run packages/core/tests/retrieval/cohere-reranker.test.ts packages/core/tests/retrieval/voyage-reranker.test.ts
  • ran pnpm vitest run packages/core/tests/retrieval/workers-ai-reranker.test.ts
  • ran pnpm typecheck
  • added tests verifying that non finite topN values resolve to [] and that fetch is not called

AI assistance

none

Checklist

  • this pr targets dev
  • i was assigned the linked issue
  • pnpm -r build, pnpm test and pnpm -r typecheck pass locally
  • tests added or updated where it makes sense
  • for a bug fix: the new test fails without my change
  • changeset added (pnpm changeset) if this changes core, widget or server
  • pnpm api run and api/ committed if an exported type or option changed
  • docs updated if behavior or options changed

@bhushan4work

Copy link
Copy Markdown
Collaborator Author

@anurag629 ci is green now , could u plz review this pr

@anurag629 anurag629 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @bhushan4work, nearly there. The changeset is in and the NaN case is fixed, but one case gets worse than today.

Infinity now returns []. Number.isFinite(topN) at packages/core/src/retrieval/cohere-reranker.ts:26 and voyage-reranker.ts:26 turns it into 0. plan() at packages/core/src/retrieval/retrievers.ts:83 keeps topK: Infinity as it is (Math.max(1, Infinity)), and finish() passes it to rerank() at line 99, so a query that asks for every chunk gets no sources at all. On dev it sends top_n: 3 for three chunks. Use the check from my comment on #582 in both files:

const limit = Math.floor(topN);
if (chunks.length === 0 || !(limit > 0)) return [];

That still returns [] for NaN and -Infinity. In the new tests at packages/core/tests/retrieval/cohere-reranker.test.ts:48-55 and voyage-reranker.test.ts:48-55, keep the NaN and -Infinity lines and replace the Infinity one with a case that checks the request body has top_n: 3 (top_k: 3 for Voyage), like the test at line 33. Then change "non-finite or invalid inputs" in the changeset to NaN, since Infinity should keep working.

typecheck-test-build, e2e and changeset pass. I'll review it again when you push.

@anurag629 anurag629 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @bhushan4work. Infinity asks for every chunk again (top_n: 3 for three, covered by the new tests at packages/core/tests/retrieval/cohere-reranker.test.ts:56 and voyage-reranker.test.ts:56), and NaN and -Infinity still return [] without sending a request. All checks pass, including e2e.

@anurag629
anurag629 merged commit 32c87af into codercops:dev Nov 6, 2026
5 checks passed
@github-actions github-actions Bot added the codercops-accepted Merged PR that counts on the monthly leaderboard (oss.codercops.com) label Nov 6, 2026
@anurag629

Copy link
Copy Markdown
Member

Merged, thanks @bhushan4work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

codercops-accepted Merged PR that counts on the monthly leaderboard (oss.codercops.com)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(core): Cohere and Voyage rerankers bypass non-positive limit check on NaN topN and send invalid null payload

2 participants