Skip to content

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

fix: prevent bulk overflow and preserve mindmap results (#19892)

buua436999951f5a3a1
diff --git a/internal/ingestion/component/knowledge_compiler/component_test.go b/internal/ingestion/component/knowledge_compiler/component_test.go
index d23289762b..ea7d31905f 100644
--- a/internal/ingestion/component/knowledge_compiler/component_test.go
+++ b/internal/ingestion/component/knowledge_compiler/component_test.go
@@ -131,6 +131,9 @@ type proseChat struct{}
func (proseChat) Chat(_ context.Context, req common.ChatRequest) (*common.ChatResponse, error) {
if req.JSONMode {
+ if strings.Contains(req.SystemPrompt, "into a mind map") {
+ return &common.ChatResponse{Content: `{"id":"mindmap root","source_chunk_ids":["c1"],"children":[]}`}, nil
+ }
return &common.ChatResponse{Content: `{"ok":true}`}, nil
}
// Echo a deterministic prose reply derived from the prompt.
diff --git a/internal/ingestion/component/knowledge_compiler/mindmap/mindmap.go b/internal/ingestion/component/knowledge_compiler/mindmap/mindmap.go
index 58e58d12b2..2376bb2949 100644
--- a/internal/ingestion/component/knowledge_compiler/mindmap/mindmap.go
+++ b/internal/ingestion/component/knowledge_compiler/mindmap/mindmap.go
@@ -16,6 +16,7 @@ import (
"fmt"
"strings"
+ "ragflow/internal/agent/runtime"
"ragflow/internal/ingestion/component/knowledge_compiler/common"
"ragflow/internal/utility"
)
@@ -72,7 +73,7 @@ func Run(ctx context.Context, deps common.Deps, param common.Param, inputs commo
for i, batch := range batches {
i, batch := i, batch
jobs = append(jobs, func() error {
- var lastContent string
+ var lastParseErr error
for attempt := 0; attempt < mindmapJSONRetryMax; attempt++ {
resp, err := deps.Chat.Chat(ctx, common.ChatRequest{
LLMID: llmID,
@@ -82,18 +83,22 @@ func Run(ctx context.Context, deps common.Deps, param common.Param, inputs commo
DisableThinking: true,
})
if err != nil {
+ runtime.ReportProgressMessage(ctx, "Compiler", fmt.Sprintf(
+ "[ERROR] Mindmap batch %d/%d LLM call failed: %s",
+ i+1, len(batches), common.CompactError(err)))
return err
}
- lastContent = resp.Content
// Distinct slice index per batch → no cross-goroutine contention.
- if tree, ok := parseJSONTree(resp.Content, batch.ids); ok {
+ if tree, parseErr := parseJSONTreeWithError(resp.Content, batch.ids); parseErr == nil {
results[i].tree = tree
return nil
+ } else {
+ lastParseErr = parseErr
}
}
- // Keep the old Markdown protocol as a compatibility fallback after
- // exhausting JSON retries.
- results[i].outline = utility.Todict(utility.Dictify(utility.StripFences(lastContent)))
+ runtime.ReportProgressMessage(ctx, "Compiler", fmt.Sprintf(
+ "[ERROR] Mindmap batch %d/%d JSON parsing failed after %d attempts: %s",
+ i+1, len(batches), mindmapJSONRetryMax, common.CompactError(lastParseErr)))
return nil
})
}
@@ -104,31 +109,9 @@ func Run(ctx context.Context, deps common.Deps, param common.Param, inputs commo
return common.Outputs{}, err
}
- // Merge batch dicts in batch order (mirrors reduce(self._merge, res)) and
- // shape the final tree. Python returns a bare root when nothing parsed.
- var root *utility.Node
- allJSON := len(results) > 0
- for _, result := range results {
- if result.tree == nil {
- allJSON = false
- break
- }
- root = mergeMindmapTrees(root, result.tree)
- }
- if !allJSON {
- var merged utility.OMap
- for _, result := range results {
- if len(result.outline) == 0 {
- continue
- }
- if len(merged) == 0 {
- merged = result.outline
- } else {
- merged = utility.MergeDicts(merged, result.outline)
- }
- }
- root = utility.ShapeTree(merged)
- }
+ // Merge every successfully parsed JSON tree. A failed batch is skipped after
+ // its user-facing error is reported and must not erase other batches.
+ root := mergeMindmapBatchResults(results)
products := treeToProducts(tenantID, docID, root, mindmapSourceChunkIDs(inputs.Chunks))
@@ -296,8 +279,7 @@ func mindmapSourceChunkIDs(chunks []common.Chunk) []string {
}
type mindmapBatchResult struct {
- tree *utility.Node
- outline utility.OMap
+ tree *utility.Node
}
type jsonMindmapNode struct {
@@ -308,21 +290,26 @@ type jsonMindmapNode struct {
}
func parseJSONTree(content string, batchIDs []string) (*utility.Node, bool) {
+ tree, err := parseJSONTreeWithError(content, batchIDs)
+ return tree, err == nil
+}
+
+func parseJSONTreeWithError(content string, batchIDs []string) (*utility.Node, error) {
content, err := common.RepairJSONText(content)
if err != nil {
- return nil, false
+ return nil, err
}
var raw jsonMindmapNode
if err := json.Unmarshal([]byte(content), &raw); err != nil {
- return nil, false
+ return nil, err
}
if strings.TrimSpace(raw.ID) == "" {
raw.ID = raw.Name
}
if strings.TrimSpace(raw.ID) == "" {
- return nil, false
+ return nil, fmt.Errorf("mindmap root id is empty")
}
- return convertJSONMindmapNode(raw, batchIDs), true
+ return convertJSONMindmapNode(raw, batchIDs), nil
}
func convertJSONMindmapNode(raw jsonMindmapNode, batchIDs []string) *utility.Node {
@@ -394,6 +381,16 @@ func mergeMindmapTrees(left, right *utility.Node) *utility.Node {
return left
}
+func mergeMindmapBatchResults(results []mindmapBatchResult) *utility.Node {
+ var root *utility.Node
+ for _, result := range results {
+ if result.tree != nil {
+ root = mergeMindmapTrees(root, result.tree)
+ }
+ }
+ return root
+}
+
func mergeMindmapIDs(left, right []string) []string {
seen := make(map[string]struct{}, len(left)+len(right))
merged := make([]string, 0, len(left)+len(right))
diff --git a/internal/ingestion/component/knowledge_compiler/mindmap/mindmap_test.go b/internal/ingestion/component/knowledge_compiler/mindmap/mindmap_test.go
index b9dbacca9c..7afc3944b5 100644
--- a/internal/ingestion/component/knowledge_compiler/mindmap/mindmap_test.go
+++ b/internal/ingestion/component/knowledge_compiler/mindmap/mindmap_test.go
@@ -7,6 +7,7 @@ import (
"strings"
"testing"
+ "ragflow/internal/agent/runtime"
"ragflow/internal/ingestion/component/knowledge_compiler/common"
"ragflow/internal/utility"
)
@@ -152,6 +153,26 @@ func TestParseJSONTree_SourceChunkIDs(t *testing.T) {
}
}
+func TestMergeMindmapBatchResultsKeepsSuccessfulTrees(t *testing.T) {
+ root := mergeMindmapBatchResults([]mindmapBatchResult{
+ {tree: &utility.Node{ID: "root", Children: []*utility.Node{{ID: "child"}}}},
+ {},
+ })
+ if root == nil || len(root.Children) != 1 || root.Children[0].ID != "child" {
+ t.Fatalf("merged tree = %+v, want successful tree with child", root)
+ }
+}
+
+func TestMergeMindmapTreesDoesNotDuplicateChildren(t *testing.T) {
+ root := mergeMindmapTrees(
+ &utility.Node{ID: "root", Children: []*utility.Node{{ID: "first"}}},
+ &utility.Node{ID: "root", Children: []*utility.Node{{ID: "second"}}},
+ )
+ if root == nil || len(root.Children) != 2 {
+ t.Fatalf("merged children = %+v, want 2 unique children", root.Children)
+ }
+}
+
type retryChat struct {
calls int
}
@@ -186,3 +207,30 @@ func TestRunRetriesInvalidJSON(t *testing.T) {
t.Fatalf("products = %d, want root, child, and relation", len(outputs.Products))
}
}
+
+func TestRunReportsJSONParseFailure(t *testing.T) {
+ var messages []string
+ ctx := runtime.WithProgressMessageCallback(t.Context(), func(_, message string) {
+ messages = append(messages, message)
+ })
+ outputs, err := Run(ctx, common.Deps{
+ Chat: invalidJSONChat{},
+ }, common.Param{}, common.Inputs{
+ Chunks: []common.Chunk{{ID: "c1", Text: "source text"}},
+ })
+ if err != nil {
+ t.Fatalf("Run returned error: %v", err)
+ }
+ if len(outputs.Products) != 0 {
+ t.Fatalf("products = %d, want no products after parse failure", len(outputs.Products))
+ }
+ if len(messages) != 1 || !strings.Contains(messages[0], "[ERROR] Mindmap batch 1/1 JSON parsing failed") {
+ t.Fatalf("progress messages = %v, want one user-facing parse error", messages)
+ }
+}
+
+type invalidJSONChat struct{}
+
+func (invalidJSONChat) Chat(context.Context, common.ChatRequest) (*common.ChatResponse, error) {
+ return &common.ChatResponse{Content: "The request was rejected because it was considered high risk"}, nil
+}
diff --git a/internal/ingestion/service/ingestion_service.go b/internal/ingestion/service/ingestion_service.go
index 8a5805b24e..d48252818e 100644
--- a/internal/ingestion/service/ingestion_service.go
+++ b/internal/ingestion/service/ingestion_service.go
@@ -1150,6 +1150,8 @@ func (e *Ingestor) releaseTask(taskID string) {
}
func (e *Ingestor) defaultRunDocumentTask(ctx context.Context, ingestionTask *entity.IngestionTask) error {
+ const documentBulkSize = 32
+
docTaskCtx, err := taskpkg.LoadFromIngestionTask(ctx, ingestionTask)
if err != nil {
return fmt.Errorf("load task context for %s: %w", ingestionTask.ID, err)
@@ -1170,7 +1172,11 @@ func (e *Ingestor) defaultRunDocumentTask(ctx context.Context, ingestionTask *en
// The sink owns all document/ingestion_task_log/ingestion_task.component_total
// writes for this run; inject it into the executor so the pipeline reports
// progress to the service layer instead of touching the DAO directly.
- executor, err := taskpkg.NewPipelineExecutor(docTaskCtx, pipelineID, 0)
+ // Keep the final document write bounded. Compiler products can contain large
+ // text, source-chunk references, images, or vectors, so sending the whole
+ // document through one Elasticsearch bulk request can exceed the request
+ // size limit even when the individual chunks are valid.
+ executor, err := taskpkg.NewPipelineExecutor(docTaskCtx, pipelineID, documentBulkSize)
if err != nil {
return err
}