Skip to content

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

Fix: apply disable cite (#20302)

Lynn177fd158b191
diff --git a/internal/service/chat_pipeline.go b/internal/service/chat_pipeline.go
index 2a3cb9a8b0..2f9402046d 100644
--- a/internal/service/chat_pipeline.go
+++ b/internal/service/chat_pipeline.go
@@ -3245,11 +3245,14 @@ func (s *ChatPipelineService) decorateAnswer(
}
}
- // Include sources only when citations are enabled and at least one citation
- // actually resolves to a chunk, stripping chunk vectors. Retrieved evidence
- // without a citation must not be exposed as a document reference.
- if hasKnowledges && quote && len(citationIdx) > 0 {
- refs = make(map[string]interface{})
+ // Reference assembly. `chunks` is the evidence the answer was grounded on,
+ // so it ships whenever knowledge was retrieved and independent of the cite
+ // setting; `doc_aggs` is the list of source cards the UI opens from a
+ // citation marker, so it ships only while citations are enabled — with no
+ // marker to open them from, the cards would be dead weight. Chunk vectors
+ // never leave this function.
+ if hasKnowledges {
+ refs = make(map[string]interface{}, len(kbinfos))
for k, v := range kbinfos {
refs[k] = v
}
@@ -3267,8 +3270,9 @@ func (s *ChatPipelineService) decorateAnswer(
}
refs["chunks"] = chunksFormat(newChunks)
}
- } else if !quote {
- refs = map[string]interface{}{}
+ if !quote {
+ delete(refs, "doc_aggs")
+ }
}
// Check for invalid API key errors (outside knowledges guard).
@@ -3345,6 +3349,19 @@ func (s *ChatPipelineService) decorateAnswer(
}
}
+// chunksOnlyReference is the reference of an answer whose citations were
+// disabled: the retrieved evidence, in the client-facing shape, without
+// doc_aggs. The evidence still records what the answer was grounded on; only
+// the source cards go, since there is no citation marker left in the answer to
+// open them from.
+func chunksOnlyReference(kbinfos map[string]interface{}) map[string]interface{} {
+ refs := map[string]interface{}{}
+ if chunksRaw, ok := kbinfos["chunks"].([]map[string]interface{}); ok {
+ refs["chunks"] = chunksFormat(referenceChunks(nil, chunksRaw))
+ }
+ return refs
+}
+
// decorateHarnessAnswer formats the reasoning chat's final answer. The harness
// 'rag' tool already composed the answer WITH its own [ID:N] citations, so —
// unlike decorateAnswer for the native async_chat path — we never run
@@ -3387,7 +3404,7 @@ func (s *ChatPipelineService) decorateHarnessAnswer(answer string, kbinfos map[s
if !quote {
return AsyncChatResult{
Answer: stripCitations(think + ans),
- Reference: map[string]interface{}{},
+ Reference: chunksOnlyReference(kbinfos),
CreatedAt: float64(time.Now().Unix()),
}
}
@@ -3473,24 +3490,18 @@ func (s *ChatPipelineService) decorateHarnessAnswer(answer string, kbinfos map[s
}
}
- // Build a reference only when at least one citation resolves to a document.
- // The harness may have collected evidence without the final answer citing it;
- // that evidence must remain internal.
- var refs map[string]interface{}
- if len(citedDocIDs) > 0 {
- ref := make(map[string]interface{}, len(kbinfos))
- for k, v := range kbinfos {
- ref[k] = v
- }
- // The rendered evidence list comes first, in the order the model saw it,
- // so a marker's number is that entry's index; the remaining pool chunks
- // follow so no retrieved passage is lost. chunksFormat builds the
- // client-facing shape (content, document_name, dataset_id, ...) in NEW
- // maps, so the engine keys and the per-chunk vector never reach the
- // reference and the shared chunks stay intact.
- ref["chunks"] = chunksFormat(referenceChunks(citeIdx, chunksRaw))
- refs = ref
+ // The rendered evidence list comes first, in the order the model saw it, so a
+ // marker's number is that entry's index; the remaining pool chunks follow so
+ // no retrieved passage is lost. chunksFormat builds the client-facing shape
+ // (content, document_name, dataset_id, ...) in NEW maps, so the engine keys
+ // and the per-chunk vector never reach the reference and the shared chunks
+ // stay intact. The evidence ships even when the final answer cited none of
+ // it; doc_aggs, absent here, is what stays gated on `quote`.
+ refs := make(map[string]interface{}, len(kbinfos))
+ for k, v := range kbinfos {
+ refs[k] = v
}
+ refs["chunks"] = chunksFormat(referenceChunks(citeIdx, chunksRaw))
return AsyncChatResult{
Answer: think + ans,
diff --git a/internal/service/chat_pipeline_test.go b/internal/service/chat_pipeline_test.go
index 338cdaac0e..d9d2f7abce 100644
--- a/internal/service/chat_pipeline_test.go
+++ b/internal/service/chat_pipeline_test.go
@@ -792,7 +792,10 @@ func TestDecorateAnswer_VectorStrippedFromReference(t *testing.T) {
}
}
-func TestDecorateAnswer_NoReferencesWhenQuoteDisabled(t *testing.T) {
+// TestDecorateAnswer_QuoteDisabledKeepsChunks pins the cite=false shape of the
+// reference: the retrieved evidence still ships, but doc_aggs does not — with
+// the citation markers stripped there is nothing left to open those cards from.
+func TestDecorateAnswer_QuoteDisabledKeepsChunks(t *testing.T) {
s := &ChatPipelineService{}
timer, _ := newTimerAndPrompt()
kb := map[string]interface{}{
@@ -801,8 +804,12 @@ func TestDecorateAnswer_NoReferencesWhenQuoteDisabled(t *testing.T) {
}
result := s.decorateAnswer(t.Context(), "Answer", kb, "", nil, 0, timer,
nil, 0, false, nil, "", nil, "", nil, true)
- if result.Reference == nil || len(result.Reference) != 0 {
- t.Fatalf("disabled citations must explicitly clear references: %#v", result.Reference)
+ if _, has := result.Reference["doc_aggs"]; has {
+ t.Fatalf("disabled citations must drop doc_aggs: %#v", result.Reference)
+ }
+ chunks, ok := result.Reference["chunks"].([]map[string]interface{})
+ if !ok || len(chunks) != 1 || chunks[0]["id"] != "c1" {
+ t.Fatalf("disabled citations must still ship the evidence chunks: %#v", result.Reference["chunks"])
}
if result.Answer != "Answer" {
t.Fatalf("answer changed: %q", result.Answer)
@@ -2044,11 +2051,16 @@ func TestAsyncChatHarnessQuote(t *testing.T) {
if !final.Final || final.Answer != want || streamed.String() != want {
t.Fatalf("stream=%q final=%+v, want %q", streamed.String(), final, want)
}
- if (len(final.Reference) > 0) != tt.wantQuote {
- t.Fatalf("references=%#v, quote=%v", final.Reference, tt.wantQuote)
+ // The evidence ships either way; only doc_aggs follows the cite switch.
+ chunks, _ := final.Reference["chunks"].([]map[string]interface{})
+ if len(chunks) != 1 {
+ t.Fatalf("reference chunks = %#v, want the harness evidence", final.Reference["chunks"])
}
- if !tt.wantQuote && (final.Reference == nil || strings.Contains(reasoning.String(), "[ID:")) {
- t.Fatal("disabled citations must be removed from reasoning and explicitly clear references")
+ if _, hasDocAggs := final.Reference["doc_aggs"]; hasDocAggs != tt.wantQuote {
+ t.Fatalf("doc_aggs presence = %v, quote=%v: %#v", hasDocAggs, tt.wantQuote, final.Reference)
+ }
+ if !tt.wantQuote && strings.Contains(reasoning.String(), "[ID:") {
+ t.Fatalf("disabled citations must be removed from reasoning, got %q", reasoning.String())
}
})
}
@@ -2150,9 +2162,11 @@ func thinkEventWireKeys(t *testing.T, ev ThinkEvent) []string {
return keys
}
-// TestDecorateHarnessAnswerUncitedOmitsReference ensures an agentic answer
-// without citation markers does not expose the internal evidence pool.
-func TestDecorateHarnessAnswerUncitedOmitsReference(t *testing.T) {
+// TestDecorateHarnessAnswerUncitedKeepsChunks pins that an agentic answer using
+// no citation marker still carries the evidence it was grounded on: the pool is
+// the provenance of the answer, and the cite switch governs the doc_aggs cards
+// rather than the passages themselves.
+func TestDecorateHarnessAnswerUncitedKeepsChunks(t *testing.T) {
kbinfos := map[string]interface{}{
"chunks": []map[string]interface{}{
{
@@ -2170,8 +2184,9 @@ func TestDecorateHarnessAnswerUncitedOmitsReference(t *testing.T) {
s := &ChatPipelineService{}
res := s.decorateHarnessAnswer("小狼的颜色是灰色的。", kbinfos, nil, nil, true)
- if res.Reference != nil {
- t.Fatalf("uncited answer must not carry a reference, got %#v", res.Reference)
+ chunks, _ := res.Reference["chunks"].([]map[string]interface{})
+ if len(chunks) != 1 || chunks[0]["id"] != "c1" {
+ t.Fatalf("reference chunks = %#v, want the evidence pool", res.Reference["chunks"])
}
}
@@ -2241,8 +2256,11 @@ func TestDecorateHarnessAnswerResolvesRenderedPosition(t *testing.T) {
if strings.Contains(res.Answer, "[ID:1]") {
t.Fatalf("an unresolvable marker must be dropped, got %q", res.Answer)
}
- if res.Reference != nil {
- t.Fatalf("an answer without a resolvable citation must not carry a reference, got %#v", res.Reference)
+ // The unresolvable marker is dropped, but the evidence it was drawn from
+ // still ships.
+ remaining, _ := res.Reference["chunks"].([]map[string]interface{})
+ if len(remaining) != 1 || remaining[0]["document_name"] != "wolf.jpg" {
+ t.Fatalf("reference chunks = %#v, want the evidence passage", res.Reference["chunks"])
}
}
diff --git a/internal/service/citation_test.go b/internal/service/citation_test.go
index e4bdef6952..e71860be83 100644
--- a/internal/service/citation_test.go
+++ b/internal/service/citation_test.go
@@ -593,8 +593,9 @@ func TestDecorateQuote(t *testing.T) {
// TestDecorateHarnessAnswerDropsCitationsForNotFoundAnswer is the end-to-end
// shape of the report: an answer that only says the knowledge base has no answer
-// must come back with neither [ID:n] markers nor a reference, even though the
-// evidence pool and a resolvable marker are present.
+// must come back without [ID:n] markers, even though the evidence pool and a
+// resolvable marker are present. The evidence itself still ships — what the
+// cite switch removes is the doc_aggs cards the markers would have opened.
func TestDecorateHarnessAnswerDropsCitationsForNotFoundAnswer(t *testing.T) {
kbinfos := map[string]interface{}{
"chunks": []map[string]interface{}{
@@ -611,7 +612,11 @@ func TestDecorateHarnessAnswerDropsCitationsForNotFoundAnswer(t *testing.T) {
if strings.Contains(res.Answer, "[ID:") {
t.Fatalf("not-found answer still carries citation markers: %q", res.Answer)
}
- if len(res.Reference) != 0 {
- t.Fatalf("not-found answer still carries a reference: %#v", res.Reference)
+ if _, has := res.Reference["doc_aggs"]; has {
+ t.Fatalf("not-found answer still carries doc_aggs: %#v", res.Reference)
+ }
+ chunks, _ := res.Reference["chunks"].([]map[string]interface{})
+ if len(chunks) != 1 || chunks[0]["id"] != "c0" {
+ t.Fatalf("reference chunks = %#v, want the evidence passage", res.Reference["chunks"])
}
}