~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
refactor(citation): pass collections explicitly to source-tagged formatter (#4096)
LearningCircuita2c9721bfb01
diff --git a/src/local_deep_research/text_optimization/citation_formatter.py b/src/local_deep_research/text_optimization/citation_formatter.pyindex 8fc8df53e3..94cbf372b6 100644--- a/src/local_deep_research/text_optimization/citation_formatter.py+++ b/src/local_deep_research/text_optimization/citation_formatter.py@@ -150,11 +150,6 @@ class CitationFormatter:# Parse sourcessources = self._parse_sources(sources_content)- # Optional per-citation collection tags (RAG / library results).- # Only the source-tagged mode consults this; cheap to compute up- # front and stashing on ``self`` avoids threading another- # parameter through every ``_format_*`` method.- self._collections_for_format = self._parse_collections(sources_content)# Format citations in documentif self.mode == CitationMode.NUMBER_HYPERLINKS:@@ -175,7 +170,9 @@ class CitationFormatter:)elif self.mode == CitationMode.SOURCE_TAGGED_HYPERLINKS:formatted_content = self._format_source_tagged_hyperlinks(- document_content, sources+ document_content,+ sources,+ self._parse_collections(sources_content),)else:formatted_content = document_content@@ -430,7 +427,10 @@ class CitationFormatter:return collectionsdef _format_source_tagged_hyperlinks(- self, content: str, sources: Dict[str, Tuple[str, str]]+ self,+ content: str,+ sources: Dict[str, Tuple[str, str]],+ collections: Dict[str, str],) -> str:"""Replace ``[N]`` with ``[[source-N]](url)``.@@ -440,19 +440,16 @@ class CitationFormatter:domain otherwise, or ``local`` for empty/file URLs. ``N`` isthe original global citation number — labels never collide andthe suffix always matches the bibliography ordering.- """- # Filter sources that have URLs, plus carry local entries through- # with an empty URL (they get the ``local-N`` label and no link).- usable_sources = {- num: (title, url) for num, (title, url) in sources.items()- }- # Find the sources block again to re-parse the optional- # ``Collection:`` lines. The caller already split this off in- # ``format_document`` but didn't pass it through; reparse from- # the original content via the existing helper to stay local.- # (Cheap — single regex scan over the sources block.)- collections = self._collections_for_format+ Args:+ content: Document body (sources section already split off).+ sources: ``{citation_num: (title, url)}`` parsed from the+ sources block.+ collections: ``{citation_num: collection_name}`` parsed from+ optional ``Collection:`` lines in the sources block+ (empty dict when no library/RAG hits are cited). Wins+ over URL-derived tags when present for a given citation.+ """def format_link(citation_num, data):_, url = data@@ -470,17 +467,15 @@ class CitationFormatter:)# Handle comma-separated citations like [1, 2, 3]- content = self._replace_comma_citations(- content, usable_sources, format_link- )+ content = self._replace_comma_citations(content, sources, format_link)- formatter = self._create_citation_formatter(usable_sources, format_link)+ formatter = self._create_citation_formatter(sources, format_link)# Handle individual citationsdef replace_citation(match):return (formatter(match.group(1))- if match.group(1) in usable_sources+ if match.group(1) in sourceselse match.group(0))diff --git a/tests/text_optimization/test_citation_formatter.py b/tests/text_optimization/test_citation_formatter.pyindex 418563847c..fc7c5d9f8b 100644--- a/tests/text_optimization/test_citation_formatter.py+++ b/tests/text_optimization/test_citation_formatter.py@@ -680,6 +680,42 @@ See [1] and [2].# No URL → also "local", and no hyperlink.assert "[local-2]" in result+ def test_source_tagged_hyperlinks_no_state_leak_across_calls(self):+ """Calling ``format_document`` twice on the same formatter+ instance must not leak collection metadata between calls. Doc 1+ has a ``Collection:`` line for citation [1]; doc 2 has no+ collection line at all, so its [1] must fall back to the+ URL-derived tag, not pick up doc 1's collection."""+ doc_with_collection = """# Doc 1++See [1].++## Sources++[1] Local doc with collection+ URL: /library/document/abc+ Collection: project-alpha+"""+ doc_without_collection = """# Doc 2++See [1].++## Sources++[1] ArXiv paper, no collection+ URL: https://arxiv.org/abs/2024.9999+"""+ formatter = CitationFormatter(CitationMode.SOURCE_TAGGED_HYPERLINKS)++ result1 = formatter.format_document(doc_with_collection)+ assert "[project-alpha-1]" in result1++ # Second call: arxiv URL, no Collection: line in the sources block.+ # Must resolve to arxiv via URLClassifier, NOT to project-alpha.+ result2 = formatter.format_document(doc_without_collection)+ assert "[[arxiv-1]](https://arxiv.org/abs/2024.9999)" in result2+ assert "project-alpha" not in result2+def test_unicode_lenticular_bracket_citations(self):"""Test that Unicode lenticular brackets【】are recognized and converted."""content = """# Research Report