~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
fix(report): strip rogue subsection sources and trailing citation dumps (#6454)
Sd51afed7a111
diff --git a/changelog.d/6454.bugfix.md b/changelog.d/6454.bugfix.mdnew file mode 100644index 0000000000..d844b31bba--- /dev/null+++ b/changelog.d/6454.bugfix.md@@ -0,0 +1 @@+Strip rogue subsection bibliographies and trailing citation dumps while preserving legitimate numbered lists, markdown reference links, and footnote glosses.diff --git a/src/local_deep_research/citation_handlers/standard_citation_handler.py b/src/local_deep_research/citation_handlers/standard_citation_handler.pyindex 3b59cd2445..287701b6b4 100644--- a/src/local_deep_research/citation_handlers/standard_citation_handler.py+++ b/src/local_deep_research/citation_handlers/standard_citation_handler.py@@ -33,7 +33,7 @@ Sources:Current time is {current_timestamp} UTC for verifying temporal references in sources.-Provide a detailed analysis with citations. Do not create the bibliography, it will be provided automatically. Never make up sources. Never write or create urls. Only write text relevant to the question. Example format: "According to the research [1], ..."+Provide a detailed analysis with inline citations. Citations must only appear inline within your sentences (e.g. "According to the research [1], ..."). Do NOT create a bibliography, sources list, or list of citations at the end, as sources will be provided automatically. Never make up sources. Never write or create urls. Only write text relevant to the question."""response = self._invoke_with_streaming(prompt)@@ -93,7 +93,7 @@ New Sources:Current time is {current_timestamp} UTC for verifying temporal references in sources.Reflect information from sources critically based on: {fact_check_response}. Never invent sources.-Provide a detailed answer with citations. Do not create the bibliography, it will be provided automatically. Example format: "According to [1], ..." """+Provide a detailed answer with inline citations. Citations must only appear inline within your sentences (e.g. "According to [1], ..."). Do NOT create a bibliography, sources list, or list of citations at the end, as sources will be provided automatically."""response = self._invoke_with_streaming(prompt)diff --git a/src/local_deep_research/report_generator.py b/src/local_deep_research/report_generator.pyindex 099ee5bade..30ba9bff04 100644--- a/src/local_deep_research/report_generator.py+++ b/src/local_deep_research/report_generator.py@@ -43,10 +43,14 @@ _SUBSECTION_OUTPUT_GUIDANCE = ("subsection heading for you; starting with your own heading line ""creates a visible duplicate next to it in the final report.\n""2. Do NOT end your output with a sources, references, bibliography, "- "citations, key references, or selected bibliography section. The "- "framework appends a single consolidated source list after every "- "subsection is written; including your own bibliography duplicates "- "the same source list.\n"+ "citations, key references, or selected bibliography section or list. "+ "Do NOT include any trailing heading or label (such as 'Sources:', "+ "'**Sources:**', or 'References:'), and do NOT append a list or block "+ "of citations/links at the end of your response. Citations must ONLY "+ "appear inline within sentences (e.g. 'According to [1], ...'). "+ "The framework automatically compiles and appends the consolidated "+ "source list after all subsections are completed; including your own "+ "trailing sources or bibliography duplicates it.\n""3. Begin your output with prose (a paragraph or a table), not a ""heading and not an italic purpose statement. You may use '###' / ""'####' / deeper levels for internal sub-subheadings inside this "@@ -90,23 +94,36 @@ _BIB_HEADINGS_CHAIN = (rf"(?:[ \t]*(?:,|and|&|/)[ \t]+{_BIB_KEYWORD_PATTERN})*")-_BIB_QUALIFIER = (- r"(?:"- r"[ \t]+for[ \t]+[^\n]+"- r"|[ \t]*\([^\n]+\)"- r"|[ \t]*[:\-–—][ \t]*[^\n]+"- r")?"+# ATX-style heading (with #) allowing:+# - levels 1-6 with optional empty heading line preceding it (e.g. "###\n**Sources:**")+# - optional section numbers (e.g. "1." or "1.1")+# - optional bold/italic markers ("**Sources**", "*References:*")+# - optional trailing colons, dashes, periods, or qualifiers+_ATX_BIB_HEADING = (+ rf"[ \t]{{0,3}}#{{1,6}}[ \t]*(?:\n[ \t]*)?"+ rf"(?:\d+(?:\.\d+)*\.?[ \t]+)?"+ rf"(?:\*\*|\*|__|_)?{_BIB_HEADINGS_CHAIN}(?:\*\*|\*|__|_)?(?:"+ rf"[ \t]*[:\-\u2013\u2014.][ \t]*[^\n]*"+ rf"|[ \t]+for[ \t]+[^\n]+"+ rf"|[ \t]*\([^\n]+\)"+ rf")?[ \t]*$"+)++# Pseudo-heading (without #) where the keyword line acts as a section header:+# - bold or italic wrapped: "**Sources:**", "**Sources**", "*References:*"+# - or plain keyword ending with a colon or dash: "Sources:", "References:"+_PSEUDO_BIB_HEADING = (+ r"[ \t]{0,3}"+ rf"(?:"+ rf"(?:\*\*|\*|__|_){_BIB_HEADINGS_CHAIN}[:\-\u2013\u2014.]?(?:\*\*|\*|__|_)"+ rf"|"+ rf"{_BIB_HEADINGS_CHAIN}[ \t]*[:\-\u2013\u2014]"+ rf")[ \t]*$")-# Bibliography-style heading at levels 1-6. Used to locate the start of a-# per-subsection sources block that the framework already consolidates at-# the end of the whole report. The closed label grammar ensures substantive-# headings like "### Sources and Methods" or "## Reference Architecture" are preserved.+# Bibliography-style heading at levels 1-6 or pseudo-heading line._BIBLIOGRAPHY_HEADING_RE = re.compile(- rf"(?m)^[ \t]{{0,3}}#{{1,6}}[ \t]+"- rf"{_BIB_HEADINGS_CHAIN}"- rf"{_BIB_QUALIFIER}"- r"[ \t]*$",+ rf"(?m)^(?:{_ATX_BIB_HEADING}|{_PSEUDO_BIB_HEADING})",re.IGNORECASE,)@@ -116,12 +133,80 @@ _BIBLIOGRAPHY_HEADING_RE = re.compile(_NEXT_HEADING_RE = re.compile(r"(?m)^[ \t]{0,3}#{1,6}[ \t]+")# Decorative horizontal rules the LLM often wraps around bibliography-# blocks (``---`` on its own line).-_HR_LINE_RE = re.compile(r"(?m)^[ \t]*-{3,}[ \t]*\n?")+# blocks (``---``, ``***``, or ``___`` on their own line).+_HR_LINE_RE = re.compile(+ r"(?m)^[ \t]{0,3}(?:(?:-[ \t]*){3,}|(?:_[ \t]*){3,}|(?:\*[ \t]*){3,})[ \t]*\n?"+)++# Empty heading line (e.g. `###`) or decorative horizontal rule line (`---`, `***`, `___`)+_EMPTY_HEADING_OR_HR_RE = re.compile(+ r"^[ \t]*(?:#{1,6}|(?:-[ \t]*){3,}|(?:_[ \t]*){3,}|(?:\*[ \t]*){3,})[ \t]*$"+)-# Citation-list shape: digit+dot, bullet, or bracket citation start+# Reference-style link definitions in markdown: [1]: https://... or [name]: url+_MD_LINK_DEF_RE = re.compile(r"^[ \t]*\[[^\]]+\]:[ \t]*\S+")++# Ordinary ordered or bulleted list item prefix+_ORDERED_OR_BULLET_LIST_RE = re.compile(r"^[ \t]*(?:[-*•]|\d+[\.\)])\s+")++# Citation-list shape for ATX headings (## Sources, ### References):+# digit+dot, bullet, bracket citation start (single or double brackets),+# lenticular brackets, URL, DOI, or markdown link.+# Excludes markdown reference-style link definitions ("[1]: https://...")._CITATION_LINE_RE = re.compile(- r"^\s*(?:\d+[\.\)]\s+|[-*•]\s+|\[\d|\[cite|\bhttps?://|\bdoi:)",+ r"^\s*(?:"+ r"\d+[\.\)]\s+"+ r"|[-*•]\s+"+ r"|\[\[\d+\]\](?:\([^)]*\))?"+ r"|\[\d+\](?!\:)"+ r"|【\d+】"+ r"|\[cite\d*\]"+ r"|\bhttps?://"+ r"|\bdoi:\s*\S+"+ r"|\[[^\]]+\]\((?:https?://|/library/)[^)]+\)"+ r")",+ re.IGNORECASE,+)++# Citation-list shape for pseudo-headings (Sources:, **Sources** without #):+# Must carry explicit citation markers (brackets, links, URLs, DOIs), NOT plain numbered/bullet lists.+_PSEUDO_BIB_CITATION_LINE_RE = re.compile(+ r"^\s*(?:(?:[-*•]|\d+[\.\)])\s+)?"+ r"(?:"+ r"\[\[\d+\]\](?:\([^)]*\))?"+ r"|\[\d+\](?!\:)"+ r"|【\d+】"+ r"|\[cite\d*\]"+ r"|\bhttps?://"+ r"|\bdoi:\s*\S+"+ r"|\[[^\]]+\]\((?:https?://|/library/)[^)]+\)"+ r")",+ re.IGNORECASE,+)++# Single citation token for concatenated line check:+# Supports single brackets [1], double brackets [[1]], optional markdown link target,+# [cite1], or Chinese/Japanese lenticular brackets 【1】+_CONCATENATED_CITATION_ITEM = (+ r"(?:(?:\[\[\d+\]\]|\[\d+\]|\[cite\d*\])(?:\([^)]*\))?|【\d+】)"+)++# Line composed purely of citation markers, brackets, links, and commas.+# Structured without overlapping leading/trailing whitespace quantifiers to prevent ReDoS.+_CONCATENATED_CITATIONS_LINE_RE = re.compile(+ rf"^(?:[,\s]*{_CONCATENATED_CITATION_ITEM})+[,\s]*$"+)++# Unheaded citation list item start (for trailing citation blocks lacking any heading).+# Excludes numbered/bullet lists, markdown link definitions, and bare URLs without citation markers.+# Requires double brackets [[...]], single brackets with link target/source, cite tag, or DOI.+_UNHEADED_CITATION_ITEM_RE = re.compile(+ r"^\s*(?:"+ r"\[\[\d+\]\](?:\([^)]*\))?"+ r"|\[\d+\](?!\:)(?:\((?:https?://|/library/)[^)]+\)|\s+(?:https?://|doi:))"+ r"|\[cite\d*\]"+ r"|doi:\s*\S+"+ r")",re.IGNORECASE,)@@ -714,6 +799,14 @@ class IntegratedReportGenerator:if _is_in_spans(match.start(), fence_spans):continue+ matched_heading_text = match.group(0).lstrip()+ is_atx_heading = matched_heading_text.startswith("#")+ bib_line_re = (+ _CITATION_LINE_RE+ if is_atx_heading+ else _PSEUDO_BIB_CITATION_LINE_RE+ )+after_heading = match.end()next_heading_start = Nonenext_heading_is_bib = False@@ -767,8 +860,40 @@ class IntegratedReportGenerator:# Empty block — treat as bibliography (heading with no body)is_bib_block = Trueelse:+ # Intervening prose safeguard: a bibliography heading must introduce+ # citations directly (or via an immediate note / 1-line lead-in).+ # If substantive prose precedes citations, the heading is introducing+ # substantive analysis, not an embedded bibliography.+ first_line = non_empty_lines[0]+ first_is_bib = (+ bib_line_re.match(first_line)+ or _BIB_NOTE_RE.match(first_line)+ or _HR_LINE_RE.match(first_line)+ )+ if not first_is_bib:+ # Check if it's a short 1-line lead-in immediately followed by citations+ second_is_citation = len(+ non_empty_lines+ ) > 1 and bib_line_re.match(non_empty_lines[1])+ is_lead_in = (+ len(first_line.strip()) < 120+ and second_is_citation+ and bool(+ re.search(+ r"\b(?:sources?|references?|consulted|cited|following|basis)\b",+ first_line,+ re.IGNORECASE,+ )+ )+ )+ has_early_note = any(+ _BIB_NOTE_RE.match(ln) for ln in non_empty_lines[:3]+ )+ if not (is_lead_in or has_early_note):+ continue+citation_lines = sum(- 1 for ln in non_empty_lines if _CITATION_LINE_RE.match(ln)+ 1 for ln in non_empty_lines if bib_line_re.match(ln))# Require explicit italic note boilerplate (e.g. "*(Note: ... bibliography ...)*")# rather than matching arbitrary prose containing "bibliography" (S1).@@ -796,16 +921,27 @@ class IntegratedReportGenerator:bib_removed = Trueblock_start = match.start()prefix = content[cursor:block_start]- hr_matches = list(_HR_LINE_RE.finditer(prefix))- if hr_matches:- cut_idx = len(prefix)- for hr_m in reversed(hr_matches):- between = prefix[hr_m.end() : cut_idx]- if between.strip():- break- cut_idx = hr_m.start()- if cut_idx < len(prefix):- block_start = cursor + cut_idx+ prefix_lines = prefix.splitlines(keepends=True)+ cut_idx = len(prefix)+ curr_offset = len(prefix)+ found_delimiter = False+ for pline in reversed(prefix_lines):+ line_start = curr_offset - len(pline)+ abs_pos = cursor + line_start+ if _is_in_spans(abs_pos, fence_spans):+ break+ pstripped = pline.strip()+ if not pstripped:+ curr_offset = line_start+ continue+ if _EMPTY_HEADING_OR_HR_RE.match(pstripped):+ found_delimiter = True+ cut_idx = line_start+ curr_offset = line_start+ else:+ break+ if found_delimiter and cut_idx < len(prefix):+ block_start = cursor + cut_idxpieces.append(content[cursor:block_start])@@ -818,7 +954,7 @@ class IntegratedReportGenerator:for ln in lines:line_start = after_heading + offsetif not _is_in_spans(line_start, fence_spans) and (- _CITATION_LINE_RE.match(ln)+ bib_line_re.match(ln)or (_BIB_NOTE_RE.match(ln) and offset < 300)):last_citation_end = offset + len(ln)@@ -872,7 +1008,7 @@ class IntegratedReportGenerator:is_bib_line = bool(outside_fenceand (- _CITATION_LINE_RE.match(ln)+ bib_line_re.match(ln)or (_BIB_NOTE_RE.match(ln) and non_empty_seen <= 3)or _HR_LINE_RE.match(ln))@@ -892,7 +1028,148 @@ class IntegratedReportGenerator:if bib_removed:pieces.append(content[cursor:])- return "".join(pieces)+ content = "".join(pieces)+ return self._strip_trailing_citation_blocks(content)++ def _strip_trailing_citation_blocks(self, content: str) -> str:+ """Remove unheaded trailing citation blocks or concatenated citations.++ When the LLM is instructed not to include a sources section heading,+ some models omit the heading entirely but append a list or block of+ citations at the end of the response (e.g. consecutive lines of+ `[[111]](/library/...)` or a concatenated line of citations).++ This defensive helper scans backwards from the end of the content+ outside code fences and strips trailing citation blocks while+ preserving all substantive prose, tables, code fences, reference-style+ link definitions, further-reading URLs, quiz answer keys, and CJK notes.+ """+ if not content:+ return content++ fence_spans = _get_code_fence_spans(content)+ lines = content.splitlines(keepends=True)+ if not lines:+ return content++ line_offsets: List[int] = []+ curr = 0+ for ln in lines:+ line_offsets.append(curr)+ curr += len(ln)++ # Skip trailing empty / whitespace lines+ cut_idx = len(lines)+ while cut_idx > 0 and not lines[cut_idx - 1].strip():+ cut_idx -= 1++ if cut_idx == 0:+ return content++ # Never strip inside code fences+ if _is_in_spans(line_offsets[cut_idx - 1], fence_spans):+ return content++ citation_lines_count = 0+ concatenated_lines_count = 0+ bib_heading_preceded = False+ idx = cut_idx++ while idx > 0:+ offset = line_offsets[idx - 1]+ if _is_in_spans(offset, fence_spans):+ break++ line_str = lines[idx - 1].strip()+ if not line_str:+ idx -= 1+ continue++ # Reference-style link definitions ([1]: https://...) must NEVER be stripped+ if _MD_LINK_DEF_RE.match(line_str):+ break++ # Plain numbered or bulleted list items must NEVER be stripped as unheaded citations+ if _ORDERED_OR_BULLET_LIST_RE.match(line_str):+ break++ # Concatenated citation clusters (e.g. [[217]][[218]] or [[1]](/a), [2], [[3]])+ if _CONCATENATED_CITATIONS_LINE_RE.match(line_str):+ # Must contain double brackets [[...]], cite tags, or adjacent bracket tokens;+ # exclude plain numbered markdown links like [1](url) [2](url)+ if (+ "[[" in line_str+ or "[cite" in line_str.lower()+ or bool(re.search(r"\]\s*\[|】\s*[【\[]|\]\s*【", line_str))+ ):+ concatenated_lines_count += 1+ idx -= 1+ continue+ break++ if _UNHEADED_CITATION_ITEM_RE.match(line_str):+ citation_lines_count += 1+ idx -= 1+ continue++ # Support indented continuation lines within a citation entry.+ # When scanning backwards, find the preceding non-indented line.+ if lines[idx - 1].startswith((" ", "\t")):+ p = idx - 2+ while p >= 0 and lines[p].startswith((" ", "\t")):+ p -= 1+ if p >= 0 and _UNHEADED_CITATION_ITEM_RE.match(+ lines[p].strip()+ ):+ idx -= 1+ continue++ if _BIB_NOTE_RE.match(line_str):+ idx -= 1+ continue++ # Check if preceded by an explicit bibliography heading or label+ if citation_lines_count > 0 or concatenated_lines_count > 0:+ if _BIBLIOGRAPHY_HEADING_RE.match(line_str):+ bib_heading_preceded = True+ idx -= 1+ continue+ if _EMPTY_HEADING_OR_HR_RE.match(line_str):+ idx -= 1+ continue++ break++ # Only strip if:+ # 1. Found a concatenated citation cluster+ # 2. Preceded by an explicit bibliography heading/label and found >= 1 citation items+ # 3. Found >= 2 citation items in an unheaded block (bare --- does NOT arm single items)+ should_strip = False+ if concatenated_lines_count > 0:+ should_strip = True+ elif bib_heading_preceded and citation_lines_count >= 1:+ should_strip = True+ elif citation_lines_count >= 2:+ should_strip = True++ if should_strip:+ # Also consume any preceding empty headings, labels, or HRs+ while idx > 0:+ offset = line_offsets[idx - 1]+ if _is_in_spans(offset, fence_spans):+ break+ pstripped = lines[idx - 1].strip()+ if not pstripped:+ idx -= 1+ continue+ if _EMPTY_HEADING_OR_HR_RE.match(+ pstripped+ ) or _BIBLIOGRAPHY_HEADING_RE.match(pstripped):+ idx -= 1+ else:+ break+ return "".join(lines[:idx])+return contentdef _strip_subsection_boilerplate(@@ -947,7 +1224,11 @@ class IntegratedReportGenerator:new_content = self._strip_leading_italic_purpose(new_content, purpose)pre_bib_len = len(new_content)new_content = self._strip_embedded_bibliographies(new_content)- bib_chars_removed = pre_bib_len - len(new_content)+ pre_trailing_len = len(new_content)+ new_content = self._strip_trailing_citation_blocks(new_content)+ bib_chars_removed = (pre_bib_len - pre_trailing_len) + (+ pre_trailing_len - len(new_content)+ )# R2: fence-aware whitespace collapse — never rewrite inside code fencesfence_spans = _get_code_fence_spans(new_content)diff --git a/tests/report/test_report_generator_helpers.py b/tests/report/test_report_generator_helpers.pyindex fc35fa8dcf..6df78d2184 100644--- a/tests/report/test_report_generator_helpers.py+++ b/tests/report/test_report_generator_helpers.py@@ -867,6 +867,92 @@ class TestStripSubsectionBoilerplate:in loguru_caplog.text)+ def test_production_entry_preserves_reference_style_link_definitions(+ self, generator+ ):+ content = (+ "Analysis prose of subsection [1] and further details [2].\n\n"+ "[1]: https://arxiv.org/abs/1234.5678\n"+ "[2]: https://arxiv.org/abs/8765.4321\n"+ )+ result = self._strip(generator, content, name="Section")+ assert "[1]: https://arxiv.org/abs/1234.5678" in result+ assert "[2]: https://arxiv.org/abs/8765.4321" in result++ def test_production_entry_preserves_trailing_further_reading_urls(+ self, generator+ ):+ content = (+ "Analysis prose of subsection.\n\n"+ "To go deeper, read:\n"+ "https://example.com/deep1\n"+ "https://example.com/deep2\n"+ )+ result = self._strip(generator, content, name="Section")+ assert "https://example.com/deep1" in result+ assert "https://example.com/deep2" in result++ def test_production_entry_preserves_single_url_under_horizontal_rule(+ self, generator+ ):+ content = (+ "Agrarian structure analysis.\n\n"+ "---\n\n"+ "https://example.com/source\n"+ )+ result = self._strip(generator, content, name="Section")+ assert "https://example.com/source" in result+ assert "---" in result++ def test_production_entry_preserves_sources_colon_with_numbered_list(+ self, generator+ ):+ content = (+ "Sources:\n"+ "1. Clone the repository.\n"+ "2. Run make deps.\n"+ "3. Verify the checksums.\n"+ )+ result = self._strip(generator, content, name="Section")+ assert "1. Clone the repository." in result+ assert "2. Run make deps." in result+ assert "3. Verify the checksums." in result++ def test_production_entry_preserves_bold_sources_with_prose_and_list(+ self, generator+ ):+ content = (+ "**Sources**\n\n"+ "This section discusses the sources of variation.\n\n"+ "1. Environmental factors.\n"+ "2. Genetic predispositions.\n"+ "3. Measurement error.\n\n"+ "Conclusion prose follows.\n"+ )+ result = self._strip(generator, content, name="Section")+ assert "**Sources**" in result+ assert "This section discusses the sources of variation." in result+ assert "1. Environmental factors." in result+ assert "Conclusion prose follows." in result++ def test_production_entry_preserves_cjk_footnotes_quiz_and_numbered_links(+ self, generator+ ):+ cjk_content = "Analysis prose.\n\n【1】 第一条注释\n【2】 第二条注释\n"+ assert "【1】 第一条注释" in self._strip(+ generator, cjk_content, name="Section"+ )++ quiz_content = "Analysis prose.\n\n[1] true\n[2] false\n"+ assert "[1] true" in self._strip(+ generator, quiz_content, name="Section"+ )++ links_content = "Analysis prose.\n\n[1](https://example.com/1) [2](https://example.com/2)\n"+ assert "[1](https://example.com/1)" in self._strip(+ generator, links_content, name="Section"+ )+# ── _strip_embedded_bibliographies ──@@ -1015,3 +1101,269 @@ class TestStripEmbeddedBibliographies:assert "## Sources" not in _SUBSECTION_OUTPUT_GUIDANCEassert "'## Sources'" not in _SUBSECTION_OUTPUT_GUIDANCE++ def test_strips_empty_heading_with_bold_sources_label(self, generator):+ content = (+ "Analysis prose of subsection.\n\n"+ "###\n"+ "**Sources:**\n"+ "[[34]](/library/document/1/chunks#chunk-52) First source title.\n"+ "[[35]](/library/document/2/chunks#chunk-45) Second source title.\n"+ )+ result = self._strip(generator, content)+ assert "**Sources:**" not in result+ assert "###" not in result+ assert "[[34]]" not in result+ assert "First source title" not in result+ assert result.strip() == "Analysis prose of subsection."++ def test_strips_pseudo_heading_sources_label(self, generator):+ content = (+ "Analysis prose of subsection.\n\n"+ "**Sources:**\n"+ "[1] First source title.\n"+ "[2] Second source title.\n"+ )+ result = self._strip(generator, content)+ assert "**Sources:**" not in result+ assert "[1] First source" not in result+ assert result.strip() == "Analysis prose of subsection."++ def test_strips_unheaded_trailing_citation_list(self, generator):+ content = (+ "Manusmriti analysis concludes here [110].\n\n"+ "[[111]](/library/doc/1) Sastra commemoration volume.\n"+ "[[112]](/library/doc/2) Hopkins and Bhandarkar on Manu origin.\n"+ "[[113]](/library/doc/3) Constitutional development.\n"+ )+ result = self._strip(generator, content)+ assert "Sastra commemoration volume" not in result+ assert "Hopkins and Bhandarkar" not in result+ assert result.strip() == "Manusmriti analysis concludes here [110]."++ def test_strips_unheaded_trailing_concatenated_citations(self, generator):+ content = (+ "Mughal administrative layers concluded here [216].\n\n"+ "[[217]](/library/doc/1)[[218]](/library/doc/2)[[219]](/library/doc/3)"+ )+ result = self._strip(generator, content)+ assert "[[217]]" not in result+ assert "[[218]]" not in result+ assert (+ result.strip()+ == "Mughal administrative layers concluded here [216]."+ )++ def test_concatenated_citations_line_re_redos_resistant(self):+ from local_deep_research.report_generator import (+ _CONCATENATED_CITATIONS_LINE_RE,+ )++ # Pathological string designed to trigger exponential backtracking in vulnerable regexes+ evil_string = "\u30100\u3011" + ("\t\u30100\u3011" * 50) + "X"+ assert not _CONCATENATED_CITATIONS_LINE_RE.match(evil_string)++ valid_line = "[[1]](/a), [2], [[3]], \u30104\u3011"+ assert _CONCATENATED_CITATIONS_LINE_RE.match(valid_line)++ def test_preserves_single_citation_preceded_by_bare_horizontal_rule(+ self, generator+ ):+ content = (+ "Agrarian structure analysis.\n\n"+ "---\n\n"+ "[1] https://example.com/source\n"+ )+ result = self._strip(generator, content)+ assert "https://example.com/source" in result+ assert "---" in result+ assert result.strip() == content.strip()++ def test_preserves_reference_style_link_definitions(self, generator):+ content = (+ "Analysis prose of subsection [1] and further details [2].\n\n"+ "[1]: https://arxiv.org/abs/1234.5678\n"+ "[2]: https://arxiv.org/abs/8765.4321\n"+ )+ result = self._strip(generator, content)+ assert "[1]: https://arxiv.org/abs/1234.5678" in result+ assert "[2]: https://arxiv.org/abs/8765.4321" in result+ assert result.strip() == content.strip()++ def test_preserves_trailing_further_reading_urls(self, generator):+ content = (+ "Analysis prose of subsection.\n\n"+ "To go deeper, read:\n"+ "https://example.com/deep1\n"+ "https://example.com/deep2\n"+ )+ result = self._strip(generator, content)+ assert "https://example.com/deep1" in result+ assert "https://example.com/deep2" in result+ assert result.strip() == content.strip()++ def test_preserves_single_url_under_horizontal_rule(self, generator):+ content = (+ "Agrarian structure analysis.\n\n"+ "---\n\n"+ "https://example.com/source\n"+ )+ result = self._strip(generator, content)+ assert "https://example.com/source" in result+ assert "---" in result+ assert result.strip() == content.strip()++ def test_preserves_sources_colon_followed_by_ordinary_numbered_list(+ self, generator+ ):+ content = (+ "Sources:\n"+ "1. Clone the repository.\n"+ "2. Run make deps.\n"+ "3. Verify the checksums.\n"+ )+ result = self._strip(generator, content)+ assert "1. Clone the repository." in result+ assert "2. Run make deps." in result+ assert "3. Verify the checksums." in result+ assert result.strip() == content.strip()++ def test_preserves_bold_sources_label_with_intervening_prose_and_list(+ self, generator+ ):+ content = (+ "**Sources**\n\n"+ "This section discusses the sources of variation.\n\n"+ "1. Environmental factors.\n"+ "2. Genetic predispositions.\n"+ "3. Measurement error.\n\n"+ "Conclusion prose follows.\n"+ )+ result = self._strip(generator, content)+ assert "**Sources**" in result+ assert "This section discusses the sources of variation." in result+ assert "1. Environmental factors." in result+ assert "Conclusion prose follows." in result++ def test_preserves_cjk_footnote_glosses(self, generator):+ content = "Analysis prose.\n\n【1】 第一条注释\n【2】 第二条注释\n"+ result = self._strip(generator, content)+ assert "【1】 第一条注释" in result+ assert "【2】 第二条注释" in result++ def test_preserves_quiz_answer_keys(self, generator):+ content = "Analysis prose.\n\n[1] true\n[2] false\n"+ result = self._strip(generator, content)+ assert "[1] true" in result+ assert "[2] false" in result++ def test_preserves_numbered_markdown_link_lines(self, generator):+ content = (+ "Analysis prose.\n\n"+ "[1](https://example.com/1) [2](https://example.com/2)\n"+ )+ result = self._strip(generator, content)+ assert "[1](https://example.com/1)" in result+ assert "[2](https://example.com/2)" in result++ def test_preserves_code_fences_with_delimiters_in_embedded_bib_scan(+ self, generator+ ):+ content = (+ "Analysis prose.\n\n"+ "```markdown\n"+ "---\n"+ "### Example Heading\n"+ "```\n\n"+ "## Sources\n"+ "[1] https://example.org/one\n"+ "[2] https://example.org/two\n"+ )+ result = self._strip(generator, content)+ assert "```markdown\n---\n### Example Heading\n```" in result+ assert "## Sources" not in result+ assert "https://example.org/one" not in result++ def test_strips_unheaded_trailing_citations_with_wrapped_last_entry(+ self, generator+ ):+ content = (+ "Manusmriti analysis concludes here [110].\n\n"+ "[[111]](/library/doc/1) Sastra commemoration volume.\n"+ "[[112]](/library/doc/2) Hopkins and Bhandarkar on Manu origin.\n"+ "[[113]](/library/doc/3) Constitutional development of the early\n"+ " classical period in northern India.\n"+ )+ result = self._strip(generator, content)+ assert "[[111]]" not in result+ assert "[[112]]" not in result+ assert "[[113]]" not in result+ assert "classical period in northern India" not in result+ assert result.strip() == "Manusmriti analysis concludes here [110]."++ def test_preserves_substantive_table_with_citations(self, generator):+ table = (+ "| Aspect | Observations | Supporting Sources |\n"+ "|---|---|---|\n"+ "| Land Revenue | Peasant class formation | [[162]] Primary survey |\n"+ )+ content = "Analysis prose.\n\n" + table+ result = self._strip(generator, content)+ assert "Peasant class formation" in result+ assert "[[162]] Primary survey" in result++ def test_preserves_prose_ending_with_inline_citation(self, generator):+ content = "This historical transformation is well documented [[115]]."+ result = self._strip(generator, content)+ assert (+ result.strip()+ == "This historical transformation is well documented [[115]]."+ )++ def test_preserves_trailing_code_block_with_brackets(self, generator):+ content = (+ "Here is the data structure:\n\n```python\n[100, 200, 300]\n```"+ )+ result = self._strip(generator, content)+ assert "[100, 200, 300]" in result++ def test_strips_empty_heading_separated_by_blank_lines_from_sources_label(+ self, generator+ ):+ content = (+ "Analysis prose of subsection.\n\n"+ "###\n\n"+ "**Sources:**\n"+ "[[34]](/library/document/1) First source title.\n"+ "[[35]](/library/document/2) Second source title.\n"+ )+ result = self._strip(generator, content)+ assert "**Sources:**" not in result+ assert "###" not in result+ assert "[[34]]" not in result+ assert result.strip() == "Analysis prose of subsection."++ def test_strips_trailing_citations_with_sources_colon_label(+ self, generator+ ):+ content = (+ "Analysis prose of subsection.\n\n"+ "Sources:\n"+ "[1] https://example.com/one\n"+ )+ result = self._strip(generator, content)+ assert "Sources:" not in result+ assert "https://example.com/one" not in result+ assert result.strip() == "Analysis prose of subsection."++ def test_preserves_numbered_list_in_conclusion(self, generator):+ content = (+ "The system had three key characteristics:\n"+ "1. Endogamous social units\n"+ "2. Hereditary occupational specialization\n"+ "3. Hierarchical ritual status\n"+ )+ result = self._strip(generator, content)+ assert "1. Endogamous social units" in result+ assert "2. Hereditary occupational specialization" in result+ assert "3. Hierarchical ritual status" in result