From 6bf7c631e42b6ba3a05eb2c4e4e2c43495177d07 Mon Sep 17 00:00:00 2001 From: mpichini1 Date: Fri, 13 Nov 2026 19:49:05 +0100 Subject: [PATCH] fix: preserve PageIndex sources in report citations --- .../agents/research/utils/citation_manager.py | 12 ++-- .../agents/research/test_citation_manager.py | 72 +++++++++++++++++++ 2 files changed, 80 insertions(+), 4 deletions(-) diff --git a/deeptutor/agents/research/utils/citation_manager.py b/deeptutor/agents/research/utils/citation_manager.py index 916ffa190..a240678a9 100644 --- a/deeptutor/agents/research/utils/citation_manager.py +++ b/deeptutor/agents/research/utils/citation_manager.py @@ -17,6 +17,12 @@ from deeptutor.utils.json_parser import parse_json_response _RAG_SOURCE_FIELDS = ("chunks", "documents", "sources", "context", "retrieved_docs") +def _is_rag_tool(tool_type: str) -> bool: + return tool_type in ("rag", "rag_naive", "rag_hybrid") or tool_type.startswith( + ("pageindex_cloud_", "pageindex_oss_") + ) + + def _as_text(value: Any) -> str: """Normalize optional source metadata without leaking ``None`` values.""" if value is None: @@ -302,9 +308,7 @@ class CitationManager: try: tool_type_lower = tool_type.lower() - if tool_type_lower in ("rag", "rag_naive", "rag_hybrid") or tool_type_lower.startswith( - ("pageindex_cloud_", "pageindex_oss_") - ): + if _is_rag_tool(tool_type_lower): citation_info = self._extract_rag_citation( citation_id, tool_type, raw_answer, tool_trace, tool_metadata ) @@ -584,7 +588,7 @@ class CitationManager: if tool_type == "paper_search": return self._format_paper_search_apa(citation) - if tool_type in ("rag", "rag_naive", "rag_hybrid"): + if _is_rag_tool(tool_type): query = html.escape(str(citation.get("query", ""))) kb_name = citation.get("kb_name", "") sources = citation.get("sources", []) or [] diff --git a/tests/agents/research/test_citation_manager.py b/tests/agents/research/test_citation_manager.py index 08b7c87af..b587f1f05 100644 --- a/tests/agents/research/test_citation_manager.py +++ b/tests/agents/research/test_citation_manager.py @@ -4,8 +4,13 @@ from __future__ import annotations import json from types import SimpleNamespace +from unittest.mock import AsyncMock + +import pytest from deeptutor.agents.research.utils.citation_manager import CitationManager +from deeptutor.services.rag.pipelines.pageindex.storage import CLOUD_PROVIDER, OSS_PROVIDER +from deeptutor.services.rag.pipelines.pageindex.tools import PageIndexSDKTool def _trace() -> SimpleNamespace: @@ -103,3 +108,70 @@ def test_rag_citation_falls_back_to_the_answer_without_metadata(tmp_path) -> Non assert citation["kb_name"] == "kb" assert citation["total_sources"] == 1 + + +@pytest.mark.asyncio +@pytest.mark.parametrize("provider", [CLOUD_PROVIDER, OSS_PROVIDER]) +@pytest.mark.parametrize( + ("query", "kb_name", "filename", "expected"), + [ + ( + "What is gravity?", + "Physics", + "Gravitation.pdf", + "RAG: What is gravity? [KB: Physics] [Sources: Gravitation.pdf]", + ), + ( + " & mass", + "Physics & ", + "A&B .pdf", + "RAG: <gravity> & mass [KB: Physics & <lab>] " + "[Sources: A&B <notes>.pdf]", + ), + ], +) +async def test_pageindex_report_citation_preserves_tool_sources( + tmp_path, provider, query, kb_name, filename, expected +): + sdk_tool = SimpleNamespace( + name="get_page_content", + description="Read pages", + params_json_schema={}, + on_invoke_tool=AsyncMock( + return_value=json.dumps( + { + "success": True, + "doc_name": filename, + "content": [{"page": 3, "text": "Grounded answer"}], + } + ) + ), + ) + tool = PageIndexSDKTool( + sdk_tool, provider=provider, kb_name=kb_name, doc_ids={filename: "doc-1"} + ) + result = await tool.execute(doc_name=filename, pages="3") + manager = CitationManager("research-pageindex", cache_dir=tmp_path) + trace = SimpleNamespace(query=query, summary="Retrieved sources", timestamp="now") + + assert manager.add_citation("CIT-1-01", tool.name, trace, result.content, result.metadata) + assert manager.format_citation_for_report("CIT-1-01") == expected + + +@pytest.mark.parametrize("tool_type", ["rag", "rag_naive", "rag_hybrid"]) +def test_existing_rag_report_citation_keeps_sources(tmp_path, tool_type): + manager = CitationManager("research-rag", cache_dir=tmp_path) + metadata = {"kb_name": "Notes", "sources": [{"title": "Document"}]} + assert manager.add_citation("CIT-1-01", tool_type, _trace(), "Answer", metadata) + assert manager.format_citation_for_report("CIT-1-01") == ( + "RAG: What is RAG? [KB: Notes] [Sources: Document]" + ) + + +def test_web_report_citation_keeps_escaped_links(tmp_path): + manager = CitationManager("research-web", cache_dir=tmp_path) + metadata = {"citations": [{"title": "A & B", "url": "https://example.com/?a=1&b=2"}]} + assert manager.add_citation("CIT-1-01", "web_search", _trace(), "Answer", metadata) + assert manager.format_citation_for_report("CIT-1-01") == ( + 'Web Search: What is RAG?
A & B' + )