Skip to content

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

fix: sync navigation tree with document status (#19653)

buua4360a1771b2f46b
diff --git a/internal/ingestion/knowledge_compile/consumer.go b/internal/ingestion/knowledge_compile/consumer.go
index 5ae0839f2d..01395b6858 100644
--- a/internal/ingestion/knowledge_compile/consumer.go
+++ b/internal/ingestion/knowledge_compile/consumer.go
@@ -697,6 +697,13 @@ func (c *Consumer) processBatch(ctx context.Context, tenant, kb, token string, e
}
return err
}
+ if err := c.removeNavLocked(ctx, tenant, kb, token, delIDs); err != nil {
+ if errors.Is(err, errClaimSuperseded) {
+ common.Info("knowledge_compile: batch stale before navigation removal, aborting (rewrite barrier)",
+ zap.String("dataset_id", kb))
+ }
+ return err
+ }
}
if len(completed) == 0 && len(wikiDiff.affectedKeys) == 0 {
if err := c.commitWikiContributions(ctx, tenant, kb, wikiDiff.currentByDoc, retractedDocIDs); err != nil {
@@ -1804,3 +1811,25 @@ func (c *Consumer) upsertNavLocked(ctx context.Context, tenant, kb, token string
return nil
})
}
+
+// removeNavLocked removes the retracted documents from the dataset navigation
+// tree under the same claim-fenced write lock used by navigation upserts. The
+// document-level tree/structure products remain in storage for a later enable;
+// only their dataset-level navigation projections are removed here.
+func (c *Consumer) removeNavLocked(ctx context.Context, tenant, kb, token string, docIDs []string) error {
+ if len(docIDs) == 0 {
+ return nil
+ }
+ ns := nav.GetNavService()
+ if ns == nil {
+ return fmt.Errorf("knowledge_compile: nav service unavailable while removing documents")
+ }
+ return c.withWriteLock(ctx, kb, token, func() error {
+ for _, docID := range docIDs {
+ if err := ns.RemoveDoc(ctx, tenant, kb, docID); err != nil {
+ return fmt.Errorf("knowledge_compile: nav remove %s: %w", docID, err)
+ }
+ }
+ return nil
+ })
+}
diff --git a/internal/ingestion/knowledge_compile/nav_lifecycle_test.go b/internal/ingestion/knowledge_compile/nav_lifecycle_test.go
new file mode 100644
index 0000000000..3ab6001dc4
--- /dev/null
+++ b/internal/ingestion/knowledge_compile/nav_lifecycle_test.go
@@ -0,0 +1,142 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package knowledge_compile
+
+import (
+ "context"
+ "testing"
+
+ kccommon "ragflow/internal/ingestion/component/knowledge_compiler/common"
+ "ragflow/internal/service/nav"
+)
+
+type recordingNavService struct {
+ removed []string
+ upserted []nav.UpsertDocInput
+ removeErr error
+}
+
+func (f *recordingNavService) UpsertDoc(_ context.Context, input nav.UpsertDocInput) error {
+ f.upserted = append(f.upserted, input)
+ return nil
+}
+
+func (f *recordingNavService) RemoveDoc(_ context.Context, _, _, docID string) error {
+ if f.removeErr != nil {
+ return f.removeErr
+ }
+ f.removed = append(f.removed, docID)
+ return nil
+}
+
+func (f *recordingNavService) Search(context.Context, string, string, string, []float32, []string, int) ([]nav.NavHit, error) {
+ return nil, nil
+}
+
+func (f *recordingNavService) ListClusters(context.Context, string, string, int, int) ([]nav.NavNode, int64, error) {
+ return nil, 0, nil
+}
+
+func (f *recordingNavService) ListChildren(context.Context, string, string, string, int, int) ([]nav.NavNode, int64, error) {
+ return nil, 0, nil
+}
+
+func (f *recordingNavService) SummariesByDocIDs(context.Context, string, string, []string) map[string]string {
+ return nil
+}
+
+func TestProcessBatchRemovesRetractedDocumentsFromNavigation(t *testing.T) {
+ previous := nav.GetNavService()
+ recorder := &recordingNavService{}
+ nav.SetNavService(recorder)
+ defer nav.SetNavService(previous)
+
+ consumer := NewConsumer(
+ NewFakeScheduler(),
+ WithReader(&fakeReader{}),
+ WithWriter(&fakeWriter{}),
+ withWikiContributionStore(&memoryWikiContributionStore{items: map[string]wikiDocumentContribution{}}),
+ )
+
+ err := consumer.processBatch(context.Background(), "tenant-1", "kb-1", "", []BacklogEntry{
+ {DocID: "doc-disabled", EventType: string(EventTypeDisabled)},
+ {DocID: "doc-deleted", EventType: string(EventTypeDeleted)},
+ }, nil)
+ if err != nil {
+ t.Fatalf("processBatch failed: %v", err)
+ }
+ if len(recorder.removed) != 2 {
+ t.Fatalf("removed navigation docs = %v, want two documents", recorder.removed)
+ }
+ if recorder.removed[0] != "doc-deleted" || recorder.removed[1] != "doc-disabled" {
+ t.Fatalf("removed navigation docs = %v, want sorted document ids", recorder.removed)
+ }
+}
+
+func TestProcessBatchRetriesWhenNavigationRemovalFails(t *testing.T) {
+ previous := nav.GetNavService()
+ recorder := &recordingNavService{removeErr: context.Canceled}
+ nav.SetNavService(recorder)
+ defer nav.SetNavService(previous)
+
+ consumer := NewConsumer(
+ NewFakeScheduler(),
+ WithReader(&fakeReader{}),
+ WithWriter(&fakeWriter{}),
+ withWikiContributionStore(&memoryWikiContributionStore{items: map[string]wikiDocumentContribution{}}),
+ )
+
+ err := consumer.processBatch(context.Background(), "tenant-1", "kb-1", "", []BacklogEntry{
+ {DocID: "doc-disabled", EventType: string(EventTypeDisabled)},
+ }, nil)
+ if err == nil {
+ t.Fatal("processBatch should fail when navigation removal fails so the batch can be retried")
+ }
+}
+
+func TestProcessBatchReaddsEnabledDocumentToNavigation(t *testing.T) {
+ previous := nav.GetNavService()
+ recorder := &recordingNavService{}
+ nav.SetNavService(recorder)
+ defer nav.SetNavService(previous)
+
+ consumer := NewConsumer(
+ NewFakeScheduler(),
+ WithReader(&fakeReader{products: []kccommon.Product{{
+ DocID: "doc-enabled",
+ TenantID: "tenant-1",
+ Variant: kccommon.VariantTree,
+ Content: "enabled document summary",
+ Meta: map[string]any{"kind": "root"},
+ }}}),
+ WithWriter(&fakeWriter{}),
+ WithDeduperFactory(func(string) (Deduper, error) { return NewNoopDeduper(), nil }),
+ withWikiContributionStore(&memoryWikiContributionStore{items: map[string]wikiDocumentContribution{}}),
+ )
+
+ err := consumer.processBatch(context.Background(), "tenant-1", "kb-1", "", []BacklogEntry{
+ {DocID: "doc-enabled", EventType: string(EventTypeEnabled), Variants: []string{string(kccommon.VariantTree)}},
+ }, nil)
+ if err != nil {
+ t.Fatalf("processBatch failed: %v", err)
+ }
+ if len(recorder.upserted) != 1 {
+ t.Fatalf("upserted navigation docs = %d, want 1", len(recorder.upserted))
+ }
+ if recorder.upserted[0].DocID != "doc-enabled" || recorder.upserted[0].Summary != "enabled document summary" {
+ t.Fatalf("upserted navigation input = %+v, want enabled document summary", recorder.upserted[0])
+ }
+}
diff --git a/internal/service/nav/nav.go b/internal/service/nav/nav.go
index c24150624c..ff1405cf86 100644
--- a/internal/service/nav/nav.go
+++ b/internal/service/nav/nav.go
@@ -84,9 +84,8 @@ type NavService interface {
// UpsertDoc places one document summary into the nav tree (incremental,
// ES-backed read-modify-write; deterministic placement in the minimal loop).
UpsertDoc(ctx context.Context, in UpsertDocInput) error
- // RemoveDoc removes a document's nav rows for the given doc. The minimal-loop
- // implementation deletes the nav_doc row(s) for the doc; empty-cluster
- // cascade cleanup is NOT yet implemented.
+ // RemoveDoc removes a document's nav rows for the given doc and prunes empty
+ // clusters from the dataset navigation tree.
RemoveDoc(ctx context.Context, tenantID, kbID, docID string) error
// Search runs query KNN over nav rows and returns the routed doc ids.