Skip to content

fix: harden LLM rerank fallback for reasoning models - #2711

Open
guiyangyuan wants to merge 2 commits into
Tencent:mainfrom
guiyangyuan:fix/llm-rerank-truncation
Open

fix: harden LLM rerank fallback for reasoning models#2711
guiyangyuan wants to merge 2 commits into
Tencent:mainfrom
guiyangyuan:fix/llm-rerank-truncation

Conversation

@guiyangyuan

@guiyangyuan guiyangyuan commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Description

Harden the knowledge search LLM reranker when a reasoning-capable model exhausts its output budget or returns a partial structured score list.

  • disable model thinking for the deterministic scoring call and reserve output tokens for providers that ignore the option
  • require the response to contain exactly one score per passage instead of padding or truncating partial output
  • stop subsequent rerank batches after incomplete output and fall back to the original search scores for all remaining results
  • add regression coverage for token exhaustion and incomplete score lists

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

  • 🐛 Bug fix
  • ✨ New feature
  • 💥 Breaking change
  • 📚 Documentation update
  • 🎨 Refactor
  • ⚡ Performance improvement
  • 🧪 Test
  • 🔧 Configuration / Build / CI

Related Issue

Partially addresses #2703.

Testing

  • go test ./internal/agent/tools -run 'TestRerankChatStubStreamIsClosed|TestRerankWithLLMStopsAfterIncompleteOutput|TestParseScoresFromResponseRequiresExactCount|TestRerankThreshold_default|TestFilterRerankRankResults|TestApplyModelRerankScores' -count=1
  • go test -race ./internal/agent/tools -run 'TestRerankChatStubStreamIsClosed|TestRerankWithLLMStopsAfterIncompleteOutput|TestParseScoresFromResponseRequiresExactCount' -count=1
  • go vet ./internal/agent/tools
  • git diff --check upstream/main...HEAD

The full go test ./internal/agent/tools -count=1 command compiled but did not finish in the local environment after more than two minutes and was interrupted without test failure output. golangci-lint was not available locally.

Checklist

  • git diff --check origin/main...HEAD passes
  • Changed source files are formatted
  • Targeted tests for the changed packages/components pass
  • Diff-scoped lint passes where applicable (for Go: golangci-lint run --new-from-rev=origin/main ./...)
  • Full-repository checks were run, or any unrelated/environment-dependent failures are documented above
  • Self-reviewed the code
  • Added/updated tests covering the change
  • Updated related documentation (not applicable)
  • Breaking changes are clearly called out (none)

Screenshots / Recordings

Not applicable; this is a backend-only change.

@guiyangyuan
guiyangyuan marked this pull request as ready for review August 14, 2026 07:19
Copilot AI lite review requested due to automatic review settings August 14, 2026 07:19

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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_tokens with 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants