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.

fix(citations): recognize colon and hash source phrases (#7762)

Chanhyo Jung2385b7d7f944
diff --git a/changelog.d/7527.bugfix.md b/changelog.d/7527.bugfix.md
new file mode 100644
index 0000000000..e8316fb8a6
--- /dev/null
+++ b/changelog.d/7527.bugfix.md
@@ -0,0 +1 @@
+Keep cited sources when reports use explicit phrases such as `Sources: 1, 2` or `Source #2` (Refs #7527).
diff --git a/src/local_deep_research/utilities/search_utilities.py b/src/local_deep_research/utilities/search_utilities.py
index 36e4efa106..53ea0c7f50 100644
--- a/src/local_deep_research/utilities/search_utilities.py
+++ b/src/local_deep_research/utilities/search_utilities.py
@@ -375,11 +375,12 @@ _BRACKET_NUMERIC_RE = re.compile(
_TAGGED_CITATION_RE = re.compile(r"\[(?P<tag>[A-Za-z0-9._/\-]+)-(?P<idx>\d+)\]")
# "Source 1", "Sources 1 and 2", "sources 1, 2 & 3", "Sources 1-3"
+# Optional ":"/"#" separators ("Sources: 1, 2", "Source #2") are accepted.
# (case-insensitive). Dash/en-dash/em-dash ranges are part of the capture
# so "Sources 1-3" parses the whole range instead of just the "1" prefix
# (which previously kept only source 1 with no fallback).
_SOURCE_PHRASE_RE = re.compile(
- r"(?i)\bsources?\s+(?P<nums>\d+(?:\s*[\-–—]\s*\d+)?"
+ r"(?i)\bsources?(?:\s*[:#]\s*|\s+)(?P<nums>\d+(?:\s*[\-–—]\s*\d+)?"
r"(?:\s*(?:,|and|&)\s*\d+(?:\s*[\-–—]\s*\d+)?)*)"
)
@@ -1064,7 +1065,8 @@ def extract_cited_indices_and_urls(
unreliable (caller falls back to the full list without truncating prose).
Recognized signals: numeric brackets (outer spaces allowed:
- ``[ 1, 2 ]``), tagged citations, ``Source N`` phrases, absolute URLs,
+ ``[ 1, 2 ]``), tagged citations, ``Source N`` / ``Sources: N`` /
+ ``Source #N`` phrases, absolute URLs,
markdown-link library destinations (``](/library/…)``) AND bare
library routes (``/library/document/…``, ``/lib/document/…``) mentioned
outside link syntax. Route-prefix case is normalized for both bare
diff --git a/tests/utilities/test_source_phrase_punctuation.py b/tests/utilities/test_source_phrase_punctuation.py
new file mode 100644
index 0000000000..fd5bfd1b71
--- /dev/null
+++ b/tests/utilities/test_source_phrase_punctuation.py
@@ -0,0 +1,120 @@
+"""Explicit Source separators reuse the existing citation parser (Refs #7527)."""
+
+import pytest
+
+from local_deep_research.utilities.search_utilities import (
+ _MAX_RANGE_ENDPOINT_DIGITS,
+ _MAX_RANGE_SPAN,
+ _MAX_TOTAL_EXPANSION,
+ extract_cited_indices_and_urls,
+ filter_cited_links,
+)
+
+
+def links():
+ return [
+ {"index": str(i), "url": f"https://source{i}.example/Article"}
+ for i in range(1, 6)
+ ]
+
+
[email protected]("word", ["Source", "Sources", "source", "SOURCES"])
+ "separator", [":", "#", ": ", "# ", " :", " #", " : ", " # "]
+)
+ "numbers", ["2, 3", "2 and 3", "2 & 3", "2-3", "2–3", "2—3"]
+)
+def test_punctuation_extraction(word, separator, numbers):
+ assert extract_cited_indices_and_urls(f"{word}{separator}{numbers}.") == (
+ {2, 3},
+ set(),
+ False,
+ )
+
+
[email protected]("phrase", ["Sources: 2, 3", "Source #2 and 3"])
[email protected]("mode", ["strict", "fallback"])
+def test_punctuation_mixed_filter(links, phrase, mode):
+ prose = f"Claim [1]. Further evidence {phrase}."
+ kept = filter_cited_links(links, prose, mode=mode)
+ assert kept == links[:3]
+ assert all(actual is original for actual, original in zip(kept, links))
+
+
[email protected]("phrase", ["Sources: 2, 3", "Source #2"])
+def test_disabled_preserves_original_object(links, phrase):
+ assert filter_cited_links(links, phrase, mode="disabled") is links
+
+
+ "prose", ["Source 2", "Sources 2 and 3", "[2, 3]", "[2–3]"]
+)
+def test_control_existing_signals(links, prose):
+ expected = {2} if prose == "Source 2" else {2, 3}
+ assert extract_cited_indices_and_urls(prose) == (expected, set(), False)
+ for mode in ("strict", "fallback"):
+ assert filter_cited_links(links, prose, mode=mode) == [
+ link for link in links if int(link["index"]) in expected
+ ]
+
+
+ "phrase",
+ [
+ "Source2",
+ "Sources2",
+ "resource2",
+ "resource:2",
+ "Source/2",
+ "Source=2",
+ "Source;2",
+ "Source-2",
+ "Source::2",
+ "Source##2",
+ "Source :#2",
+ ],
+)
+def test_control_requires_explicit_delimiter(links, phrase):
+ prose = f"[1] alongside {phrase}."
+ assert extract_cited_indices_and_urls(prose) == ({1}, set(), False)
+ for mode in ("strict", "fallback"):
+ assert filter_cited_links(links, prose, mode=mode) == links[:1]
+
+
[email protected]("fence", ["```", "~~~"])
+def test_control_fences_and_url_identity(links, fence):
+ prose = (
+ f"{fence}\nSources: 2, 3; Source #4\n{fence}\n"
+ "[1] https://SOURCE2.example/Article"
+ )
+ assert extract_cited_indices_and_urls(prose) == (
+ {1},
+ {links[1]["url"]},
+ False,
+ )
+ for mode in ("strict", "fallback"):
+ assert filter_cited_links(links, prose, mode=mode) == links[:2]
+
+
[email protected]("separator", [" ", ": ", " #"])
[email protected]("budget", ["range", "digits", "aggregate"])
+def test_shared_limits_fail_safe(links, separator, budget):
+ if budget == "range":
+ numbers = f"2-{2 + _MAX_RANGE_SPAN + 1}"
+ elif budget == "digits":
+ numbers = "9" * (_MAX_RANGE_ENDPOINT_DIGITS + 1)
+ else:
+ # Repeated legal ranges still consume the shared expansion budget.
+ numbers = ", ".join(
+ [f"2-{2 + _MAX_RANGE_SPAN}"]
+ * (_MAX_TOTAL_EXPANSION // (_MAX_RANGE_SPAN + 1) + 1)
+ )
+ prose = f"[1] Sources{separator}{numbers}"
+ indices, urls, indeterminate = extract_cited_indices_and_urls(prose)
+ assert 1 in indices
+ assert urls == set()
+ assert indeterminate
+ for mode in ("strict", "fallback"):
+ assert filter_cited_links(links, prose, mode=mode) is links