RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix: filter self-loop knowledge compiler relations (#19721)
buua436bc9b29b6a0b5
diff --git a/internal/ingestion/component/knowledge_compiler/mindmap/mindmap.go b/internal/ingestion/component/knowledge_compiler/mindmap/mindmap.goindex 30c5b5a876..58e58d12b2 100644--- a/internal/ingestion/component/knowledge_compiler/mindmap/mindmap.go+++ b/internal/ingestion/component/knowledge_compiler/mindmap/mindmap.go@@ -212,7 +212,7 @@ func treeToProducts(tenantID, docID string, root *utility.Node, fallbackSourceChp := queue[0]queue = queue[1:]for _, child := range p.node.Children {- if child.ID == "" {+ if child == nil || child.ID == "" {continue}// Entity: child node (dedup by id so a DAG-shaped tree does not emit@@ -241,24 +241,28 @@ func treeToProducts(tenantID, docID string, root *utility.Node, fallbackSourceCh})}// Relation: parent → child edge (type = "related", Python default).- out = append(out, common.Product{- ID: common.StableRowID(tenantID, docID, string(common.VariantMindmap), "relation", p.parent, child.ID),- DocID: docID,- TenantID: tenantID,- Variant: common.VariantMindmap,- Content: payloadJSON(map[string]any{- "source": p.parent,- "target": child.ID,- "type": "related",- }),- Meta: map[string]any{- "kind": "relation",- "from": p.parent,- "to": child.ID,- "relation_type": "related",- "compile_kwd": "mindmap",- },- })+ // A node may be repeated as the synthetic root when batch roots are+ // merged; do not persist that self-loop.+ if p.parent != child.ID {+ out = append(out, common.Product{+ ID: common.StableRowID(tenantID, docID, string(common.VariantMindmap), "relation", p.parent, child.ID),+ DocID: docID,+ TenantID: tenantID,+ Variant: common.VariantMindmap,+ Content: payloadJSON(map[string]any{+ "source": p.parent,+ "target": child.ID,+ "type": "related",+ }),+ Meta: map[string]any{+ "kind": "relation",+ "from": p.parent,+ "to": child.ID,+ "relation_type": "related",+ "compile_kwd": "mindmap",+ },+ })+ }queue = append(queue, pending{child, child.ID})}}diff --git a/internal/ingestion/component/knowledge_compiler/mindmap/mindmap_test.go b/internal/ingestion/component/knowledge_compiler/mindmap/mindmap_test.goindex 5027a49eeb..b9dbacca9c 100644--- a/internal/ingestion/component/knowledge_compiler/mindmap/mindmap_test.go+++ b/internal/ingestion/component/knowledge_compiler/mindmap/mindmap_test.go@@ -116,6 +116,23 @@ func TestTreeToProducts_EmptyAndNil(t *testing.T) {}}+func TestTreeToProducts_SkipsSelfLoop(t *testing.T) {+ root := &utility.Node{ID: "root", Children: []*utility.Node{+ {ID: "root"},+ {ID: "child"},+ }}+ products := treeToProducts("t1", "d1", root, nil)++ if len(products) != 3 {+ t.Fatalf("products = %d, want root entity, child entity, and one relation", len(products))+ }+ for _, product := range products {+ if product.Meta["kind"] == "relation" && product.Meta["from"] == product.Meta["to"] {+ t.Fatalf("self-loop relation was emitted: %+v", product.Meta)+ }+ }+}+func TestParseJSONTree_SourceChunkIDs(t *testing.T) {content := `{"id":"root","source_chunk_ids":["c1","outside"],"children":[{"id":"child","source_chunk_ids":["c2"],"children":[]}]}`root, ok := parseJSONTree(content, []string{"c1", "c2"})diff --git a/internal/ingestion/component/knowledge_compiler/structure/compile.go b/internal/ingestion/component/knowledge_compiler/structure/compile.goindex f6a01dde29..b13b0dde3f 100644--- a/internal/ingestion/component/knowledge_compiler/structure/compile.go+++ b/internal/ingestion/component/knowledge_compiler/structure/compile.go@@ -220,6 +220,9 @@ func buildRows(ctx context.Context, deps common.Deps, cfg CompileConfig, nodes,specs = append(specs, spec{kind: "entity", payload: p})}for _, p := range edges {+ if isSelfLoopRelation(p, srcField, tgtField) {+ continue+ }specs = append(specs, spec{kind: "relation", payload: p})}if len(specs) == 0 {@@ -289,6 +292,40 @@ func buildRows(ctx context.Context, deps common.Deps, cfg CompileConfig, nodes,return rows, nil}+func isSelfLoopRelation(payload map[string]any, sourceField, targetField string) bool {+ from := relationEndpoint(payload, sourceField, "source", "src", "from")+ to := relationEndpoint(payload, targetField, "target", "tgt", "to")+ return isSelfLoop(from, to)+}++func isSelfLoop(from, to string) bool {+ from, to = strings.TrimSpace(from), strings.TrimSpace(to)+ return from != "" && from == to+}++func filterSelfLoopRelations(rows []common.Product) []common.Product {+ filtered := make([]common.Product, 0, len(rows))+ for _, row := range rows {+ if kind, _ := row.Meta["kind"].(string); kind == "relation" {+ from, _ := row.Meta["from"].(string)+ to, _ := row.Meta["to"].(string)+ if payload := parsePayload(row.Content); payload != nil {+ if payloadFrom := relationEndpoint(payload, "", "source", "src", "from"); payloadFrom != "" {+ from = payloadFrom+ }+ if payloadTo := relationEndpoint(payload, "", "target", "tgt", "to"); payloadTo != "" {+ to = payloadTo+ }+ }+ if isSelfLoop(from, to) {+ continue+ }+ }+ filtered = append(filtered, row)+ }+ return filtered+}+// entityName mirrors _struct_graph_entity's name resolution// (name → text → term → title, "-1" sentinel rejected).func entityName(payload map[string]any) string {diff --git a/internal/ingestion/component/knowledge_compiler/structure/graph.go b/internal/ingestion/component/knowledge_compiler/structure/graph.goindex fdb8751fbc..460ef77da7 100644--- a/internal/ingestion/component/knowledge_compiler/structure/graph.go+++ b/internal/ingestion/component/knowledge_compiler/structure/graph.go@@ -83,6 +83,9 @@ func graphRelation(payload map[string]any) map[string]any {if src == "" || tgt == "" {return nil}+ if src == tgt {+ return nil+ }typ := strings.TrimSpace(stringOf(payload["type"]))if typ == "" {typ = "related"diff --git a/internal/ingestion/component/knowledge_compiler/structure/structure.go b/internal/ingestion/component/knowledge_compiler/structure/structure.goindex dbfcc7c811..32e56e6f37 100644--- a/internal/ingestion/component/knowledge_compiler/structure/structure.go+++ b/internal/ingestion/component/knowledge_compiler/structure/structure.go@@ -223,7 +223,7 @@ func Run(ctx context.Context, deps common.Deps, param common.Param, inputs commoreturn common.Outputs{}, err}stats := deduper.Stats()- prods := deduper.Rows()+ prods := filterSelfLoopRelations(deduper.Rows())runtime.ReportProgressMessage(ctx, "Compiler", fmt.Sprintf("%s-template: dedup done: %d row(s), %d duplicate(s) dropped",compileType, len(prods), stats.DuplicatesDropped))diff --git a/internal/ingestion/component/knowledge_compiler/structure/structure_test.go b/internal/ingestion/component/knowledge_compiler/structure/structure_test.goindex 5c26cc380c..23ceb0ce9a 100644--- a/internal/ingestion/component/knowledge_compiler/structure/structure_test.go+++ b/internal/ingestion/component/knowledge_compiler/structure/structure_test.go@@ -557,6 +557,34 @@ func TestStructureAliasRewrite(t *testing.T) {}}+func TestStructureSkipsSelfLoopRelations(t *testing.T) {+ deps := common.Deps{Embed: hashEmbedder{dim: 4}}+ cfg := CompileConfig{+ TenantID: "t1",+ DocID: "d1",+ Type: TypeHypergraph,+ ParserConfig: graphParserConfig(),+ }+ rows, err := buildRows(context.Background(), deps, cfg, nil, []map[string]any{+ {"type": "linked", "source": "Root", "target": "Root"},+ {"type": "linked", "source": "Root", "target": "Child"},+ }, []string{"c1"})+ if err != nil {+ t.Fatalf("buildRows: %v", err)+ }+ if len(rows) != 1 {+ t.Fatalf("rows = %d, want only the non-self relation", len(rows))+ }++ filtered := filterSelfLoopRelations(append(rows, common.Product{+ Content: payloadJSON(map[string]any{"source": "Child", "target": "Child", "type": "linked"}),+ Meta: map[string]any{"kind": "relation", "from": "Child", "to": "Child"},+ }))+ if len(filtered) != 1 {+ t.Fatalf("filtered rows = %d, want self-loop removed", len(filtered))+ }+}+// ---- merge unit tests ----func TestLLMMergeDeciderContracts(t *testing.T) {