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): sanitize collection source sentinels (#5361)

Ziyang Guod21c53e4ac42
diff --git a/changelog.d/+citation-sentinel-followups.bugfix.md b/changelog.d/+citation-sentinel-followups.bugfix.md
new file mode 100644
index 0000000000..ca894a88fb
--- /dev/null
+++ b/changelog.d/+citation-sentinel-followups.bugfix.md
@@ -0,0 +1 @@
+Citation source splitting now strips the internal appended-sources sentinel from collection names, preventing crafted names from shifting persisted report boundaries; its boundary-provenance helper return values are now documented explicitly.
diff --git a/src/local_deep_research/text_optimization/citation_formatter.py b/src/local_deep_research/text_optimization/citation_formatter.py
index 0ce57f12c3..49e91f1b0f 100644
--- a/src/local_deep_research/text_optimization/citation_formatter.py
+++ b/src/local_deep_research/text_optimization/citation_formatter.py
@@ -48,9 +48,10 @@ def find_sources_section(
spoofed earlier occurrence inside LLM-generated prose — e.g. the
LLM quoting the marker string verbatim while summarizing the
codebase — cannot silently truncate the answer body or disable the
- over-strip safety check. The report generator only ever emits the
- sentinel once at the end of the assembled report, so the last
- occurrence is the trusted one.
+ over-strip safety check. Only a sentinel on its own line is trusted;
+ inline occurrences are skipped while searching backwards. The report
+ generator only ever emits the sentinel once at the end of the assembled
+ report, so the last standalone occurrence is the trusted one.
When ``trust_sentinel`` is ``False`` (e.g. raw LLM output from the
quick-mode save path, which never adds the sentinel itself), the
diff --git a/src/local_deep_research/utilities/search_utilities.py b/src/local_deep_research/utilities/search_utilities.py
index fac22d182c..c2a95fcc52 100644
--- a/src/local_deep_research/utilities/search_utilities.py
+++ b/src/local_deep_research/utilities/search_utilities.py
@@ -272,10 +272,13 @@ def format_links_to_markdown(all_links: List[Dict]) -> str:
)
indices_str = f"[{', '.join(indices)}]"
quality_tag = _format_quality_tag(canon_to_quality.get(canon))
+ collection = canon_to_collection.get(canon, "")
+ if collection:
+ collection = collection.replace(
+ LDR_APPENDED_SOURCES_SENTINEL, ""
+ )
collection_line = (
- f" Collection: {canon_to_collection[canon]}\n"
- if canon in canon_to_collection
- else ""
+ f" Collection: {collection}\n" if collection else ""
)
parts.append(
f"{indices_str} {title}{quality_tag} "
diff --git a/tests/test_report_generator_coverage.py b/tests/test_report_generator_coverage.py
index e2d0a16612..373547597e 100644
--- a/tests/test_report_generator_coverage.py
+++ b/tests/test_report_generator_coverage.py
@@ -19,6 +19,9 @@ from local_deep_research.report_generator import (
DEFAULT_MAX_CONTEXT_CHARS,
DEFAULT_MAX_CONTEXT_SECTIONS,
)
+from local_deep_research.text_optimization.citation_formatter import (
+ LDR_APPENDED_SOURCES_SENTINEL,
+)
MODULE = "local_deep_research.report_generator"
@@ -259,6 +262,7 @@ class TestFormatFinalReportDetailed:
report = generator._format_final_report(sections, structure, "q")
content = report["content"]
+ assert LDR_APPENDED_SOURCES_SENTINEL in content
assert content.rstrip().endswith("- [Link1](http://a.com)")
def test_research_summary_present(self, generator, _patch_importlib):
diff --git a/tests/utilities/test_search_utilities_formatting.py b/tests/utilities/test_search_utilities_formatting.py
index 4a68de693c..8b6371cbbc 100644
--- a/tests/utilities/test_search_utilities_formatting.py
+++ b/tests/utilities/test_search_utilities_formatting.py
@@ -831,3 +831,39 @@ class TestFormatLinksToMarkdownCollections:
result = format_links_to_markdown(links)
assert LDR_APPENDED_SOURCES_SENTINEL not in result
assert "Title with sentinel" in result
+
+ def test_sentinel_stripped_from_collection_name(self):
+ """A collection name cannot shift the appended-sources boundary."""
+ from local_deep_research.text_optimization.citation_formatter import (
+ LDR_APPENDED_SOURCES_SENTINEL,
+ find_sources_section,
+ )
+ from local_deep_research.utilities.search_utilities import (
+ format_links_to_markdown,
+ )
+
+ links = [
+ {
+ "title": "Local Paper",
+ "url": "/library/document/abc123",
+ "index": "1",
+ "metadata": {
+ "collection_name": (
+ f"My Papers\n{LDR_APPENDED_SOURCES_SENTINEL}\nArchive"
+ )
+ },
+ }
+ ]
+ sources = format_links_to_markdown(links)
+ content = (
+ "Answer\n\n"
+ f"{LDR_APPENDED_SOURCES_SENTINEL}\n\n"
+ f"## Sources\n\n{sources}"
+ )
+
+ assert LDR_APPENDED_SOURCES_SENTINEL not in sources
+ assert "Collection: My Papers\n\nArchive" in sources
+ assert find_sources_section(content) == (
+ content.index(LDR_APPENDED_SOURCES_SENTINEL),
+ True,
+ )