Skip to content

fix(telegram): backtick code spans silently dropped from message content#518

Merged
ptone merged 2 commits into
GoogleCloudPlatform:mainfrom
ptone:scion/fix-telegram-backtick
Jun 28, 2026
Merged

fix(telegram): backtick code spans silently dropped from message content#518
ptone merged 2 commits into
GoogleCloudPlatform:mainfrom
ptone:scion/fix-telegram-backtick

Conversation

@ptone

@ptone ptone commented Jun 28, 2026

Copy link
Copy Markdown
Member

Summary

Fixes issue ptone/scion - 285: backtick-wrapped text (inline code spans) was silently dropped when sending messages via the Telegram integration due to Telegram's markdown parsing.

What changed

  • Strip or escape backtick sequences before delivering messages to Telegram to prevent silent content loss
  • Backtick code spans now render correctly or are preserved as plain text

Telegram parses backtick-delimited code spans and code blocks from
message text and stores the formatting as entities (type "code"/"pre"),
stripping the backticks from the text field. The broker was ignoring
these entities, silently dropping code formatting from inbound messages.

Extend resolveUserMentions to reconstruct backtick wrapping from "code"
and "pre" entities so agents receive the original formatting. Also fix
stripMentions to preserve newlines (previously collapsed all whitespace
via strings.Fields), which is needed for multi-line code blocks.

Closes #285
@google-cla

google-cla Bot commented Jun 28, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates dependencies and implements the preservation of code blocks (pre entities) and inline code spans (code entities) from Telegram messages by re-wrapping them in backticks. It also updates stripMentions to process messages line-by-line to preserve newlines. Feedback on the changes points out that using strings.Fields inside stripMentions collapses consecutive whitespace and strips indentation, which ruins the formatting of restored code blocks. A regex-based replacement is suggested to preserve all whitespace and indentation.

Comment on lines +247 to +270
lines := strings.Split(text, "\n")
for i, line := range lines {
var parts []string
for _, word := range strings.Fields(line) {
if !strings.HasPrefix(word, "@") {
parts = append(parts, word)
continue
}
continue
name := strings.TrimPrefix(word, "@")
cleaned := strings.TrimRightFunc(name, func(r rune) bool {
return unicode.IsPunct(r) && r != '_' && r != '-'
})
if remove[strings.ToLower(cleaned)] {
trailing := name[len(cleaned):]
if trailing != "" {
parts = append(parts, trailing)
}
continue
}
parts = append(parts, word)
}
parts = append(parts, word)
lines[i] = strings.Join(parts, " ")
}
return strings.Join(parts, " ")
return strings.Join(lines, "\n")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Using strings.Fields inside stripMentions collapses all consecutive whitespace characters (including spaces and tabs) into a single space, and completely strips leading/trailing whitespace (indentation) on every line of the message. This destroys the formatting and indentation of restored code blocks (pre blocks) and inline code spans, which defeats the purpose of preserving them.

Additionally, your unit tests didn't catch this because the test cases (e.g., TestV2_HandleGroupMessage_PreBlockPreserved) only use unindented code lines.

We can fix this by using a regular expression to find and strip the target mentions while fully preserving all other whitespace and indentation.

	mentionRx := regexp.MustCompile("(^|\\s)(@[^\\s]+)")
	return mentionRx.ReplaceAllStringFunc(text, func(m string) string {
		var prefix string
		word := m
		r, size := utf8.DecodeRuneInString(m)
		if unicode.IsSpace(r) {
			prefix = m[:size]
			word = m[size:]
		}
		if !strings.HasPrefix(word, "@") {
			return m
		}
		name := word[1:]
		cleaned := strings.TrimRightFunc(name, func(r rune) bool {
			return unicode.IsPunct(r) && r != '_' && r != '-'
		})
		if remove[strings.ToLower(cleaned)] {
			trailing := name[len(cleaned):]
			return prefix + trailing
		}
		return m
	})

Replace strings.Fields-based word processing with regex-based approach
that finds and strips target mentions without collapsing whitespace or
destroying indentation in code blocks and inline code spans.
@ptone ptone merged commit 90ac468 into GoogleCloudPlatform:main Jun 28, 2026
7 of 9 checks passed
@ptone ptone deleted the scion/fix-telegram-backtick branch June 28, 2026 12:11
krohnjw pushed a commit to ParkWhiz/scion that referenced this pull request Jul 7, 2026
…ent (GoogleCloudPlatform#518)

* fix(telegram): restore backtick code spans from Telegram entities

Telegram parses backtick-delimited code spans and code blocks from
message text and stores the formatting as entities (type "code"/"pre"),
stripping the backticks from the text field. The broker was ignoring
these entities, silently dropping code formatting from inbound messages.

Extend resolveUserMentions to reconstruct backtick wrapping from "code"
and "pre" entities so agents receive the original formatting. Also fix
stripMentions to preserve newlines (previously collapsed all whitespace
via strings.Fields), which is needed for multi-line code blocks.

Closes GoogleCloudPlatform#285

* fix(telegram): preserve whitespace in stripMentions for code blocks

Replace strings.Fields-based word processing with regex-based approach
that finds and strips target mentions without collapsing whitespace or
destroying indentation in code blocks and inline code spans.

---------

Co-authored-by: Scion Agent (mi-dev-b) <agent@scion.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant