Skip to content

Commit 4bcb004

Browse files
author
razvan
committed
feat: add min_score parameter with auto-threshold to rag_search
- New optional 'min_score' parameter (0.0-1.0) filters low-relevance results - Auto-threshold: when top score > 0.70 and min_score not specified, results below 40% of top score are automatically pruned - Documented in tool description for AI agent discovery - Only applied to rag_search (other tools are deterministic/AST-based)
1 parent 5e617e5 commit 4bcb004

File tree

1 file changed

+40
-7
lines changed

1 file changed

+40
-7
lines changed

internal/service/tools/smart_search.go

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,19 @@ func (t *SmartSearchTool) Description() string {
4747
"Set 'include_docs' to true to also search project documentation (README, guides, Markdown files) alongside code. " +
4848
"Use 'mode'=\"strict_code\" when you ONLY want to see implementation logic exactly (Go, Python, etc) and strictly ignore documentation. " +
4949
"Use 'mode'=\"strict_docs\" when searching for architectural plans or summaries. " +
50-
"Use 'mode'=\"all\" or omit for broad scans."
50+
"Use 'mode'=\"all\" or omit for broad scans. " +
51+
"Set 'min_score' (0.0-1.0) to filter out low-relevance results. When omitted, an automatic threshold is applied: " +
52+
"if the top result scores above 0.70, results below 40% of the top score are automatically pruned."
5153
}
5254

5355
type SmartSearchInput struct {
54-
Query string `json:"query"`
55-
FilePath string `json:"file_path,omitempty"`
56-
Limit int `json:"limit,omitempty"`
57-
IncludeFullContent bool `json:"include_full_content,omitempty"`
58-
IncludeDocs bool `json:"include_docs,omitempty"`
59-
Mode string `json:"mode,omitempty"`
56+
Query string `json:"query"`
57+
FilePath string `json:"file_path,omitempty"`
58+
Limit int `json:"limit,omitempty"`
59+
MinScore float32 `json:"min_score,omitempty"`
60+
IncludeFullContent bool `json:"include_full_content,omitempty"`
61+
IncludeDocs bool `json:"include_docs,omitempty"`
62+
Mode string `json:"mode,omitempty"`
6063
}
6164

6265
// highConfidenceThreshold: if top result score exceeds this, return full content.
@@ -65,6 +68,12 @@ const highConfidenceThreshold = 0.85
6568
// compactResultCap: max results above which we always go compact.
6669
const compactResultCap = 4
6770

71+
// autoScoreThresholdTrigger: if top score exceeds this, apply auto-filtering.
72+
const autoScoreThresholdTrigger = 0.70
73+
74+
// autoScoreThresholdRatio: results below topScore * ratio are pruned.
75+
const autoScoreThresholdRatio = 0.40
76+
6877
func (t *SmartSearchTool) Register(server *mcp.Server) {
6978
mcp.AddTool(server, &mcp.Tool{
7079
Name: t.Name(),
@@ -200,6 +209,30 @@ func (t *SmartSearchTool) Execute(ctx context.Context, input SmartSearchInput) (
200209
}
201210
merged = filtered
202211

212+
// Score filtering
213+
// If min_score is specified, use it directly. Otherwise, apply auto-threshold:
214+
// when top score > 0.70, prune results below 40% of top score.
215+
if len(merged) > 0 {
216+
effectiveMinScore := input.MinScore
217+
if effectiveMinScore <= 0 && merged[0].score > autoScoreThresholdTrigger {
218+
effectiveMinScore = merged[0].score * autoScoreThresholdRatio
219+
}
220+
221+
if effectiveMinScore > 0 {
222+
var scoreFiltered []mergedResult
223+
for _, m := range merged {
224+
if m.score >= effectiveMinScore {
225+
scoreFiltered = append(scoreFiltered, m)
226+
}
227+
}
228+
nDropped := len(merged) - len(scoreFiltered)
229+
if nDropped > 0 {
230+
logger.Instance.Debug("rag_search: filtered %d results below min_score=%.2f (effective)", nDropped, effectiveMinScore)
231+
}
232+
merged = scoreFiltered
233+
}
234+
}
235+
203236
// Apply tree-based grouping for documentation chunks
204237
merged = t.groupDocsByTree(merged)
205238

0 commit comments

Comments
 (0)