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 <willem.jiang@gmail.com>
This commit is contained in:
lihongyuan99 2026-11-26 22:32:28 +08:00 committed by GitHub
parent 5758c1034e
commit af7a6ade13
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 40 additions and 7 deletions

View File

@ -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):

View File

@ -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 `# <title>` 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

View File

@ -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