~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 LLM-emitted duplicate subsection headings and bibliographies (#5294)
S3379c7fd335c
diff --git a/changelog.d/5294.bugfix.md b/changelog.d/5294.bugfix.mdnew file mode 100644index 0000000000..d5eb4af6fb--- /dev/null+++ b/changelog.d/5294.bugfix.md@@ -0,0 +1 @@+Detailed-mode reports no longer stack a duplicate subsection title under the framework's `## i.j Name` heading, and no longer keep per-subsection `## Sources` / `## References` / `### Selected Bibliography…` blocks that the model sometimes emits; prompts now forbid those patterns and `IntegratedReportGenerator` strips a leading restated heading (including sibling bleed, Roman/bold prefixes, and curly quotes), a mirrored italic purpose line, and embedded bibliography blocks before appending each subsection's content.diff --git a/src/local_deep_research/report_generator.py b/src/local_deep_research/report_generator.pyindex 299b14a5bf..f250fb345c 100644--- a/src/local_deep_research/report_generator.py+++ b/src/local_deep_research/report_generator.py@@ -1,4 +1,5 @@import importlib+import refrom typing import Any, Dict, List, Optionalfrom datetime import datetime, UTC@@ -23,6 +24,202 @@ DEFAULT_MAX_CONTEXT_CHARS = (4000 # Max characters for context (safe for smaller local models))+# Spelled-out output rules appended to every per-subsection prompt. Small+# and/or quantized local models routinely ignore long, vague instructions,+# so this is intentionally short, numbered, and refers to the actual+# rendering consequences (a duplicate heading, a duplicate bibliography)+# rather than abstract guidance.+#+# The framework always (a) inserts `## i.j Name` itself before appending+# LLM content and (b) appends one consolidated `## Sources` block to the+# whole report at the end of _format_final_report. The LLM does not need+# to repeat either of those things, and historically has — producing+# visibly stacked duplicate headings and duplicate "## Sources" sections+# per subsection.+_SUBSECTION_OUTPUT_GUIDANCE = (+ "\n\nOUTPUT FORMAT RULES (FOLLOW EXACTLY):\n"+ "1. Do NOT start your output with a Markdown heading of any level "+ "(#, ##, ###, ####, etc.). The framework already inserts the "+ "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 '## Sources' block to the entire report after every "+ "subsection is written; including your own bibliography duplicates "+ "the same source list.\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 "+ "subsection only."+)++# Pre-compiled normalisers used by _strip_subsection_boilerplate. These+# are best-effort cleanups that run on the LLM's raw `current_knowledge`+# before it is appended to the section — they do NOT touch the+# framework's own headings or its trailing '## Sources' block.+#+# Leading heading: any ATX heading (level 1-6) at the very start of the+# subsection content. Limited to a single match so we never strip a real+# sub-subheading used to organise the body. Real reports show the LLM+# almost always opens with a redundant ``### <subsection name>`` (or a+# sibling's name, or a Roman-numeral-prefixed restatement) immediately+# under the framework's own ``## i.j Name`` heading.+_LEADING_HEADING_RE = re.compile(+ r"\A[ \t\n]*[ \t]{0,3}#{1,6}[ \t]+[^\n]*",+)++# Leading italic purpose statement that mirrors the framework's+# ``_<purpose>_`` subtitle (e.g. ``_To summarise the section's scope..._``).+# Supports single underscores (_..._) or single asterisks (*...*).+# Limited to a single match at the start.+_LEADING_ITALIC_PURPOSE_RE = re.compile(+ r"\A[ \t\n]*(?:_[^_\n]+_|(?:\*(?!\*)[^*\n]+\*))[ \t]*",+)++_BIB_KEYWORD_PATTERN = (+ r"(?:"+ r"Sources?|References?|Bibliograph(?:y|ies)|Citations?|"+ r"Key[ \t]+References?|Selected[ \t]+Bibliograph(?:y|ies)|"+ r"Works?[ \t]+Cited|Cited[ \t]+Works?|"+ r"Additional[ \t]+Resources?|Further[ \t]+Reading"+ r")"+)++_BIB_HEADINGS_CHAIN = (+ rf"{_BIB_KEYWORD_PATTERN}"+ rf"(?:[ \t]*(?:,|and|&|/)[ \t]+{_BIB_KEYWORD_PATTERN})*"+)++_BIB_QUALIFIER = (+ r"(?:"+ r"[ \t]+for[ \t]+[^\n]+"+ r"|[ \t]*\([^\n]+\)"+ r"|[ \t]*[:\-–—][ \t]*[^\n]+"+ r")?"+)++# 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_HEADING_RE = re.compile(+ rf"(?m)^[ \t]{{0,3}}#{{1,6}}[ \t]+"+ rf"{_BIB_HEADINGS_CHAIN}"+ rf"{_BIB_QUALIFIER}"+ r"[ \t]*$",+ re.IGNORECASE,+)++# Next ATX heading at levels 1-6 — marks the end of a bibliography block+# when more subsection content follows it (common when the LLM appends a+# "Selected Bibliography" mid-stream and then continues writing).+_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?")++# Citation-list shape: digit+dot, bullet, or bracket citation start+_CITATION_LINE_RE = re.compile(+ r"^\s*(?:\d+[\.\)]\s+|[-*•]\s+|\[\d|\[cite|\bhttps?://|\bdoi:)",+ re.IGNORECASE,+)++# Italic note line matching LLM boilerplate (e.g. "*(Note: ... bibliography ...)*")+_BIB_NOTE_RE = re.compile(+ r"^\s*[\*_]\s*\(?Note\b[^\n]*?\b(?:bibliography|sources|references|citations)\b",+ re.IGNORECASE,+)+++def _get_code_fence_spans(text: str) -> List[tuple[int, int]]:+ """Return character index ranges (start, end) that are inside code blocks.++ Recognizes:+ - Backtick fenced blocks (``` or ```` etc.), paired by delimiter type and length.+ - Tilde fenced blocks (~~~ or ~~~~ etc.), paired by delimiter type and length.+ - Indented code blocks (lines indented by 4+ spaces or tabs, preceded by a blank line).+ """+ spans: List[tuple[int, int]] = []+ if not text:+ return spans++ lines = text.splitlines(keepends=True)+ in_fence = False+ fence_char = ""+ fence_len = 0+ fence_start = 0++ in_indented = False+ indented_start = 0+ last_indented_end = 0+ prev_blank = True++ line_offset = 0+ fence_open_re = re.compile(r"^[ \t]{0,3}(`{3,}|~{3,})")++ for line in lines:+ line_start = line_offset+ line_end = line_offset + len(line)+ line_offset = line_end++ stripped = line.rstrip("\r\n")++ if in_fence:+ close_re = (+ rf"^[ \t]{{0,3}}{re.escape(fence_char)}{{{fence_len},}}[ \t]*$"+ )+ if re.match(close_re, stripped):+ in_fence = False+ spans.append((fence_start, line_end))+ continue++ open_match = fence_open_re.match(stripped)+ if open_match:+ if in_indented:+ in_indented = False+ spans.append((indented_start, last_indented_end))++ in_fence = True+ delim = open_match.group(1)+ fence_char = delim[0]+ fence_len = len(delim)+ fence_start = line_start+ prev_blank = False+ continue++ is_indented = stripped.startswith(" ") or stripped.startswith("\t")+ is_blank = not stripped.strip()++ if in_indented:+ if is_indented:+ last_indented_end = line_end+ elif is_blank:+ pass+ else:+ in_indented = False+ spans.append((indented_start, last_indented_end))+ else:+ if is_indented and prev_blank:+ in_indented = True+ indented_start = line_start+ last_indented_end = line_end++ prev_blank = is_blank++ if in_fence:+ spans.append((fence_start, len(text)))+ elif in_indented:+ spans.append((indented_start, last_indented_end))++ return spans+++def _is_in_spans(pos: int, spans: List[tuple[int, int]]) -> bool:+ """Return True if pos falls within any range in spans."""+ return any(start <= pos < end for start, end in spans)+def get_report_generator(search_system=None):"""Return an instance of the report generator with default settings.@@ -232,11 +429,12 @@ class IntegratedReportGenerator:falls back to hard truncation.Args:- text: The text to truncate- max_chars: Maximum characters allowed+ text: Text to truncate.+ max_chars: Maximum characters allowed.Returns:- Truncated text with [...truncated] marker if truncation occurred+ Truncated text with ``[...truncated]`` marker if truncation+ occurred, otherwise the original text unchanged."""if len(text) <= max_chars:return text@@ -258,9 +456,483 @@ class IntegratedReportGenerator:if last_sentence_end > min_acceptable:return truncated[:last_sentence_end] + "\n[...truncated]"- # Fallback to hard truncation+ # Fall back to hard truncationreturn truncated + "\n[...truncated]"+ @staticmethod+ def _normalize_heading_text(text: str) -> str:+ """Normalize heading text for fuzzy comparison against subsection names.++ Strips markdown bold markers, leading Roman-numeral / alphabetic /+ numeric enumeration prefixes (``II.``, ``A.``, ``1.``), collapses+ whitespace, and lowercases. Used only for matching — never written+ back into the report.+ """+ cleaned = text.strip()+ # Drop surrounding bold/italic markers the LLM wraps titles in+ # (``**Title**``, ``*Title*``, ``__Title__``). Avoid regex here —+ # CodeQL flags ``^[*_]+|[*_]+$`` as polynomial on long ``*`` runs.+ cleaned = cleaned.strip("*_").strip()+ cleaned = re.sub(+ r"^(?:"+ r"[IVXLCDM]+\.|" # Roman numerals: II. III. IV. (uppercase only to avoid "Mix." false strip)+ r"[A-Z]\.|" # Single letters: A. B.+ r"\d+\." # Arabic numerals: 1. 2.+ r")\s+",+ "",+ cleaned,+ )+ # Normalise curly/smart quotes and dashes so "Author's" (U+2019)+ # matches "Author's" (ASCII apostrophe) and en/em dashes collapse+ # to a plain hyphen for comparison.+ cleaned = cleaned.translate(+ str.maketrans(+ "\u2018\u2019\u201c\u201d\u2013\u2014\u2212", "''\"\"---"+ )+ )+ return re.sub(r"\s+", " ", cleaned).strip().lower()++ @classmethod+ def _heading_restates_name(cls, heading_text: str, name: str) -> bool:+ """Return True if *heading_text* is a restatement of *name*.++ Handles exact matches, prefix matches (heading extends the name+ with a colon/dash subtitle), and the reverse (name is a longer+ form of a short heading). Empty names never match.+ """+ if not name or not heading_text:+ return False+ norm_heading = cls._normalize_heading_text(heading_text)+ norm_name = cls._normalize_heading_text(name)+ if not norm_heading or not norm_name:+ return False+ if norm_heading == norm_name:+ return True+ # Heading extends the subsection name with a subtitle (e.g. "Name: subtitle")+ # Delimiters only: bare space prefixes (e.g. "Introduction to …")+ # are intentional non-matches. En/em dashes are already folded to+ # ASCII "-" by _normalize_heading_text.+ if norm_heading.startswith(norm_name):+ rest = norm_heading[len(norm_name) :].lstrip()+ if rest and rest[0] in (":", "-", "("):+ return True+ # Name extends a short heading with a subtitle.+ if norm_name.startswith(norm_heading):+ rest = norm_name[len(norm_heading) :].lstrip()+ if rest and rest[0] in (":", "-", "("):+ return True+ return False++ def _strip_leading_heading(+ self,+ content: str,+ subsection_name: str,+ sibling_subsection_names: Optional[List[str]] = None,+ ) -> str:+ """Drop a single leading heading line only when it restates subsection or sibling name."""+ leading = _LEADING_HEADING_RE.match(content)+ if not leading:+ return content++ fence_spans = _get_code_fence_spans(content)+ if _is_in_spans(leading.start(), fence_spans):+ return content++ heading_text = re.sub(r"^\s*#{1,6}[ \t]+", "", leading.group(0)).strip()+ # Subsection name: allow subtitle extensions (Name: subtitle)+ if self._heading_restates_name(heading_text, subsection_name):+ end_pos = leading.end()+ while end_pos < len(content) and content[end_pos] in "\r\n":+ end_pos += 1+ return content[end_pos:]+ # Siblings: require exact normalized equality (avoid over-strip of+ # "### <Sibling>: Subtitle" organizers)+ norm_heading = self._normalize_heading_text(heading_text)+ for sibling in sibling_subsection_names or []:+ if sibling and norm_heading == self._normalize_heading_text(+ sibling+ ):+ end_pos = leading.end()+ while end_pos < len(content) and content[end_pos] in "\r\n":+ end_pos += 1+ return content[end_pos:]+ return content++ def _strip_leading_italic_purpose(+ self,+ content: str,+ purpose: Optional[str] = None,+ ) -> str:+ """Drop a single leading italic purpose statement if it mirrors purpose or boilerplate."""+ leading_italic = _LEADING_ITALIC_PURPOSE_RE.match(content)+ if not leading_italic:+ return content++ fence_spans = _get_code_fence_spans(content)+ if _is_in_spans(leading_italic.start(), fence_spans):+ return content++ italic_raw = leading_italic.group(0).strip()+ italic_text = italic_raw.strip("_*").strip()+ should_strip = False++ if purpose:+ norm_italic = self._normalize_heading_text(italic_text)+ norm_purpose = self._normalize_heading_text(purpose)+ # Guard against degenerate purpose that normalizes to "" (e.g. "***")+ # — startswith("") is always True and would strip any italic line.+ if norm_italic and norm_purpose:+ if (+ norm_italic == norm_purpose+ or norm_italic.startswith(norm_purpose)+ or norm_purpose.startswith(norm_italic)+ or self._heading_restates_name(italic_text, purpose)+ ):+ should_strip = True++ if not should_strip:+ norm_italic = self._normalize_heading_text(italic_text)+ if norm_italic:+ # Narrow unconditional boilerplate markers — always safe to strip+ if norm_italic.startswith(+ (+ "purpose:",+ "scope:",+ "this subsection ",+ "this section ",+ )+ ):+ should_strip = True+ # Verb-prefix boilerplate: check for relevance to purpose to avoid+ # eating legitimate epigraphs like "_To review the evidence is to understand the past._"+ # when purpose is unrelated (S2).+ elif purpose is not None and norm_italic.startswith(+ (+ "to summarize",+ "to summarise",+ "to clarify",+ "to examine",+ "to outline",+ "to provide",+ "to describe",+ "to detail",+ "to analyze",+ "to analyse",+ "to explore",+ "to present",+ "to discuss",+ "to evaluate",+ "to investigate",+ "to review",+ "to assess",+ "aims to",+ "designed to",+ )+ ):+ norm_purpose = self._normalize_heading_text(purpose)+ if norm_purpose:+ _stop_words = {+ "to",+ "the",+ "and",+ "for",+ "that",+ "this",+ "with",+ "from",+ "summarize",+ "summarise",+ "clarify",+ "examine",+ "outline",+ "provide",+ "describe",+ "detail",+ "analyze",+ "analyse",+ "explore",+ "present",+ "discuss",+ "evaluate",+ "investigate",+ "review",+ "assess",+ "aims",+ "designed",+ "section",+ "subsection",+ }+ purpose_words = {+ w+ for w in re.findall(+ r"\b[a-z0-9]{3,}\b", norm_purpose.lower()+ )+ if w not in _stop_words+ }+ italic_words = {+ w+ for w in re.findall(+ r"\b[a-z0-9]{3,}\b", norm_italic.lower()+ )+ if w not in _stop_words+ }+ if (+ self._heading_restates_name(italic_text, purpose)+ or norm_italic.startswith(norm_purpose)+ or norm_purpose.startswith(norm_italic)+ or bool(purpose_words & italic_words)+ ):+ should_strip = True++ if should_strip:+ end_pos = leading_italic.end()+ while end_pos < len(content) and content[end_pos] in "\r\n":+ end_pos += 1+ return content[end_pos:]+ return content++ def _strip_embedded_bibliographies(self, content: str) -> str:+ """Remove every embedded bibliography block outside code blocks.++ Only removes a heading that looks like a bibliography *and* whose+ following block looks like a citation list — this prevents+ substantive headings such as "### Sources of Bias..." or+ "### Sources. Data Collection Methodology" from being deleted with+ their analysis body (R1). Consecutive bibliography headings no longer+ consume intervening prose (R8).+ """+ fence_spans = _get_code_fence_spans(content)+ pieces: List[str] = []+ cursor = 0+ bib_removed = False++ for match in _BIBLIOGRAPHY_HEADING_RE.finditer(content):+ if _is_in_spans(match.start(), fence_spans):+ continue++ after_heading = match.end()+ next_heading_start = None+ next_heading_is_bib = False+ for nh in _NEXT_HEADING_RE.finditer(content, after_heading):+ if not _is_in_spans(nh.start(), fence_spans):+ next_heading_start = nh.start()+ # Peek if the next heading itself is a bibliography heading+ # — if so, we will trim the current block to its citation+ # list end rather than to the heading (R8).+ heading_line = content[nh.start() :].split("\n", 1)[0]+ if _BIBLIOGRAPHY_HEADING_RE.match(heading_line):+ next_heading_is_bib = True+ break++ block_end = (+ next_heading_start+ if next_heading_start is not None+ else len(content)+ )+ block_text = content[after_heading:block_end]++ # Body-shape safeguard: only treat as bibliography if the block+ # contains citation-like lines. Require at least one citation+ # marker, or >30% of non-empty lines look like citations, to+ # avoid deleting substantive analysis headings that happen to+ # match the label grammar (e.g. "### Sources for the Analysis"+ # with prose body).+ non_empty_lines = [+ ln for ln in block_text.splitlines() if ln.strip()+ ]+ if not non_empty_lines:+ # Empty block — treat as bibliography (heading with no body)+ is_bib_block = True+ else:+ citation_lines = sum(+ 1 for ln in non_empty_lines if _CITATION_LINE_RE.match(ln)+ )+ # Require explicit italic note boilerplate (e.g. "*(Note: ... bibliography ...)*")+ # rather than matching arbitrary prose containing "bibliography" (S1).+ has_bib_note = any(+ _BIB_NOTE_RE.match(ln) for ln in non_empty_lines[:3]+ )+ if citation_lines == 0 and not has_bib_note:+ # No citation shape and no italic bib note — preserve heading + body+ continue+ # If citation lines present, require they are not a tiny minority+ # unless block is short (e.g. "- item" single line)+ if citation_lines == 0:+ is_bib_block = has_bib_note+ elif len(non_empty_lines) <= 3:+ is_bib_block = citation_lines >= 1+ else:+ is_bib_block = (+ citation_lines / len(non_empty_lines)+ ) >= 0.3 or citation_lines >= 2++ if not is_bib_block:+ continue++ # Passed safeguard — this is a real bibliography block to remove+ bib_removed = True+ block_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++ pieces.append(content[cursor:block_start])++ # R8: consecutive bib headings — end at citation list, not at next bib heading+ if next_heading_is_bib:+ # Find end of citation list within block_text+ lines = block_text.splitlines(keepends=True)+ offset = 0+ last_citation_end = 0+ for ln in lines:+ if _CITATION_LINE_RE.match(ln) or (+ _BIB_NOTE_RE.match(ln) and offset < 300+ ):+ last_citation_end = offset + len(ln)+ # Also keep consecutive citation lines; stop at first+ # non-citation prose that is not a blank/bib note?+ offset += len(ln)+ # Also include trailing blank lines after citations, but not+ # intervening prose before next bib heading+ if last_citation_end > 0:+ # Advance cursor to after the citation list (preserve+ # intervening prose between two bib headings)+ cursor = after_heading + last_citation_end+ # Skip following blank lines / HRs but not prose+ while cursor < block_end and content[cursor] in " \t\r\n-":+ # Only skip whitespace/HRs, stop at prose+ # Peek next non-whitespace chunk+ nxt = content[cursor:].lstrip(" \t\r\n")+ if nxt.startswith("-") and nxt[1:2] in " \t":+ # Another HR-like line, skip it+ cursor += len(content[cursor:]) - len(nxt)+ # consume the HR line+ eol = content.find("\n", cursor)+ cursor = eol + 1 if eol != -1 else len(content)+ else:+ break+ if cursor >= block_end:+ break+ # Ensure we don't overshoot into next bib heading's prefix;+ # if cursor is before next_heading_start, keep it, else+ # fall back to next_heading_start+ if cursor > block_end:+ cursor = block_end+ else:+ cursor = block_end+ else:+ if next_heading_start is not None:+ cursor = next_heading_start+ else:+ cursor = len(content)++ if bib_removed:+ pieces.append(content[cursor:])+ return "".join(pieces)+ return content++ def _strip_subsection_boilerplate(+ self,+ content: str,+ subsection_name: str,+ section_name: str,+ sibling_subsection_names: Optional[List[str]] = None,+ purpose: Optional[str] = None,+ ) -> str:+ """Strip boilerplate the LLM tends to emit around subsection content.++ Small and/or quantized local models routinely produce three artefacts+ that pollute the rendered report even when the OUTPUT FORMAT RULES+ spelled out in the subsection prompt explicitly forbid them:++ 1. A redundant leading heading (stripped by :meth:`_strip_leading_heading`)+ 2. An italic purpose statement (stripped by :meth:`_strip_leading_italic_purpose`)+ 3. An embedded bibliography block (stripped by :meth:`_strip_embedded_bibliographies`)++ This helper normalises the per-subsection content so the rendered+ report stays clean even when the model ignored the prompt. It is a+ defensive cleanup, not a substitute for the prompt rules.++ Args:+ content: Raw ``current_knowledge`` returned for one subsection.+ subsection_name: The subsection's name (used for matching and+ logging).+ section_name: The parent section's name (used for logging).+ sibling_subsection_names: Optional names of other subsections+ in the same section. When provided, a leading heading that+ restates a *sibling's* name (context-bleed) is also stripped.+ purpose: Optional purpose string for the subsection. Used to+ verify if a leading italic line mirrors the subsection purpose.++ Returns:+ ``content`` with a redundant leading heading removed when it+ restates this or a sibling subsection name, a leading+ italic-purpose statement removed, and any embedded bibliography+ blocks removed. Returns content unchanged if no artefacts are+ found, aside from collapsing 3+ blank lines to 2 and+ normalising trailing whitespace (collapsed outside code fences+ only, so fenced blocks are never rewritten).+ """+ if not content:+ return content++ original_len = len(content)+ new_content = self._strip_leading_heading(+ content, subsection_name, sibling_subsection_names+ )+ new_content = self._strip_leading_italic_purpose(new_content, purpose)+ new_content = self._strip_embedded_bibliographies(new_content)++ # R2: fence-aware whitespace collapse — never rewrite inside code fences+ fence_spans = _get_code_fence_spans(new_content)+ if not fence_spans:+ collapsed = re.sub(r"\n{3,}", "\n\n", new_content).strip()+ else:+ parts: List[str] = []+ last = 0+ for s, e in sorted(fence_spans):+ # collapse outside fence+ parts.append(re.sub(r"\n{3,}", "\n\n", new_content[last:s]))+ # keep fence content verbatim+ parts.append(new_content[s:e])+ last = e+ parts.append(re.sub(r"\n{3,}", "\n\n", new_content[last:]))+ collapsed = "".join(parts).strip()+ new_content = collapsed+ if new_content:+ new_content += "\n"++ if len(new_content) != original_len:+ # R5: make silent deletions observable+ if not new_content.strip() and content.strip():+ logger.warning(+ "Stripped subsection boilerplate emptied non-trivial content for "+ f"'{section_name} > {subsection_name}': "+ f"{original_len} -> {len(new_content)} chars — "+ "check for over-strip (R1/R3 regression)"+ )+ elif len(new_content) < 0.5 * original_len:+ logger.warning(+ "Stripped subsection boilerplate heavily truncated content for "+ f"'{section_name} > {subsection_name}': "+ f"{original_len} -> {len(new_content)} chars"+ )+ else:+ logger.debug(+ "Stripped subsection boilerplate for "+ f"'{section_name} > {subsection_name}': "+ f"{original_len} -> {len(new_content)} chars"+ )+ return new_content+def _build_previous_context(self, accumulated_findings: List[str]) -> str:"""Build context block from previously generated sections.@@ -435,6 +1107,7 @@ class IntegratedReportGenerator:f"Use tables to organize information where applicable. "f"For conclusion sections: synthesize key findings and provide forward-looking insights. "f"Build upon the research findings from earlier sections to create a cohesive narrative."+ f"{_SUBSECTION_OUTPUT_GUIDANCE}")else:# Subsection-level prompt - more focused@@ -452,6 +1125,7 @@ class IntegratedReportGenerator:f"Use tables to organize information where applicable. "f"IMPORTANT: Avoid repeating information that would logically be covered in other sections - focus on what makes this subsection unique. "f"Previous research exists - find specific angles for this subsection."+ f"{_SUBSECTION_OUTPUT_GUIDANCE}")logger.info(@@ -512,11 +1186,52 @@ class IntegratedReportGenerator:# Add the researched content for this subsectionif subsection_results.get("current_knowledge"):generated_content = subsection_results["current_knowledge"]- section_content.append(generated_content)- # Accumulate for context in subsequent sections- accumulated_findings.append(- f"[{section['name']} > {subsection['name']}]\n{generated_content}"- )+ # Strip the boilerplate the LLM tends to emit around+ # subsections: a redundant leading heading that mirrors+ # the framework's own `## i.j Name` heading, an italic+ # purpose statement that mirrors the framework's+ # `_<purpose>_` subtitle, and an embedded+ # '## Sources' / '## References' bibliography block+ # (the framework appends one master '## Sources' to+ # the whole report). Small/quantized local models+ # routinely ignore the OUTPUT FORMAT RULES in the+ # prompt above; this normalisation step guarantees a+ # clean rendered output regardless.+ sibling_names = [+ s["name"]+ for s in section["subsections"]+ if s["name"] != subsection["name"]+ ]+ try:+ generated_content = self._strip_subsection_boilerplate(+ generated_content,+ subsection_name=subsection["name"],+ section_name=section["name"],+ sibling_subsection_names=sibling_names,+ purpose=subsection.get("purpose"),+ )+ except Exception:+ logger.exception(+ "Boilerplate strip failed for "+ f"'{section['name']} > {subsection['name']}' — "+ "using raw LLM content"+ )+ # Fall back to raw content so a cosmetic step never+ # aborts the entire multi-section report.+ generated_content = (+ subsection_results.get("current_knowledge", "")+ or ""+ )+ if generated_content.strip():+ section_content.append(generated_content)+ # Accumulate for context in subsequent sections+ accumulated_findings.append(+ f"[{section['name']} > {subsection['name']}]\n{generated_content}"+ )+ else:+ section_content.append(+ "*Limited information was found for this subsection.*\n"+ )else:section_content.append("*Limited information was found for this subsection.*\n"diff --git a/tests/report/test_report_generator_helpers.py b/tests/report/test_report_generator_helpers.pyindex fae1ccdca3..f6b6758dc7 100644--- a/tests/report/test_report_generator_helpers.py+++ b/tests/report/test_report_generator_helpers.py@@ -173,3 +173,511 @@ class TestBuildPreviousContext:assert "Finding 8" in resultassert "Finding 9" in resultassert "Finding 0" not in result+++# ── _strip_subsection_boilerplate ──+++class TestNormalizeHeadingText:+ """Tests for _normalize_heading_text."""++ def test_strips_bold_and_roman_prefix(self, generator):+ assert (+ generator._normalize_heading_text(+ "**II. Market Structure and Competition: Dual**"+ )+ == "market structure and competition: dual"+ )++ def test_strips_arabic_prefix(self, generator):+ assert generator._normalize_heading_text("1. Foo Bar") == "foo bar"++ def test_empty(self, generator):+ assert generator._normalize_heading_text("") == ""+++class TestHeadingRestatesName:+ """Tests for _heading_restates_name against observed LLM patterns."""++ def test_exact_match(self, generator):+ assert generator._heading_restates_name(+ "Climate Drivers and Feedback Loops",+ "Climate Drivers and Feedback Loops",+ )++ def test_extended_with_colon_subtitle(self, generator):+ assert generator._heading_restates_name(+ "Regional Case Studies (North, South, Coastal): "+ "Granular Data on Outcomes",+ "Regional Case Studies (North, South, Coastal)",+ )++ def test_roman_prefix_and_bold(self, generator):+ assert generator._heading_restates_name(+ "**III. Supply Chain Resilience Within the Industry: "+ "Upstream Dependencies**",+ "Supply Chain Resilience Within the Industry",+ )++ def test_en_dash_extension(self, generator):+ assert generator._heading_restates_name(+ "Early Phase (1925–1947): Author's 'Foundational Essay' "+ "– Textual Genesis and Comparative Mechanics",+ "Early Phase (1925–1947): Author's 'Foundational Essay'",+ )++ def test_curly_apostrophe_matches_ascii(self, generator):+ # LLM used U+2019 RIGHT SINGLE QUOTATION MARK in place of ASCII '.+ assert generator._heading_restates_name(+ "Early Phase (1925–1947): Author\u2019s 'Foundational Essay' "+ "– Textual Genesis",+ "Early Phase (1925–1947): Author's 'Foundational Essay'",+ )++ def test_unrelated_heading_does_not_match(self, generator):+ assert not generator._heading_restates_name(+ "I. Strategic Mobilization and Event-Catalyzed Shifts",+ "Mainstream & Dominant Phases (1980–2014+): "+ "The Landmark Events, Coalition Wins",+ )++ def test_does_not_match_substantive_space_prefix(self, generator):+ assert not generator._heading_restates_name(+ "Introduction to Methods",+ "Introduction",+ )++ def test_empty_name_never_matches(self, generator):+ assert not generator._heading_restates_name("Anything", "")+++class TestStripSubsectionBoilerplate:+ """Tests for _strip_subsection_boilerplate using generic report patterns."""++ def _strip(+ self,+ generator,+ content,+ name="Name",+ section="Sec",+ siblings=None,+ purpose=None,+ ):+ return generator._strip_subsection_boilerplate(+ content,+ subsection_name=name,+ section_name=section,+ sibling_subsection_names=siblings,+ purpose=purpose,+ )++ def test_empty_content(self, generator):+ assert self._strip(generator, "") == ""++ def test_strips_exact_duplicate_leading_heading(self, generator):+ content = (+ "### Climate Drivers and Feedback Loops\n\n"+ "The source material asserts that under the framework...\n\n"+ "#### 1. The Historical Construction\n"+ "More body.\n"+ )+ result = self._strip(+ generator,+ content,+ name="Climate Drivers and Feedback Loops",+ )+ assert not result.lstrip().startswith("#")+ assert "The source material asserts" in result+ assert "#### 1. The Historical Construction" in result++ def test_strips_roman_prefixed_restatement(self, generator):+ content = (+ "### III. Board-Executive Leadership Overlap: Dual Roles\n\n"+ "Body paragraph about dual roles.\n"+ )+ result = self._strip(+ generator,+ content,+ name="Board-Executive Leadership Overlap",+ )+ assert "### III." not in result+ assert "Body paragraph about dual roles." in result++ def test_strips_bold_wrapped_restatement(self, generator):+ content = (+ "### **Regulatory Status: Open vs. Restricted**\n\n"+ "Metric convergence paragraph.\n"+ )+ result = self._strip(+ generator,+ content,+ name="Regulatory Status: Open vs. Restricted",+ )+ assert "Regulatory Status" not in result.split("\n")[0]+ assert "Metric convergence paragraph." in result++ def test_strips_sibling_bleed_heading(self, generator):+ # LLM copied a sibling subsection's heading into this one's content.+ content = (+ "### The Policy Goal Spectrum\n\n"+ "While the source material posits...\n"+ )+ result = self._strip(+ generator,+ content,+ name="Distinction Between Theory and Practice",+ siblings=["The Policy Goal Spectrum"],+ )+ assert "Policy Goal Spectrum" not in result+ assert "While the source material posits" in result++ def test_preserves_unrelated_organisational_opener(self, generator):+ # Leading ### is a free-form organiser, not a restatement of the+ # subsection name — must be kept.+ content = (+ "### **I. Strategic Mobilization and Event-Catalyzed Shifts "+ "(Regional Focus)**\n\n"+ "The transition of the movement...\n"+ )+ result = self._strip(+ generator,+ content,+ name=(+ "Mainstream & Dominant Phases (1980–2014+): "+ "The Landmark Events, Coalition Wins"+ ),+ )+ assert "Strategic Mobilization" in result+ assert "The transition of the movement" in result++ def test_strips_leading_italic_purpose(self, generator):+ content = (+ "_To summarise the section's scope and limits..._\n\n"+ "Prose begins here.\n"+ )+ result = self._strip(+ generator,+ content,+ name="Anything",+ purpose="To summarise the section's scope and limits...",+ )+ assert "_To summarise" not in result+ assert "Prose begins here." in result++ def test_preserves_legitimate_leading_epigraph_italic(self, generator):+ content = "_In memory of those affected._\n\nProse begins here.\n"+ result = self._strip(+ generator,+ content,+ name="Historical Context",+ purpose="Examine social impacts",+ )+ assert "_In memory of those affected._" in result+ assert "Prose begins here." in result++ def test_strips_purpose_matching_subtitle(self, generator):+ content = (+ "_To examine historical impacts and social consequences._\n\n"+ "Prose begins here.\n"+ )+ result = self._strip(+ generator,+ content,+ name="Historical Context",+ purpose="To examine historical impacts and social consequences.",+ )+ assert "_To examine" not in result+ assert "Prose begins here." in result++ def test_strips_heading_then_italic_purpose(self, generator):+ content = (+ "### Foo Bar\n_To clarify the distinction._\n\nActual prose.\n"+ )+ result = self._strip(+ generator,+ content,+ name="Foo Bar",+ purpose="To clarify the distinction.",+ )+ assert "### Foo Bar" not in result+ assert "_To clarify" not in result+ assert "Actual prose." in result++ def test_strips_trailing_sources_block(self, generator):+ content = (+ "Body of the subsection with findings.\n\n"+ "## Sources\n\n"+ "1. Some paper (2020).\n"+ "2. Another paper (2021).\n"+ )+ result = self._strip(generator, content, name="Whatever")+ assert "## Sources" not in result+ assert "Some paper" not in result+ assert "Body of the subsection with findings." in result++ def test_strips_selected_bibliography_mid_stream(self, generator):+ # Bibliography appears mid-content, then more prose continues.+ content = (+ "Closing paragraph of the analysis.\n\n"+ "---\n"+ "### Selected Bibliography for Subsection IV: Media "+ "(Supplementary)\n"+ "*(Note: The main report bibliography will be updated.)*\n\n"+ "1. Smith, Jane. Industry Trends.\n"+ "2. Doe, John. Comparative Policy Analysis.\n\n"+ "---\n"+ "### Detailed Content Expansion: New Source Integration\n\n"+ "#### I. Deep Dive\n"+ "Continued analysis after the bib block.\n"+ )+ result = self._strip(+ generator,+ content,+ name="Media, Journalism, and Academic Freedom",+ )+ assert "Selected Bibliography" not in result+ assert "Smith, Jane" not in result+ assert "Doe, John" not in result+ assert "main report bibliography will be updated" not in result+ assert "Detailed Content Expansion" in result+ assert "Continued analysis after the bib block." in result+ assert "Closing paragraph of the analysis." in result++ def test_midstream_bib_with_h4_continuation(self, generator):+ content = (+ "Closing paragraph of the analysis.\n\n"+ "### Selected Bibliography for Subsection IV\n"+ "1. Smith, Jane. Industry Trends.\n\n"+ "#### Deep Dive: Downstream Effects\n"+ "Continued analysis after the bib block.\n"+ )+ result = self._strip(+ generator,+ content,+ name="Media, Journalism, and Academic Freedom",+ )+ assert "Selected Bibliography" not in result+ assert "Smith, Jane" not in result+ assert "#### Deep Dive: Downstream Effects" in result+ assert "Continued analysis after the bib block." in result++ def test_substantive_headings_not_overstripped(self, generator):+ for heading in (+ "### Sources of Bias in the Sampling Frame",+ "## Reference Architecture",+ "### Citation Network Analysis",+ ):+ content = f"Intro paragraph.\n\n{heading}\nBody analysis.\n"+ result = self._strip(generator, content, name="Analysis")+ assert heading in result+ assert "Body analysis." in result++ def test_fenced_code_block_sources_preserved(self, generator):+ content = (+ "Intro text.\n\n"+ "```python\n"+ "## Sources\n"+ "def get_sources():\n"+ " return []\n"+ "```\n\n"+ "Following prose paragraph.\n"+ )+ result = self._strip(generator, content, name="Python Implementation")+ assert "## Sources" in result+ assert "def get_sources():" in result+ assert "Following prose paragraph." in result++ def test_strips_references_and_citations_variants(self, generator):+ for heading in (+ "## References",+ "### Bibliography",+ "## Key References",+ "### Works Cited",+ "## Further Reading",+ "#### Sources",+ "#### References",+ ):+ content = f"Prose.\n\n{heading}\n\n- item\n"+ result = self._strip(generator, content, name="X")+ assert heading not in result+ assert "Prose." in result+ assert "- item" not in result++ def test_preserves_internal_body_headings(self, generator):+ content = (+ "### Organizational Structure and Financing\n\n"+ "Intro paragraph.\n\n"+ "#### 1. Organizational Governance\n"+ "Detail A.\n\n"+ "#### 2. Financial Architecture\n"+ "Detail B.\n"+ )+ result = self._strip(+ generator,+ content,+ name="Organizational Structure and Financing",+ )+ assert "#### 1. Organizational Governance" in result+ assert "#### 2. Financial Architecture" in result+ # Leading ### stripped; #### body headings kept.+ assert not any(+ line.startswith("### ") and not line.startswith("####")+ for line in result.splitlines()+ )++ def test_no_boilerplate_unchanged_substance(self, generator):+ content = "Just a plain paragraph with no headings at all.\n"+ result = self._strip(generator, content, name="Plain")+ assert "Just a plain paragraph" in result++ def test_preserves_thematic_break_when_no_bib_stripped(self, generator):+ content = "Paragraph 1.\n\n---\n\nParagraph 2.\n"+ result = self._strip(generator, content, name="Plain")+ assert "---" in result+ assert "Paragraph 1." in result+ assert "Paragraph 2." in result++ def test_leading_whitespace_before_heading(self, generator):+ content = "\n\n### Foo Bar\n\nBody.\n"+ result = self._strip(generator, content, name="Foo Bar")+ assert "### Foo Bar" not in result+ assert "Body." in result++ def test_substantive_headings_with_and_or_qualifier_preserved(+ self, generator+ ):+ for heading in (+ "### Sources and Methods",+ "### Sources of Bias in the Sampling Frame",+ "## Reference Architecture",+ "### Citation Network Analysis",+ "### Resources and Logistics",+ "### Further Reading and Next Steps",+ ):+ content = f"Intro paragraph.\n\n{heading}\nBody content.\n"+ result = self._strip(generator, content, name="Analysis")+ assert heading in result+ assert "Body content." in result++ def test_code_fence_variants_and_indented_code_preserved(self, generator):+ # 4-backtick fence wrapping 3-backticks+ content_backtick = (+ "Intro.\n\n````markdown\n```\n## Sources\n```\n````\n\nOutro.\n"+ )+ res_bt = self._strip(generator, content_backtick, name="Code")+ assert "## Sources" in res_bt+ assert "Outro." in res_bt++ # Tilde fence ~~~+ content_tilde = "Intro.\n\n~~~\n## Sources\n~~~\n\nOutro.\n"+ res_tilde = self._strip(generator, content_tilde, name="Code")+ assert "## Sources" in res_tilde+ assert "Outro." in res_tilde++ # Indented code block (4 spaces)+ content_indented = (+ "Intro.\n\n ## Sources\n def foo(): pass\n\nOutro.\n"+ )+ res_ind = self._strip(generator, content_indented, name="Code")+ assert "## Sources" in res_ind+ assert "Outro." in res_ind++ def test_mixed_thematic_break_and_bib_removal(self, generator):+ content = (+ "Paragraph 1.\n\n"+ "---\n\n"+ "Paragraph 2.\n\n"+ "---\n\n"+ "## Sources\n"+ "- Source 1\n"+ "- Source 2\n"+ )+ result = self._strip(generator, content, name="Section")+ assert "Paragraph 1." in result+ assert "Paragraph 2." in result+ assert "---" in result # Legitimate thematic break kept+ assert "## Sources" not in result+ assert "- Source 1" not in result++ def test_asterisk_italic_purpose_and_quote_preservation(self, generator):+ # Epigraph / quote should be preserved+ content_quote = "_To be, or not to be._\n\nProse paragraph.\n"+ res_quote = self._strip(+ generator, content_quote, name="Hamlet", purpose="Analyze play"+ )+ assert "_To be, or not to be._" in res_quote+ assert "Prose paragraph." in res_quote++ # Asterisk italic purpose matching purpose should be stripped+ content_asterisk = "*To analyze play.* \n\nProse paragraph.\n"+ res_ast = self._strip(+ generator,+ content_asterisk,+ name="Hamlet",+ purpose="To analyze play.",+ )+ assert "*To analyze play.*" not in res_ast+ assert "Prose paragraph." in res_ast++ # Asterisk explicit purpose should be stripped+ content_exp = "*Purpose: Overview of system.*\n\nProse paragraph.\n"+ res_exp = self._strip(generator, content_exp, name="Hamlet")+ assert "*Purpose: Overview of system.*" not in res_exp+ assert "Prose paragraph." in res_exp++ def test_bibliography_heading_with_prose_containing_word_bibliography_preserved(+ self, generator+ ):+ # S1 regression test: ### Bibliography followed by prose about bibliographic history must NOT be deleted.+ content = (+ "### Bibliography\n"+ "The bibliography of this era is particularly rich with primary historical records.\n"+ "Scholars have long examined these records to document changes in policy.\n"+ )+ result = self._strip(generator, content, name="Historical Overview")+ assert "### Bibliography" in result+ assert "The bibliography of this era is particularly rich" in result++ def test_sources_heading_with_prose_body_preserved(self, generator):+ # S3 lockdown test: ## Sources followed by substantive prose must be preserved by body-shape safeguard.+ content = (+ "## Sources\n"+ "This subsection analyzes the primary sources of macroeconomic instability in the region.\n"+ "Inflation and currency devaluation remain central concerns.\n"+ )+ result = self._strip(generator, content, name="Macroeconomic Analysis")+ assert "## Sources" in result+ assert "primary sources of macroeconomic instability" in result++ def test_unrelated_epigraph_with_verb_prefix_preserved(self, generator):+ # S2 regression test: _To review the evidence is to understand the past._ preserved when purpose is unrelated.+ content = (+ "_To review the evidence is to understand the past._\n\n"+ "Prose body of the analysis.\n"+ )+ result = self._strip(+ generator,+ content,+ name="Quantum Computing",+ purpose="Analyze quantum computing advancements in hardware",+ )+ assert "_To review the evidence is to understand the past._" in result+ assert "Prose body of the analysis." in result++ def test_matching_verb_prefix_italic_purpose_stripped(self, generator):+ # S2: Italic line starting with verb prefix that shares content words with purpose is stripped.+ content = (+ "_To review quantum computing advancements in hardware._\n\n"+ "Prose body of the analysis.\n"+ )+ result = self._strip(+ generator,+ content,+ name="Quantum Computing",+ purpose="To review quantum computing advancements in hardware",+ )+ assert (+ "_To review quantum computing advancements in hardware._"+ not in result+ )+ assert "Prose body of the analysis." in result