Skip to content

feat(gateway): stream Telegram edits safely — salvage of #22264#22518

Merged
kshitijk4poor merged 2 commits into
mainfrom
salvage/telegram-streaming-plain-text
May 9, 2026
Merged

feat(gateway): stream Telegram edits safely — salvage of #22264#22518
kshitijk4poor merged 2 commits into
mainfrom
salvage/telegram-streaming-plain-text

Conversation

@kshitijk4poor

Copy link
Copy Markdown
Collaborator

Summary

Salvage of #22264 by @nik1t7n — cherry-picked onto current main with authorship preserved, plus AUTHOR_MAP entry.

What this PR does

Makes non-final Telegram streaming edits (edit_message) send content as plain text instead of going through format_message() + MarkdownV2. During token-by-token streaming, partial markdown (e.g., **bold without closing **) frequently triggers Telegram's MarkdownV2 parser rejection. The old code would catch the rejection, then retry as plain text — doubling API calls per intermediate edit with zero benefit.

The fix: intermediate edits (finalize=False) bypass format_message() and parse_mode=MarkdownV2, sending content directly as plain text. Final edits (finalize=True) still go through the full MarkdownV2 + fallback-to-plain path, preserving rich formatting for the completed message.

How this was verified

  • finalize=False IS passed by the streaming consumer (stream_consumer.py:926-930) — the PR code path is actively exercised, not dead code.
  • format_message() does only MarkdownV2 conversion + character escaping — no sanitization, truncation, or routing logic is skipped.
  • The plain-text path is inside the same outer try/except as the old path, so message_too_long, flood control, and generic errors are handled identically.
  • Side-effect: send_progress_messages in gateway/run.py:13472 and _try_strip_cursor/_send_fallback_final in stream_consumer.py also take the plain-text path — all correct since they send status lines / cursor-stripped already-rendered text.

Diff

  • nik1t7n's commit (cherry-picked, authorship preserved): gateway/platforms/telegram.py +8, tests/gateway/test_telegram_format.py +41, cli-config.yaml.example +1
  • Our AUTHOR_MAP commit: adds 20nik.nosov21@gmail.com and 90299797+nik1t7n@users.noreply.github.comnik1t7n

Test plan

  • bash scripts/run_tests.sh tests/gateway/test_telegram_format.py → 90 passed
  • Full telegram regression: 12 test files, 301 passed, 0 failed
  • Ruff diff vs origin/main: All checks passed

Closes

Closes #22264.


Credit to @nik1t7n (Nikita Nosov) for the fix.

nik1t7n and others added 2 commits May 9, 2026 16:57
Nikita Nosov (nik1t7n, PR #22264) — first-time contributor email
and noreply alias.
@kshitijk4poor kshitijk4poor merged commit f6d45e5 into main May 9, 2026
14 of 15 checks passed
@kshitijk4poor kshitijk4poor deleted the salvage/telegram-streaming-plain-text branch May 9, 2026 11:34
@github-actions

github-actions Bot commented May 9, 2026

Copy link
Copy Markdown
Contributor

🔎 Lint report: salvage/telegram-streaming-plain-text vs origin/main

ruff

Total: 0 on HEAD, 0 on base (➖ 0)

🆕 New issues: none

✅ Fixed issues: none

Unchanged: 0 pre-existing issues carried over.

ty (type checker)

Total: 7887 on HEAD, 7887 on base (➖ 0)

🆕 New issues (3):

Rule Count
invalid-argument-type 3
First entries
run_agent.py:6822: [invalid-argument-type] invalid-argument-type: Argument to function `build_anthropic_client` is incorrect: Expected `str`, found `str | dict[Unknown, Unknown] | Any | ... omitted 3 union elements`
run_agent.py:12818: [invalid-argument-type] invalid-argument-type: Argument to function `_is_oauth_token` is incorrect: Expected `str`, found `str | dict[Unknown, Unknown] | Any | ... omitted 3 union elements`
run_agent.py:12821: [invalid-argument-type] invalid-argument-type: Argument to function `len` is incorrect: Expected `Sized`, found `(str & ~AlwaysFalsy) | (dict[Unknown, Unknown] & ~AlwaysFalsy) | (Any & ~AlwaysFalsy) | ... omitted 3 union elements`

✅ Fixed issues (3):

Rule Count
invalid-argument-type 3
First entries
run_agent.py:6822: [invalid-argument-type] invalid-argument-type: Argument to function `build_anthropic_client` is incorrect: Expected `str`, found `str | dict[Unknown | str, Unknown | str | dict[str, str]] | Any | ... omitted 3 union elements`
run_agent.py:12821: [invalid-argument-type] invalid-argument-type: Argument to function `len` is incorrect: Expected `Sized`, found `(str & ~AlwaysFalsy) | (dict[Unknown | str, Unknown | str | dict[str, str]] & ~AlwaysFalsy) | (Any & ~AlwaysFalsy) | ... omitted 3 union elements`
run_agent.py:12818: [invalid-argument-type] invalid-argument-type: Argument to function `_is_oauth_token` is incorrect: Expected `str`, found `str | dict[Unknown | str, Unknown | str | dict[str, str]] | Any | ... omitted 3 union elements`

Unchanged: 4171 pre-existing issues carried over.

Diagnostics are surfaced as warnings — this check never fails the build.

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.

2 participants