Skip to content

~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.

perf(citations): avoid duplicate structured destination processing (#7983)

LearningCircuit54a61feb7510
diff --git a/changelog.d/7960.bugfix.md b/changelog.d/7960.bugfix.md
new file mode 100644
index 0000000000..319b18f82e
--- /dev/null
+++ b/changelog.d/7960.bugfix.md
@@ -0,0 +1 @@
+Process each structured citation destination once while adapting sources, reducing report-formatting work.
diff --git a/src/local_deep_research/text_optimization/citation_formatter.py b/src/local_deep_research/text_optimization/citation_formatter.py
index e64c447933..e20cc60ca8 100644
--- a/src/local_deep_research/text_optimization/citation_formatter.py
+++ b/src/local_deep_research/text_optimization/citation_formatter.py
@@ -743,11 +743,14 @@ class CitationFormatter:
def _src_url(s):
return _safe_link_destination(s.get("url") or s.get("link") or "")
- adapted: Dict[str, Tuple[str, str]] = {
- str(s["index"]): (s.get("title", "Untitled"), _src_url(s))
- for s in sources
- if _src_url(s) and s.get("index") is not None
- }
+ adapted: Dict[str, Tuple[str, str]] = {}
+ for source in sources:
+ destination = _src_url(source)
+ if destination and source.get("index") is not None:
+ adapted[str(source["index"])] = (
+ source.get("title", "Untitled"),
+ destination,
+ )
if not adapted:
return content
diff --git a/tests/text_optimization/test_structured_source_adaptation_cost.py b/tests/text_optimization/test_structured_source_adaptation_cost.py
new file mode 100644
index 0000000000..c650a5a02f
--- /dev/null
+++ b/tests/text_optimization/test_structured_source_adaptation_cost.py
@@ -0,0 +1,102 @@
+"""Pin destination-processing work without machine-dependent timing gates."""
+
+import pytest
+
+from local_deep_research.text_optimization import citation_formatter as module
+
+
+ "mode",
+ [
+ mode
+ for mode in module.CitationMode
+ if mode != module.CitationMode.NO_HYPERLINKS
+ ],
+)
+def test_source_adaptation_does_not_repeat_destination_processing(
+ monkeypatch, mode
+):
+ original, checked = module._safe_link_destination, []
+
+ def observe(value):
+ checked.append(value)
+ return original(value)
+
+ monkeypatch.setattr(module, "_safe_link_destination", observe)
+ sources = [
+ {"index": 1, "title": "First", "url": "https://example.com/one"},
+ {"index": 2, "title": "Second", "link": "https://example.org/two"},
+ {"index": 3, "title": "No destination"},
+ {"index": None, "url": "https://example.com/unindexed"},
+ ]
+ formatter = module.CitationFormatter(mode)
+ render_name = {
+ module.CitationMode.NUMBER_HYPERLINKS: "_format_number_hyperlinks",
+ module.CitationMode.DOMAIN_HYPERLINKS: "_format_domain_hyperlinks",
+ module.CitationMode.DOMAIN_ID_HYPERLINKS: "_format_domain_id_hyperlinks",
+ module.CitationMode.DOMAIN_ID_ALWAYS_HYPERLINKS: "_format_domain_id_always_hyperlinks",
+ module.CitationMode.SOURCE_TAGGED_HYPERLINKS: "_format_source_tagged_hyperlinks",
+ }[mode]
+ render, adaptation_checks = getattr(formatter, render_name), []
+
+ def after_adaptation(*args, **kwargs):
+ adaptation_checks.append(list(checked))
+ return render(*args, **kwargs)
+
+ monkeypatch.setattr(formatter, render_name, after_adaptation)
+ output = formatter.apply_inline_hyperlinks(
+ "[1, 2] Source 1 [3] [9]", sources
+ )
+ assert len(adaptation_checks) == 1
+ adapted = adaptation_checks[0]
+ assert len(adapted) <= len(sources)
+ assert adapted.count("https://example.com/one") <= 1
+ assert adapted.count("https://example.org/two") <= 1
+ assert output.count("https://example.com/one") == 2
+ assert output.count("https://example.org/two") == 1
+ assert "[3]" in output and "[9]" in output
+
+
+def test_duplicate_indices_keep_last_source():
+ sources = [
+ {"index": 1, "url": "https://example.com/old"},
+ {"index": 1, "link": "https://example.com/new"},
+ ]
+ assert (
+ module.CitationFormatter().apply_inline_hyperlinks("[1]", sources)
+ == "[[1]](https://example.com/new)"
+ )
+
+
+def test_destination_results_are_fresh_for_each_call():
+ formatter = module.CitationFormatter()
+ sources = [{"index": 1, "url": "https://example.com/first"}]
+ assert (
+ formatter.apply_inline_hyperlinks("[1]", sources)
+ == "[[1]](https://example.com/first)"
+ )
+ sources[0]["url"] = "https://example.com/second"
+ assert (
+ formatter.apply_inline_hyperlinks("[1]", sources)
+ == "[[1]](https://example.com/second)"
+ )
+
+
[email protected]("case", ["no_links", "empty_body", "empty_sources"])
+def test_existing_early_returns_do_no_destination_work(monkeypatch, case):
+ def unexpected(_value):
+ raise AssertionError("Early return processed a destination")
+
+ monkeypatch.setattr(module, "_safe_link_destination", unexpected)
+ formatter = module.CitationFormatter(
+ module.CitationMode.NO_HYPERLINKS
+ if case == "no_links"
+ else module.CitationMode.NUMBER_HYPERLINKS
+ )
+ body = "" if case == "empty_body" else "[1]"
+ sources = (
+ []
+ if case == "empty_sources"
+ else [{"index": 1, "url": "https://example.com/one"}]
+ )
+ assert formatter.apply_inline_hyperlinks(body, sources) == body