Skip to content

fix(telegram): always apply MarkdownV2 formatting on edit_message - #42423

Closed
ToBAIaeS wants to merge 1 commit into
NousResearch:mainfrom
ToBAIaeS:fix/telegram-edit-message-markdownv2
Closed

fix(telegram): always apply MarkdownV2 formatting on edit_message#42423
ToBAIaeS wants to merge 1 commit into
NousResearch:mainfrom
ToBAIaeS:fix/telegram-edit-message-markdownv2

Conversation

@ToBAIaeS

@ToBAIaeS ToBAIaeS commented Jun 8, 2026

Copy link
Copy Markdown

Update

Bug 1 (edit_message(finalize=False) skips MarkdownV2) is now fixed by #42421 (merged). This PR now focuses on the two remaining bugs that weren't addressed:

Bug 2: Incomplete escaping inside fenced code blocks and inline code (still present)

format_message() step 1 (_protect_fenced) and step 2 (inline code) only escaped \ and ` inside code content. Per the Telegram MarkdownV2 spec, all 12 special characters must be escaped inside <pre>/<code> too:

In all other places characters must be escaped inside pre and code.

Unescaped # in shell comments, ! in output, - in flags, etc. caused Can't parse entities: character '#' is reserved errors from the Telegram API. The fallback (plain text, previously unlogged) then replaced the formatted message with raw text.

Fix: _protect_fenced() and _protect_inline() now use _escape_mdv2() to escape all 12 MarkdownV2 special characters inside code blocks and inline code.

Bug 3: Triple-backticks in terminal commands break code-block detection (still present)

When a terminal command contains ``` sequences (e.g. gh pr edit --body with code examples), the non-greedy regex ([\s\S]*?) in format_message() matches the inner ``` as a closing fence, splitting the outer bash code block into multiple fragments. Content between the fragments (### Bug 2: ...) is left unprotected and rendered as raw markdown.

Fix: run.py _bash_block construction now escapes inner ``` sequences before wrapping in the outer fenced block.

Testing

Verified on a live Hermes gateway (3 profiles, Telegram DM):

  • Multiple sequential tool calls with # comments → renders correctly ✅
  • Code blocks with #, !, -, +, . → all escaped and preserved ✅
  • Terminal commands containing nested `` sequences → single code block ✅
  • Mixed content (tool output + plain text between calls) ✅

@alt-glitch alt-glitch added type/bug Something isn't working comp/gateway Gateway runner, session dispatch, delivery platform/telegram Telegram bot adapter P2 Medium — degraded but workaround exists labels Jun 8, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Overlaps/competes with #42421 and #42308 — all three make Telegram edit_message/progress edits re-apply MarkdownV2 formatting (regression from raw-text finalize=False branch). Maintainers should pick one.

@ToBAIaeS
ToBAIaeS force-pushed the fix/telegram-edit-message-markdownv2 branch from 4b228a5 to e6e0aba Compare June 8, 2026 22:50
Previously, edit_message(finalize=False) sent content as plain text
without parse_mode=MarkdownV2, while edit_message(finalize=True) and
send() both applied MarkdownV2 formatting. This caused tool progress
edits to replace a correctly formatted message with raw markdown text,
breaking code blocks, headers, bold, and other formatting in the
Telegram chat.

The fix always applies format_message() + parse_mode=MARKDOWN_V2
regardless of the finalize flag, with the same fallback to plain text
on parse errors. Added logging for the fallback case (previously
silent).

Fixes: NousResearch#25710 (related)
@ToBAIaeS
ToBAIaeS force-pushed the fix/telegram-edit-message-markdownv2 branch from e6e0aba to 78c7e22 Compare June 8, 2026 23:05
@ToBAIaeS

ToBAIaeS commented Jun 8, 2026

Copy link
Copy Markdown
Author

Thanks @alt-glitch for the pointer! #42421 addresses Bug 1 (the finalize=False plain-text path) — this PR now focuses on the two remaining issues:

  1. Incomplete MarkdownV2 escaping inside code blocks_protect_fenced() only escaped \ and \ when the spec requires all 12 special chars escaped inside <pre>/<code> too. This causes Can't parse entities: character '#' is reserved errors → plain-text fallback.
  2. Nested ``` in terminal commands — breaks the non-greedy code-block regex, splitting a single bash block into fragments with unprotected markdown between them.

Both are independent of the finalize fix and still present after #42421. Happy to rebase onto main if needed.

@ToBAIaeS

ToBAIaeS commented Jun 8, 2026

Copy link
Copy Markdown
Author

Closing in favor of a new focused PR for the remaining bugs (code-block escaping issues). Bug 1 (edit_message finalize) is already fixed by #42421.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/gateway Gateway runner, session dispatch, delivery P2 Medium — degraded but workaround exists platform/telegram Telegram bot adapter type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants