Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions gateway/platforms/telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -2188,14 +2188,11 @@ async def edit_message(
)

try:
if not finalize:
await self._bot.edit_message_text(
chat_id=int(chat_id),
message_id=int(message_id),
text=content,
)
return SendResult(success=True, message_id=message_id)

# Always apply MarkdownV2 formatting on edit, even for
# intermediate (finalize=False) updates. Without this, a
# progress edit that follows an initial formatted send()
# replaces the MarkdownV2-rendered message with raw plain
# text — breaking code blocks, bold, headers, etc.
formatted = self.format_message(content)
try:
await self._bot.edit_message_text(
Expand All @@ -2209,6 +2206,10 @@ async def edit_message(
if "not modified" in str(fmt_err).lower():
return SendResult(success=True, message_id=message_id)
# Fallback: retry without markdown formatting
logger.warning(
"[%s] MarkdownV2 edit failed, falling back to plain text: %s",
self.name, fmt_err,
)
await self._bot.edit_message_text(
chat_id=int(chat_id),
message_id=int(message_id),
Expand Down Expand Up @@ -4369,15 +4370,16 @@ def _ph(value: str) -> str:
text = _wrap_markdown_tables(text)

# 1) Protect fenced code blocks (``` ... ```)
# Per MarkdownV2 spec, \ and ` inside pre/code must be escaped.
# Per MarkdownV2 spec, these characters must be escaped inside
# pre/code blocks too: \ ` * _ { } [ ] ( ) # + - . ! >
def _protect_fenced(m):
raw = m.group(0)
# Split off opening ``` (with optional language) and closing ```
open_end = raw.index('\n') + 1 if '\n' in raw[3:] else 3
opening = raw[:open_end]
body_and_close = raw[open_end:]
body = body_and_close[:-3]
body = body.replace('\\', '\\\\').replace('`', '\\`')
body = _escape_mdv2(body)
return _ph(opening + body + '```')

text = re.sub(
Expand All @@ -4387,10 +4389,15 @@ def _protect_fenced(m):
)

# 2) Protect inline code (`...`)
# Escape \ inside inline code per MarkdownV2 spec.
# Per MarkdownV2 spec, all special chars must be escaped inside code.
def _protect_inline(m):
inner = m.group(0)[1:-1] # strip outer backticks
escaped = _escape_mdv2(inner)
return _ph(f'`{escaped}`')

text = re.sub(
r'(`[^`]+`)',
lambda m: _ph(m.group(0).replace('\\', '\\\\')),
_protect_inline,
text,
)

Expand Down
5 changes: 4 additions & 1 deletion gateway/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -12990,7 +12990,10 @@ def progress_callback(event_type: str, tool_name: str = None, preview: str = Non
and isinstance(args.get("command"), str)
and args["command"].strip()
):
_bash_block = f"```bash\n{args['command'].rstrip()}\n```"
# Escape triple-backticks inside the command so they don't
# break the fenced code-block boundary that wraps them.
_cmd = args['command'].rstrip().replace('```', '\\`\\`\\`')
_bash_block = f"```bash\n{_cmd}\n```"

# Verbose mode: show detailed arguments, respects tool_preview_length
if progress_mode == "verbose":
Expand Down