From 57da853adaecb1baa80c2fdd804e410313d6ab73 Mon Sep 17 00:00:00 2001 From: Divyansh Kulshrestha Date: Fri, 4 Dec 2026 19:50:23 +0530 Subject: [PATCH 1/7] bug(pipelines): recognise CommonMark fence info strings in the ingest parser Fixes #322 Signed-off-by: Divyansh Kulshrestha --- .../pipelines/utils/canonical_rag_ingest.py | 2 +- tests/test_canonical_fences.py | 69 +++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 tests/test_canonical_fences.py diff --git a/docs-agent-mcp/pipelines/utils/canonical_rag_ingest.py b/docs-agent-mcp/pipelines/utils/canonical_rag_ingest.py index 20f6e98c..af8a6690 100644 --- a/docs-agent-mcp/pipelines/utils/canonical_rag_ingest.py +++ b/docs-agent-mcp/pipelines/utils/canonical_rag_ingest.py @@ -25,7 +25,7 @@ CHARS_PER_TOKEN = 4 HEADING_RE = re.compile(r"^(#{1,6})\s+(.+?)\s*$") -FENCE_START_RE = re.compile(r"^(`{3,}|~{3,})(\w*)\s*$") +FENCE_START_RE = re.compile(r"^(`{3,}|~{3,})[ \t]*([^\s`{|]*)[^`|]*$") GFM_TABLE_ROW_RE = re.compile(r"^\s*\|") ALERT_RE = re.compile( r"\{\{%\s*alert\b[^%]*%\}\}(.*?)\{\{%\s*/alert\s*%\}\}", diff --git a/tests/test_canonical_fences.py b/tests/test_canonical_fences.py new file mode 100644 index 00000000..fe55534c --- /dev/null +++ b/tests/test_canonical_fences.py @@ -0,0 +1,69 @@ +"""Tests for fenced code block detection in the canonical ingest parser (#322).""" + +import sys +from pathlib import Path + +import pytest + +ROOT = Path(__file__).resolve().parents[1] / "docs-agent-mcp" +for path in (ROOT / "pipelines", ROOT / "pipelines" / "utils"): + if str(path) not in sys.path: + sys.path.insert(0, str(path)) + +import canonical_rag_ingest # noqa: E402 + + +def blocks(text): + """Parse text and return (block_type, content) pairs in document order.""" + doc = canonical_rag_ingest.parse_canonical_document(text) + return [(b.block_type, b.content) for s in doc.sections for b in s.blocks] + + +@pytest.mark.parametrize( + "opening_fence", + [ + "```python", # baseline: already recognised + "``` python", # space before the language + "```shell-session", # hyphen in the language + "```c++", # non-word characters in the language + "```yaml {linenos=true}", # Hugo-style fence attributes + ], +) +def test_opening_fence_variants_are_parsed_as_code(opening_fence): + text = f"Intro:\n\n{opening_fence}\ndef f():\n return 1\n```\n\nAfter.\n" + + result = blocks(text) + + assert [kind for kind, _ in result] == ["prose", "code_fence", "prose"] + assert " return 1" in result[1][1] # indentation preserved + assert result[2][1] == "After." + + +def test_unrecognised_fence_does_not_invert_the_rest_of_the_page(): + text = "Intro:\n\n``` python\ndef f():\n return 1\n```\n\nAfter.\n\n```bash\nls\n```\n" + + result = blocks(text) + + assert [kind for kind, _ in result] == ["prose", "code_fence", "prose", "code_fence"] + assert result[2][1] == "After." + assert "ls" in result[3][1] + + +def test_comment_lines_inside_code_are_not_headings(): + text = "# Title\n\n``` python\n# a comment, not a heading\nx = 1\n```\n" + + doc = canonical_rag_ingest.parse_canonical_document(text) + + headings = [s.heading for s in doc.sections] + assert "a comment, not a heading" not in headings + + +@pytest.mark.parametrize( + "line", + [ + "```inline code```", # backtick info strings cannot contain backticks + "``` |", # closing fence inside a converted HTML table row + ], +) +def test_non_fence_lines_are_not_openers(line): + assert canonical_rag_ingest.FENCE_START_RE.match(line) is None From f23764bce696cbd1cd8e856760fed50136f0ddf2 Mon Sep 17 00:00:00 2001 From: Divyansh Kulshrestha Date: Fri, 4 Dec 2026 20:08:00 +0530 Subject: [PATCH 2/7] bug(pipelines): document the fence opener pattern Signed-off-by: Divyansh Kulshrestha --- docs-agent-mcp/pipelines/utils/canonical_rag_ingest.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs-agent-mcp/pipelines/utils/canonical_rag_ingest.py b/docs-agent-mcp/pipelines/utils/canonical_rag_ingest.py index af8a6690..555b8df0 100644 --- a/docs-agent-mcp/pipelines/utils/canonical_rag_ingest.py +++ b/docs-agent-mcp/pipelines/utils/canonical_rag_ingest.py @@ -25,6 +25,11 @@ CHARS_PER_TOKEN = 4 HEADING_RE = re.compile(r"^(#{1,6})\s+(.+?)\s*$") +# CommonMark fence opener: 3+ backticks or tildes, optional spaces, then an +# info string. The first word of the info string is the language; the rest +# (e.g. Hugo attributes like {linenos=true}) is ignored. Backtick info strings +# may not contain backticks, so inline ```code``` lines are not fences. A `|` +# marks a table cell (fences inside converted HTML tables), not an opener. FENCE_START_RE = re.compile(r"^(`{3,}|~{3,})[ \t]*([^\s`{|]*)[^`|]*$") GFM_TABLE_ROW_RE = re.compile(r"^\s*\|") ALERT_RE = re.compile( From 7a8a6ef0a422bdf68d6670803ccf506c162c0d80 Mon Sep 17 00:00:00 2001 From: Divyansh Kulshrestha Date: Fri, 4 Dec 2026 20:31:49 +0530 Subject: [PATCH 3/7] bug(pipelines): allow backticks in tilde fence info strings Signed-off-by: Divyansh Kulshrestha --- docs-agent-mcp/pipelines/utils/canonical_rag_ingest.py | 2 +- tests/test_canonical_fences.py | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/docs-agent-mcp/pipelines/utils/canonical_rag_ingest.py b/docs-agent-mcp/pipelines/utils/canonical_rag_ingest.py index 555b8df0..b3c9d5ee 100644 --- a/docs-agent-mcp/pipelines/utils/canonical_rag_ingest.py +++ b/docs-agent-mcp/pipelines/utils/canonical_rag_ingest.py @@ -30,7 +30,7 @@ # (e.g. Hugo attributes like {linenos=true}) is ignored. Backtick info strings # may not contain backticks, so inline ```code``` lines are not fences. A `|` # marks a table cell (fences inside converted HTML tables), not an opener. -FENCE_START_RE = re.compile(r"^(`{3,}|~{3,})[ \t]*([^\s`{|]*)[^`|]*$") +FENCE_START_RE = re.compile(r"^(?!`+[^`]+`)(`{3,}|~{3,})[ \t]*([^\s{|]*)[^|]*$") GFM_TABLE_ROW_RE = re.compile(r"^\s*\|") ALERT_RE = re.compile( r"\{\{%\s*alert\b[^%]*%\}\}(.*?)\{\{%\s*/alert\s*%\}\}", diff --git a/tests/test_canonical_fences.py b/tests/test_canonical_fences.py index fe55534c..7129f7b0 100644 --- a/tests/test_canonical_fences.py +++ b/tests/test_canonical_fences.py @@ -58,6 +58,12 @@ def test_comment_lines_inside_code_are_not_headings(): assert "a comment, not a heading" not in headings +def test_tilde_fence_info_string_may_contain_backticks(): + match = canonical_rag_ingest.FENCE_START_RE.match("~~~sh title=`example`") + assert match is not None + assert match.group(2) == "sh" + + @pytest.mark.parametrize( "line", [ From 471825e51091b78c067e59c33c44879298c0baa6 Mon Sep 17 00:00:00 2001 From: Divyansh Kulshrestha Date: Fri, 4 Dec 2026 21:16:52 +0530 Subject: [PATCH 4/7] bug(pipelines): close fences only on a line of fence characters Signed-off-by: Divyansh Kulshrestha --- .../pipelines/utils/canonical_rag_ingest.py | 3 ++- tests/test_canonical_fences.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/docs-agent-mcp/pipelines/utils/canonical_rag_ingest.py b/docs-agent-mcp/pipelines/utils/canonical_rag_ingest.py index b3c9d5ee..cede88ad 100644 --- a/docs-agent-mcp/pipelines/utils/canonical_rag_ingest.py +++ b/docs-agent-mcp/pipelines/utils/canonical_rag_ingest.py @@ -323,7 +323,8 @@ def parse_canonical_document(content: str) -> CanonicalDocument: closed = False while idx < len(lines): block_lines.append(lines[idx]) - if lines[idx].strip().startswith(fence_char * fence_len): + closing = lines[idx].strip() + if len(closing) >= fence_len and set(closing) == {fence_char}: closed = True idx += 1 break diff --git a/tests/test_canonical_fences.py b/tests/test_canonical_fences.py index 7129f7b0..74c5a244 100644 --- a/tests/test_canonical_fences.py +++ b/tests/test_canonical_fences.py @@ -73,3 +73,17 @@ def test_tilde_fence_info_string_may_contain_backticks(): ) def test_non_fence_lines_are_not_openers(line): assert canonical_rag_ingest.FENCE_START_RE.match(line) is None + + +@pytest.mark.parametrize( + "text", + [ + "````markdown\n```bash\nls\n```\n````\n\nAfter.\n", # shorter fence inside a longer one + "```python\nx = 1\n```bash\ny = 2\n```\n\nAfter.\n", # info string on a would-be closer + ], +) +def test_closing_fence_contains_only_fence_characters(text): + result = blocks(text) + + assert [kind for kind, _ in result] == ["code_fence", "prose"] + assert result[1][1] == "After." From f1149bfd65a09394717b6a52dd86c5ce00fba84d Mon Sep 17 00:00:00 2001 From: Divyansh Kulshrestha Date: Fri, 4 Dec 2026 21:52:51 +0530 Subject: [PATCH 5/7] bug(pipelines): only treat a pipe right after the fence as a table cell Signed-off-by: Divyansh Kulshrestha --- .../pipelines/utils/canonical_rag_ingest.py | 5 +++-- tests/test_canonical_fences.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/docs-agent-mcp/pipelines/utils/canonical_rag_ingest.py b/docs-agent-mcp/pipelines/utils/canonical_rag_ingest.py index cede88ad..07f9503d 100644 --- a/docs-agent-mcp/pipelines/utils/canonical_rag_ingest.py +++ b/docs-agent-mcp/pipelines/utils/canonical_rag_ingest.py @@ -29,8 +29,9 @@ # info string. The first word of the info string is the language; the rest # (e.g. Hugo attributes like {linenos=true}) is ignored. Backtick info strings # may not contain backticks, so inline ```code``` lines are not fences. A `|` -# marks a table cell (fences inside converted HTML tables), not an opener. -FENCE_START_RE = re.compile(r"^(?!`+[^`]+`)(`{3,}|~{3,})[ \t]*([^\s{|]*)[^|]*$") +# right after the fence marks a table cell (fences inside converted HTML +# tables), not an opener; pipes later in the info string are allowed. +FENCE_START_RE = re.compile(r"^(?!`+[^`]+`)(`{3,}|~{3,})(?![ \t]*\|)[ \t]*([^\s{]*).*$") GFM_TABLE_ROW_RE = re.compile(r"^\s*\|") ALERT_RE = re.compile( r"\{\{%\s*alert\b[^%]*%\}\}(.*?)\{\{%\s*/alert\s*%\}\}", diff --git a/tests/test_canonical_fences.py b/tests/test_canonical_fences.py index 74c5a244..f1bd9c14 100644 --- a/tests/test_canonical_fences.py +++ b/tests/test_canonical_fences.py @@ -58,6 +58,19 @@ def test_comment_lines_inside_code_are_not_headings(): assert "a comment, not a heading" not in headings +@pytest.mark.parametrize( + ("line", "language"), + [ + ("~~~sh title=a|b", "sh"), + ("```js title=a|b", "js"), + ], +) +def test_pipes_later_in_the_info_string_are_allowed(line, language): + match = canonical_rag_ingest.FENCE_START_RE.match(line) + assert match is not None + assert match.group(2) == language + + def test_tilde_fence_info_string_may_contain_backticks(): match = canonical_rag_ingest.FENCE_START_RE.match("~~~sh title=`example`") assert match is not None @@ -69,6 +82,7 @@ def test_tilde_fence_info_string_may_contain_backticks(): [ "```inline code```", # backtick info strings cannot contain backticks "``` |", # closing fence inside a converted HTML table row + "~~~ |", ], ) def test_non_fence_lines_are_not_openers(line): From e90cb6330c9849581396f04f0d5b7534462bfc50 Mon Sep 17 00:00:00 2001 From: Divyansh Kulshrestha Date: Fri, 4 Dec 2026 21:56:56 +0530 Subject: [PATCH 6/7] bug(pipelines): document the closing-fence rule and cover other-character closers Signed-off-by: Divyansh Kulshrestha --- docs-agent-mcp/pipelines/utils/canonical_rag_ingest.py | 3 +++ tests/test_canonical_fences.py | 2 ++ 2 files changed, 5 insertions(+) diff --git a/docs-agent-mcp/pipelines/utils/canonical_rag_ingest.py b/docs-agent-mcp/pipelines/utils/canonical_rag_ingest.py index 07f9503d..278373e0 100644 --- a/docs-agent-mcp/pipelines/utils/canonical_rag_ingest.py +++ b/docs-agent-mcp/pipelines/utils/canonical_rag_ingest.py @@ -324,6 +324,9 @@ def parse_canonical_document(content: str) -> CanonicalDocument: closed = False while idx < len(lines): block_lines.append(lines[idx]) + # CommonMark closer: only the opening fence character, at least + # as many as the opener (so a ```lang line inside a block, or a + # ~~~ line inside a ``` block, does not end it). closing = lines[idx].strip() if len(closing) >= fence_len and set(closing) == {fence_char}: closed = True diff --git a/tests/test_canonical_fences.py b/tests/test_canonical_fences.py index f1bd9c14..9f51470d 100644 --- a/tests/test_canonical_fences.py +++ b/tests/test_canonical_fences.py @@ -94,6 +94,8 @@ def test_non_fence_lines_are_not_openers(line): [ "````markdown\n```bash\nls\n```\n````\n\nAfter.\n", # shorter fence inside a longer one "```python\nx = 1\n```bash\ny = 2\n```\n\nAfter.\n", # info string on a would-be closer + "```text\n~~~\nstill code\n```\n\nAfter.\n", # other fence character does not close + "```text\ncode\n``` \n\nAfter.\n", # trailing spaces after the closer are allowed ], ) def test_closing_fence_contains_only_fence_characters(text): From 034864b5721cb9b2abac312bed5d8a7cd3f85145 Mon Sep 17 00:00:00 2001 From: Divyansh Kulshrestha Date: Fri, 4 Dec 2026 22:04:37 +0530 Subject: [PATCH 7/7] bug(pipelines): keep the whole fence marker in the opener match Signed-off-by: Divyansh Kulshrestha --- .../pipelines/utils/canonical_rag_ingest.py | 6 ++++-- tests/test_canonical_fences.py | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/docs-agent-mcp/pipelines/utils/canonical_rag_ingest.py b/docs-agent-mcp/pipelines/utils/canonical_rag_ingest.py index 278373e0..e8fd0be4 100644 --- a/docs-agent-mcp/pipelines/utils/canonical_rag_ingest.py +++ b/docs-agent-mcp/pipelines/utils/canonical_rag_ingest.py @@ -30,8 +30,10 @@ # (e.g. Hugo attributes like {linenos=true}) is ignored. Backtick info strings # may not contain backticks, so inline ```code``` lines are not fences. A `|` # right after the fence marks a table cell (fences inside converted HTML -# tables), not an opener; pipes later in the info string are allowed. -FENCE_START_RE = re.compile(r"^(?!`+[^`]+`)(`{3,}|~{3,})(?![ \t]*\|)[ \t]*([^\s{]*).*$") +# tables), not an opener; pipes later in the info string are allowed. The +# marker group must take the whole run of fence characters, so `~~~~ |` cannot +# match as a shorter `~~~` fence with language `~`. +FENCE_START_RE = re.compile(r"^(?!`+[^`]+`)(`{3,}(?!`)|~{3,}(?!~))(?![ \t]*\|)[ \t]*([^\s{]*).*$") GFM_TABLE_ROW_RE = re.compile(r"^\s*\|") ALERT_RE = re.compile( r"\{\{%\s*alert\b[^%]*%\}\}(.*?)\{\{%\s*/alert\s*%\}\}", diff --git a/tests/test_canonical_fences.py b/tests/test_canonical_fences.py index 9f51470d..9d11ea68 100644 --- a/tests/test_canonical_fences.py +++ b/tests/test_canonical_fences.py @@ -71,6 +71,21 @@ def test_pipes_later_in_the_info_string_are_allowed(line, language): assert match.group(2) == language +@pytest.mark.parametrize( + ("line", "fence", "language"), + [ + ("````python", "````", "python"), + ("~~~~sh title=a|b", "~~~~", "sh"), + ("~~~~~", "~~~~~", ""), + ], +) +def test_long_fence_markers_are_captured_whole(line, fence, language): + match = canonical_rag_ingest.FENCE_START_RE.match(line) + assert match is not None + assert match.group(1) == fence + assert match.group(2) == language + + def test_tilde_fence_info_string_may_contain_backticks(): match = canonical_rag_ingest.FENCE_START_RE.match("~~~sh title=`example`") assert match is not None @@ -83,6 +98,9 @@ def test_tilde_fence_info_string_may_contain_backticks(): "```inline code```", # backtick info strings cannot contain backticks "``` |", # closing fence inside a converted HTML table row "~~~ |", + "~~~~ |", # the marker must not shrink to let a fence char act as language + "```` |", + "~~~~|", ], ) def test_non_fence_lines_are_not_openers(line):