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(report): preserve sources after placeholder headings (#6051)

Daniel Peng42f71f327da0
diff --git a/changelog.d/6051.bugfix.md b/changelog.d/6051.bugfix.md
new file mode 100644
index 0000000000..c84d9b11c6
--- /dev/null
+++ b/changelog.d/6051.bugfix.md
@@ -0,0 +1 @@
+Reports now retain their structured bibliography when model-generated prose contains a placeholder `## Sources` heading.
diff --git a/src/local_deep_research/web/services/report_assembly_service.py b/src/local_deep_research/web/services/report_assembly_service.py
index 5b16f042c6..50565df56c 100644
--- a/src/local_deep_research/web/services/report_assembly_service.py
+++ b/src/local_deep_research/web/services/report_assembly_service.py
@@ -7,6 +7,15 @@ combined `answer + ## Sources + ## Research Metrics` view on demand for
display and export — chat reads `research.report_content` directly and
never goes through this module.
+The legacy Sources guard expects the first non-blank entry to use the
+bracketed bibliography shape (`[N]`, grouped `[N, M]`, or historical `[]`)
+and to carry an indented `URL:` continuation line. Indexed entries mirror
+`text_optimization.citation_formatter._BIB_SOURCES_PATTERN`; the empty-index
+variant covers rows emitted before every source had an index. The currently
+dormant `advanced_search_system.findings.repository.format_links` instead
+emits `1. Title` entries, so it must not become a persisted Sources emitter
+without extending this guard and its round-trip tests.
+
DO NOT write the assembled output back to `research.report_content` —
that column must stay answer-only. Writing assembled output back would
silently re-introduce the regex over-strip class of bugs that
@@ -24,8 +33,17 @@ from ...utilities.search_utilities import format_links_to_markdown
from ...utilities.url_utils import canonical_url_key
# Line-anchored regexes for the legacy-row guard. See `assemble_full_report`
-# for why a substring `in body` check is too loose.
-_LEGACY_SOURCES_RE = re.compile(r"^## Sources\b", re.MULTILINE)
+# for why a substring `in body` check is too loose. A Sources heading alone is
+# not enough: LLMs sometimes emit one followed by placeholder prose. Require
+# the first non-blank line to be a bracket-shaped legacy bibliography entry,
+# including grouped indices and the historical empty-index shape, and require
+# its URL continuation line so bracket-led prose cannot trip the guard.
+_LEGACY_SOURCES_RE = re.compile(
+ r"^## Sources\b[^\r\n]*\r?\n"
+ r"(?:[ \t]*\r?\n)*[ \t]*\[(?:\d+(?:[ \t]*,[ \t]*\d+)*)?\]"
+ r"[^\r\n]*\r?\n[ \t]+URL:[ \t]*\S",
+ re.MULTILINE,
+)
_LEGACY_METRICS_RE = re.compile(r"^## Research Metrics\b", re.MULTILINE)
diff --git a/tests/web/services/test_report_assembly_service.py b/tests/web/services/test_report_assembly_service.py
index 523da024dc..38944c49d3 100644
--- a/tests/web/services/test_report_assembly_service.py
+++ b/tests/web/services/test_report_assembly_service.py
@@ -13,6 +13,9 @@ from local_deep_research.database.models.research import (
ResearchHistory,
ResearchResource,
)
+from local_deep_research.utilities.search_utilities import (
+ format_links_to_markdown,
+)
from local_deep_research.web.services.report_assembly_service import (
_build_metrics_markdown,
_build_sources_markdown,
@@ -204,6 +207,66 @@ class TestAssembleFullReport:
assert "https://old.com" in out
assert "https://new.com" not in out
+ def test_legacy_row_with_repeated_source_not_double_rendered(
+ self, db_session
+ ):
+ """A formatter-grouped source can carry multiple citation indices."""
+ legacy_sources = format_links_to_markdown(
+ [
+ {
+ "title": "Old Source",
+ "url": "https://old.com",
+ "index": index,
+ }
+ for index in (1, 3)
+ ]
+ )
+ assert legacy_sources.startswith("[1, 3]")
+ legacy_body = "The answer body.\n\n## Sources\n\n" + legacy_sources
+ research = _mk_research(db_session, report_content=legacy_body)
+ _mk_resource(
+ db_session,
+ research.id,
+ url="https://new.com",
+ title="New",
+ )
+
+ out = assemble_full_report(research, db_session)
+
+ assert out.count("## Sources") == 1
+ assert "https://old.com" in out
+ assert "https://new.com" not in out
+
+ def test_legacy_row_with_indexless_source_not_double_rendered(
+ self, db_session
+ ):
+ """A persisted formatter row can have an empty citation index."""
+ legacy_sources = format_links_to_markdown(
+ [
+ {
+ "title": "Index-less Old Source",
+ "url": "https://old.com",
+ }
+ ]
+ )
+ assert legacy_sources.startswith(
+ "[] Index-less Old Source (source nr: )\n URL: https://old.com"
+ )
+ legacy_body = "The answer body.\n\n## Sources\n\n" + legacy_sources
+ research = _mk_research(db_session, report_content=legacy_body)
+ _mk_resource(
+ db_session,
+ research.id,
+ url="https://new.com",
+ title="New",
+ )
+
+ out = assemble_full_report(research, db_session)
+
+ assert out.count("## Sources") == 1
+ assert "https://old.com" in out
+ assert "https://new.com" not in out
+
def test_legacy_row_with_inline_metrics_not_double_rendered(
self, db_session
):
@@ -254,6 +317,35 @@ class TestAssembleFullReport:
# gives count == 2 — both legitimate occurrences.
assert out.count("## Sources") == 2
+ def test_placeholder_sources_heading_does_not_trigger_legacy_guard(
+ self, db_session
+ ):
+ """An LLM placeholder heading is not an embedded bibliography."""
+ body = (
+ "Section 1 prose [[1]](https://example.com/a).\n\n"
+ "## Sources\n"
+ "[Note: The framework will append the consolidated "
+ "'## Sources' block after this subsection.]\n\n"
+ "Section 2 prose [[2]](https://example.com/b).\n\n"
+ "## Sources\n"
+ "[Note: The framework will append a consolidated "
+ "'## Sources' block to the entire report.]\n\n"
+ "Section 3 prose [[3]](https://example.com/c)."
+ )
+ research = _mk_research(db_session, report_content=body)
+ _mk_resource(
+ db_session,
+ research.id,
+ url="https://structured.example/source",
+ title="Structured source",
+ )
+
+ out = assemble_full_report(research, db_session)
+
+ assert "https://structured.example/source" in out
+ assert out.count("\n## Sources\n") == 3
+ assert out.rfind("\n## Sources\n") > out.index("Section 3 prose")
+
def test_falls_back_to_row_order_when_index_missing(
self, db_session, caplog
):