diff --git a/nanobot/channels/telegram/runtime.py b/nanobot/channels/telegram/runtime.py index 42ba780bb..b4d249551 100644 --- a/nanobot/channels/telegram/runtime.py +++ b/nanobot/channels/telegram/runtime.py @@ -100,6 +100,22 @@ class _LivenessTrackedRequest(BaseRequest): return result +_FENCE_OPEN_RE = re.compile(r'^[ \t]*(`{3,}|~{3,})(.*)$') +_INLINE_FENCE_RE = re.compile(r'```(?:[^\n]*\n)?([\s\S]*?)```') + + +def _opening_code_fence(line: str) -> str | None: + match = _FENCE_OPEN_RE.match(line) + if match and not (match.group(1)[0] == '`' and '`' in match.group(2)): + return match.group(1) + return None + + +def _closes_code_fence(line: str, fence: str) -> bool: + closing = line.strip() + return len(closing) >= len(fence) and closing == fence[0] * len(closing) + + def _split_telegram_markdown(content: str, max_len: int) -> list[str]: """Split raw Telegram Markdown without leaving fenced code blocks unbalanced.""" if not content: @@ -116,13 +132,33 @@ def _split_telegram_markdown(content: str, max_len: int) -> list[str]: return content[fence_pos:] return content[fence_pos:line_end] - def split_inside_fenced_code_block(pos: int) -> tuple[bool, int, str]: - if content[:pos].count("```") % 2 == 0: - return False, -1, "" - opening = content.rfind("```", 0, pos) - if opening < 0: - return True, -1, "```" - return True, opening, fence_line(opening) + def split_inside_fenced_code_block(pos: int) -> tuple[int, str, str] | None: + fence = None + opening = -1 + inline = False + offset = 0 + for line in content[:pos].split("\n"): + if fence is not None and not inline: + if _closes_code_fence(line, fence): + fence = None + else: + marker = _opening_code_fence(line) if fence is None else None + if marker is not None: + fence = marker + opening = offset + inline = False + else: + # Keep the legacy renderer's inline triple-backtick blocks. + for match in re.finditer("```", line): + if fence is None: + fence = "```" + opening = offset + match.start() + inline = True + else: + fence = None + inline = False + offset += len(line) + 1 + return (opening, fence_line(opening), fence) if fence is not None else None chunks: list[str] = [] while content: @@ -137,12 +173,13 @@ def _split_telegram_markdown(content: str, max_len: int) -> list[str]: if pos <= 0: pos = max_len - inside_code, opening, fence = split_inside_fenced_code_block(pos) - if inside_code: + active_fence = split_inside_fenced_code_block(pos) + if active_fence is not None: + opening, fence, marker = active_fence if opening > 0: pos = opening else: - closing = "\n```" + closing = "\n" + marker min_code_pos = len(fence) if content.startswith(fence + "\n"): min_code_pos += 1 @@ -205,10 +242,6 @@ def _strip_md(s: str) -> str: return s.strip() -_FENCE_OPEN_RE = re.compile(r'^[ \t]*(`{3,}|~{3,})(.*)$') -_INLINE_FENCE_RE = re.compile(r'```(?:[^\n]*\n)?([\s\S]*?)```') - - def _replace_fenced_code(text: str, replace: Callable[[str], str]) -> str: """Replace fenced code blocks with ``replace(code)``. @@ -221,12 +254,10 @@ def _replace_fenced_code(text: str, replace: Callable[[str], str]) -> str: out: list[str] = [] i = 0 while i < len(lines): - match = _FENCE_OPEN_RE.match(lines[i]) - if match and not (match.group(1)[0] == '`' and '`' in match.group(2)): - fence = match.group(1) + fence = _opening_code_fence(lines[i]) + if fence is not None: for j in range(i + 1, len(lines)): - closing = lines[j].strip() - if len(closing) >= len(fence) and closing == fence[0] * len(closing): + if _closes_code_fence(lines[j], fence): out.append(replace(''.join(line + '\n' for line in lines[i + 1:j]))) i = j + 1 break diff --git a/nanobot/channels/telegram/tests/test_telegram_channel.py b/nanobot/channels/telegram/tests/test_telegram_channel.py index 32c400958..4b703695d 100644 --- a/nanobot/channels/telegram/tests/test_telegram_channel.py +++ b/nanobot/channels/telegram/tests/test_telegram_channel.py @@ -207,26 +207,28 @@ def _assert_code_blocks_render_balanced(chunks: list[str]) -> None: assert html.count("
") == html.count("
") -def test_split_telegram_markdown_inside_code_block_moves_before_fence() -> None: - content = "Intro paragraph.\n```python\nprint('a')\nprint('b')\n```\nDone" +@pytest.mark.parametrize("prefix", ["Intro paragraph.\n", "Intro paragraph. "]) +def test_split_telegram_markdown_inside_code_block_moves_before_fence(prefix) -> None: + content = prefix + "```python\nprint('a')\nprint('b')\n```\nDone" chunks = _split_telegram_markdown(content, max_len=35) - assert chunks[0] == "Intro paragraph.\n" + assert chunks[0] == prefix assert chunks[1].startswith("```python\nprint('a')") _assert_code_blocks_render_balanced(chunks) -def test_split_telegram_markdown_long_code_block_closes_and_reopens() -> None: - content = "```python\n" + ("print('line one')\n" * 6) + "```\nDone" +@pytest.mark.parametrize("fence", ["```", "~~~", "````"]) +def test_split_telegram_markdown_long_code_block_closes_and_reopens(fence) -> None: + content = f"{fence}python\n" + ("print('line one')\n" * 6) + f"{fence}\nDone" chunks = _split_telegram_markdown(content, max_len=60) assert len(chunks) > 1 assert all(len(chunk) <= 60 for chunk in chunks) - assert chunks[0].startswith("```python\n") - assert chunks[0].endswith("\n```") - assert chunks[1].startswith("```python\n") + assert chunks[0].startswith(f"{fence}python\n") + assert chunks[0].endswith(f"\n{fence}") + assert chunks[1].startswith(f"{fence}python\n") _assert_code_blocks_render_balanced(chunks) @@ -251,6 +253,20 @@ def test_split_telegram_markdown_multiple_code_blocks() -> None: _assert_code_blocks_render_balanced(chunks) +def test_split_telegram_markdown_keeps_nested_shorter_fence() -> None: + content = "````markdown\n```python\n" + "# raw example\n" * 40 + "```\n````" + + chunks = _split_telegram_markdown(content, max_len=80) + + assert len(chunks) > 1 + assert all(len(chunk) <= 80 for chunk in chunks) + assert all(chunk.startswith("````markdown\n") and chunk.endswith("\n````") for chunk in chunks) + html = "".join(_markdown_to_telegram_html(chunk) for chunk in chunks) + assert html.count("# raw example") == 40 + assert html.count("```") == 2 + assert "" not in html + + def test_split_telegram_markdown_leading_whitespace_before_fence() -> None: content = "\n```python\n" + ("print('line one')\n" * 6) + "```\nDone" @@ -262,25 +278,26 @@ def test_split_telegram_markdown_leading_whitespace_before_fence() -> None: _assert_code_blocks_render_balanced(chunks) -def test_split_telegram_markdown_long_single_line_code_body() -> None: +@pytest.mark.parametrize("fence", ["```", "~~~", "````"]) +def test_split_telegram_markdown_long_single_line_code_body(fence) -> None: """Long fence bodies with no interior newlines must still advance.""" body = "a" * 4500 - content = f"```\n{body}\n```" + content = f"{fence}\n{body}\n{fence}" chunks = _split_telegram_markdown(content, TELEGRAM_MAX_MESSAGE_LEN) assert len(chunks) > 1 assert all(len(chunk) <= TELEGRAM_MAX_MESSAGE_LEN for chunk in chunks) - assert chunks[0].startswith("```\n") - assert chunks[0].endswith("\n```") - assert chunks[1].startswith("```\n") + assert chunks[0].startswith(f"{fence}\n") + assert chunks[0].endswith(f"\n{fence}") + assert chunks[1].startswith(f"{fence}\n") reassembled = [] for chunk in chunks: part = chunk.split("\n", 1)[1] - if part.endswith("\n```"): - part = part[:-4] - elif part.endswith("```"): - part = part[:-3] + if part.endswith(f"\n{fence}"): + part = part[:-(len(fence) + 1)] + elif part.endswith(fence): + part = part[:-len(fence)] reassembled.append(part) assert "".join(reassembled) == body _assert_code_blocks_render_balanced(chunks) @@ -1616,7 +1633,8 @@ async def test_send_delta_stream_end_html_expansion_does_not_overflow() -> None: @pytest.mark.asyncio -async def test_send_delta_stream_end_splits_long_code_block_before_html_rendering() -> None: +@pytest.mark.parametrize("fence", ["```", "~~~", "````"]) +async def test_send_delta_stream_end_splits_long_code_block_before_html_rendering(fence) -> None: """Final streamed replies must not split Telegram HTML inside
."""
     channel = TelegramChannel(
         TelegramConfig(enabled=True, token="123:abc", allow_from=["*"]),
@@ -1626,7 +1644,7 @@ async def test_send_delta_stream_end_splits_long_code_block_before_html_renderin
     channel._app.bot.edit_message_text = AsyncMock()
     channel._app.bot.send_message = AsyncMock(return_value=SimpleNamespace(message_id=99))
 
-    raw_text = "```python\n" + ("print(\"line\")\n" * 450) + "```\nDone"
+    raw_text = f"{fence}python\n" + ("print(\"line\")\n" * 450) + f"{fence}\nDone"
     channel._stream_bufs["123"] = _StreamBuf(text=raw_text, message_id=7, last_edit=0.0)
 
     await channel.send_delta("123", "", stream_end=True)
@@ -1642,6 +1660,9 @@ async def test_send_delta_stream_end_splits_long_code_block_before_html_renderin
     for html in html_chunks:
         assert len(html) <= 4096
         assert html.count("
") == html.count("
") + if 'print("line")' in html: + assert html.startswith("
")
+    assert "".join(html_chunks).count('print("line")') == 450
     assert "123" not in channel._stream_bufs