fix: harden LLM rerank fallback for reasoning models - #2711
Open
guiyangyuan wants to merge 2 commits into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
This PR hardens the knowledge_search LLM-based rerank fallback path to handle reasoning-capable models that may exhaust output budgets or return incomplete score lists, ensuring the system avoids wasted latency and preserves original retrieval ordering when structured rerank output is unusable.
Changes:
- Disables model “thinking” for deterministic rerank scoring calls and increases
max_tokenswith a fixed reserve. - Requires an exact score count per batch; on incomplete/invalid output, stops further batches and falls back to original retrieval scores for remaining results.
- Adds regression tests covering token exhaustion (
finish_reason=length) and incomplete score lists.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| internal/agent/tools/knowledge_search.go | Tightens LLM rerank output validation, disables reasoning for scoring calls, and short-circuits remaining batches on incomplete output. |
| internal/agent/tools/knowledge_search_rerank_test.go | Adds regression tests to ensure rerank stops after invalid batches and enforces exact score counts. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+30
to
+36
| func (*rerankChatStub) ChatStream( | ||
| context.Context, | ||
| []chat.Message, | ||
| *chat.ChatOptions, | ||
| ) (<-chan types.StreamResponse, error) { | ||
| return nil, nil | ||
| } |
sakitam-fdd
added a commit
to sakitam-fdd/WeKnora
that referenced
this pull request
Aug 21, 2026
Port Tencent#2711 into stability Wave 1 using GitHub's three-way merge, preserving intervening main changes.
sakitam-fdd
added a commit
to sakitam-fdd/WeKnora
that referenced
this pull request
Aug 21, 2026
Port Tencent#2763 and Tencent#2711 after current-main review and fork-side validation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Harden the knowledge search LLM reranker when a reasoning-capable model exhausts its output budget or returns a partial structured score list.
This avoids spending additional LLM latency after the first unusable batch and preserves the original retrieval ranking instead of manufacturing missing scores.
Type of Change
Related Issue
Partially addresses #2703.
Testing
go test ./internal/agent/tools -run 'TestRerankChatStubStreamIsClosed|TestRerankWithLLMStopsAfterIncompleteOutput|TestParseScoresFromResponseRequiresExactCount|TestRerankThreshold_default|TestFilterRerankRankResults|TestApplyModelRerankScores' -count=1go test -race ./internal/agent/tools -run 'TestRerankChatStubStreamIsClosed|TestRerankWithLLMStopsAfterIncompleteOutput|TestParseScoresFromResponseRequiresExactCount' -count=1go vet ./internal/agent/toolsgit diff --check upstream/main...HEADThe full
go test ./internal/agent/tools -count=1command compiled but did not finish in the local environment after more than two minutes and was interrupted without test failure output.golangci-lintwas not available locally.Checklist
git diff --check origin/main...HEADpassesgolangci-lint run --new-from-rev=origin/main ./...)Screenshots / Recordings
Not applicable; this is a backend-only change.