RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
Fix agentic search bugs (#19615)
Yingfeng2477151c65b3
diff --git a/api/db/services/dialog_service.py b/api/db/services/dialog_service.pyindex 6ce31c5f8b..27ab24471b 100644--- a/api/db/services/dialog_service.py+++ b/api/db/services/dialog_service.py@@ -1884,13 +1884,38 @@ async def gen_mindmap(question, kb_ids, tenant_id, search_config={}):return mind_map.output+def _bound_dataset_names(dialog) -> str:+ """Comma-joined names of the dialog's bound datasets ("" when none).++ Exposed through the agentic graph's untrusted evidence block (appended to+ the "Evidence:" section of the compose user content): the prompt must+ still NAME the bound datasets, but as runtime data they must not be+ injected into the reasoning system prompt via ``{knowledge}`` — that+ placeholder is trusted-template content only. Defaulting ``{knowledge}``+ to the names rendered an empty `` `` `` on first turn (templates like+ "derived solely from this dataset: ``{knowledge}``"), which the outer+ model read as "the dataset is empty" — answering the canned "not found+ in the dataset!" line without ever calling the ``rag`` tool (first-turn+ short-circuit, observed 2026-09-14).+ """+ if not getattr(dialog, "kb_ids", None):+ return ""+ try:+ kbs = KnowledgebaseService.get_by_ids(dialog.kb_ids) or []+ except Exception: # noqa: BLE001 — names are cosmetic; never block the render+ return ""+ return ", ".join(kb.name for kb in kbs if getattr(kb, "name", ""))++def _render_reasoning_system_prompt(dialog, prompt_config: dict, kwargs: dict) -> str:"""Render the dialog-level system prompt for the reasoning agent path.Mirrors the substitutions ``async_chat`` performs for the non-reasoning pathso that configured system prompts are honored when reasoning is enabled.- The ``{knowledge}`` placeholder is defaulted to an empty string because the- agentic graph supplies retrieved evidence through its own evidence block.+ The ``{knowledge}`` placeholder is trusted-template content only: when the+ caller does not supply a value it renders empty. Bound dataset names are+ runtime data — they are exposed through the agentic graph's untrusted+ evidence block, never through the system prompt."""system = prompt_config.get("system", "")if not system:@@ -1902,7 +1927,6 @@ def _render_reasoning_system_prompt(dialog, prompt_config: dict, kwargs: dict) -param_keys = [p["key"] for p in prompt_config.get("parameters", [])]if dialog.kb_ids and "knowledge" not in param_keys and "{knowledge}" in system:param_keys.append("knowledge")- kwargs.setdefault("knowledge", "")for p in prompt_config.get("parameters", []):if p["key"] == "knowledge":@@ -1913,6 +1937,11 @@ def _render_reasoning_system_prompt(dialog, prompt_config: dict, kwargs: dict) -system = system.replace("{%s}" % p["key"], " ")fmt_kwargs = dict(kwargs)+ # {knowledge} is trusted-template content only: mutable runtime data+ # (bound dataset names) must never be injected into the system prompt+ # through it. The bound dataset names are exposed through the untrusted+ # evidence block instead (see the agentic compose in+ # rag/advanced_rag/agentic_rag_graph.py).fmt_kwargs.setdefault("knowledge", "")return system.format(**fmt_kwargs)@@ -2088,6 +2117,9 @@ async def rag_agent(dialog, messages, stream=True, **kwargs):rerank_candidates_count=dialog.rerank_candidates_count,top_k=dialog.top_k,)+ # Mutable runtime data: exposed via the untrusted evidence block in the+ # agentic compose, never via the reasoning system prompt's {knowledge}.+ rag_tools._bound_dataset_names = _bound_dataset_names(dialog)async def decorate_answer(answer):nonlocal rag_tools, messagesdiff --git a/cmd/ragflow_server.go b/cmd/ragflow_server.goindex 310b557d9a..0e085d38d6 100644--- a/cmd/ragflow_server.go+++ b/cmd/ragflow_server.go@@ -1054,7 +1054,7 @@ func startServer(ctx context.Context) {Images: req.Images,TextAttachments: req.TextAttachments,})- res := service.HarnessResult{Chunks: r.Chunks, DocAggs: r.DocAggs, Answer: r.Answer}+ res := service.HarnessResult{Chunks: r.Chunks, DocAggs: r.DocAggs, Answer: r.Answer, SlotCitations: r.SlotCitations}if r.Kbinfos != nil {res.Memory = r.Kbinfos.Memoryres.PreSummary = r.Kbinfos.PreSummarydiff --git a/internal/rag/advanced_rag/agentic_rag.go b/internal/rag/advanced_rag/agentic_rag.goindex 5f08d4284f..79966e495c 100644--- a/internal/rag/advanced_rag/agentic_rag.go+++ b/internal/rag/advanced_rag/agentic_rag.go@@ -572,6 +572,12 @@ type RunResponse struct {// Kbinfos carries the full accumulated state (including the lossless// memory store) for callers that need more than the summary above.Kbinfos *harness.Kbinfos+ // SlotCitations maps a slot-table id ("0", "1", ...) to the evidence+ // chunk ids that filled the slot. The chat pipeline's citation decoration+ // uses it to rewrite leaked "[ID:Slot N]" markers into citations of the+ // chunk the slot was filled from (those markers index the internal slot+ // table — nothing the user can open).+ SlotCitations map[string][]string}// AnswerSink forwards a partially produced answer while the model is stilldiff --git a/internal/rag/advanced_rag/agentic_rag_graph.go b/internal/rag/advanced_rag/agentic_rag_graph.goindex 0c49d3854f..f8ebd75c1f 100644--- a/internal/rag/advanced_rag/agentic_rag_graph.go+++ b/internal/rag/advanced_rag/agentic_rag_graph.go@@ -3548,6 +3548,17 @@ func NewAgenticLoop() AgenticLoop {// Surface the loop's outcome on the RunResponse.resp.Slots = append(resp.Slots, st.SlotTable.State...)+ // Slot citations: the chat pipeline's citation decoration rewrites+ // leaked "[ID:Slot N]" markers with these evidence chunk ids. Plain+ // ids (not positions) so the mapping survives the caller's later+ // evidence-pool narrowing (selectEvidence).+ if len(st.SlotEvidence) > 0 {+ sc := make(map[string][]string, len(st.SlotEvidence))+ for sid, ev := range st.SlotEvidence {+ sc[sid] = ev.EvidenceIDs+ }+ resp.SlotCitations = sc+ }// Research findings: the SCA-reviewed draft. NOTE: this is the research// draft, NOT the final answer — RAGTools.Run composes the final cited// answer afterwards from KB.PreSummary (see composeFinalAnswer), exactlydiff --git a/internal/rag/prompts/citation_prompt.md b/internal/rag/prompts/citation_prompt.mdindex 3db2814289..ca9b208a78 100644--- a/internal/rag/prompts/citation_prompt.md+++ b/internal/rag/prompts/citation_prompt.md@@ -4,6 +4,7 @@ Based on the provided document or chat history, add citations to the input text## Technical Rules:- Use format: [ID:i] or [ID:i] [ID:j] for multiple sources+- Cite each source INDIVIDUALLY: [ID:i][ID:j]. NEVER merge consecutive citations into a range such as [ID:i-j] or [ID:i~j]- Place citations at the end of sentences, before punctuation- Maximum 4 citations per sentence- DO NOT cite content not from <context></context>@@ -120,3 +121,4 @@ REMEMBER:- When in doubt, ask: "Would a fact-checker need to verify this?"- Place citations at sentence end, before punctuation- Format likes this is FORBIDDEN: [ID:0, ID:5, ID:...]. It MUST be separated like, [ID:0][ID:5]...+- Range citations like [ID:1-3] are FORBIDDEN too. Multiple sources MUST be spelled out individually: [ID:1][ID:2][ID:3].diff --git a/internal/rag/prompts/generator.go b/internal/rag/prompts/generator.goindex 8078dda229..d60c5caae0 100644--- a/internal/rag/prompts/generator.go+++ b/internal/rag/prompts/generator.go@@ -113,7 +113,8 @@ var DefaultCiteRules = CitationPrompt("")// appends after rendering CITATION_PROMPT_TEMPLATE: the examples in the// template carry illustrative IDs, and without this the model can emit them// instead of the real chunk IDs it was given.-const citationIDSuffix = "\n\nIMPORTANT: The example IDs above (45, 46, 78, etc.) are illustrative only. Use the actual chunk IDs from the provided knowledge blocks."+const citationIDSuffix = "\n\nIMPORTANT: The example IDs above (45, 46, 78, etc.) are illustrative only. Use the actual chunk IDs from the provided knowledge blocks." ++ " Cite each source INDIVIDUALLY as [ID:i][ID:j]; NEVER merge consecutive citations into a range such as [ID:i-j]."// CitationPrompt mirrors Python rag/prompts/generator.citation_prompt: return// the citation-rules text the final answer must follow. The user-defineddiff --git a/internal/service/chat_harness_prompt_test.go b/internal/service/chat_harness_prompt_test.gonew file mode 100644index 0000000000..df9ca93bbd--- /dev/null+++ b/internal/service/chat_harness_prompt_test.go@@ -0,0 +1,91 @@+package service++import (+ "strings"+ "testing"++ "ragflow/internal/entity"+)++// Regression lock for the first-turn empty dataset binding: the agentic+// (reasoning) system prompt used to render {knowledge} as "" — the web UI's+// default template then read "derived solely from this dataset: ``" and the+// outer model answered the canned "not found in the dataset!" line without+// calling the terminal `rag` tool (logs/api_server_9384.log 2026-09-14+// 13:50:14: answer_chars=59, zero graph LLM calls).++func TestHarnessBoundDatasetNames(t *testing.T) {+ cases := []struct {+ name string+ kbs []*entity.Knowledgebase+ want string+ }{+ {"nil", nil, ""},+ {"empty", []*entity.Knowledgebase{}, ""},+ {+ "filters nil and unnamed",+ []*entity.Knowledgebase{nil, {ID: "kb1"}, {ID: "kb2", Name: "三国演义"}},+ "三国演义",+ },+ {+ "joins multiple names",+ []*entity.Knowledgebase{{Name: "三国演义"}, {Name: "三国志"}},+ "三国演义, 三国志",+ },+ }+ for _, c := range cases {+ t.Run(c.name, func(t *testing.T) {+ if got := harnessBoundDatasetNames(c.kbs); got != c.want {+ t.Fatalf("harnessBoundDatasetNames(%v) = %q, want %q", c.kbs, got, c.want)+ }+ })+ }+}++// TestHarnessSystemPromptFirstTurnNonEmpty pins the end-to-end first-turn+// render: with datasets bound, the dialog system prompt's {knowledge}+// placeholder (backtick-wrapped in the web UI default) must carry the bound+// dataset names — never an empty “ binding.+func TestHarnessSystemPromptFirstTurnNonEmpty(t *testing.T) {+ // The web UI default template (web/src/locales en.ts systemInitialValue):+ // the placeholder is wrapped in backticks.+ template := "You are an intelligent assistant. Your primary function is to answer questions based strictly on the provided knowledge base.\n\n**Essential Rules:**\n - Your answer must be derived **solely** from this dataset: `{knowledge}`.\n"++ s := &ChatPipelineService{}+ kws := map[string]interface{}{+ "date": "2026-09-14 06:00:00",+ }+ if _, ok := kws["knowledge"]; !ok {+ kws["knowledge"] = harnessBoundDatasetNames([]*entity.Knowledgebase{+ {ID: "kb1", Name: "三国演义"},+ })+ }+ rendered := s.formatPrompt(template, kws)++ if strings.Contains(rendered, "``") {+ t.Fatalf("first-turn system prompt rendered an empty dataset binding:\n%s", rendered)+ }+ if !strings.Contains(rendered, "三国演义") {+ t.Fatalf("first-turn system prompt does not carry the bound dataset name:\n%s", rendered)+ }+ if strings.Contains(rendered, "{knowledge}") {+ t.Fatalf("{knowledge} placeholder was not substituted:\n%s", rendered)+ }+}++// TestHarnessSystemPromptCallerSuppliedKnowledgeWins: a caller-supplied+// knowledge value must not be overwritten by the dataset-name default.+func TestHarnessSystemPromptCallerSuppliedKnowledgeWins(t *testing.T) {+ s := &ChatPipelineService{}+ kws := map[string]interface{}{"knowledge": "caller evidence"}+ if _, ok := kws["knowledge"]; !ok {+ kws["knowledge"] = harnessBoundDatasetNames([]*entity.Knowledgebase{{Name: "三国演义"}})+ }+ rendered := s.formatPrompt("Context: {knowledge}", kws)+ if !strings.Contains(rendered, "caller evidence") {+ t.Fatalf("caller-supplied knowledge was overwritten:\n%s", rendered)+ }+ if strings.Contains(rendered, "三国演义") {+ t.Fatalf("dataset-name default leaked over the caller-supplied value:\n%s", rendered)+ }+}diff --git a/internal/service/chat_pipeline.go b/internal/service/chat_pipeline.goindex ba67310463..e7661b9f3a 100644--- a/internal/service/chat_pipeline.go+++ b/internal/service/chat_pipeline.go@@ -743,7 +743,12 @@ func (s *ChatPipelineService) AsyncChat(// dialog, prompt_config, kwargs) to RAGTools// (dialog_service.py:2084): the dialog system prompt rendered// with the caller kwargs, a UTC date, and {knowledge} defaulted- // to "" — the agentic graph supplies evidence itself.+ // to the BOUND DATASET NAMES — the agentic graph supplies the+ // evidence itself, but an empty binding made the outer model+ // read the prompt as "the dataset is empty" and answer the+ // canned "not found in the dataset!" line without calling the+ // terminal `rag` tool (first-turn short-circuit, observed+ // 2026-09-14: answer_chars=59, zero graph LLM calls).harnessSystemPrompt := ""if sp, ok := chat.PromptConfig["system"].(string); ok && sp != "" {kws := make(map[string]interface{}, len(kwargs)+2)@@ -752,11 +757,11 @@ func (s *ChatPipelineService) AsyncChat(}kws["date"] = time.Now().UTC().Format("2006-01-02 15:04:05")if _, ok := kws["knowledge"]; !ok {- kws["knowledge"] = ""+ kws["knowledge"] = harnessBoundDatasetNames(kbs)}harnessSystemPrompt = s.formatPrompt(sp, kws)}- hk, harnessAnswer, hErr := s.retrieveViaHarness(ctx, question, kbs, docIDs, imageFiles, attachments, thinkingMode, chat.TenantID, chat.LLMID, chat.ID, webSearch, sink, harnessSystemPrompt)+ hk, slotCites, harnessAnswer, hErr := s.retrieveViaHarness(ctx, question, kbs, docIDs, imageFiles, attachments, thinkingMode, chat.TenantID, chat.LLMID, chat.ID, webSearch, sink, harnessSystemPrompt)// 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@@ -771,7 +776,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)+ final := s.decorateHarnessAnswer(harnessAnswer, kbinfos, slotCites)final.Final = trueout <- finalreturn@@ -3079,7 +3084,7 @@ 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{}) AsyncChatResult {+func (s *ChatPipelineService) decorateHarnessAnswer(answer string, kbinfos map[string]interface{}, slotCitations map[string][]string) AsyncChatResult {think := ""ans := answerif strings.Contains(answer, "</think>") {@@ -3091,6 +3096,18 @@ func (s *ChatPipelineService) decorateHarnessAnswer(answer string, kbinfos map[schunksRaw, _ := kbinfos["chunks"].([]map[string]interface{})+ // 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)++ // 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))+// 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).@@ -3197,7 +3214,8 @@ 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."+ "(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]."}// -----------------------------------------------------------------------@@ -4657,6 +4675,25 @@ func getChunkValue(chunk map[string]interface{}, k1, k2 string) interface{} {return chunk[k2]}+// harnessBoundDatasetNames renders the {knowledge} default for the agentic+// (reasoning) system prompt: the comma-joined names of the chat's bound+// datasets. Python parity: dialog_service._render_reasoning_system_prompt /+// _bound_dataset_names. The agentic graph supplies the retrieved evidence+// itself, but the prompt must still NAME the bound datasets — defaulting the+// placeholder to "" left the web UI's default template rendering as+// "derived solely from this dataset: “", which the outer model read as an+// empty dataset and answered the canned "not found in the dataset!" line+// without ever calling the terminal `rag` tool.+func harnessBoundDatasetNames(kbs []*entity.Knowledgebase) string {+ names := make([]string, 0, len(kbs))+ for _, kb := range kbs {+ if kb != nil && kb.Name != "" {+ names = append(names, kb.Name)+ }+ }+ return strings.Join(names, ", ")+}+// HarnessRequest carries the minimal inputs the chat pipeline hands to the// agentic-RAG harness for evidence collection.type HarnessRequest struct {@@ -4714,6 +4751,10 @@ type HarnessResult struct {// it directly as the reply instead of re-generating via a second model pass —// mirroring Python's terminal `rag` tool.Answer string+ // SlotCitations maps a slot-table id ("0", ...) to the evidence chunk ids+ // 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}// harnessRetriever is wired at server bootstrap (cmd/ragflow_server.go:889) to@@ -4766,9 +4807,9 @@ func toolLoopLine(sink func(delta string, isThink bool), line string) {sink(line+thinkLineBreak, true)}-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), dialogSystemPrompt string) (map[string]interface{}, 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), dialogSystemPrompt string) (map[string]interface{}, map[string][]string, string, error) {if harnessRetriever == nil {- return nil, "", fmt.Errorf("harness retriever not wired at bootstrap")+ return nil, nil, "", fmt.Errorf("harness retriever not wired at bootstrap")}kbIDs := kbIDStrings(kbs)// Only non-naive modes run the agentic loop these lines describe, and only@@ -4793,7 +4834,7 @@ func (s *ChatPipelineService) retrieveViaHarness(ctx context.Context, question sSystemPrompt: dialogSystemPrompt,})if err != nil {- return nil, "", err+ return nil, nil, "", err}if narrate {// Python distinguishes the terminal tool from an observation: an answer@@ -4816,7 +4857,7 @@ func (s *ChatPipelineService) retrieveViaHarness(ctx context.Context, question sif res.PreSummary != "" {kbinfos["pre_summary"] = res.PreSummary}- return kbinfos, res.Answer, nil+ return kbinfos, res.SlotCitations, res.Answer, nil}// toAnySlice widens a []map[string]any to []interface{} so the doc_aggs fielddiff --git a/internal/service/chat_pipeline_test.go b/internal/service/chat_pipeline_test.goindex aa167dd8d2..3fbbc55f11 100644--- a/internal/service/chat_pipeline_test.go+++ b/internal/service/chat_pipeline_test.go@@ -1569,7 +1569,7 @@ func TestRetrieveViaHarnessEmitsToolLoopLines(t *testing.T) {var got []stringvar thinks []bools := &ChatPipelineService{}- _, answer, err := s.retrieveViaHarness(context.Background(), "q", nil, nil, nil, "", "high", "t", "m", "sess", nil, collectSink(&got, &thinks), "")+ _, _, answer, err := s.retrieveViaHarness(context.Background(), "q", nil, nil, nil, "", "high", "t", "m", "sess", nil, collectSink(&got, &thinks), "")if err != nil {t.Fatalf("retrieveViaHarness: %v", err)}@@ -1611,7 +1611,7 @@ func TestRetrieveViaHarnessToolLoopObservation(t *testing.T) {var got []stringvar thinks []bools := &ChatPipelineService{}- if _, _, err := s.retrieveViaHarness(context.Background(), "q", nil, nil, nil, "", "high", "t", "m", "sess", nil, collectSink(&got, &thinks), ""); err != nil {+ if _, _, _, err := s.retrieveViaHarness(context.Background(), "q", nil, nil, nil, "", "high", "t", "m", "sess", nil, collectSink(&got, &thinks), ""); err != nil {t.Fatalf("retrieveViaHarness: %v", err)}joined := strings.Join(got, "")@@ -1631,7 +1631,7 @@ func TestRetrieveViaHarnessNaiveSkipsToolLoop(t *testing.T) {var got []stringvar thinks []bools := &ChatPipelineService{}- if _, _, err := s.retrieveViaHarness(context.Background(), "q", nil, nil, nil, "", "naive", "t", "m", "sess", nil, collectSink(&got, &thinks), ""); err != nil {+ if _, _, _, err := s.retrieveViaHarness(context.Background(), "q", nil, nil, nil, "", "naive", "t", "m", "sess", nil, collectSink(&got, &thinks), ""); err != nil {t.Fatalf("retrieveViaHarness: %v", err)}if len(got) != 0 {@@ -1663,7 +1663,7 @@ func TestDecorateHarnessAnswerReferenceUsesClientChunkShape(t *testing.T) {}s := &ChatPipelineService{}- res := s.decorateHarnessAnswer("The answer [ID:0]", kbinfos)+ res := s.decorateHarnessAnswer("The answer [ID:0]", kbinfos, nil)if res.Reference == nil {t.Fatal("a cited answer must carry a reference")}@@ -1683,3 +1683,43 @@ func TestDecorateHarnessAnswerReferenceUsesClientChunkShape(t *testing.T) {t.Error("the shared source chunk lost its vector: the reference must not mutate it")}}++// 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+// instead — with that chunk present in the reference payload.+func TestDecorateHarnessAnswerRewritesSlotCitations(t *testing.T) {+ kbinfos := map[string]interface{}{+ "chunks": []map[string]interface{}{+ {"chunk_id": "c1", "content_with_weight": "unrelated", "doc_id": "d0", "docnm_kwd": "Other"},+ {"chunk_id": "c9", "content_with_weight": "eiffel", "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(+ "The tower opened in 1889 [ID:Slot 0].",+ kbinfos,+ map[string][]string{"0": {"c9"}},+ )+ 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)+ }+ chunks, _ := res.Reference["chunks"].([]map[string]interface{})+ // The reference keeps the whole citation pool (Python parity); it must at+ // least carry the slot's evidence chunk so [ID:1] is openable.+ found := false+ for _, c := range chunks {+ if c["content"] == "eiffel" {+ found = true+ }+ }+ if !found {+ t.Fatalf("reference must carry the slot's evidence chunk, got %#v", res.Reference["chunks"])+ }+}diff --git a/internal/service/citation.go b/internal/service/citation.goindex d427070b74..eec36c7700 100644--- a/internal/service/citation.go+++ b/internal/service/citation.go@@ -20,6 +20,7 @@ import ("context""math""regexp"+ "strconv""strings")@@ -367,3 +368,103 @@ func RepairBadCitationFormats(answer string) string {}return working}++// slotCitationPattern matches slot-table citations that leak into the final+// answer. The compose prompt's Research Summary renders the slot draft+// ("- slot 0 [entity]: ..."), and models occasionally cite those internal+// lines with the citation format instead of the evidence blocks:+// "[ID:Slot 0]", "[ID: slot 0]", "[Slot 0]". Such markers index the internal+// slot table — no chunk the user can open — so they must be rewritten into+// citations of the chunk the slot was filled from, or dropped.+var slotCitationPattern = regexp.MustCompile(`(?i)\[\s*(?:ID\s*[:: ]*\s*)?slot\s*([0-9]+)\s*\]`)++// 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+// 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+// decoration-layer fix under the "every citation must locate a chunk" rule.+func RepairSlotCitations(answer string, slotCitations map[string][]string, chunks []map[string]interface{}) string {+ if answer == "" || len(slotCitations) == 0 || !slotCitationPattern.MatchString(answer) {+ return answer+ }+ posByID := make(map[string]int, len(chunks))+ for i, c := range chunks {+ if id := chunkCitationID(c); id != "" {+ if _, dup := posByID[id]; !dup {+ posByID[id] = i+ }+ }+ }+ var b strings.Builder+ b.Grow(len(answer))+ last := 0+ for _, m := range slotCitationPattern.FindAllStringSubmatchIndex(answer, -1) {+ slotID := answer[m[2]:m[3]]+ repl := ""+ for _, eid := range slotCitations[slotID] {+ if pos, ok := posByID[eid]; ok {+ repl = "[ID:" + strconv.Itoa(pos) + "]"+ break+ }+ }+ b.WriteString(answer[last:m[0]])+ b.WriteString(repl)+ last = m[1]+ }+ b.WriteString(answer[last:])+ return b.String()+}++// chunkCitationID returns a pool chunk's id under the keys the harness uses+// (chunk_id, then id — harness.ChunkIDOf's order).+func chunkCitationID(c map[string]interface{}) string {+ for _, k := range []string{"chunk_id", "id"} {+ if s, ok := c[k].(string); ok && s != "" {+ return s+ }+ }+ return ""+}++// rangeCitationPattern matches range-merged citations: "[ID:1-3]", "[ID: 1 - 3]",+// "[ID:1~3]". Models sometimes compress consecutive individual citations+// ([ID:1][ID:2][ID:3]) into such a range on their own — no code path produces+// it, and no chunk resolves through it, so it must be normalized defensively.+var rangeCitationPattern = regexp.MustCompile(`(?i)\[\s*ID\s*[:: ]*\s*([0-9]+)\s*[-–—~~]\s*([0-9]+)\s*\]`)++// 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.+func ExpandRangeCitations(answer string, poolSize int) string {+ if answer == "" || !rangeCitationPattern.MatchString(answer) {+ return answer+ }+ var b strings.Builder+ b.Grow(len(answer) * 2)+ last := 0+ for _, m := range rangeCitationPattern.FindAllStringSubmatchIndex(answer, -1) {+ a, _ := strconv.Atoi(normalizeArabicDigits(answer[m[2]:m[3]]))+ c, _ := strconv.Atoi(normalizeArabicDigits(answer[m[4]:m[5]]))+ if a > c {+ a, c = c, a+ }+ b.WriteString(answer[last:m[0]])+ if a >= 0 && c < poolSize {+ for i := a; i <= c; i++ {+ b.WriteString("[ID:" + strconv.Itoa(i) + "]")+ }+ }+ last = m[1]+ }+ b.WriteString(answer[last:])+ return b.String()+}diff --git a/internal/service/citation_test.go b/internal/service/citation_test.goindex 6d9e446a08..2c03461b75 100644--- a/internal/service/citation_test.go+++ b/internal/service/citation_test.go@@ -333,3 +333,179 @@ func TestRepairBadCitationFormats(t *testing.T) {}}}++// -----------------------------------------------------------------------+// RepairSlotCitations — leaked "[ID:Slot N]" markers (Bug: slot-table+// citations in the final answer, unlocatable by the user).+// -----------------------------------------------------------------------++func TestRepairSlotCitations_MapsSlotToEvidenceChunk(t *testing.T) {+ chunks := []map[string]interface{}{+ {"chunk_id": "c1", "content": "one"},+ {"chunk_id": "c2", "content": "two"},+ {"id": "c9", "content": "slot evidence"},+ }+ answer := "Gustave Eiffel designed it [ID:Slot 0]. It opened in 1889."+ got := RepairSlotCitations(answer, map[string][]string{"0": {"c9", "missing"}}, chunks)+ if strings.Contains(got, "Slot") {+ t.Fatalf("slot marker leaked: %q", got)+ }+ if !strings.Contains(got, "[ID:2]") {+ t.Fatalf("slot marker not mapped to the evidence chunk position: %q", got)+ }+ if got != "Gustave Eiffel designed it [ID:2]. It opened in 1889." {+ t.Fatalf("unexpected rewrite: %q", got)+ }+}++func TestRepairSlotCitations_DropsUnresolvable(t *testing.T) {+ chunks := []map[string]interface{}{{"chunk_id": "c1", "content": "one"}}+ answer := "Fact one [ID:Slot 0]. Fact two [Slot 1]."+ got := RepairSlotCitations(answer, map[string][]string{"0": {"not-in-pool"}, "1": {"also-missing"}}, chunks)+ if strings.Contains(strings.ToLower(got), "slot") {+ t.Fatalf("unresolvable slot markers must be dropped, got %q", got)+ }+ if got != "Fact one . Fact two ." {+ t.Fatalf("unexpected rewrite: %q", got)+ }+}++func TestRepairSlotCitations_MarkerVariants(t *testing.T) {+ chunks := []map[string]interface{}{{"chunk_id": "a"}, {"id": "b"}}+ cases := []struct {+ marker string+ want string+ }{+ {"[ID:Slot 1]", "[ID:1]"},+ {"[ID: slot 1]", "[ID:1]"},+ {"[Slot 1]", "[ID:1]"},+ {"[slot 1]", "[ID:1]"},+ }+ for _, c := range cases {+ got := RepairSlotCitations("x "+c.marker+" y", map[string][]string{"1": {"b"}}, chunks)+ if !strings.Contains(got, c.want) {+ t.Errorf("marker %q: got %q, want it rewritten to %q", c.marker, got, c.want)+ }+ }+}++func TestRepairSlotCitations_NoopWithoutSlotMarkersOrMap(t *testing.T) {+ answer := "Plain answer [ID:0]."+ if got := RepairSlotCitations(answer, nil, nil); got != answer {+ t.Fatalf("nil map must be a no-op, got %q", got)+ }+ if got := RepairSlotCitations(answer, map[string][]string{"0": {"c"}}, nil); got != answer {+ t.Fatalf("no slot markers must be a no-op, got %q", got)+ }+}++// -----------------------------------------------------------------------+// ExpandRangeCitations — range-merged citations ("[ID:1-3]") the model+// produces on its own (no code path emits them).+// -----------------------------------------------------------------------++func TestExpandRangeCitations(t *testing.T) {+ cases := []struct {+ name string+ answer string+ poolSize int+ want string+ }{+ {+ "plain range",+ "Fact [ID:1-3].",+ 5,+ "Fact [ID:1][ID:2][ID:3].",+ },+ {+ "spaced dash variant",+ "Fact [ID: 1 - 3].",+ 5,+ "Fact [ID:1][ID:2][ID:3].",+ },+ {+ "tilde variant",+ "Fact [ID:1~3].",+ 5,+ "Fact [ID:1][ID:2][ID:3].",+ },+ {+ "reversed bounds swap",+ "Fact [ID:3-1].",+ 5,+ "Fact [ID:1][ID:2][ID:3].",+ },+ {+ "single-bound range",+ "Fact [ID:2-2].",+ 5,+ "Fact [ID:2].",+ },+ {+ "out of bounds dropped",+ "Fact [ID:99-102].",+ 5,+ "Fact .",+ },+ {+ "partially out of bounds dropped",+ "Fact [ID:2-99].",+ 5,+ "Fact .",+ },+ {+ "empty pool dropped",+ "Fact [ID:1-3].",+ 0,+ "Fact .",+ },+ {+ "no ranges untouched",+ "Fact [ID:1] and [ID:2].",+ 5,+ "Fact [ID:1] and [ID:2].",+ },+ }+ for _, c := range cases {+ t.Run(c.name, func(t *testing.T) {+ if got := ExpandRangeCitations(c.answer, c.poolSize); got != c.want {+ t.Fatalf("ExpandRangeCitations(%q, %d) = %q, want %q", c.answer, c.poolSize, got, c.want)+ }+ })+ }+}++// TestDecorateHarnessAnswerExpandsRangeCitations pins the end-to-end fix for+// the [ID:1-3] report: the final answer carries only individual citations,+// each resolving to a pooled chunk, and only the cited doc reaches the+// reference.+func TestDecorateHarnessAnswerExpandsRangeCitations(t *testing.T) {+ kbinfos := map[string]interface{}{+ "chunks": []map[string]interface{}{+ {"chunk_id": "c0", "content_with_weight": "a", "doc_id": "d1", "docnm_kwd": "Doc One"},+ {"chunk_id": "c1", "content_with_weight": "b", "doc_id": "d1", "docnm_kwd": "Doc One"},+ {"chunk_id": "c2", "content_with_weight": "c", "doc_id": "d1", "docnm_kwd": "Doc One"},+ {"chunk_id": "c3", "content_with_weight": "d", "doc_id": "d1", "docnm_kwd": "Doc One"},+ },+ "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{}+ res := s.decorateHarnessAnswer("The range claim holds [ID:1-3].", kbinfos, nil)++ if strings.Contains(res.Answer, "1-3") {+ t.Fatalf("final answer still carries the range citation: %q", res.Answer)+ }+ 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)+ }+ }+ // Every cited chunk belongs to Doc One, so the uncited Doc Two must be+ // filtered out of the reference (recall_docs = cited docs).+ if aggs, _ := res.Reference["doc_aggs"].([]interface{}); len(aggs) != 1 {+ t.Fatalf("reference doc_aggs = %#v, want the single cited doc", res.Reference["doc_aggs"])+ }+}diff --git a/rag/advanced_rag/agentic_rag.py b/rag/advanced_rag/agentic_rag.pyindex 79cf1082fd..53c7a6c4c9 100644--- a/rag/advanced_rag/agentic_rag.py+++ b/rag/advanced_rag/agentic_rag.py@@ -217,6 +217,61 @@ def _cache_similar(return shared / min(len(aw), len(bw)) >= _RAG_CACHE_MIN_OVERLAP+_SLOT_CITATION_RE = re.compile(r"(?i)\[\s*ID\s*[:: ]*\s*slot\s*(\d+)\s*\]")+_RANGE_CITATION_RE = re.compile(r"(?i)\[\s*ID\s*[:: ]*\s*([0-9]+)\s*[-–—~~]\s*([0-9]+)\s*\]")+++def _repair_slot_citation_markers(answer, slot_evidence, cite_chunk_ids):+ """Rewrite [ID:Slot N] markers into real [ID:k] citations.++ DESIGN ENHANCEMENT (Go parity: internal/service/citation.go+ RepairSlotCitations): the model occasionally cites slot-table rows —+ internal markers that cannot be located in the source document. Each+ marker is rewritten to the [ID:k] of the slot's first evidence chunk that+ is present in the citation pool (1-based kb_prompt numbering); slots+ without pool-resident evidence have the marker removed — an internal+ marker must not reach the answer.+ """+ if not answer or not slot_evidence or not cite_chunk_ids:+ return answer+ pos_by_id = {}+ for i, cid in enumerate(cite_chunk_ids):+ if cid and cid not in pos_by_id:+ pos_by_id[cid] = i + 1++ def _repl(m):+ meta = slot_evidence.get(m.group(1)) or {}+ for eid in meta.get("evidence_ids") or []:+ pos = pos_by_id.get(str(eid))+ if pos is not None:+ return f"[ID:{pos}]"+ return "" # unresolvable internal marker: drop++ return _SLOT_CITATION_RE.sub(_repl, answer)+++def _expand_range_citation_markers(answer, pool_size):+ """Expand range-merged citations back into individual ones.++ DESIGN ENHANCEMENT (Go parity: ExpandRangeCitations): models sometimes+ compress consecutive [ID:1][ID:2][ID:3] into [ID:1-3]; a range cannot be+ resolved to a single source, so it is expanded when both bounds are valid+ 1-based citation-pool positions and dropped otherwise.+ """+ if not answer or pool_size <= 0:+ return answer++ def _repl(m):+ a, b = int(m.group(1)), int(m.group(2))+ if a > b:+ a, b = b, a+ if a < 1 or b > pool_size:+ return "" # out of range: unresolvable, drop+ return "".join(f"[ID:{i}]" for i in range(a, b + 1))++ return _RANGE_CITATION_RE.sub(_repl, answer)++class RAGTools:def __init__(self,@@ -884,6 +939,19 @@ class RAGTools:self.answer_sink(delta, kind == "think")final = re.sub(r"\(\**(ID:\d+)\**\)", r"[\1]", final)+ # DESIGN ENHANCEMENT (Go parity: internal/service/citation.go+ # RepairSlotCitations / ExpandRangeCitations): the model sometimes+ # cites slot-table rows ("[ID:Slot 0]") — internal markers that+ # cannot be located in the source document — and occasionally+ # compresses consecutive [ID:n] citations into ranges. Both forms+ # are unresolvable and must never reach the answer.+ final = _repair_slot_citation_markers(+ final,+ getattr(self, "_rag_slot_evidence", None) or {},+ getattr(self, "_rag_cite_chunk_ids", None) or [],+ )+ final = _expand_range_citation_markers(final, len(getattr(self, "_rag_cite_chunk_ids", None) or []))+# Cache the freshly produced answer for later near-identical questions.if question and final and not self.text_attachments_content:self._rag_cache[question] = (final, _question_keywords(question))diff --git a/rag/advanced_rag/agentic_rag_graph.py b/rag/advanced_rag/agentic_rag_graph.pyindex 5f939e3227..d671467d84 100644--- a/rag/advanced_rag/agentic_rag_graph.py+++ b/rag/advanced_rag/agentic_rag_graph.py@@ -861,6 +861,13 @@ async def _compose_answer_from_evidence(state: AgenticState, tools, token_queue:_CITE_CHUNK_CAP = 6cite_chunks = ranked[:_CITE_CHUNK_CAP] or all_chunks+ # DESIGN ENHANCEMENT (Go parity: RunResponse.SlotCitations): expose the+ # slot evidence ids and the citation-pool chunk ids so the rag tool's+ # post-processing can rewrite unresolvable [ID:Slot N] markers into real+ # chunk citations (see _repair_slot_citation_markers in agentic_rag.py)+ # and expand range-merged citations (see _expand_range_citation_markers).+ tools._rag_slot_evidence = state.get("slot_evidence") or {}+ tools._rag_cite_chunk_ids = [str(c.get("chunk_id") or c.get("id") or "") for c in cite_chunks]evidence_kbinfos = dict(kbinfos, chunks=cite_chunks)evidence_blocks = kb_prompt(evidence_kbinfos, min(tools.chat_mdl.max_length, _EVIDENCE_BUDGET_TOKENS))evidence = "\n".join(evidence_blocks) if isinstance(evidence_blocks, list) else str(evidence_blocks)@@ -932,7 +939,11 @@ async def _compose_answer_from_evidence(state: AgenticState, tools, token_queue:"provided evidence, those three take precedence.")- parts.append(f"Evidence:\n{evidence}")+ # DESIGN NOTE: dataset names are mutable runtime data — exposed here in+ # the untrusted evidence block (user directive), never via the system+ # prompt's {knowledge} placeholder (trusted template content only).+ bound = (getattr(tools, "_bound_dataset_names", "") or "").strip()+ parts.append(f"Evidence:\n{('Bound datasets: ' + bound + '\n') if bound else ''}{evidence}")user_content = "\n".join(parts)_LOG.info(diff --git a/rag/prompts/citation_prompt.md b/rag/prompts/citation_prompt.mdindex 9c50c8e01f..09b14c54de 100644--- a/rag/prompts/citation_prompt.md+++ b/rag/prompts/citation_prompt.md@@ -1,9 +1,9 @@-Based on the provided document or chat history, add citations to the input text using the format specified later.+Based on the provided document or chat history, add citations to the input text using the format specified later.# Citation Requirements:## Technical Rules:-- Use format: [ID:i] or [ID:i] [ID:j] for multiple sources+- Use format: [ID:i] or [ID:i] [ID:j] for multiple sources. NEVER merge consecutive citations into a range such as [ID:i-j] or [ID:i~j] — ranges cannot be resolved to a source.- Place citations at the end of sentences, before punctuation- Maximum 4 citations per sentence- DO NOT cite content not from <context></context>@@ -13,7 +13,7 @@ Based on the provided document or chat history, add citations to the input text## What MUST Be Cited:1. **Quantitative data**: Numbers, percentages, statistics, measurements-2. **Temporal claims**: Dates, timeframes, sequences of events+2. **Temporal claims**: Dates, timeframes, sequences of events3. **Causal relationships**: Claims about cause and effect4. **Comparative statements**: Rankings, comparisons, superlatives5. **Technical definitions**: Specialized terms, concepts, methodologies@@ -34,13 +34,13 @@ Based on the provided document or chat history, add citations to the input textID: 45└── Content: The global smartphone market grew by 7.8% in Q3 2024, with Samsung holding 20.6% market share and Apple at 15.8%.-ID: 46+ID: 46└── Content: 5G adoption reached 1.5 billion users worldwide by October 2024.</context>USER: How is the smartphone market performing?-ASSISTANT:+ASSISTANT:The smartphone industry is showing strong recovery. The global smartphone market grew by 7.8% in Q3 2024 [ID:45]. Samsung leads with 20.6% market share, followed by Apple at 15.8% [ID:45]. This growth is partly driven by 5G technology, which reached 1.5 billion users worldwide by October 2024 [ID:46].## Example 2: Causal Relationships and Predictions@@ -114,9 +114,10 @@ ASSISTANT:--- Examples END ----REMEMBER:+REMEMBER:- Cite FACTS, not opinions or transitions- Each citation supports the ENTIRE sentence- When in doubt, ask: "Would a fact-checker need to verify this?"- Place citations at sentence end, before punctuation- Format likes this is FORBIDDEN: [ID:0, ID:5, ID:...]. It MUST be separated like, [ID:0][ID:5]...+- Range citations like [ID:1-3] are FORBIDDEN too. Multiple sources MUST be spelled out individually: [ID:1][ID:2][ID:3].diff --git a/rag/prompts/generator.py b/rag/prompts/generator.pyindex e88ea4edde..fa5e2a55f0 100644--- a/rag/prompts/generator.py+++ b/rag/prompts/generator.py@@ -225,7 +225,12 @@ PROMPT_JINJA_ENV = SandboxedEnvironment(autoescape=False, trim_blocks=True, lstrdef citation_prompt(user_defined_prompts: dict = {}) -> str:template = PROMPT_JINJA_ENV.from_string(user_defined_prompts.get("citation_guidelines", CITATION_PROMPT_TEMPLATE))- return template.render() + "\n\nIMPORTANT: The example IDs above (45, 46, 78, etc.) are illustrative only. Use the actual chunk IDs from the provided knowledge blocks."+ suffix = "\n\nIMPORTANT: The example IDs above (45, 46, 78, etc.) are illustrative only. Use the actual chunk IDs from the provided knowledge blocks."+ # Range citations cannot be resolved to a single source; the ban is+ # appended unconditionally so it also holds when a user overrides the+ # template (matches Go's citationIDSuffix behavior).+ suffix += " Cite each source INDIVIDUALLY: [ID:i][ID:j]. NEVER merge consecutive citations into a range such as [ID:i-j] or [ID:i~j]."+ return template.render() + suffixdef citation_plus(sources: str) -> str:diff --git a/test/unit_test/api/db/services/test_dialog_service_rag_agent_messages.py b/test/unit_test/api/db/services/test_dialog_service_rag_agent_messages.pyindex 734c90e0c2..318c624fe5 100644--- a/test/unit_test/api/db/services/test_dialog_service_rag_agent_messages.py+++ b/test/unit_test/api/db/services/test_dialog_service_rag_agent_messages.py@@ -222,16 +222,69 @@ def test_rag_agent_preserves_multimodal_content_parts(monkeypatch):@pytest.mark.p2-def test_render_reasoning_system_prompt_substitutes_date_and_knowledge():- """The reasoning path should honor the dialog system prompt like async_chat does."""+def test_render_reasoning_system_prompt_substitutes_date_and_knowledge(monkeypatch):+ """The reasoning path should honor the dialog system prompt like async_chat does.++ {knowledge} is trusted-template content only: mutable runtime data (bound+ dataset names) must NOT be injected into the reasoning system prompt+ through it. When the caller does not supply a value it renders empty —+ the bound dataset names are exposed through the agentic graph's untrusted+ evidence block instead (see rag/advanced_rag/agentic_rag_graph.py).+ """dialog = SimpleNamespace(kb_ids=["kb-1"])prompt_config = {"system": "Role: pirate. Date: {date}. Context: '{knowledge}'."}kwargs = {}+ monkeypatch.setattr(+ dialog_service.KnowledgebaseService,+ "get_by_ids",+ lambda ids: [SimpleNamespace(name="Pirate KB")],+ )rendered = dialog_service._render_reasoning_system_prompt(dialog, prompt_config, kwargs)assert rendered.startswith("Role: pirate. Date: 2")assert "Context: ''." in rendered+ assert "Pirate KB" not in rendered+++def test_render_reasoning_system_prompt_caller_supplied_knowledge_wins(monkeypatch):+ """A caller-supplied knowledge value must not be overwritten by the default."""+ dialog = SimpleNamespace(kb_ids=["kb-1"])+ prompt_config = {"system": "Context: '{knowledge}'."}+ kwargs = {"knowledge": "caller evidence"}+ monkeypatch.setattr(+ dialog_service.KnowledgebaseService,+ "get_by_ids",+ lambda ids: [SimpleNamespace(name="Pirate KB")],+ )++ rendered = dialog_service._render_reasoning_system_prompt(dialog, prompt_config, kwargs)++ assert rendered == "Context: 'caller evidence'."+++def test_render_reasoning_system_prompt_empty_knowledge_is_backward_compatible(monkeypatch):+ """A dialog without datasets (or unnamed ones) still renders the template.++ get_by_ids failures are swallowed: names are cosmetic and must never block+ the prompt render.+ """+ dialog = SimpleNamespace(kb_ids=["kb-1"])+ prompt_config = {"system": "Context: '{knowledge}'."}++ def _boom(ids):+ raise RuntimeError("no db in unit test")++ monkeypatch.setattr(dialog_service.KnowledgebaseService, "get_by_ids", _boom)++ rendered = dialog_service._render_reasoning_system_prompt(dialog, prompt_config, {})++ assert rendered == "Context: ''."++ no_kb = dialog_service._render_reasoning_system_prompt(SimpleNamespace(kb_ids=[]), prompt_config, {})+ assert no_kb == "Context: ''."@pytest.mark.p2