Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(agent): clamp dataset navigation max_docs to the default budget (#21755)

cielb6b4531915b5
diff --git a/internal/agent/tool/dataset_navigation.go b/internal/agent/tool/dataset_navigation.go
index 824ee5121d..4391c33a4a 100644
--- a/internal/agent/tool/dataset_navigation.go
+++ b/internal/agent/tool/dataset_navigation.go
@@ -133,9 +133,11 @@ func (d *DatasetNavigationByTree) InvokableRun(ctx context.Context, argumentsInJ
if args.Topic == "" {
return "", fmt.Errorf("dataset_navigation: topic is required")
}
- // Per-request max_docs overrides the node default; default to a sane cap.
+ // Per-request max_docs may narrow the budget; it never raises it. The model's
+ // argument JSON is not schema-validated, so the ceiling must be enforced here,
+ // after mergeDefaults, to cover both the request value and the node default.
maxDocs := args.MaxDocs
- if maxDocs <= 0 {
+ if maxDocs <= 0 || maxDocs > datasetNavigationDefaultMaxDocs {
maxDocs = datasetNavigationDefaultMaxDocs
}
diff --git a/internal/agent/tool/dataset_navigation_test.go b/internal/agent/tool/dataset_navigation_test.go
index c8760a7223..577c463a33 100644
--- a/internal/agent/tool/dataset_navigation_test.go
+++ b/internal/agent/tool/dataset_navigation_test.go
@@ -3,6 +3,7 @@ package tool
import (
"context"
"encoding/json"
+ "fmt"
"sync"
"testing"
@@ -262,6 +263,42 @@ func TestDatasetNavigation_BlankRequestScopeFallsBackToDefaults(t *testing.T) {
}
}
+// TestDatasetNavigation_MaxDocsCannotExceedCap pins the ceiling on the
+// doc-count budget: the model's argument JSON is not schema-validated, so a
+// max_docs above the cap must be clamped down to it, while a smaller value still
+// narrows the result.
+func TestDatasetNavigation_MaxDocsCannotExceedCap(t *testing.T) {
+ hits := make([]nav.NavHit, 0, 20)
+ for i := 0; i < 20; i++ {
+ hits = append(hits, nav.NavHit{Type: nav.TypeNavDoc, DocID: fmt.Sprintf("d%02d", i)})
+ }
+ cases := []struct {
+ name string
+ args string
+ want int
+ }{
+ {"above the cap", `{"topic":"X","dataset_ids":["kb1"],"max_docs":5000}`, datasetNavigationDefaultMaxDocs},
+ {"below the cap", `{"topic":"X","dataset_ids":["kb1"],"max_docs":2}`, 2},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ fake := &navRoutingFake{hits: hits}
+ prev := nav.GetNavService()
+ nav.SetNavService(fake)
+ defer func() { nav.SetNavService(prev) }()
+
+ tool := NewDatasetNavigationByTree()
+ out, err := tool.InvokableRun(navTestContext(t), tc.args)
+ if err != nil {
+ t.Fatalf("InvokableRun: %v", err)
+ }
+ if docs := decodeNavDocs(t, out); len(docs) != tc.want {
+ t.Errorf("docs = %d, want %d", len(docs), tc.want)
+ }
+ })
+ }
+}
+
// TestCanvasTenantID_FromCanvasState asserts the tenant id resolves from canvas
// state, falling back to user_id.
func TestCanvasTenantID_FromCanvasState(t *testing.T) {