Skip to content

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

Fix citation of agentic rag (#19791)

qinling021012262e7fe3de
diff --git a/cmd/ragflow_server.go b/cmd/ragflow_server.go
index b2825c9776..7f1816466f 100644
--- a/cmd/ragflow_server.go
+++ b/cmd/ragflow_server.go
@@ -1317,7 +1317,7 @@ func startServer(ctx context.Context, args *serverArgs) error {
Images: req.Images,
TextAttachments: req.TextAttachments,
})
- res := service.HarnessResult{Chunks: r.Chunks, DocAggs: r.DocAggs, Answer: r.Answer, SlotCitations: r.SlotCitations}
+ res := service.HarnessResult{Chunks: r.Chunks, DocAggs: r.DocAggs, Answer: r.Answer, SlotCitations: r.SlotCitations, CiteChunkIDs: r.CiteChunkIDs}
if r.Kbinfos != nil {
res.Memory = r.Kbinfos.Memory
res.PreSummary = r.Kbinfos.PreSummary
diff --git a/internal/agent/retrievalbridge/runtime_adapter.go b/internal/agent/retrievalbridge/runtime_adapter.go
index 6dd41af4e4..2daf46945d 100644
--- a/internal/agent/retrievalbridge/runtime_adapter.go
+++ b/internal/agent/retrievalbridge/runtime_adapter.go
@@ -99,6 +99,7 @@ func (a *RuntimeAdapter) Search(ctx context.Context, db *gorm.DB, req agentrunt.
DocumentName: c.DocumentName,
DatasetID: c.DatasetID,
ImageID: c.ImageID,
+ DocType: c.DocType,
URL: c.URL,
Positions: c.Positions,
Score: c.Score,
diff --git a/internal/agent/runtime/retrieval.go b/internal/agent/runtime/retrieval.go
index f0646cf73c..325efb9bb7 100644
--- a/internal/agent/runtime/retrieval.go
+++ b/internal/agent/runtime/retrieval.go
@@ -55,6 +55,10 @@ type RetrievalChunk struct {
Score float64
TermSimilarity float64
VectorSimilarity float64
+ // DocType is the engine's doc_type_kwd ("text" / "image" / "table").
+ // Carried so an image chunk reaches the harness evidence pool with its type
+ // intact and the answer reference card can still render it as an image.
+ DocType string
}
// RetrievalRequest is the input to RetrievalService.Search.
diff --git a/internal/agent/tool/retrieval_nlp.go b/internal/agent/tool/retrieval_nlp.go
index 27133d9d7a..8d628f0508 100644
--- a/internal/agent/tool/retrieval_nlp.go
+++ b/internal/agent/tool/retrieval_nlp.go
@@ -572,6 +572,7 @@ func translateChunk(raw map[string]any) RetrievalChunk {
DocumentName: stringFromMap(raw, "docnm_kwd"),
DatasetID: stringFromMap(raw, "kb_id"),
ImageID: firstStringFromMap(raw, "image_id", "img_id"),
+ DocType: firstStringFromMap(raw, "doc_type_kwd", "doc_type"),
URL: firstStringFromMap(raw, "url", "document_url", "doc_url"),
Positions: firstValueFromMap(raw, "positions", "position_int"),
MomID: stringFromMap(raw, "mom_id"),
diff --git a/internal/agent/tool/retrieval_service.go b/internal/agent/tool/retrieval_service.go
index cbd1c9435a..8459c6a5f9 100644
--- a/internal/agent/tool/retrieval_service.go
+++ b/internal/agent/tool/retrieval_service.go
@@ -48,6 +48,11 @@ type RetrievalChunk struct {
// for top-level chunks. Threaded through so the harness can run
// retrieval_by_children (child fragments are promoted to their parent chunk).
MomID string
+ // DocType is the engine's doc_type_kwd ("text" / "image" / "table"). It is
+ // what lets the answer reference card render an image chunk as an image
+ // instead of dropping the marker, so it must survive the hop to the harness
+ // evidence pool alongside ImageID.
+ DocType string
}
// RetrievalRequest is the input to RetrievalService.Search.
diff --git a/internal/rag/advanced_rag/agentic_rag.go b/internal/rag/advanced_rag/agentic_rag.go
index f0ff7b3f2a..ff74d8009e 100644
--- a/internal/rag/advanced_rag/agentic_rag.go
+++ b/internal/rag/advanced_rag/agentic_rag.go
@@ -580,6 +580,10 @@ type RunResponse struct {
// chunk the slot was filled from (those markers index the internal slot
// table — nothing the user can open).
SlotCitations map[string][]string
+ // CiteChunkIDs is the ordered id list of the chunks the final-answer call
+ // showed the model as numbered evidence (harness.Kbinfos.CiteChunkIDs).
+ // A "[ID:n]" marker the model wrote refers to position n in this list.
+ CiteChunkIDs []string
}
// AnswerSink forwards a partially produced answer while the model is still
@@ -1388,6 +1392,16 @@ func composeFinalAnswer(ctx context.Context, deps RAGTools, req harness.RunReque
if deps.ComposeAnswer != nil && !*deps.ComposeAnswer {
return
}
+ // The compose publishes the ordered evidence list it rendered (kb's
+ // CiteChunkIDs) while it runs; hand it to the caller so the chat pipeline
+ // can resolve the answer's [ID:n] markers against that same list. Covered by
+ // a defer because both the streaming and the one-shot path return from
+ // inside the branches below.
+ defer func() {
+ if kb != nil {
+ resp.CiteChunkIDs = kb.CiteChunkIDs
+ }
+ }()
adeps := AnswerDeps{
Model: deps.Model,
CiteRules: deps.CiteRules,
diff --git a/internal/rag/advanced_rag/agentic_rag_graph.go b/internal/rag/advanced_rag/agentic_rag_graph.go
index d8aadca9f8..28e4d98f10 100644
--- a/internal/rag/advanced_rag/agentic_rag_graph.go
+++ b/internal/rag/advanced_rag/agentic_rag_graph.go
@@ -5316,7 +5316,23 @@ func (d AnswerDeps) answerPromptWithEvidence(kb *harness.Kbinfos, question strin
if maxTokens <= 0 {
maxTokens = evidenceBudgetTokens
}
- evidence := strings.Join(prompts.KBPrompt(citeChunks, maxTokens), "\n")
+ // Publish the ordered evidence list the model is about to see so the chat
+ // pipeline can resolve the answer's [ID:n] markers against THE SAME list and put
+ // it in front of the reference (harness.Kbinfos.CiteChunkIDs →
+ // RunResponse.CiteChunkIDs → decorateHarnessAnswer). The blocks are numbered
+ // 0-based because the client renders a marker by indexing reference.chunks with
+ // its number, which the model has to write itself; citeChunks is the
+ // similarity-ranked, capped selection (not the pool in order), so the reference
+ // is ordered to match it.
+ //
+ // The ids come from the render's SOURCE indices, not from citeChunks: a chunk
+ // with no content renders no block, so walking citeChunks would publish an id for
+ // the skipped chunk and put every marker past it one block off.
+ blocks, sources := prompts.KBPromptZeroBasedWithSourceIndices(citeChunks, maxTokens)
+ if kb != nil {
+ kb.CiteChunkIDs = citeChunkIDsAt(citeChunks, sources)
+ }
+ evidence := strings.Join(blocks, "\n")
summary := ""
record := composedRecord(kb)
@@ -5442,6 +5458,22 @@ func ComposeAnswerStream(ctx context.Context, deps AnswerDeps, model harness.Str
return AnswerResult{Answer: answer, Partial: partial}, nil
}
+// zeroBasedEvidenceRule states the evidence numbering for the compose model.
+//
+// The blocks this call renders are numbered from 0 because the client resolves a
+// marker by indexing reference.chunks with its number, and the answer reaches that
+// client as the model streams it. Saying so guards against the "the first source is
+// 1" habit: a model that falls back on it cites the second passage and leaves the
+// first one unreachable (its markers are still openable, they just point one block
+// off).
+//
+// It belongs at the call site, not in CitationPrompt: that text is shared with the
+// agent canvas, which numbers its blocks by hash id (component/prompts/citation.go).
+const zeroBasedEvidenceRule = "\n\n# Evidence ids\n" +
+ "The evidence blocks below are numbered from 0: the FIRST block is [ID:0]. " +
+ "Cite the id printed at the start of the block you used, exactly as printed — " +
+ "do not renumber the blocks yourself."
+
// composeSystem builds the system prompt, mirroring Python's precedence rules.
//
// LANGUAGE IS DELIBERATELY LEFT OVERRIDABLE: "answer in the same language as the
@@ -5453,7 +5485,7 @@ func (d AnswerDeps) composeSystem() string {
// Mirror Python compose_system: the citation rules are citation_prompt
// (citation_prompt.md) with an optional user-defined override
// (RAGConfig.CiteRules / user_defined_prompts).
- rules := prompts.CitationPrompt(d.CiteRules)
+ rules := prompts.CitationPrompt(d.CiteRules) + zeroBasedEvidenceRule
system := strings.ReplaceAll(harness.FinalAnswerSystem, "{cite_rules}", rules)
if sp := strings.TrimSpace(d.SystemPrompt); sp != "" {
system = fmt.Sprintf("%s\n\n# Assistant configuration (set by the user)\n%s\n\nFollow the configuration above for language, tone, style, format and any other presentational instruction, including where it overrides the language rule above. Where it conflicts with the citation rules, attribute fidelity, or the requirement to answer only from the provided evidence, those three take precedence.",
@@ -5467,6 +5499,25 @@ func cleanAnswer(s string) string {
return strings.TrimSpace(reAnswerThink.ReplaceAllString(s, ""))
}
+// citeChunkIDsAt returns the citation id of every chunk that rendered a block, in
+// render order, from the render's source indices.
+//
+// It takes the SOURCE INDICES rather than the chunk list because the renderer emits
+// no block for a chunk without content: one id per RENDERED block, or the published
+// list runs ahead of the numbering at every skip and each marker past it lands on
+// the wrong passage. A chunk without an id keeps its slot as an empty string —
+// dropped, it would shift the later blocks the same way (citePoolIdx resolves "" to
+// -1, so that block's own citation is dropped, but the blocks after it stay put).
+func citeChunkIDsAt(chunks []map[string]any, sources []int) []string {
+ out := make([]string, len(sources))
+ for i, src := range sources {
+ if src >= 0 && src < len(chunks) {
+ out[i] = harness.ChunkIDOf(chunks[src])
+ }
+ }
+ return out
+}
+
// rankBySimilarity mirrors Python's sorted(..., key=similarity or score,
// reverse=True). Stable, so equal-scored chunks keep their retrieval order.
func rankBySimilarity(chunks []map[string]any) []map[string]any {
diff --git a/internal/rag/advanced_rag/agentic_rag_graph_test.go b/internal/rag/advanced_rag/agentic_rag_graph_test.go
index 90b32eebcd..75f4da71f8 100644
--- a/internal/rag/advanced_rag/agentic_rag_graph_test.go
+++ b/internal/rag/advanced_rag/agentic_rag_graph_test.go
@@ -8,6 +8,7 @@ import (
"fmt"
"log"
"os"
+ "reflect"
"sort"
"strings"
"sync"
@@ -21,6 +22,7 @@ import (
"ragflow/internal/entity"
"ragflow/internal/rag/advanced_rag/harness"
"ragflow/internal/rag/advanced_rag/slots"
+ "ragflow/internal/rag/prompts"
)
// ---------------------------------------------------------------------------
@@ -2510,6 +2512,64 @@ func TestRunAgenticComposesFromResearchFindings(t *testing.T) {
}
}
+// TestComposePublishesCiteChunkIDs pins the evidence contract the chat pipeline
+// resolves citations against: the compose numbers the blocks 0-based (the client
+// indexes reference.chunks with the marker's number, and the answer is streamed
+// before any rewrite could apply) and publishes THAT order — similarity-ranked
+// and capped — on harness.Kbinfos.CiteChunkIDs, because the reference is built in
+// that order and a marker against the pool by position would land on the wrong
+// chunk (or past the end when the pool is shorter than the render cap, which is
+// how an agentic answer came back with no reference at all).
+func TestComposePublishesCiteChunkIDs(t *testing.T) {
+ kb := &harness.Kbinfos{
+ Chunks: []map[string]any{
+ {"chunk_id": "c1", "content": "the wolf is grey", "similarity": 0.1},
+ {"chunk_id": "c2", "content": "the wolf is small", "similarity": 0.9},
+ // No content: kbpBlock renders no block for it, so it must not take a
+ // slot in the published list either — an id for it would put every
+ // marker after it one block off the passage the model cited.
+ {"chunk_id": "c3", "content": " ", "similarity": 0.5},
+ },
+ }
+ mdl := &fakeModel{replies: []*harness.ModelReply{{Content: "it is grey [ID:0]."}}}
+ if res := ComposeAnswer(context.Background(), AnswerDeps{Model: mdl}, kb, "what colour is it?", false, false); res.Failed {
+ t.Fatal("composition must succeed")
+ }
+ got := mdl.lastUserPrompt()
+ for _, want := range []string{"ID: 0", "ID: 1"} {
+ if !strings.Contains(got, want) {
+ t.Errorf("evidence missing %q:\n%s", want, got)
+ }
+ }
+ if strings.Contains(got, "ID: 2") {
+ t.Errorf("evidence numbering ran past the rendered blocks:\n%s", got)
+ }
+ // The rendered order is the similarity ranking, not the pool order: the
+ // higher-similarity chunk is block 0. The blank chunk sits between the two
+ // rendered blocks and contributes no id.
+ want := []string{"c2", "c1"}
+ if !reflect.DeepEqual(kb.CiteChunkIDs, want) {
+ t.Fatalf("CiteChunkIDs = %v, want one id per rendered block in render order %v", kb.CiteChunkIDs, want)
+ }
+}
+
+// TestComposeSystemDeclaresZeroBasedEvidence pins the prompt-side guard for the
+// evidence numbering: the compose renders 0-based block ids and the client indexes
+// reference.chunks with the marker's number, so the model has to be told — one on
+// "the first source is 1" cites the second passage and never the first. The rule
+// belongs to the callers that render 0-based blocks, not to CitationPrompt:
+// CitationPrompt is shared with the agent canvas, which numbers its blocks by hash
+// id, so a base declared there would be wrong.
+func TestComposeSystemDeclaresZeroBasedEvidence(t *testing.T) {
+ sys := AnswerDeps{}.composeSystem()
+ if !strings.Contains(sys, "the FIRST block is [ID:0]") {
+ t.Fatalf("compose system prompt must state the 0-based evidence numbering:\n%s", sys)
+ }
+ if strings.Contains(prompts.CitationPrompt(""), "the FIRST block is [ID:0]") {
+ t.Fatal("CitationPrompt must not declare a base: the canvas render numbers its blocks by hash id")
+ }
+}
+
// TestAnswerPromptCarriesTargetContract pins the EXTREME-SELECTION guardrail
// (Python :643-652). Without it a "longest/shortest/most…" question tends to be
// answered with the most common or first-listed candidate rather than the
diff --git a/internal/rag/advanced_rag/harness/doc_fetch.go b/internal/rag/advanced_rag/harness/doc_fetch.go
index 10dbb206d7..5aa3d13bb4 100644
--- a/internal/rag/advanced_rag/harness/doc_fetch.go
+++ b/internal/rag/advanced_rag/harness/doc_fetch.go
@@ -146,7 +146,13 @@ func summarizeDocument(ctx context.Context, deps SearchDeps, docID string, maxTo
deps.KB = &Kbinfos{}
}
added := deps.KB.Merge(chunks, aggs)
- blocks, sources := prompts.KBPromptWithSourceIndices(deps.KB.Chunks, budget)
+ // Pool positions, not rendered positions, are the block ids — 0-based, like every
+ // other evidence render. These are the tool's evidence, not the final answer's:
+ // the compose re-renders the pool it wants to cite with its own numbering
+ // (CiteChunkIDs) and the chat pipeline resolves the answer's markers against that
+ // list. What the ids must do here is stay addressable, which a pool position does
+ // whatever the render skipped.
+ blocks, sources := prompts.KBPromptPoolIndexed(deps.KB.Chunks, budget)
blockAt := make(map[int]string, len(sources))
for i, src := range sources {
if _, seen := blockAt[src]; !seen {
@@ -179,6 +185,10 @@ func summarizeDocument(ctx context.Context, deps SearchDeps, docID string, maxTo
if !deps.DoRefer {
return fresh
}
+ // The blocks are numbered by pool position: were this path ever reached with
+ // do_refer=true, the rules would need the same 0-based sentence the compose adds
+ // (advanced_rag.zeroBasedEvidenceRule) — CitationPrompt itself cannot carry it,
+ // the canvas renders hash ids.
header := "# Citation rules\nApply the following rules VERBATIM to your final answer.\n\n" +
prompts.CitationPrompt(deps.CiteRules) + "\n\n----\n\n"
return append([]string{header}, fresh...)
diff --git a/internal/rag/advanced_rag/harness/kbinfos.go b/internal/rag/advanced_rag/harness/kbinfos.go
index a6aeb4076a..a943327a09 100644
--- a/internal/rag/advanced_rag/harness/kbinfos.go
+++ b/internal/rag/advanced_rag/harness/kbinfos.go
@@ -153,6 +153,22 @@ type Kbinfos struct {
// there. Guarded by ledgerMu.
patternFindings string
patternPassed bool
+ // CiteChunkIDs is the ordered id list of the chunks the final-answer call
+ // rendered as numbered evidence — Python's tools._rag_cite_chunk_ids
+ // (agentic_rag_graph.py:870). The renderer ranks the pool by similarity and
+ // caps it (citeChunkCap), so this list is NOT Chunks in pool order, and a
+ // "[ID:n]" the model writes refers to position n in THIS list — one entry per
+ // RENDERED block, so a chunk the renderer skipped (no content) holds no
+ // position here either. The chat pipeline resolves citations against it
+ // (RunResponse.CiteChunkIDs → HarnessResult.CiteChunkIDs →
+ // decorateHarnessAnswer); without it, a marker was resolved against Chunks by
+ // position and landed on the wrong chunk — or past the end when the pool held
+ // fewer chunks than the cap.
+ //
+ // Written once per final-answer call, on the goroutine that composes it (the
+ // concurrent research slots have already joined), and read by the caller after
+ // the call returns — it is not part of the pool lock's protected state.
+ CiteChunkIDs []string
// cache is the per-request retrieval cache (Python tools.search_cache). It
// is initialised lazily via cacheOnce so a zero-value Kbinfos is usable.
cache *searchCache
diff --git a/internal/rag/advanced_rag/harness/tool_executor.go b/internal/rag/advanced_rag/harness/tool_executor.go
index 2f0c5a9d12..77cb4d4036 100644
--- a/internal/rag/advanced_rag/harness/tool_executor.go
+++ b/internal/rag/advanced_rag/harness/tool_executor.go
@@ -1748,12 +1748,21 @@ func chunksToMaps(chunks []runtime.RetrievalChunk) []map[string]any {
out := make([]map[string]any, 0, len(chunks))
for _, c := range chunks {
out = append(out, map[string]any{
- "chunk_id": c.ID,
- "content": c.Content,
- "doc_id": c.DocumentID,
- "docnm_kwd": c.DocumentName,
- "dataset_id": c.DatasetID,
- "kb_id": c.DatasetID,
+ "chunk_id": c.ID,
+ "content": c.Content,
+ // content_with_weight is the engine/storage spelling of the body.
+ // Carried as an alias because the shared reference builder
+ // (chunksFormat → getValue("content_with_weight", "content")) and
+ // the citation prompt read the storage name; without it those
+ // readers get an empty body from an agentic chunk.
+ "content_with_weight": c.Content,
+ "doc_id": c.DocumentID,
+ "docnm_kwd": c.DocumentName,
+ "dataset_id": c.DatasetID,
+ "kb_id": c.DatasetID,
+ // doc_type_kwd is what marks a chunk as an image (or table); the
+ // reference card renders it and drops the marker without it.
+ "doc_type_kwd": c.DocType,
"mom_id": c.MomID,
"similarity": c.Score,
"image_id": c.ImageID,
diff --git a/internal/rag/advanced_rag/harness/tool_executor_test.go b/internal/rag/advanced_rag/harness/tool_executor_test.go
index 78b9180e88..793885f959 100644
--- a/internal/rag/advanced_rag/harness/tool_executor_test.go
+++ b/internal/rag/advanced_rag/harness/tool_executor_test.go
@@ -1562,3 +1562,30 @@ func TestSetDirectionWidensTheQueryBudget(t *testing.T) {
}
noPool.MarkSetDirection()
}
+
+// TestChunksToMapsCarriesReferenceFields pins the fields the answer reference
+// card and the shared citation readers expect on an agentic chunk:
+// doc_type_kwd (the image/table marker) and the content_with_weight spelling of
+// the body. Without them an image chunk reached the response as an untyped,
+// bodyless reference while the naive path rendered it fine.
+func TestChunksToMapsCarriesReferenceFields(t *testing.T) {
+ got := chunksToMaps([]runtime.RetrievalChunk{{
+ ID: "c1",
+ Content: "wolf",
+ ImageID: "kb-doc",
+ DocType: "image",
+ }})
+ if len(got) != 1 {
+ t.Fatalf("len = %d, want 1", len(got))
+ }
+ if got[0]["doc_type_kwd"] != "image" {
+ t.Errorf("doc_type_kwd = %v, want image", got[0]["doc_type_kwd"])
+ }
+ if got[0]["content_with_weight"] != "wolf" || got[0]["content"] != "wolf" {
+ t.Errorf("content fields = %v/%v, want wolf/wolf",
+ got[0]["content_with_weight"], got[0]["content"])
+ }
+ if got[0]["image_id"] != "kb-doc" {
+ t.Errorf("image_id = %v, want kb-doc", got[0]["image_id"])
+ }
+}
diff --git a/internal/rag/prompts/generator.go b/internal/rag/prompts/generator.go
index d60c5caae0..6496b1cdaf 100644
--- a/internal/rag/prompts/generator.go
+++ b/internal/rag/prompts/generator.go
@@ -136,19 +136,20 @@ func CitationPrompt(userDefined string) string {
return strings.TrimSpace(string(data)) + citationIDSuffix
}
-// kbpBlock renders one knowledge block (id / title / url / metadata / content)
-// for the given 1-based index. ok is false when the chunk carries no content,
-// which KBPrompt treats as "skip" (mirrors Python's `if not c: continue`).
+// kbpBlock renders one knowledge block (id / title / url / metadata / content) with
+// id as its number — 1-based for kb_prompt parity, 0-based or a pool position for the
+// citation renderers. ok is false when the chunk carries no content, which the
+// renderers treat as "skip" (mirrors Python's `if not c: continue`).
//
// The block starts with a newline, mirroring Python's `"\nID: {}".format(...)`,
// so a caller that joins the blocks with "\n" renders the same prompt Python
// does.
-func kbpBlock(c map[string]any, index int) (string, bool) {
+func kbpBlock(c map[string]any, id int) (string, bool) {
content := chunkText(c)
if strings.TrimSpace(content) == "" {
return "", false
}
- block := fmt.Sprintf("\nID: %d", index)
+ block := fmt.Sprintf("\nID: %d", id)
if title := chunkTitle(c); title != "" {
block += "\n├── Title: " + flattenNewlines(title)
}
@@ -176,8 +177,12 @@ func kbpBlock(c map[string]any, index int) (string, bool) {
// KBPrompt mirrors rag/prompts/generator.kb_prompt: render chunks into the
// numbered knowledge blocks the citation rules refer to.
//
-// Blocks are numbered by position (1-based) so the model's [n] citations match
-// the block order it sees, matching Python's `ID: {i}` rendering (hash_id=False).
+// Blocks are numbered by rendered position, 1-based, matching Python's `ID: {i}`
+// (hash_id=False). No Go caller whose numbers reach a citation marker uses that
+// numbering any more — a marker names the client's index into reference.chunks,
+// which is 0-based — so this is kept for kb_prompt parity. What the paths render
+// with instead: KBPromptZeroBasedWithSourceIndices (agentic compose) and
+// KBPromptPoolIndexed (summarize_document).
//
// The budget is applied to the COMPLETE rendered block (title / url / metadata /
// content), measured in TOKENS with the same cl100k_base encoder Python's
@@ -192,6 +197,24 @@ func KBPrompt(chunks []map[string]any, maxTokens int) []string {
return blocks
}
+// KBPromptZeroBasedWithSourceIndices renders the blocks 0-based — the first is
+// "ID: 0" — and reports, for each, the index in chunks it came from.
+//
+// 0-based because the client resolves a citation marker by using its number as an
+// index into reference.chunks, and it renders the answer as the model streams it, so
+// the model has to write that index itself. The agentic compose renders with this
+// and publishes the same order on harness.Kbinfos.CiteChunkIDs, which
+// decorateHarnessAnswer puts in front of the reference. A 1-based render made every
+// marker point one chunk off — or past the end when the pool was shorter than the
+// render cap.
+//
+// The source indices are what the published id list must be gathered through:
+// kbpBlock renders no block for a chunk with no content, so walking the chunk list
+// instead would carry an id for every skipped chunk and shift every marker after it.
+func KBPromptZeroBasedWithSourceIndices(chunks []map[string]any, maxTokens int) ([]string, []int) {
+ return kbPromptWithIDs(chunks, maxTokens, func(_, rendered int) int { return rendered })
+}
+
// KBPromptWithSourceIndices is KBPrompt plus, for every rendered block, the
// index in chunks it was rendered from.
//
@@ -201,6 +224,23 @@ func KBPrompt(chunks []map[string]any, maxTokens int) []string {
// chunks[i]. Slicing by a chunk count can therefore drop readable blocks or
// point past the end.
func KBPromptWithSourceIndices(chunks []map[string]any, maxTokens int) ([]string, []int) {
+ return kbPromptWithIDs(chunks, maxTokens, func(_, rendered int) int { return rendered + 1 })
+}
+
+// KBPromptPoolIndexed is KBPromptWithSourceIndices with each block numbered by the
+// POSITION OF ITS CHUNK in chunks instead of by its place in the rendered list (see
+// kbPromptWithIDs). Used where a marker has to name a chunk in the pool rather than
+// a block rendered for the client — the summarize_document blocks, whose ids stay
+// addressable however much the render skipped.
+func KBPromptPoolIndexed(chunks []map[string]any, maxTokens int) ([]string, []int) {
+ return kbPromptWithIDs(chunks, maxTokens, func(idx, _ int) int { return idx })
+}
+
+// kbPromptWithIDs renders the blocks and reports, for each, the index in chunks it
+// came from. idAt decides the id printed on a block: its chunk's position (idx) or
+// its place among the rendered blocks (rendered) — the two diverge as soon as a
+// chunk is skipped or the token budget cuts the list short.
+func kbPromptWithIDs(chunks []map[string]any, maxTokens int, idAt func(idx, rendered int) int) ([]string, []int) {
out := make([]string, 0, len(chunks))
sources := make([]int, 0, len(chunks))
used := 0
@@ -208,7 +248,7 @@ func KBPromptWithSourceIndices(chunks []map[string]any, maxTokens int) ([]string
if c == nil {
continue
}
- block, ok := kbpBlock(c, len(out)+1)
+ block, ok := kbpBlock(c, idAt(idx, len(out)))
if !ok {
continue
}
diff --git a/internal/rag/prompts/generator_test.go b/internal/rag/prompts/generator_test.go
index 962676095d..fb25bb6e70 100644
--- a/internal/rag/prompts/generator_test.go
+++ b/internal/rag/prompts/generator_test.go
@@ -247,3 +247,75 @@ func TestKBPromptWithSourceIndicesTracksRenderedChunks(t *testing.T) {
t.Fatalf("KBPrompt = %v, want the same blocks as KBPromptWithSourceIndices", plain)
}
}
+
+// TestKBPromptNumbersFirstBlockOne pins kb_prompt's numbering: "ID: 1" is the
+// first rendered block. It is kept for parity with Python's kb_prompt — no Go
+// renderer whose numbers reach a citation marker uses it (the agentic compose and
+// the naive chat path both render 0-based, the numbering the client indexes).
+func TestKBPromptNumbersFirstBlockOne(t *testing.T) {
+ chunks := []map[string]any{
+ {"chunk_id": "c0", "content": "first body"},
+ {"chunk_id": "c1", "content": "second body"},
+ }
+ blocks := KBPrompt(chunks, 100000)
+ if len(blocks) != 2 {
+ t.Fatalf("blocks = %d, want 2", len(blocks))
+ }
+ if !strings.HasPrefix(blocks[0], "\nID: 1\n") {
+ t.Errorf("first block = %q, want it numbered \"ID: 1\"", blocks[0])
+ }
+ if !strings.HasPrefix(blocks[1], "\nID: 2\n") {
+ t.Errorf("second block = %q, want it numbered \"ID: 2\"", blocks[1])
+ }
+}
+
+// TestKBPromptPoolIndexedUsesChunkPositions pins the numbering the
+// summarize_document blocks use: the id is the chunk's position in the pool, not
+// its place among the rendered blocks. A chunk that renders no block (empty
+// content) must not shift the ids of the chunks after it, because a marker the
+// model echoes has to name the position the chat pipeline resolves citations in.
+func TestKBPromptPoolIndexedUsesChunkPositions(t *testing.T) {
+ chunks := []map[string]any{
+ {"chunk_id": "c0", "content": ""}, // renders no block
+ {"chunk_id": "c1", "content": "first body"},
+ {"chunk_id": "c2", "content": "second body"},
+ }
+ blocks, sources := KBPromptPoolIndexed(chunks, 100000)
+ if len(blocks) != 2 {
+ t.Fatalf("blocks = %d, want 2 (the empty chunk renders nothing)", len(blocks))
+ }
+ if want := []int{1, 2}; !reflect.DeepEqual(sources, want) {
+ t.Fatalf("sources = %v, want %v", sources, want)
+ }
+ if !strings.HasPrefix(blocks[0], "\nID: 1\n") {
+ t.Errorf("first block = %q, want the id of its chunk position (1), not its render position (0)", blocks[0])
+ }
+ if !strings.HasPrefix(blocks[1], "\nID: 2\n") {
+ t.Errorf("second block = %q, want \"ID: 2\"", blocks[1])
+ }
+}
+
+// TestKBPromptZeroBasedNumbersFirstBlockZero pins the numbering the agentic
+// compose renders: the client resolves a citation marker by using its number as
+// an index into reference.chunks, and the streamed answer reaches that client
+// before the pipeline could rewrite anything, so the model has to be shown — and
+// so write — the index itself.
+func TestKBPromptZeroBasedNumbersFirstBlockZero(t *testing.T) {
+ chunks := []map[string]any{
+ {"chunk_id": "c0", "content": "first body"},
+ {"chunk_id": "c1", "content": "second body"},
+ }
+ blocks, sources := KBPromptZeroBasedWithSourceIndices(chunks, 100000)
+ if want := []int{0, 1}; !reflect.DeepEqual(sources, want) {
+ t.Fatalf("sources = %v, want %v", sources, want)
+ }
+ if len(blocks) != 2 {
+ t.Fatalf("blocks = %d, want 2", len(blocks))
+ }
+ if !strings.HasPrefix(blocks[0], "\nID: 0\n") {
+ t.Errorf("first block = %q, want it numbered \"ID: 0\"", blocks[0])
+ }
+ if !strings.HasPrefix(blocks[1], "\nID: 1\n") {
+ t.Errorf("second block = %q, want it numbered \"ID: 1\"", blocks[1])
+ }
+}
diff --git a/internal/service/chat_pipeline.go b/internal/service/chat_pipeline.go
index e4f63698ee..37d42e5791 100644
--- a/internal/service/chat_pipeline.go
+++ b/internal/service/chat_pipeline.go
@@ -813,7 +813,7 @@ func (s *ChatPipelineService) AsyncChat(
if kwargs["store_history_messages"] == false {
history = messages
}
- hk, slotCites, harnessAnswer, hErr := s.retrieveViaHarness(ctx, question, kbs, docIDs, imageFiles, attachments, thinkingMode, chat.TenantID, chat.LLMID, chat.ID, webSearch, sink, thinkSink, harnessSystemPrompt, history)
+ hk, slotCites, citeChunkIDs, harnessAnswer, hErr := s.retrieveViaHarness(ctx, question, kbs, docIDs, imageFiles, attachments, thinkingMode, chat.TenantID, chat.LLMID, chat.ID, webSearch, sink, thinkSink, harnessSystemPrompt, history)
// The harness streams think-then-answer inside ONE compose call.
// Close the block here, once that call (and its trailing
// narration line) has returned: a reasoning-only run would
@@ -828,7 +828,7 @@ func (s *ChatPipelineService) AsyncChat(
if harnessAnswer != "" {
common.Info("harness produced final cited answer; short-circuiting",
zap.Int("answer_chars", len(harnessAnswer)))
- final := s.decorateHarnessAnswer(harnessAnswer, kbinfos, slotCites)
+ final := s.decorateHarnessAnswer(harnessAnswer, kbinfos, slotCites, citeChunkIDs)
final.Final = true
out <- final
return
@@ -2622,6 +2622,10 @@ func (s *ChatPipelineService) kbPrompt(kbinfos map[string]interface{}, maxTokens
continue
}
+ // The id is the chunk's pool position, which is also its index in the
+ // reference the client receives (chunksFormat(chunksRaw)), so a marker copied
+ // from a block opens exactly that passage. 0-based: the first block is "ID: 0".
+ // (The compose renders a ranked subset instead, hence its own CiteChunkIDs order.)
cnt := fmt.Sprintf("\nID: %d", i)
cnt += drawNode("Title", getMapString(ck, "docnm_kwd", "document_name"))
cnt += drawNode("URL", getMapString(ck, "url"))
@@ -2804,7 +2808,7 @@ func (s *ChatPipelineService) buildChatDriver(ctx context.Context, chat *entity.
// Returns the number of chunks that gained a vector.
//
// Skips:
-// - chunks that already have a non-empty `vector`
+// - chunks whose `vector` already carries signal (non-zero components)
// - chunks without a `chunk_id`
//
// Errors are non-fatal: caller logs and proceeds with whatever vectors
@@ -2815,7 +2819,10 @@ func (s *ChatPipelineService) buildChatDriver(ctx context.Context, chat *entity.
// Parameters:
// - tenantIDs: tenant ID(s) to derive index/table names (ragflow_<tid>).
// If empty, no fetch is attempted.
-func HydrateChunkVectors(ctx context.Context, kbinfos map[string]interface{}, tenantIDs []string, kbIDs []string, docEngine engine.DocEngine) (int, error) {
+// - dim: embedding dimension, used to name the q_{dim}_vec field. Zero means
+// "infer from the chunks"; the agentic evidence pool carries no chunk
+// vectors at all, so its caller passes the model dimension explicitly.
+func HydrateChunkVectors(ctx context.Context, kbinfos map[string]interface{}, tenantIDs []string, kbIDs []string, dim int, docEngine engine.DocEngine) (int, error) {
if kbinfos == nil {
return 0, nil
}
@@ -2830,22 +2837,27 @@ func HydrateChunkVectors(ctx context.Context, kbinfos map[string]interface{}, te
return 0, nil
}
- // Auto-detect vector dimension from chunks that already carry a
- // vector. If none do, there is nothing to hydrate against.
- var dim int
+ // A vector counts as present only when it carries signal. The agentic
+ // evidence pool reaches this function with no `vector` key at all (the
+ // runtime chunk shape has no vector field), and the naive path ships zero
+ // placeholders for chunks whose embedding was not selected. Python refills
+ // both, because _hydrate_chunk_vectors treats a chunk as already hydrated
+ // only when `any(x for x in v)` (dialog_service.py:93-95). A length check
+ // alone skips exactly the chunks hydration exists for, leaving them at zero
+ // similarity where they can never be cited.
+ if dim <= 0 {
+ dim = firstChunkVectorDim(chunksRaw)
+ }
var missing []string
for _, cm := range chunksRaw {
- if cv, ok := cm["vector"].([]float64); ok && len(cv) > 0 {
- if dim == 0 {
- dim = len(cv)
- }
+ if vectorHasSignal(chunkVector(cm)) {
continue
}
if cid, ok := cm["chunk_id"].(string); ok && cid != "" {
missing = append(missing, cid)
}
}
- if len(missing) == 0 || dim == 0 || len(tenantIDs) == 0 {
+ if len(missing) == 0 || dim <= 0 || len(tenantIDs) == 0 {
return 0, nil
}
@@ -2857,18 +2869,20 @@ func HydrateChunkVectors(ctx context.Context, kbinfos map[string]interface{}, te
return 0, err
}
- // Stitch the vectors back onto the chunks.
+ // Stitch the vectors back onto the chunks. FetchChunkVectors answers with
+ // zeros for chunks it could not resolve, so only a vector with signal counts
+ // as a hit.
hits := 0
for _, cm := range chunksRaw {
- if cv, ok := cm["vector"].([]float64); ok && len(cv) > 0 {
+ if vectorHasSignal(chunkVector(cm)) {
continue
}
cid, _ := cm["chunk_id"].(string)
if cid == "" {
continue
}
- vec, ok := vectors[cid]
- if !ok || len(vec) == 0 {
+ vec := vectors[cid]
+ if !vectorHasSignal(vec) {
continue
}
cm["vector"] = vec
@@ -2879,6 +2893,51 @@ func HydrateChunkVectors(ctx context.Context, kbinfos map[string]interface{}, te
return hits, nil
}
+// chunkVector reads a chunk's embedding vector in the shapes the in-process
+// layers produce ([]float64; []interface{} after a JSON round-trip).
+func chunkVector(cm map[string]interface{}) []float64 {
+ switch v := cm["vector"].(type) {
+ case []float64:
+ return v
+ case []interface{}:
+ out := make([]float64, 0, len(v))
+ for _, item := range v {
+ f, ok := item.(float64)
+ if !ok {
+ return nil
+ }
+ out = append(out, f)
+ }
+ return out
+ }
+ return nil
+}
+
+// vectorHasSignal reports whether a vector has any non-zero component, mirroring
+// Python's `any(x for x in v)` (dialog_service.py:94). A placeholder of zeros is
+// the shape both retrieval paths leave behind for a chunk whose embedding was
+// not selected, and it must be refilled rather than treated as hydrated.
+func vectorHasSignal(v []float64) bool {
+ for _, x := range v {
+ if x != 0 {
+ return true
+ }
+ }
+ return false
+}
+
+// firstChunkVectorDim returns the dimension carried by the first chunk vector,
+// which is the dimension naming the q_{dim}_vec field. Zero means no chunk
+// carries a vector at all.
+func firstChunkVectorDim(chunksRaw []map[string]interface{}) int {
+ for _, cm := range chunksRaw {
+ if v := chunkVector(cm); len(v) > 0 {
+ return len(v)
+ }
+ }
+ return 0
+}
+
// embeddingModelEmbedder adapts an EmbeddingModel to the Embedder interface.
type embeddingModelEmbedder struct {
embModel *modelModule.EmbeddingModel
@@ -2947,28 +3006,44 @@ func (s *ChatPipelineService) decorateAnswer(
chunksRaw, ok := kbinfos["chunks"].([]map[string]interface{})
if ok && len(chunksRaw) > 0 {
// P7 — _hydrate_chunk_vectors. Mirrors
- // dialog_service.py:794. If any chunk lacks a `vector`
- // field (true for the ES path; Infinity ships vectors
- // inline), fetch them in one batched engine call. We only
- // need this when we'll actually call insertCitations
- // (i.e., the LLM didn't already emit markers).
+ // dialog_service.py:794. Fetch the chunk embeddings insertCitations
+ // scores against, in one batched engine call. The agentic evidence
+ // pool reaches here with no `vector` key at all and the naive path
+ // 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 _, err := HydrateChunkVectors(ctx, kbinfos, tenantIDs, nil, engine.Get()); err != nil {
+ dim := firstChunkVectorDim(chunksRaw)
+ if dim <= 0 {
+ // One short probe encode buys the q_{dim}_vec field name.
+ // Every citation-scored answer already pays for a full
+ // sentence encode, so this is not a new call class.
+ probe, perr := (&embeddingModelEmbedder{embModel: embModel}).Encode(ctx, []string{"x"})
+ if perr == nil && len(probe) > 0 {
+ dim = len(probe[0])
+ }
+ }
+ if _, err := HydrateChunkVectors(ctx, kbinfos, tenantIDs, nil, dim, engine.Get()); err != nil {
common.Warn("hydrate chunk vectors failed", zap.Error(err))
}
}
if embModel != nil && !HasCitationMarkers(ans) {
- // Build chunkVectors aligned with chunksRaw.
+ // Build chunkVectors aligned with chunksRaw. A chunk that still
+ // has no vector scores as zeros — exactly what Python does when
+ // it substitutes [0.0]*dim on a dimension mismatch
+ // (rag/nlp/search.py:463-468): that chunk simply never wins a
+ // citation, instead of one vector-less chunk (an image chunk, a
+ // web snippet) suppressing every citation in the answer.
+ dim := firstChunkVectorDim(chunksRaw)
chunkVectors := make([][]float64, len(chunksRaw))
- allVec := len(chunksRaw) > 0
for i, cm := range chunksRaw {
- cv, _ := cm["vector"].([]float64)
- chunkVectors[i] = cv
- if len(cv) == 0 {
- allVec = false
+ cv := chunkVector(cm)
+ if len(cv) != dim {
+ cv = make([]float64, dim)
}
+ chunkVectors[i] = cv
}
- if allVec {
+ if dim > 0 {
embedder := &embeddingModelEmbedder{embModel: embModel}
if decorated, cited := InsertCitations(ctx, ans, NewSourcedChunks(chunksRaw), embedder, chunkVectors); len(cited) > 0 {
ans = decorated
@@ -3141,7 +3216,25 @@ func (s *ChatPipelineService) decorateAnswer(
// here. We only resolve the existing markers, repair bad formats, filter
// doc_aggs to the cited docs, and build the reference from the harness citation
// pool (chunks stripped of their vectors). Prompt stays empty, matching Python.
-func (s *ChatPipelineService) decorateHarnessAnswer(answer string, kbinfos map[string]interface{}, slotCitations map[string][]string) AsyncChatResult {
+//
+// The answer's [ID:n] markers index the evidence list the compose rendered for
+// the model — harness.Kbinfos.CiteChunkIDs, passed in as citeChunkIDs. That list
+// is similarity-ranked and capped, and it is numbered 0-based: the client resolves
+// a marker by using its number as an index into reference.chunks, and the text it
+// already streamed carries the model's own numbers (this function repairs and drops
+// markers, and rewrites the slot-table ones, but never renumbers a citation). The
+// reference is therefore built with that list in front, in the rendered order, so
+// marker n is the n-th rendered passage — and the rest of the pool follows so no
+// source is lost. Canonical "[ID:n]" markers that name nothing are dropped (a bare
+// "[5]" is left alone: in prose it may be a footnote or a year). The reference is
+// returned whenever the pool is non-empty, so an answer the model composed without
+// markers still carries its sources.
+//
+// Precondition for marker n to open passage n: the rendered blocks resolve to pool
+// chunks in order. A block whose id the pool does not know keeps its slot — an empty
+// entry the reference carries — so the later blocks keep their numbers; its own
+// citations are dropped, since the user could not open them.
+func (s *ChatPipelineService) decorateHarnessAnswer(answer string, kbinfos map[string]interface{}, slotCitations map[string][]string, citeChunkIDs []string) AsyncChatResult {
think := ""
ans := answer
if strings.Contains(answer, "</think>") {
@@ -3152,40 +3245,58 @@ 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)
+
+ // 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
+ // order, which is the pre-CiteChunkIDs behavior.
+ citeIdx := citePoolIdx(chunksRaw, citeChunkIDs)
+ citeChunks := citeChunksByIdx(citeIdx, chunksRaw)
// Slot-table markers ("[ID:Slot 0]") are internal references the compose
// model leaks from the Research Summary's slot draft — they index the slot
// table, not any chunk the user can open. Rewrite them into citations of
// the chunk that filled the slot (or drop them) BEFORE the marker scan, so
- // they resolve like any other citation.
- ans = RepairSlotCitations(ans, slotCitations, chunksRaw)
+ // they resolve like any other citation. The rewrite emits the evidence
+ // block's 0-based index in the rendered list, the numbering the client
+ // indexes.
+ ans = RepairSlotCitations(ans, slotCitations, citeChunks)
// Range-merged citations ("[ID:1-3]") are the model compressing
// consecutive individual citations on its own; expand them back so every
// marker resolves to exactly one chunk (out-of-range ranges are dropped).
- ans = ExpandRangeCitations(ans, len(chunksRaw))
+ // Bounds are 0-based indexes in the rendered list.
+ ans = ExpandRangeCitations(ans, len(citeChunks))
- // Collect existing [ID:N] markers from the harness answer. Python runs
- // normalize_arabic_digits then CITATION_MARKER_PATTERN (dialog_service.py:
- // 2103-2107) and bounds each index by len(chunks).
- citationIdx := make(map[int]struct{})
- for _, ci := range ExtractCitationMarkers(ans, len(chunksRaw)) {
- if ci >= 0 && ci < len(chunksRaw) {
- citationIdx[ci] = struct{}{}
- }
- }
- // repair_bad_citation_formats (dialog_service.py:2109), then re-scan so any
- // repaired markers are also honoured.
+ // repair_bad_citation_formats (dialog_service.py:2109), then resolve the
+ // 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.
ans = RepairBadCitationFormats(ans)
- for _, ci := range ExtractCitationMarkers(ans, len(chunksRaw)) {
- if ci >= 0 && ci < len(chunksRaw) {
- citationIdx[ci] = struct{}{}
- }
+ ans, citedIdx := ResolveCitationMarkers(ans, citeIdx)
+
+ // 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
+ // the model actually emitted, before any repair). No judgement is attached —
+ // see zeroBasedEvidenceRule for the prompt-side guard on the numbering.
+ if len(citeIdx) > 0 {
+ common.Info("agentic citation markers",
+ zap.Int("rendered_blocks", len(citeIdx)),
+ zap.Ints("raw_markers", rawMarkers),
+ zap.Int("resolved_blocks", len(citedIdx)),
+ // Which figure points at which chunk, and whether that chunk can show a
+ // picture at all: "why is there no image under Fig. n" is asked far more
+ // often than "the numbering was off", and from the page the two look the
+ // same. See citationAudit.
+ zap.String("cited", citationAudit(citeIdx, citedIdx, chunksRaw)),
+ )
}
- // Map cited chunk indices to doc_ids (dialog_service.py:2111-2122).
+ // Map cited chunks to doc_ids (dialog_service.py:2111-2122).
citedDocIDs := make(map[string]struct{})
- for ci := range citationIdx {
+ for _, ci := range citedIdx {
if ci >= 0 && ci < len(chunksRaw) {
if docID, ok := chunksRaw[ci]["doc_id"].(string); ok && docID != "" {
citedDocIDs[docID] = struct{}{}
@@ -3213,22 +3324,27 @@ func (s *ChatPipelineService) decorateHarnessAnswer(answer string, kbinfos map[s
}
}
- // refs = deepcopy(kbinfos) if doc_ids else [] ; drop each chunk's vector
- // (dialog_service.py:2129-2132). AsyncChatResult.Reference is a map, so an
- // empty Python [] maps to a nil map (callers/frontend already tolerate a
- // missing reference).
+ // 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.
var refs map[string]interface{}
- if len(citedDocIDs) > 0 {
- ref := make(map[string]interface{})
+ if len(chunksRaw) > 0 {
+ ref := make(map[string]interface{}, len(kbinfos))
for k, v := range kbinfos {
ref[k] = v
}
- if cRaw, ok := ref["chunks"].([]map[string]interface{}); ok {
- // 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(cRaw)
- }
+ // 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
}
@@ -3246,6 +3362,122 @@ func (s *ChatPipelineService) decorateHarnessAnswer(answer string, kbinfos map[s
}
}
+// citePoolIdx maps the ordered evidence list the compose rendered for the model
+// (harness.Kbinfos.CiteChunkIDs) onto the pool: entry i is the index in pool of
+// the i-th rendered block. A block whose id is empty or unknown stays in the list
+// as -1 so the blocks after it keep the number the model saw; its citations simply
+// cannot be resolved. An empty id list — a harness that published none — falls
+// back to pool order, where block i is pool[i].
+func citePoolIdx(pool []map[string]interface{}, ids []string) []int {
+ if len(ids) == 0 {
+ out := make([]int, len(pool))
+ for i := range pool {
+ out[i] = i
+ }
+ return out
+ }
+ byID := make(map[string]int, len(pool))
+ for i, c := range pool {
+ if id := chunkCitationID(c); id != "" {
+ if _, dup := byID[id]; !dup {
+ byID[id] = i
+ }
+ }
+ }
+ out := make([]int, len(ids))
+ for i, id := range ids {
+ idx, found := byID[id]
+ if id == "" || !found {
+ idx = -1
+ }
+ out[i] = idx
+ }
+ return out
+}
+
+// citeChunksByIdx gathers the rendered evidence list from its pool positions,
+// keeping -1 entries as nil so positions stay aligned with the numbering the model
+// was shown (a nil chunk matches no slot-citation evidence id and resolves no
+// marker, but it must not shift the blocks after it).
+func citeChunksByIdx(citeIdx []int, pool []map[string]interface{}) []map[string]interface{} {
+ out := make([]map[string]interface{}, len(citeIdx))
+ for i, pi := range citeIdx {
+ if pi >= 0 && pi < len(pool) {
+ out[i] = pool[pi]
+ }
+ }
+ return out
+}
+
+// referenceChunks orders the reference payload: the evidence list the compose
+// rendered first, in exactly that order, then the rest of the pool. The client
+// resolves a citation marker by indexing this list with the number the model
+// wrote, so the rendered order has to come first — and one entry per RENDERED
+// block, at the block's own index. A block whose chunk cannot be located in the
+// pool (or a repeat of one already placed) still occupies its slot, as a nil
+// chunk: dropping it would slide every later marker one position down, onto a
+// passage the model never cited. Such a block's marker is dropped from the answer
+// upstream, so the slot is a gap in the sources panel, not a broken citation.
+//
+// The trailing chunks keep every retrieved passage reachable from the sources
+// panel.
+func referenceChunks(citeIdx []int, pool []map[string]interface{}) []map[string]interface{} {
+ if len(citeIdx) == 0 {
+ return pool
+ }
+ placed := make(map[int]struct{}, len(citeIdx))
+ out := make([]map[string]interface{}, 0, len(pool)+len(citeIdx))
+ for _, pi := range citeIdx {
+ if pi < 0 || pi >= len(pool) {
+ out = append(out, nil)
+ continue
+ }
+ if _, dup := placed[pi]; dup {
+ out = append(out, nil)
+ continue
+ }
+ placed[pi] = struct{}{}
+ out = append(out, pool[pi])
+ }
+ for i, c := range pool {
+ if _, dup := placed[i]; dup {
+ continue
+ }
+ out = append(out, c)
+ }
+ return out
+}
+
+// citationAudit renders the resolved citations for the observability log: for every
+// cited pool chunk, the figure number(s) that point at it and whether the chunk
+// carries a picture.
+//
+// Fig. n is the client's label for rendered block n-1 (the marker's number plus
+// one), and the client draws a picture under a figure only when that chunk has an
+// image_id. So "Fig. n shows no image" has two very different explanations — the
+// marker resolved to a text-only chunk (expected, nothing to show) or the chunk
+// behind that figure is not the one the model cited (a resolution bug) — and they
+// look identical on the page. This line tells them apart without a re-run.
+func citationAudit(citeIdx, citedIdx []int, chunksRaw []map[string]interface{}) string {
+ cited := make(map[int]struct{}, len(citedIdx))
+ for _, ci := range citedIdx {
+ cited[ci] = struct{}{}
+ }
+ parts := make([]string, 0, len(citedIdx))
+ for block, ci := range citeIdx {
+ if _, ok := cited[ci]; !ok {
+ continue
+ }
+ if ci < 0 || ci >= len(chunksRaw) {
+ continue
+ }
+ imageID, _ := getChunkValue(chunksRaw[ci], "image_id", "img_id").(string)
+ parts = append(parts, fmt.Sprintf("Fig.%d=%s image=%t",
+ block+1, chunkCitationID(chunksRaw[ci]), imageID != ""))
+ }
+ return strings.Join(parts, " ")
+}
+
// langfuseExtractTimeElapsed extracts the time-elapsed + token-usage
// block from the prompt and applies the \n → " \n" substitution.
// Mirrors dialog_service.py:853-854:
@@ -3271,8 +3503,10 @@ func (s *ChatPipelineService) extractVisibleAnswer(text string) string {
// Mirrors Python's citation_prompt() in rag/prompts/generator.py.
func citationPrompt() string {
return "\n\n### Citation\nWhen answering, please cite sources using the format [ID:N] " +
- "(where N is the chunk number) after each sentence where the information from that chunk is used. " +
- "Cite each source individually ([ID:1][ID:2]); never merge consecutive citations into a range such as [ID:1-3]."
+ "after each sentence where the information from that chunk is used, " +
+ "where N is the id printed at the start of the evidence block (" +
+ "the blocks are numbered from 0, so the FIRST block is [ID:0]). " +
+ "Cite each source individually ([ID:0][ID:1]); never merge consecutive citations into a range such as [ID:1-3]."
}
// -----------------------------------------------------------------------
@@ -4723,10 +4957,12 @@ func chunksFormat(chunksRaw []map[string]interface{}) []map[string]interface{} {
return result
}
-// getChunkValue returns the first non-nil value from a chunk map, trying k1 first then k2.
-// Mirrors Python's get_value helper in rag/prompts/generator.py:37-38.
+// getChunkValue returns chunk[k1] when the key is present, otherwise chunk[k2].
+// Mirrors Python's get_value helper in rag/prompts/generator.py:37-38
+// (`d.get(k1, d.get(k2))`), which falls back on KEY ABSENCE only: a key present
+// with a nil/empty value is returned as-is rather than skipping to k2.
func getChunkValue(chunk map[string]interface{}, k1, k2 string) interface{} {
- if v, ok := chunk[k1]; ok && v != nil {
+ if v, ok := chunk[k1]; ok {
return v
}
return chunk[k2]
@@ -4817,6 +5053,11 @@ type HarnessResult struct {
// that filled it (RunResponse.SlotCitations). The citation decoration uses
// it to rewrite leaked "[ID:Slot N]" markers into locatable chunk citations.
SlotCitations map[string][]string
+ // CiteChunkIDs is the ordered evidence list the compose rendered for the
+ // model (RunResponse.CiteChunkIDs): similarity-ranked and capped, so it is
+ // NOT Chunks in pool order. The answer's "[ID:n]" markers index THIS list —
+ // resolving them against Chunks by position is what lost citations.
+ CiteChunkIDs []string
}
// harnessRetriever is wired at server bootstrap (cmd/ragflow_server.go:889) to
@@ -4880,9 +5121,9 @@ func harnessThinkSink(ctx context.Context, out chan<- AsyncChatResult) func(Thin
}
}
-func (s *ChatPipelineService) retrieveViaHarness(ctx context.Context, question string, kbs []*entity.Knowledgebase, docIDs []string, images []string, textAttachments string, thinkingMode, tenantID, modelID, sessionID string, webSearch func(context.Context, []string) ([]string, error), answerSink func(delta string, isThink bool), thinkSink func(ThinkEvent), dialogSystemPrompt string, messages []map[string]interface{}) (map[string]interface{}, map[string][]string, string, error) {
+func (s *ChatPipelineService) retrieveViaHarness(ctx context.Context, question string, kbs []*entity.Knowledgebase, docIDs []string, images []string, textAttachments string, thinkingMode, tenantID, modelID, sessionID string, webSearch func(context.Context, []string) ([]string, error), answerSink func(delta string, isThink bool), thinkSink func(ThinkEvent), dialogSystemPrompt string, messages []map[string]interface{}) (map[string]interface{}, map[string][]string, []string, string, error) {
if harnessRetriever == nil {
- return nil, nil, "", fmt.Errorf("harness retriever not wired at bootstrap")
+ return nil, nil, nil, "", fmt.Errorf("harness retriever not wired at bootstrap")
}
kbIDs := kbIDStrings(kbs)
// The "[Tool loop]" narration is NOT synthesized here. The outer react loop
@@ -4907,7 +5148,7 @@ func (s *ChatPipelineService) retrieveViaHarness(ctx context.Context, question s
SystemPrompt: dialogSystemPrompt,
})
if err != nil {
- return nil, nil, "", err
+ return nil, nil, nil, "", err
}
kbinfos := map[string]interface{}{
@@ -4921,7 +5162,7 @@ func (s *ChatPipelineService) retrieveViaHarness(ctx context.Context, question s
if res.PreSummary != "" {
kbinfos["pre_summary"] = res.PreSummary
}
- return kbinfos, res.SlotCitations, res.Answer, nil
+ return kbinfos, res.SlotCitations, res.CiteChunkIDs, res.Answer, nil
}
// toAnySlice widens a []map[string]any to []interface{} so the doc_aggs field
diff --git a/internal/service/chat_pipeline_test.go b/internal/service/chat_pipeline_test.go
index 7208551232..776acd06c9 100644
--- a/internal/service/chat_pipeline_test.go
+++ b/internal/service/chat_pipeline_test.go
@@ -451,7 +451,7 @@ func TestFallbackToLatestUser(t *testing.T) {
func TestHydrateChunkVectors_NoChunksNoop(t *testing.T) {
hits, err := HydrateChunkVectors(t.Context(),
map[string]interface{}{"chunks": []interface{}{}},
- nil, nil, nil,
+ nil, nil, 0, nil,
)
if err != nil {
t.Errorf("unexpected error: %v", err)
@@ -464,7 +464,7 @@ func TestHydrateChunkVectors_NoChunksNoop(t *testing.T) {
// TestHydrateChunkVectors_NilKbinfosNoop pins the no-op behavior of
// the hydration helper on nil kbinfos.
func TestHydrateChunkVectors_NilKbinfosNoop(t *testing.T) {
- hits, err := HydrateChunkVectors(t.Context(), nil, nil, nil, nil)
+ hits, err := HydrateChunkVectors(t.Context(), nil, nil, nil, 0, nil)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
@@ -473,6 +473,49 @@ func TestHydrateChunkVectors_NilKbinfosNoop(t *testing.T) {
}
}
+// TestVectorSignalHelpers pins the Python parity that a zero vector is NOT
+// hydrated content: dialog_service.py:93-95 skips a chunk only when
+// `any(x for x in v)`, so a placeholder of zeros must still be queued for the
+// engine fetch. Without that, the agentic path — whose evidence chunks carry no
+// `vector` key at all — left every chunk at zero similarity, and
+// insertCitations could never cite anything, so agentic answers came back with
+// no references while naive answers (real vectors inline) had them.
+func TestVectorSignalHelpers(t *testing.T) {
+ // No vector key at all: the agentic evidence shape.
+ agentic := map[string]interface{}{"chunk_id": "c1"}
+ if vectorHasSignal(chunkVector(agentic)) {
+ t.Fatal("chunk without a vector must not count as hydrated")
+ }
+ // Zero placeholder: the naive ES shape for an unselected embedding.
+ zero := map[string]interface{}{"vector": make([]float64, 4)}
+ if vectorHasSignal(chunkVector(zero)) {
+ t.Fatal("zero placeholder must not count as hydrated")
+ }
+ if got := firstChunkVectorDim([]map[string]interface{}{agentic, zero}); got != 4 {
+ t.Fatalf("firstChunkVectorDim = %d, want 4 (the placeholder length names the field)", got)
+ }
+ // Real vector in the []interface{} shape a JSON round-trip produces.
+ real := map[string]interface{}{"vector": []interface{}{0.0, 1.0, 0.0, 0.0}}
+ if !vectorHasSignal(chunkVector(real)) {
+ t.Fatal("non-zero vector must count as hydrated")
+ }
+ if got := firstChunkVectorDim([]map[string]interface{}{real}); got != 4 {
+ t.Fatalf("firstChunkVectorDim = %d, want 4", got)
+ }
+}
+
+// TestGetChunkValueFallsBackOnAbsenceOnly pins Python's get_value semantics
+// (`d.get(k1, d.get(k2))`, rag/prompts/generator.py:37-38): the fallback fires
+// on key ABSENCE only, so a key present with a nil value wins over k2.
+func TestGetChunkValueFallsBackOnAbsenceOnly(t *testing.T) {
+ if got := getChunkValue(map[string]interface{}{"content": nil, "content_with_weight": "body"}, "content", "content_with_weight"); got != nil {
+ t.Fatalf("got %v, want nil (a present key wins even when nil)", got)
+ }
+ if got := getChunkValue(map[string]interface{}{"content_with_weight": "body"}, "content", "content_with_weight"); got != "body" {
+ t.Fatalf("got %v, want body", got)
+ }
+}
+
// --- AsyncChatResult zero-value test ---
// TestAsyncChatResult_FinalFlagDefaultsFalse pins the zero-value
@@ -1589,7 +1632,7 @@ func TestRetrieveViaHarnessDoesNotSynthesizeLoopLines(t *testing.T) {
var thinks []bool
var events []ThinkEvent
s := &ChatPipelineService{}
- _, _, answer, err := s.retrieveViaHarness(t.Context(), "q", nil, nil, nil, "", "high", "t", "m", "sess", nil, collectSink(&got, &thinks), collectThinkSink(&events), "", history)
+ _, _, _, answer, err := s.retrieveViaHarness(t.Context(), "q", nil, nil, nil, "", "high", "t", "m", "sess", nil, collectSink(&got, &thinks), collectThinkSink(&events), "", history)
if err != nil {
t.Fatalf("retrieveViaHarness: %v", err)
}
@@ -1630,7 +1673,7 @@ func TestRetrieveViaHarnessForwardsThinkSink(t *testing.T) {
// channel) and the local `got` below is the EVENT, so leave answerSink nil.
var events []ThinkEvent
s := &ChatPipelineService{}
- if _, _, _, err := s.retrieveViaHarness(t.Context(), "q", nil, nil, nil, "", "high", "t", "m", "sess", nil, nil, collectThinkSink(&events), "", nil); err != nil {
+ if _, _, _, _, err := s.retrieveViaHarness(t.Context(), "q", nil, nil, nil, "", "high", "t", "m", "sess", nil, nil, collectThinkSink(&events), "", nil); err != nil {
t.Fatalf("retrieveViaHarness: %v", err)
}
// Only what the harness reported: the pipeline adds nothing of its own.
@@ -1687,7 +1730,7 @@ func TestRetrieveViaHarnessNaiveEmitsNothing(t *testing.T) {
var got []string
var thinks []bool
s := &ChatPipelineService{}
- if _, _, _, err := s.retrieveViaHarness(t.Context(), "q", nil, nil, nil, "", "naive", "t", "m", "sess", nil, collectSink(&got, &thinks), nil, "", nil); err != nil {
+ if _, _, _, _, err := s.retrieveViaHarness(t.Context(), "q", nil, nil, nil, "", "naive", "t", "m", "sess", nil, collectSink(&got, &thinks), nil, "", nil); err != nil {
t.Fatalf("retrieveViaHarness: %v", err)
}
if len(got) != 0 {
@@ -1714,7 +1757,7 @@ func TestDecorateHarnessAnswerReferenceUsesClientChunkShape(t *testing.T) {
}
s := &ChatPipelineService{}
- res := s.decorateHarnessAnswer("The answer [ID:0]", kbinfos, nil)
+ res := s.decorateHarnessAnswer("The answer [ID:0]", kbinfos, nil, nil)
if res.Reference == nil {
t.Fatal("a cited answer must carry a reference")
}
@@ -1735,6 +1778,61 @@ func TestDecorateHarnessAnswerReferenceUsesClientChunkShape(t *testing.T) {
}
}
+// TestReferenceChunksKeepsRenderedSlots pins the index contract of the reference
+// payload: the client resolves a marker by indexing reference.chunks with the
+// number the model wrote, so entry i has to be the chunk of rendered block i. A
+// block whose chunk cannot be located keeps its slot — dropping it would slide
+// every later marker onto a passage the model never cited.
+func TestReferenceChunksKeepsRenderedSlots(t *testing.T) {
+ pool := []map[string]interface{}{
+ {"chunk_id": "p0", "content": "zero"},
+ {"chunk_id": "p1", "content": "one"},
+ {"chunk_id": "p2", "content": "two"},
+ }
+ // Rendered blocks: pool 2, one that cannot be located, pool 0. The remaining
+ // pool chunk follows so nothing retrieved is lost.
+ got := referenceChunks([]int{2, -1, 0}, pool)
+ if len(got) != 4 {
+ t.Fatalf("reference = %d entries (%v), want 3 rendered slots plus the pool remainder", len(got), got)
+ }
+ if got[0]["chunk_id"] != "p2" || got[1] != nil || got[2]["chunk_id"] != "p0" {
+ t.Fatalf("reference = %v, want p2 at block 0, an empty slot at block 1, p0 at block 2", got)
+ }
+ if got[3]["chunk_id"] != "p1" {
+ t.Errorf("reference tail = %v, want the remaining pool chunk p1", got[3])
+ }
+}
+
+// TestReferenceChunksFallsBackToPoolOrder keeps the no-CiteChunkIDs behavior: the
+// pool order is then the numbering, so the reference is the pool untouched.
+func TestReferenceChunksFallsBackToPoolOrder(t *testing.T) {
+ pool := []map[string]interface{}{{"chunk_id": "p0"}, {"chunk_id": "p1"}}
+ got := referenceChunks(nil, pool)
+ if len(got) != 2 || got[0]["chunk_id"] != "p0" || got[1]["chunk_id"] != "p1" {
+ t.Fatalf("reference = %v, want the pool in order", got)
+ }
+}
+
+// TestCitationAuditReportsFigureChunkAndImage pins the observability line that
+// answers "why does Fig. n show no image": it names the chunk behind each cited
+// figure (Fig. n = rendered block n-1) and whether that chunk has an image, so a
+// text-only chunk (nothing to draw) is distinguishable from a marker that resolved
+// to the wrong chunk.
+func TestCitationAuditReportsFigureChunkAndImage(t *testing.T) {
+ chunks := []map[string]interface{}{
+ {"chunk_id": "c0", "image_id": "img-0"},
+ {"chunk_id": "c1"},
+ {"chunk_id": "c2", "img_id": "img-2"},
+ }
+ // Rendered blocks: pool 2, pool 0, pool 1 (Fig. 1..3). The model cited blocks 0
+ // and 2, so pool 2 (image) and pool 1 (no image) come back resolved.
+ got := citationAudit([]int{2, 0, 1}, []int{2, 1}, chunks)
+ want := "Fig.1=c2 image=true Fig.3=c1 image=false"
+ if got != want {
+ t.Errorf("citationAudit = %q, want %q", got, want)
+ }
+}
+
// TestDecorateHarnessAnswerRewritesSlotCitations is the end-to-end lock for
// the leaked "[ID:Slot 0]" bug: the harness answer arrives citing the internal
// slot table, and the final answer must cite the chunk that filled the slot
@@ -1754,12 +1852,13 @@ func TestDecorateHarnessAnswerRewritesSlotCitations(t *testing.T) {
"The tower opened in 1889 [ID:Slot 0].",
kbinfos,
map[string][]string{"0": {"c9"}},
+ nil,
)
if strings.Contains(res.Answer, "[ID:Slot") {
t.Fatalf("final answer still carries the internal slot citation: %q", res.Answer)
}
if !strings.Contains(res.Answer, "[ID:1]") {
- t.Fatalf("final answer must cite the evidence chunk pool position, got %q", res.Answer)
+ t.Fatalf("final answer must cite the evidence chunk pool index, got %q", res.Answer)
}
chunks, _ := res.Reference["chunks"].([]map[string]interface{})
// The reference keeps the whole citation pool (Python parity); it must at
@@ -1835,3 +1934,198 @@ func thinkEventWireKeys(t *testing.T, ev ThinkEvent) []string {
sort.Strings(keys)
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) {
+ 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",
+ "kb_id": "kb1",
+ },
+ },
+ "doc_aggs": []interface{}{
+ map[string]interface{}{"doc_id": "d1", "doc_name": "wolf.jpg"},
+ },
+ }
+
+ 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")
+ }
+ 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"])
+ }
+ // 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"])
+ }
+}
+
+// TestDecorateHarnessAnswerResolvesRenderedPosition is the regression lock for
+// the one-passage agentic answer that came back with no reference. The compose
+// numbers its evidence 0-based and the model writes that number, so the marker
+// and the reference line up: the passage it names is openable, and a marker that
+// names nothing is removed instead of being handed to the client.
+func TestDecorateHarnessAnswerResolvesRenderedPosition(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"},
+ },
+ }
+
+ s := &ChatPipelineService{}
+ res := s.decorateHarnessAnswer("小狼的颜色是灰色的 [ID:0]。", kbinfos, nil, []string{"c1"})
+ if !strings.Contains(res.Answer, "[ID:0]") {
+ t.Fatalf("the marker is the client's index and must survive, got %q", res.Answer)
+ }
+ if res.Reference == nil {
+ t.Fatal("a one-passage agentic answer must carry a 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 cited passage", res.Reference["chunks"])
+ }
+
+ // The old numbering (a 1-based position) names no entry in a one-passage
+ // reference, so it must be dropped rather than shipped as a dead marker.
+ res = s.decorateHarnessAnswer("小狼的颜色是灰色的 [ID:1]。", kbinfos, nil, []string{"c1"})
+ 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")
+ }
+}
+
+// TestDecorateHarnessAnswerOrdersReferenceByRenderedOrder pins the ordering half
+// of the contract: the compose renders the evidence similarity-ranked (and
+// capped), so rendered position 0 is not pool[0]. The reference must put the
+// rendered list first — a marker's index is its position there — and append the
+// rest of the pool.
+func TestDecorateHarnessAnswerOrdersReferenceByRenderedOrder(t *testing.T) {
+ kbinfos := map[string]interface{}{
+ "chunks": []map[string]interface{}{
+ {"chunk_id": "c1", "content_with_weight": "unrelated", "doc_id": "d1", "docnm_kwd": "Doc One"},
+ {"chunk_id": "c2", "content_with_weight": "unrelated too", "doc_id": "d1", "docnm_kwd": "Doc One"},
+ {"chunk_id": "c3", "content_with_weight": "eiffel", "doc_id": "d2", "docnm_kwd": "Doc Two"},
+ },
+ "doc_aggs": []interface{}{
+ map[string]interface{}{"doc_id": "d1", "doc_name": "Doc One"},
+ map[string]interface{}{"doc_id": "d2", "doc_name": "Doc Two"},
+ },
+ }
+
+ s := &ChatPipelineService{}
+ // The compose ranked c3 first, so the model's [ID:0] is c3.
+ res := s.decorateHarnessAnswer(
+ "The tower opened in 1889 [ID:0].",
+ kbinfos,
+ nil,
+ []string{"c3", "c1"},
+ )
+ chunks, _ := res.Reference["chunks"].([]map[string]interface{})
+ if len(chunks) != 3 {
+ t.Fatalf("reference chunks = %#v, want the rendered list plus the remaining pool", res.Reference["chunks"])
+ }
+ if chunks[0]["content"] != "eiffel" || chunks[0]["id"] != "c3" {
+ t.Fatalf("reference[0] = %#v, want the rendered first passage c3", chunks[0])
+ }
+ if chunks[1]["id"] != "c1" || chunks[2]["id"] != "c2" {
+ t.Fatalf("reference order = %#v, want [c3 c1 c2]", chunks)
+ }
+ // Only Doc Two (c3) was cited, so the other document is filtered out.
+ aggs, _ := res.Reference["doc_aggs"].([]interface{})
+ if len(aggs) != 1 {
+ t.Fatalf("reference doc_aggs = %#v, want only the cited document", res.Reference["doc_aggs"])
+ }
+ if dam, _ := aggs[0].(map[string]interface{}); dam["doc_id"] != "d2" {
+ t.Fatalf("reference doc_aggs = %#v, want d2", aggs[0])
+ }
+}
+
+// TestDecorateHarnessAnswerDropsOnlyCanonicalMarkers pins the text-safety rule: a
+// canonical citation naming no rendered passage is removed (the user could never
+// open it), while a bare bracketed number in prose is left alone — "[2024]" is far
+// more likely to be ordinary text than a citation, and deleting user-visible text
+// is worse than a chip that cannot open.
+func TestDecorateHarnessAnswerDropsOnlyCanonicalMarkers(t *testing.T) {
+ kbinfos := map[string]interface{}{
+ "chunks": []map[string]interface{}{
+ {"chunk_id": "c1", "content_with_weight": "one", "doc_id": "d1", "docnm_kwd": "Doc One"},
+ },
+ "doc_aggs": []interface{}{map[string]interface{}{"doc_id": "d1", "doc_name": "Doc One"}},
+ }
+
+ s := &ChatPipelineService{}
+ res := s.decorateHarnessAnswer(
+ "发布于 [2024] 年 [ID:0],并在 [ID:7] 修订。",
+ kbinfos,
+ nil,
+ []string{"c1"},
+ )
+ if !strings.Contains(res.Answer, "[2024]") {
+ t.Errorf("prose brackets must survive, got %q", res.Answer)
+ }
+ if !strings.Contains(res.Answer, "[ID:0]") {
+ t.Errorf("a resolvable citation must survive, got %q", res.Answer)
+ }
+ if strings.Contains(res.Answer, "[ID:7]") {
+ t.Errorf("an unresolvable canonical marker must be dropped, got %q", res.Answer)
+ }
+}
+
+// TestDecorateHarnessAnswerKeepsPositionsAfterUnknownBlock: when the harness
+// publishes an id the pool does not carry, that block keeps its slot so the blocks
+// after it keep the numbers the model saw. A marker naming a later block must
+// still resolve to the right chunk — and to the right document for doc_aggs.
+func TestDecorateHarnessAnswerKeepsPositionsAfterUnknownBlock(t *testing.T) {
+ kbinfos := map[string]interface{}{
+ "chunks": []map[string]interface{}{
+ {"chunk_id": "c1", "content_with_weight": "one", "doc_id": "d1", "docnm_kwd": "Doc One"},
+ {"chunk_id": "c2", "content_with_weight": "two", "doc_id": "d1", "docnm_kwd": "Doc One"},
+ {"chunk_id": "c3", "content_with_weight": "eiffel", "doc_id": "d2", "docnm_kwd": "Doc Two"},
+ },
+ "doc_aggs": []interface{}{
+ map[string]interface{}{"doc_id": "d1", "doc_name": "Doc One"},
+ map[string]interface{}{"doc_id": "d2", "doc_name": "Doc Two"},
+ },
+ }
+
+ s := &ChatPipelineService{}
+ // The harness rendered c1, an unknown chunk, then c3 — so [ID:2] is c3.
+ res := s.decorateHarnessAnswer(
+ "The tower opened in 1889 [ID:2].",
+ kbinfos,
+ nil,
+ []string{"c1", "gone", "c3"},
+ )
+ if !strings.Contains(res.Answer, "[ID:2]") {
+ t.Fatalf("a marker past the unknown block must survive, got %q", res.Answer)
+ }
+ aggs, _ := res.Reference["doc_aggs"].([]interface{})
+ if len(aggs) != 1 {
+ t.Fatalf("reference doc_aggs = %#v, want only the cited document", res.Reference["doc_aggs"])
+ }
+ if dam, _ := aggs[0].(map[string]interface{}); dam["doc_id"] != "d2" {
+ t.Fatalf("reference doc_aggs = %#v, want d2 (c3)", aggs[0])
+ }
+}
diff --git a/internal/service/chat_session.go b/internal/service/chat_session.go
index f2e80ea12b..7486f2cf81 100644
--- a/internal/service/chat_session.go
+++ b/internal/service/chat_session.go
@@ -2222,7 +2222,7 @@ func (s *ChatSessionService) chunksFormat(reference map[string]interface{}) []ma
for _, chunk := range raw {
out = append(out, map[string]interface{}{
"id": getValue(chunk, "chunk_id", "id"),
- "content": getValue(chunk, "content_with_weight", "content"),
+ "content": getValue(chunk, "content", "content_with_weight"),
"document_id": getValue(chunk, "doc_id", "document_id"),
"document_name": getValue(chunk, "docnm_kwd", "document_name"),
"dataset_id": getValue(chunk, "kb_id", "dataset_id"),
diff --git a/internal/service/chat_session_test.go b/internal/service/chat_session_test.go
index 46e99f0cc7..70f41207d1 100644
--- a/internal/service/chat_session_test.go
+++ b/internal/service/chat_session_test.go
@@ -1352,6 +1352,28 @@ func TestParseCollections_ReturnNilForMalformedData(t *testing.T) {
// chunksFormat tests — verifies field normalization after the rewrite.
// ===================================================================
+// TestChunksFormat_ContentPrefersContent pins Python's precedence in
+// chunks_format: `get_value(chunk, "content", "content_with_weight")`
+// (rag/prompts/generator.py:50) reads the display field FIRST and only falls
+// back to the engine spelling. The two normally hold the same string, so the
+// order is only observable when they differ.
+func TestChunksFormat_ContentPrefersContent(t *testing.T) {
+ svc := &ChatSessionService{}
+ result := svc.chunksFormat(map[string]interface{}{
+ "chunks": []map[string]interface{}{{
+ "chunk_id": "c1",
+ "content": "display text",
+ "content_with_weight": "engine body",
+ }},
+ })
+ if len(result) != 1 {
+ t.Fatalf("expected 1 chunk, got %d", len(result))
+ }
+ if got := result[0]["content"]; got != "display text" {
+ t.Fatalf("content=%v, want display text (Python reads content first)", got)
+ }
+}
+
func TestChunksFormat_NormalizesRawFieldNames(t *testing.T) {
svc := &ChatSessionService{}
ref := map[string]interface{}{
diff --git a/internal/service/citation.go b/internal/service/citation.go
index eec36c7700..b4a145bc34 100644
--- a/internal/service/citation.go
+++ b/internal/service/citation.go
@@ -61,7 +61,8 @@ var badCitationPatterns = []*regexp.Regexp{
// 4. Compute cosine similarity between each sentence and each chunk vector.
// 5. Threshold descent (0.63 → 0.3, ×0.8 per round): find chunks where
// similarity > max*0.99. Up to 4 chunks per sentence.
-// 6. Rebuild answer text with [ID:n] markers inserted after cited sentences.
+// 6. Rebuild answer text with [ID:n] markers inserted after cited sentences,
+// where n is the cited chunk's position in chunks (see applyCitations).
//
// Returns the decorated answer and the set of cited chunk indices.
func InsertCitations(ctx context.Context, answer string, chunks []SourcedChunk, embedder Embedder, chunkVectors [][]float64) (string, []int) {
@@ -153,6 +154,11 @@ func sentenceSplit(text string) []string {
// applyCitations rebuilds the answer text with [ID:n] markers inserted after
// each cited sentence position.
+//
+// n is the cited chunk's POSITION in chunks, not its chunk id: the caller returns
+// that same list as the reference, and the client resolves a marker by indexing it
+// with the number the marker carries. A chunk id (opaque text, only digits parse)
+// would come back as a dead marker on both consumers.
func applyCitations(answer string, sentences []string, sentenceIdx []int, cites map[int][]int, chunks []SourcedChunk) (string, []int) {
blocks := strings.Split(answer, "```")
var rawPieces []string
@@ -181,7 +187,7 @@ func applyCitations(answer string, sentences []string, sentenceIdx []int, cites
for _, ci := range chunkIdxs {
if ci < len(chunks) && !seenChunks[ci] {
seenChunks[ci] = true
- markers = append(markers, " [ID:"+chunks[ci].ID+"]")
+ markers = append(markers, " [ID:"+strconv.Itoa(ci)+"]")
citedIndices = append(citedIndices, ci)
}
}
@@ -380,10 +386,11 @@ var slotCitationPattern = regexp.MustCompile(`(?i)\[\s*(?:ID\s*[:: ]*\s*)?slot
// RepairSlotCitations rewrites leaked slot-table citations into locatable
// chunk citations. slotCitations maps a slot-table id ("0", "1", ...) to the
-// evidence chunk ids that produced the slot's candidate; chunks is the
-// citation pool the downstream decoration indexes ("[ID:N]" where N is a
-// 0-based position there). A marker whose slot has no evidence chunk in the
-// pool is dropped outright — an internal marker the user cannot resolve must
+// evidence chunk ids that produced the slot's candidate; chunks is the ordered
+// evidence list the compose rendered for the model, whose block ids are 0-based
+// ("[ID:N]" where N is the block's index in that list — the index the client
+// resolves against the reference). A marker whose slot has no evidence chunk in
+// the list is dropped outright — an internal marker the user cannot resolve must
// not reach the answer. Python parity note: the same leak exists upstream
// (agentic_rag.py:885 only repairs "(ID:5)" shapes; the pre_summary slot
// draft reaches Python's compose prompt too), so this repair is a Go-side
@@ -439,11 +446,12 @@ var rangeCitationPattern = regexp.MustCompile(`(?i)\[\s*ID\s*[:: ]*\s*([0-9]+)
// ExpandRangeCitations expands range-merged citations back into individual
// ones: [ID:1-3] → [ID:1][ID:2][ID:3]. A range expands only when BOTH bounds
-// name valid citation-pool positions (0-based, bounded by poolSize); reversed
-// bounds are swapped first. Anything out of range is dropped outright — an
-// unresolvable marker must not reach the user's answer, matching the
-// slot-citation repair above. poolSize is len(kbinfos chunks): the same space
-// the downstream [ID:N] resolution indexes.
+// name valid evidence positions (0-based, bounded by poolSize — the numbering
+// the compose renders and the client indexes); reversed bounds are swapped
+// first. Anything out of range is dropped outright — an unresolvable marker must
+// not reach the user's answer, matching the slot-citation repair above. poolSize
+// is the length of the ordered evidence list the compose rendered
+// (harness.Kbinfos.CiteChunkIDs).
func ExpandRangeCitations(answer string, poolSize int) string {
if answer == "" || !rangeCitationPattern.MatchString(answer) {
return answer
@@ -468,3 +476,102 @@ func ExpandRangeCitations(answer string, poolSize int) string {
b.WriteString(answer[last:])
return b.String()
}
+
+// ResolveCitationMarkers drops the answer's canonical [ID:n] markers that name
+// nothing and returns the pool positions of the markers that do resolve.
+//
+// citeIdx maps a rendered evidence position to its chunk's index in the pool the
+// reference is built from (see citePoolIdx); -1 is a block whose chunk could not
+// be located. The rendered blocks are numbered 0-based, so a marker's number IS
+// the client's index into the reference — resolvable markers therefore keep the
+// number the model wrote.
+//
+// Only a marker carrying the literal "ID:" prefix is removed when it cannot be
+// resolved: the compose's citation rules prescribe that form, while a bare "[5]"
+// in prose is as likely to be a footnote, a version or a year, and deleting
+// user-visible text is worse than leaving a marker the client cannot open.
+//
+// The returned positions are pool indexes, de-duplicated, in first-seen order.
+func ResolveCitationMarkers(answer string, citeIdx []int) (string, []int) {
+ if answer == "" || len(citeIdx) == 0 {
+ return answer, nil
+ }
+ var b strings.Builder
+ b.Grow(len(answer))
+ last := 0
+ seen := make(map[int]struct{})
+ var out []int
+ for _, m := range CitationMarkerPattern.FindAllStringSubmatchIndex(answer, -1) {
+ n, ok := markerNumber(answer[m[2]:m[3]])
+ resolved := -1
+ if ok && n >= 0 && n < len(citeIdx) {
+ resolved = citeIdx[n]
+ }
+ if resolved >= 0 {
+ if _, dup := seen[resolved]; !dup {
+ seen[resolved] = struct{}{}
+ out = append(out, resolved)
+ }
+ continue
+ }
+ if canonicalIDMarkerPattern.MatchString(answer[m[0]:m[1]]) {
+ // A canonical citation that names no rendered block: the user could
+ // never open it, so it must not reach the answer.
+ b.WriteString(answer[last:m[0]])
+ last = m[1]
+ }
+ }
+ b.WriteString(answer[last:])
+ return b.String(), out
+}
+
+// canonicalIDMarkerPattern matches a citation marker that carries the literal
+// "ID:" prefix — the form the citation rules prescribe, and the only one that can
+// be removed from the answer without risking ordinary text like "[2024]".
+var canonicalIDMarkerPattern = regexp.MustCompile(
+ `(?i)^\[\s*ID\s*[:: ]*\s*[0-9\x{0660}-\x{0669}\x{06F0}-\x{06F9}]+\s*\]$`)
+
+// rawCitationMarkers returns the citation numbers the model wrote, in order and
+// including duplicates — the repetition is what the citation observability log in
+// decorateHarnessAnswer looks at — capped at limit entries. They are read before
+// any repair, so they are what the model actually emitted.
+func rawCitationMarkers(answer string, limit int) []int {
+ if answer == "" || limit <= 0 {
+ return nil
+ }
+ out := make([]int, 0, limit)
+ for _, m := range CitationMarkerPattern.FindAllStringSubmatch(answer, -1) {
+ if len(m) < 2 {
+ continue
+ }
+ n, ok := markerNumber(m[1])
+ if !ok {
+ continue
+ }
+ out = append(out, n)
+ if len(out) >= limit {
+ break
+ }
+ }
+ return out
+}
+
+// markerNumber parses a citation marker's digits (Arabic-Indic digits accepted)
+// into a position. ok is false for an empty or non-numeric capture.
+func markerNumber(digits string) (int, bool) {
+ s := normalizeArabicDigits(digits)
+ if s == "" {
+ return 0, false
+ }
+ n := 0
+ for _, r := range s {
+ if r < '0' || r > '9' {
+ return 0, false
+ }
+ n = n*10 + int(r-'0')
+ if n > 1<<30 {
+ return 0, false
+ }
+ }
+ return n, true
+}
diff --git a/internal/service/citation_test.go b/internal/service/citation_test.go
index 2c03461b75..8cfc0808a7 100644
--- a/internal/service/citation_test.go
+++ b/internal/service/citation_test.go
@@ -212,11 +212,13 @@ func TestInsertCitationsWithVectors_Happy(t *testing.T) {
if len(cited) == 0 {
t.Fatal("expected citations")
}
- if !strings.Contains(answer, "[ID:abc123]") {
- t.Errorf("answer should contain [ID:abc123]: %q", answer)
+ // Markers carry the chunk's position in `chunks` (the list the caller returns
+ // as the reference), not its chunk id.
+ if !strings.Contains(answer, "[ID:0]") {
+ t.Errorf("answer should contain [ID:0]: %q", answer)
}
- if !strings.Contains(answer, "[ID:def456]") {
- t.Errorf("answer should contain [ID:def456]: %q", answer)
+ if !strings.Contains(answer, "[ID:1]") {
+ t.Errorf("answer should contain [ID:1]: %q", answer)
}
}
@@ -231,7 +233,7 @@ func TestApplyCitations(t *testing.T) {
chunks := []SourcedChunk{{ID: "c1"}}
cites := map[int][]int{0: {0}}
answer, cited := applyCitations("Hello world.", []string{"Hello world."}, []int{0}, cites, chunks)
- if answer != "Hello world. [ID:c1]" {
+ if answer != "Hello world. [ID:0]" {
t.Errorf("got %q", answer)
}
if len(cited) != 1 || cited[0] != 0 {
@@ -261,8 +263,8 @@ func TestInsertCitations_Happy(t *testing.T) {
if len(cited) == 0 {
t.Fatalf("expected citations, got none. answer=%q", answer)
}
- if !strings.Contains(answer, "[ID:abc123]") || !strings.Contains(answer, "[ID:def456]") {
- t.Errorf("missing [ID:*] markers: %q", answer)
+ if !strings.Contains(answer, "[ID:0]") || !strings.Contains(answer, "[ID:1]") {
+ t.Errorf("missing positional [ID:*] markers: %q", answer)
}
}
@@ -493,11 +495,13 @@ func TestDecorateHarnessAnswerExpandsRangeCitations(t *testing.T) {
},
}
s := &ChatPipelineService{}
- res := s.decorateHarnessAnswer("The range claim holds [ID:1-3].", kbinfos, nil)
+ res := s.decorateHarnessAnswer("The range claim holds [ID:1-3].", kbinfos, nil, nil)
if strings.Contains(res.Answer, "1-3") {
t.Fatalf("final answer still carries the range citation: %q", res.Answer)
}
+ // Expanded markers keep the 0-based indexes the client resolves against
+ // reference.chunks.
for _, want := range []string{"[ID:1]", "[ID:2]", "[ID:3]"} {
if !strings.Contains(res.Answer, want) {
t.Fatalf("final answer missing expanded citation %s: %q", want, res.Answer)
diff --git a/internal/service/kb_prompt.go b/internal/service/kb_prompt.go
index d6ffaa20cb..6b5b17a6f0 100644
--- a/internal/service/kb_prompt.go
+++ b/internal/service/kb_prompt.go
@@ -67,8 +67,8 @@ func KbPrompt(chunks []SourcedChunk, maxTokens int) string {
var b strings.Builder
used := 0
- for _, ck := range chunks {
- entry := formatChunkEntry(ck)
+ for i, ck := range chunks {
+ entry := formatChunkEntry(ck, i)
tokens := tokenizer.NumTokensFromString(entry)
if used+tokens > limit {
break
@@ -79,18 +79,23 @@ func KbPrompt(chunks []SourcedChunk, maxTokens int) string {
return b.String()
}
-// formatChunkEntry renders a single chunk as a tree-structured entry for the
-// LLM prompt. Format matches Python kb_prompt() in rag/prompts/generator.py:
+// formatChunkEntry renders a single chunk as a tree-structured entry for the LLM
+// prompt. Structure matches Python kb_prompt() in rag/prompts/generator.py:
//
-// ID: <id>
+// ID: <index>
// ├── Title: <doc_name>
// ├── URL: <url>
// ├── <metadata_key>: <metadata_value>
// └── Content:
// <chunk content>
-func formatChunkEntry(ck SourcedChunk) string {
+//
+// index is the chunk's position in the list the caller returns as the reference, and
+// is 0-based because that list is what a marker's number indexes (see citation.go) —
+// a chunk id would come back as a dead marker. Python's kb_prompt numbers the blocks
+// 1-based while its own resolver reads 0-based; Go keeps both on 0.
+func formatChunkEntry(ck SourcedChunk, index int) string {
var b strings.Builder
- fmt.Fprintf(&b, "ID: %s\n", ck.ID)
+ fmt.Fprintf(&b, "ID: %d\n", index)
if ck.DocName != "" {
fmt.Fprintf(&b, "├── Title: %s\n", ck.DocName)
}
diff --git a/internal/service/kb_prompt_test.go b/internal/service/kb_prompt_test.go
index 1eb8870f71..95871bd7df 100644
--- a/internal/service/kb_prompt_test.go
+++ b/internal/service/kb_prompt_test.go
@@ -32,10 +32,14 @@ func TestKbPrompt_Format(t *testing.T) {
if result == "" {
t.Fatal("expected non-empty prompt")
}
- // Verify ID appears
- if !contains(result, "ID: abc") {
+ // The ID line carries the chunk's position, not its chunk id: the client
+ // resolves a citation marker by indexing the reference with that number.
+ if !contains(result, "ID: 0") {
t.Errorf("missing ID line: %s", result)
}
+ if contains(result, "ID: abc") {
+ t.Errorf("the block id must be the chunk position, not the chunk id: %s", result)
+ }
// Verify title
if !contains(result, "Title: Test Document") {
t.Errorf("missing title: %s", result)
@@ -61,13 +65,13 @@ func TestKbPrompt_TokenLimit(t *testing.T) {
}
// Compute limit dynamically so the test works with both the C++
// tokenizer and the rune-based fallback.
- entryTokens := tokenizer.NumTokensFromString(formatChunkEntry(chunks[0]))
+ entryTokens := tokenizer.NumTokensFromString(formatChunkEntry(chunks[0], 0))
maxToks := int(float64(entryTokens+1) / 0.97) // just enough for first
result := KbPrompt(chunks, maxToks)
- if !contains(result, "ID: 1") {
+ if !contains(result, "ID: 0") {
t.Error("first chunk should be included")
}
- if contains(result, "ID: 2") {
+ if contains(result, "ID: 1") {
t.Error("second chunk should be excluded under tight limit")
}
}
@@ -121,13 +125,13 @@ func TestKbPrompt_TokenLimitAccurate(t *testing.T) {
{ID: "1", Content: "hello"},
{ID: "2", Content: "world"},
}
- entryTokens := tokenizer.NumTokensFromString(formatChunkEntry(chunks[0]))
+ entryTokens := tokenizer.NumTokensFromString(formatChunkEntry(chunks[0], 0))
maxToks := int(float64(entryTokens+1) / 0.97) // just enough for first entry
result := KbPrompt(chunks, maxToks)
- if !contains(result, "ID: 1") {
+ if !contains(result, "ID: 0") {
t.Error("first chunk should fit")
}
- if contains(result, "ID: 2") {
+ if contains(result, "ID: 1") {
t.Errorf("second chunk should be excluded: result = %q", result)
}
}
@@ -138,7 +142,7 @@ func TestKbPrompt_AllFit(t *testing.T) {
{ID: "2", Content: "b"},
}
result := KbPrompt(chunks, 1000)
- if !contains(result, "ID: 1") || !contains(result, "ID: 2") {
+ if !contains(result, "ID: 0") || !contains(result, "ID: 1") {
t.Error("both chunks should fit under generous limit")
}
}
diff --git a/internal/service/openai_chat.go b/internal/service/openai_chat.go
index 6a39a93b20..61f5d57b7a 100644
--- a/internal/service/openai_chat.go
+++ b/internal/service/openai_chat.go
@@ -664,7 +664,7 @@ func formatChunks(chunks []map[string]interface{}) []FormattedChunk {
for _, chunk := range chunks {
out = append(out, FormattedChunk{
ID: strVal(getValue(chunk, "chunk_id", "id")),
- Content: strVal(getValue(chunk, "content_with_weight", "content")),
+ Content: strVal(getValue(chunk, "content", "content_with_weight")),
DocumentID: strVal(getValue(chunk, "doc_id", "document_id")),
DocumentName: strVal(getValue(chunk, "docnm_kwd", "document_name")),
DatasetID: strVal(getValue(chunk, "kb_id", "dataset_id")),