The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(telegram): widen fence anchors to keep mid-line and indented blocks
Brooklyn Nicholsonc82abc309a3f
diff --git a/plugins/platforms/telegram/adapter.py b/plugins/platforms/telegram/adapter.pyindex 3ca7ed643bd..8e726ac6828 100644--- a/plugins/platforms/telegram/adapter.py+++ b/plugins/platforms/telegram/adapter.py@@ -5723,22 +5723,30 @@ class TelegramAdapter(BasePlatformAdapter):# 1) Protect fenced code blocks (``` ... ```)# Per MarkdownV2 spec, \ and ` inside pre/code must be escaped.- # The fence must open and close on its own line (0-3 spaces of- # indent allowed). Anchoring to line starts keeps *inline* triple- # backticks (e.g. "the syntax is ```x``` inline") from being- # swallowed and mangled into broken MarkdownV2 <pre> entities.- # The trailing ``\r?`` on the close lets CRLF-terminated fences- # (Windows-authored content) match too, so they aren't left- # unprotected with their backticks escaped into literals.+ # A fence still opens on its own line — the opening ``` must be the+ # first triple-backtick run on that line and must end it — but the+ # line may carry arbitrary leading whitespace (list/blockquote-+ # nested code indents fences by 4+ spaces) or lead-in prose+ # ("Here is the code: ```"), both of which the line-start-only+ # anchor silently downgraded from <pre> to escaped literal text.+ # Requiring the rest of the opening line to be backtick-free is+ # what keeps *inline* triple backticks (e.g. "the syntax is+ # ```x``` inline") out of the match: a closing run can never sit+ # on the same line as the opener, and the tempered prefix cannot+ # skip past an earlier run to a later one. The closing fence must+ # sit on its own line (any indent), with optional trailing+ # whitespace and an optional ``\r`` so CRLF-terminated fences+ # (Windows-authored content) match too.def _protect_fenced(m):- opening = m.group(1) # opening fence line, incl. trailing newline- body = m.group(2) # code body (may be empty)- closing = m.group(3) # closing fence (with its indent)+ prefix = m.group(1) # lead-in text / indent before the opening fence+ opening = m.group(2) # opening ``` (with optional language) and newline+ body = m.group(3) # code body (may be empty)+ closing = m.group(4) # closing fence (with its indent)body = body.replace('\\', '\\\\').replace('`', '\\`')- return _ph(opening + body + closing)+ return prefix + _ph(opening + body + closing)text = re.sub(- r'(?m)^([ ]{0,3}`{3}[^\n]*\n)([\s\S]*?)(^[ ]{0,3}`{3})[ \t]*\r?$',+ r'(?m)^((?:(?!```)[^\n])*)(```[^`\n]*\n)([\s\S]*?)(^[ \t]*```)[ \t]*\r?$',_protect_fenced,text,)diff --git a/tests/conformance/vectors/telegram.json b/tests/conformance/vectors/telegram.jsonindex 34597de3f57..0acc5a1a0aa 100644--- a/tests/conformance/vectors/telegram.json+++ b/tests/conformance/vectors/telegram.json@@ -224,7 +224,7 @@"category": "scar","expect": "semantic","input": "`C:\\Users\\ben\\file.txt` and ```\npath = \"a\\\\b\"\n```",- "native_output": "`C:\\\\Users\\\\ben\\\\file.txt` and \\`\\``\npath = \"a\\\\\\\\b\"\n`\\`\\`"+ "native_output": "`C:\\\\Users\\\\ben\\\\file.txt` and ```\npath = \"a\\\\\\\\b\"\n```"},{"id": "backtick-in-fence",diff --git a/tests/gateway/test_telegram_format.py b/tests/gateway/test_telegram_format.pyindex 9e622ffe7f5..13c4eba7597 100644--- a/tests/gateway/test_telegram_format.py+++ b/tests/gateway/test_telegram_format.py@@ -164,6 +164,45 @@ class TestFormatMessageCodeBlocks:assert "code" in resultassert "\\`" not in result+ def test_midline_opened_fence_stays_protected(self, adapter):+ r"""A fence opened after lead-in prose on the same line ("Here is the+ code: ```") is a real <pre> block on main and must stay one: the+ line-start-only anchor downgraded it to escaped literal prose."""+ text = "Here is the code: ```python\nprint('hi')\n```"+ result = adapter.format_message(text)+ assert "```python\nprint('hi')\n```" in result+ assert "\\`" not in result++ def test_list_nested_indented_fence_stays_protected(self, adapter):+ r"""Fences indented by 4+ spaces (code nested in lists/blockquotes) are+ real <pre> blocks on main and must stay one regardless of indent."""+ text = "- item:\n ```\n code\n ```"+ result = adapter.format_message(text)+ assert " ```\n code\n ```" in result+ assert "\\`" not in result++ def test_midline_fence_after_inline_code_lead_in(self, adapter):+ r"""An inline code span in the lead-in must not stop the fence opened+ later on the same line from being protected."""+ text = "use `foo` then: ```python\nx\n```"+ result = adapter.format_message(text)+ assert "```python\nx\n```" in result+ assert "`foo`" in result+ assert "\\`" not in result++ def test_inline_pair_then_real_fence_both_handled(self, adapter):+ r"""An inline triple-backtick pair on one line must not swallow a real+ line-start fence that follows it (the pre-anchored regex over-matched+ from the inline pair; the old unanchored regex matched from it too)."""+ text = "the syntax is ```like this``` inline\n```python\nx\n```"+ result = adapter.format_message(text)+ # The real fence is protected verbatim...+ assert "```python\nx\n```" in result+ # ...the inline pair is escaped as literal text (no raw ``` for it)...+ assert "like this" in result+ # ...and exactly the one real block's two fences remain.+ assert result.count("```") == 2+@pytest.mark.asyncioasync def test_final_send_does_not_retrigger_typing(adapter):