From af7a6ade1353a74973999dc7616fa249d39f1858 Mon Sep 17 00:00:00 2001 From: lihongyuan99 <64824864+lihongyuan99@users.noreply.github.com> Date: Thu, 26 Nov 2026 22:32:28 +0800 Subject: [PATCH] fix(report-sections): preserve literal hashes in titles (#8576) * fix(report-sections): preserve literal hashes in titles * fix(report-sections): parse ATX markers once per heading --------- Co-authored-by: Willem Jiang --- .../tests/test_report_sections_extension.py | 23 +++++++++++++++++++ .../README.md | 7 ++++-- .../sections.py | 17 ++++++++++---- 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/backend/tests/test_report_sections_extension.py b/backend/tests/test_report_sections_extension.py index 353562764f..8f9ea67a38 100644 --- a/backend/tests/test_report_sections_extension.py +++ b/backend/tests/test_report_sections_extension.py @@ -99,6 +99,29 @@ def test_strip_duplicate_title_heading_variants(syspath): assert strip_duplicate_title_heading("正文没有标题", "摘要") == "正文没有标题" # A heading that does not restate the title is content, not a duplicate. assert strip_duplicate_title_heading("# 其他标题\n正文", "摘要") == "# 其他标题\n正文" + assert strip_duplicate_title_heading("# C\n正文", "C#") == "# C\n正文" + assert strip_duplicate_title_heading("# C#\n正文", "C#") == "正文" + assert strip_duplicate_title_heading("# C#\n正文", "C") == "# C#\n正文" + assert strip_duplicate_title_heading("# C# ###\n正文", "C#") == "正文" + assert strip_duplicate_title_heading("# A B\n正文", "A B") == "正文" + + +@pytest.mark.parametrize( + ("title", "opening", "body"), + [ + ("C#", "# C", "# C\n正文"), + ("Issue 42", "### # Issue 42", "### # Issue 42\n正文"), + ("#42", "# # 42", "正文"), + ("Issue 42", "### Issue 42 # ###", "### Issue 42 # ###\n正文"), + ], +) +def test_assembly_handles_literal_hashes_in_headings(load, title, opening, body): + handle = handler(load()) + save(handle, 1, title, f"{opening}\n正文") + + result = assemble(handle) + + assert result.artifact["document"] == f"## {title}\n\n{body}\n" def test_assemble_document_counts_stripped_bodies(syspath): diff --git a/examples/deerflow-extension-report-sections/README.md b/examples/deerflow-extension-report-sections/README.md index a94be39cf7..975143388d 100644 --- a/examples/deerflow-extension-report-sections/README.md +++ b/examples/deerflow-extension-report-sections/README.md @@ -13,8 +13,11 @@ full document never consumes the agent's context budget. Assembly drops a section's leading heading when it restates the section title (models habitually open each section with `# ` while the assembler already emits `## <title>`), which otherwise produces doubled headings across -the whole report. Sections are scoped per user and per thread in the plugin's -own SQLite file — no host database or host-internal imports. +the whole report. Assembly parses the opening and closing Markdown markers +once per heading, then compares the heading text ignoring whitespace. Literal +`#` characters, such as the ones in `C#` and `#42`, remain significant. +Sections are scoped per user and per thread in the plugin's own SQLite file — +no host database or host-internal imports. ## Deployment diff --git a/examples/deerflow-extension-report-sections/deerflow_extension_report_sections/sections.py b/examples/deerflow-extension-report-sections/deerflow_extension_report_sections/sections.py index 548aba0dcc..5ba5f3d050 100644 --- a/examples/deerflow-extension-report-sections/deerflow_extension_report_sections/sections.py +++ b/examples/deerflow-extension-report-sections/deerflow_extension_report_sections/sections.py @@ -25,9 +25,16 @@ _DDL = """CREATE TABLE IF NOT EXISTS report_sections ( )""" -def _norm_heading(text: str) -> str: - """Whitespace- and marker-insensitive form for heading comparison.""" - return re.sub(r"[\s#]+", "", text) +def _normalized_atx_heading(line: str) -> str | None: + """Extract an ATX heading's text, then ignore whitespace for comparison.""" + match = re.fullmatch(r" {0,3}#{1,6}(?=[ \t]|$)([^\r\n]*)", line.removesuffix("\r")) + if match is None: + return None + text = match[1].rstrip(" \t") + before_trailing_hashes = text.rstrip("#") + if before_trailing_hashes.endswith((" ", "\t")): + text = before_trailing_hashes + return "".join(text.split()) def strip_duplicate_title_heading(content: str, title: str) -> str: @@ -39,8 +46,8 @@ def strip_duplicate_title_heading(content: str, title: str) -> str: """ stripped = content.lstrip() first_line, _, rest = stripped.partition("\n") - match = re.match(r"^\s{0,3}#{1,6}\s+(.+?)\s*#*\s*$", first_line) - if match and _norm_heading(match.group(1)) == _norm_heading(title): + opening_title = _normalized_atx_heading(first_line) + if opening_title is not None and opening_title == _normalized_atx_heading(f"## {title.strip()}"): return rest.lstrip("\n") return stripped