Skip to content

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

fix: return only cited Go chat documents (#19899)

buua43602a8d8fd6b2a
diff --git a/internal/service/chat_pipeline.go b/internal/service/chat_pipeline.go
index 270cab2936..e161f06b02 100644
--- a/internal/service/chat_pipeline.go
+++ b/internal/service/chat_pipeline.go
@@ -3085,6 +3085,9 @@ func (s *ChatPipelineService) decorateAnswer(
if hasKnowledges && quote {
chunksRaw, ok := kbinfos["chunks"].([]map[string]interface{})
if ok && len(chunksRaw) > 0 {
+ think = RepairBadCitationFormats(think)
+ ans = RepairBadCitationFormats(ans)
+ normalizedOutput := normalizeArabicDigits(think + ans)
// P7 — _hydrate_chunk_vectors. Mirrors
// dialog_service.py:794. Fetch the chunk embeddings insertCitations
// scores against, in one batched engine call. The agentic evidence
@@ -3092,7 +3095,7 @@ func (s *ChatPipelineService) decorateAnswer(
// ships zero placeholders, so the dimension comes from the embedding
// model whenever no chunk carries one. Only needed when we'll
// actually call insertCitations (the LLM didn't emit markers).
- if embModel != nil && !HasCitationMarkers(ans) {
+ if embModel != nil && !HasCitationMarkers(normalizedOutput) {
dim := firstChunkVectorDim(chunksRaw)
if dim <= 0 {
// One short probe encode buys the q_{dim}_vec field name.
@@ -3133,30 +3136,16 @@ func (s *ChatPipelineService) decorateAnswer(
}
}
}
- } else {
+ } else if HasCitationMarkers(normalizedOutput) {
// P0.11 pre-check matched: collect indices from existing
- // markers instead of calling insertCitations.
- for _, ci := range ExtractCitationMarkers(ans, len(chunksRaw)) {
- if citationIdx == nil {
- citationIdx = make(map[int]struct{})
- }
+ // markers in both the thinking block and final answer.
+ citationIdx = make(map[int]struct{})
+ for _, ci := range ExtractCitationMarkers(normalizedOutput, len(chunksRaw)) {
citationIdx[ci] = struct{}{}
}
}
}
- // repair_bad_citation_formats — runs even when chunks are empty.
- // Mirrors dialog_service.py:818.
- if ok {
- ans = RepairBadCitationFormats(ans)
- for _, ci := range ExtractCitationMarkers(ans, len(chunksRaw)) {
- if citationIdx == nil {
- citationIdx = make(map[int]struct{})
- }
- citationIdx[ci] = struct{}{}
- }
- }
-
// Map cited chunk indices to doc_ids and filter doc_aggs.
// Mirrors dialog_service.py:820-824.
if len(citationIdx) > 0 {
@@ -3172,7 +3161,7 @@ func (s *ChatPipelineService) decorateAnswer(
}
}
if len(citedDocIDs) > 0 {
- if docAggsRaw, ok := kbinfos["doc_aggs"].([]interface{}); ok && len(docAggsRaw) > 0 {
+ if docAggsRaw, ok := kbinfos["doc_aggs"].([]interface{}); ok {
var filtered []interface{}
for _, da := range docAggsRaw {
if dam, ok := da.(map[string]interface{}); ok {
@@ -3183,18 +3172,16 @@ func (s *ChatPipelineService) decorateAnswer(
}
}
}
- if len(filtered) > 0 {
- kbinfos["doc_aggs"] = filtered
- }
+ kbinfos["doc_aggs"] = filtered
}
}
}
}
- // Build refs: deepcopy kbinfos and strip vectors — done whenever
- // hasKnowledges is true, regardless of quote flag.
- // Mirrors dialog_service.py:826-829.
- if hasKnowledges {
+ // Build refs only when the answer contains a citation that resolves to a
+ // chunk. Retrieved evidence without a citation must not be exposed as a
+ // document reference.
+ if hasKnowledges && len(citationIdx) > 0 {
refs = make(map[string]interface{})
for k, v := range kbinfos {
refs[k] = v
@@ -3327,7 +3314,7 @@ func (s *ChatPipelineService) decorateHarnessAnswer(answer string, kbinfos map[s
chunksRaw, _ := kbinfos["chunks"].([]map[string]interface{})
// The markers exactly as the model wrote them (before any repair), for the
// citation observability log below.
- rawMarkers := rawCitationMarkers(ans, 8)
+ rawMarkers := rawCitationMarkers(think+ans, 8)
// The evidence list the model was shown, as pool positions: citeIdx[i] is the
// pool chunk behind the i-th rendered block. Empty ids fall back to pool
@@ -3354,8 +3341,11 @@ func (s *ChatPipelineService) decorateHarnessAnswer(answer string, kbinfos map[s
// markers: each is the 0-based index of the rendered block it cites.
// Canonical markers that name no block are dropped; the resolved pool
// positions drive doc_aggs filtering.
+ think = RepairBadCitationFormats(think)
ans = RepairBadCitationFormats(ans)
- ans, citedIdx := ResolveCitationMarkers(ans, citeIdx)
+ think, thinkCitedIdx := ResolveCitationMarkers(think, citeIdx)
+ ans, answerCitedIdx := ResolveCitationMarkers(ans, citeIdx)
+ citedIdx := append(thinkCitedIdx, answerCitedIdx...)
// Citation observability: the markers the model wrote against the blocks it was
// shown, so a numbering mismatch is visible in the log (`raw_markers` is what
@@ -3384,10 +3374,10 @@ func (s *ChatPipelineService) decorateHarnessAnswer(answer string, kbinfos map[s
}
}
- // recall_docs = cited docs, or all when nothing cited (dialog_service.py:
- // 2124-2127). Only rewrite doc_aggs when we actually found cited docs.
+ // Keep only documents referenced by a citation. An uncited evidence pool is
+ // not a document reference and must not be returned to the client.
if len(citedDocIDs) > 0 {
- if docAggsRaw, ok := kbinfos["doc_aggs"].([]interface{}); ok && len(docAggsRaw) > 0 {
+ if docAggsRaw, ok := kbinfos["doc_aggs"].([]interface{}); ok {
filtered := make([]interface{}, 0, len(docAggsRaw))
for _, da := range docAggsRaw {
if dam, ok := da.(map[string]interface{}); ok {
@@ -3398,22 +3388,15 @@ func (s *ChatPipelineService) decorateHarnessAnswer(answer string, kbinfos map[s
}
}
}
- if len(filtered) > 0 {
- kbinfos["doc_aggs"] = filtered
- }
+ kbinfos["doc_aggs"] = filtered
}
}
- // refs = deepcopy(kbinfos), each chunk stripped of its vector
- // (dialog_service.py:2129-2132). Python gates this on `doc_ids` — the answer
- // cited at least one chunk — but an agentic answer the model composed without
- // markers then came back with no reference at all, while the naive path
- // returns the passages unconditionally (dialog_service.py:826-829). Build it
- // whenever the harness left a citation pool, so the user always gets the
- // sources the answer was composed from; doc_aggs stays filtered to the cited
- // documents when something was cited.
+ // 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(chunksRaw) > 0 {
+ if len(citedDocIDs) > 0 {
ref := make(map[string]interface{}, len(kbinfos))
for k, v := range kbinfos {
ref[k] = v
diff --git a/internal/service/chat_pipeline_test.go b/internal/service/chat_pipeline_test.go
index b89ea8304e..b009e30e7b 100644
--- a/internal/service/chat_pipeline_test.go
+++ b/internal/service/chat_pipeline_test.go
@@ -328,6 +328,47 @@ func TestDecorateAnswer_RepairRunsWhenQuote(t *testing.T) {
}
}
+// TestDecorateAnswer_CitesThinkBlock ensures citations emitted inside the
+// hidden thinking block still determine the returned reference documents.
+func TestDecorateAnswer_CitesThinkBlock(t *testing.T) {
+ s := &ChatPipelineService{}
+ timer, _ := newTimerAndPrompt()
+ kbinfos := map[string]interface{}{
+ "chunks": []map[string]interface{}{
+ {
+ "chunk_id": "c1",
+ "content_with_weight": "hello world",
+ "doc_id": "d1",
+ },
+ },
+ "doc_aggs": []interface{}{
+ map[string]interface{}{"doc_id": "d1", "doc_name": "doc.txt"},
+ },
+ }
+ result := s.decorateAnswer(
+ t.Context(),
+ "<think>Supported by [ID:0].</think>visible answer",
+ kbinfos,
+ "system prompt",
+ []string{"q"},
+ 0,
+ timer,
+ nil, 0.0, true,
+ nil,
+ "",
+ nil,
+ "",
+ nil,
+ true,
+ )
+ if result.Reference == nil {
+ t.Fatal("a think-block citation must carry a reference")
+ }
+ if aggs, _ := result.Reference["doc_aggs"].([]interface{}); len(aggs) != 1 || aggs[0].(map[string]interface{})["doc_id"] != "d1" {
+ t.Fatalf("reference doc_aggs = %#v, want the cited document", result.Reference["doc_aggs"])
+ }
+}
+
// TestDecorateAnswer_PreCheckSkipsInsertCitations covers P0.11: when
// the LLM already emitted canonical [ID:N] markers, insertCitations
// is skipped (so we don't double-tag). We verify by checking that
@@ -727,13 +768,13 @@ func TestDecorateAnswer_VectorStrippedFromReference(t *testing.T) {
}
result := s.decorateAnswer(
t.Context(),
- "x",
+ "x [ID:0]",
kb,
"system prompt",
[]string{"q"},
0,
timer,
- nil, 0.0, false,
+ nil, 0.0, true,
nil,
"",
nil,
@@ -2001,13 +2042,9 @@ func thinkEventWireKeys(t *testing.T, ev ThinkEvent) []string {
return keys
}
-// TestDecorateHarnessAnswerUncitedStillCarriesReference: an agentic answer the
-// model composed WITHOUT citation markers must still ship the passages it was
-// composed from. Python's `refs = ... if doc_ids else []` returned an empty
-// reference for exactly that case (a one-passage pool is the common one), which
-// left the user with an answer and nothing to open — while the naive path hands
-// the passages back unconditionally.
-func TestDecorateHarnessAnswerUncitedStillCarriesReference(t *testing.T) {
+// TestDecorateHarnessAnswerUncitedOmitsReference ensures an agentic answer
+// without citation markers does not expose the internal evidence pool.
+func TestDecorateHarnessAnswerUncitedOmitsReference(t *testing.T) {
kbinfos := map[string]interface{}{
"chunks": []map[string]interface{}{
{
@@ -2025,16 +2062,35 @@ func TestDecorateHarnessAnswerUncitedStillCarriesReference(t *testing.T) {
s := &ChatPipelineService{}
res := s.decorateHarnessAnswer("小狼的颜色是灰色的。", kbinfos, nil, nil)
- if res.Reference == nil {
- t.Fatal("an agentic answer with a citation pool must carry a reference")
+ 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]["document_name"] != "wolf.jpg" {
- t.Fatalf("reference chunks = %#v, want the pool in the client-facing shape", res.Reference["chunks"])
+}
+
+// TestDecorateHarnessAnswerCitesThinkBlock ensures citations emitted inside the
+// hidden thinking block still resolve to client-visible source documents.
+func TestDecorateHarnessAnswerCitesThinkBlock(t *testing.T) {
+ kbinfos := map[string]interface{}{
+ "chunks": []map[string]interface{}{
+ {
+ "chunk_id": "c1",
+ "content_with_weight": "the wolf is grey",
+ "doc_id": "d1",
+ "docnm_kwd": "wolf.jpg",
+ },
+ },
+ "doc_aggs": []interface{}{
+ map[string]interface{}{"doc_id": "d1", "doc_name": "wolf.jpg"},
+ },
}
- // Nothing was cited, so no document can be filtered out.
- if aggs, _ := res.Reference["doc_aggs"].([]interface{}); len(aggs) != 1 {
- t.Fatalf("reference doc_aggs = %#v, want the whole pool kept", res.Reference["doc_aggs"])
+
+ s := &ChatPipelineService{}
+ res := s.decorateHarnessAnswer("<think>Supported by [ID:0].</think>小狼的颜色是灰色的。", kbinfos, nil, []string{"c1"})
+ if res.Reference == nil {
+ t.Fatal("a think-block citation must carry a reference")
+ }
+ if aggs, _ := res.Reference["doc_aggs"].([]interface{}); len(aggs) != 1 || aggs[0].(map[string]interface{})["doc_id"] != "d1" {
+ t.Fatalf("reference doc_aggs = %#v, want the cited document", res.Reference["doc_aggs"])
}
}
@@ -2077,8 +2133,8 @@ 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.Fatal("the reference must survive a dropped marker")
+ if res.Reference != nil {
+ t.Fatalf("an answer without a resolvable citation must not carry a reference, got %#v", res.Reference)
}
}