Skip to content

fix(telegram): preserve long replies and bound chunk retries - #3747

Merged
vybe merged 3 commits into
Abilityai:devfrom
alex-furman:fix/3746-telegram-long-replies
Oct 19, 2026
Merged

vybe merged 3 commits into
Abilityai:devfrom
alex-furman:fix/3746-telegram-long-replies

Conversation

@alex-furman

@alex-furman alex-furman commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #3746.

The shared router extracts recognized fenced blocks into files and replaces their contents with (see attached: response_1.txt). Telegram ignores those files and sends only the reference. A 6,000-character fenced reply reproduces the loss on unmodified official dev 77d98132b55594cd47dcb31334104581ac54f0fa.

Telegram now keeps reply code blocks inline and splits them in order. Slack and WhatsApp retain their existing file extraction. The splitter preserves HTML entities and open formatting tags, uses a conservative UTF-16 budget including markup, and drops pathological formatting when necessary to preserve text. No document-upload subsystem is added.

Each chunk requires an acknowledged message ID. A single rate-limit retry honors waits of 0–30 seconds; longer or malformed waits stop delivery without retrying early. HTML parse failures fall back to plain text, which can still use that chunk's one rate-limit retry. An unconfirmed chunk stops both ordinary replies and proactive sends; proactive delivery reports failure instead of treating a later successful chunk as complete delivery. Safe API reasons remain in logs with token and URL removal; request exceptions containing URLs are not logged.

Validation against official dev c0ba81e16:

  • 132 affected reply/formatting/proactive/history/completion-report tests pass, plus 33 existing effect-guard, rate-limit and enablement tests.
  • Six regressions reproduced excessive flood waits, proactive suffix delivery/false success, and lost diagnostics before the fixes. Boundary tests cover 0, 30, 31, 45 and 86,400-second waits; mock HTTP checks cover unconfirmed and timed-out middle chunks.
  • Independent full review found no actionable finding; an additional 250 deterministic splitter cases preserve visible content and stay within the UTF-16 budget.
  • Existing stock-before router evidence demonstrates the dangling reference; the patched router preserves the full inline content. Other-channel extraction remains covered.
  • Mock transport does not establish live Telegram entity acceptance, real flood waits or end-to-end delivery. No deployment is included.

Confirmed chunks are not replayed within one Telegram channel attempt. This does not add durable per-chunk deduplication: a separate retry or crash recovery retains the existing effect-level behavior and can repeat earlier chunks. Automatic proactive channel selection retains its existing fallback: after a partial Telegram failure, it can send the full message via Slack or web within the same call; only the successful channel is persisted in outbound history. A new caller-level regression verifies this policy. Fresh independent review found no new blocking source defect; this limitation is explicit rather than an exactly-once claim. Full upstream CI for the new revision remains pending.

Journey Impact: none: Telegram long-reply delivery is not represented in the current journey catalog.

Mutation: the stock router emits only the attachment reference for the 6,000-character fenced reply; the patched router delivers its full content. New bounded-wait and proactive partial-delivery regressions fail on the preceding PR head and pass after correction.

@vybe

vybe commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

merge-train: held back from today's train. Thanks for this; the fix is sound, the splitter also fixes an existing hang on nested markup, and 31 of the 51 new tests fail on dev, so they really catch the bug. Two points before it rides:

  • The rate-limit wait is now uncapped. _send_message (around line 1133) and line 403 sleep max(0, retry_after) where dev used min(retry_after, 30), and the test pins a 45-second sleep. _send_message is shared with proactive messages, completion reports and webhook command replies. A long Telegram flood wait can outlast the caller's timeout, and a caller that retries while the backend still waits sends the message twice. That is a recurring duplicate-message class in this repo. Please restore the 30-second cap and treat a longer retry_after as a failed send.
  • The sendDocument path is unreachable in production. The router now skips file extraction for Telegram, and it is the only place that builds a ChannelResponse with files. So the upload-first, fallback and duplicate-filename code in send_response/_send_document, about 50 lines, plus its docs only ever run in tests. Either drop it, or keep extraction and actually upload.

Minor: log body.get("description") alongside the status. It keeps diagnostics without exposing the token.

@vybe vybe added the status-needs-fix PR has an unaddressed review/validation finding; cleared by the author's next push (#2815) label Oct 17, 2026
@alex-furman alex-furman changed the title fix(telegram): preserve long replies and confirm attachments fix(telegram): preserve long replies and bound chunk retries Oct 18, 2026
@alex-furman

Copy link
Copy Markdown
Contributor Author

@vybe Addressed in 6055cfdb9, with a caller-level regression/clarification in e1957b7d4:

  • One rate-limit retry honors waits of 0–30 seconds; longer or malformed waits fail without retrying early.
  • Removed the unreachable document-upload/fallback subsystem and its tests/docs. Code blocks stay inline.
  • Restored useful API error descriptions with token/URL removal.
  • Both Telegram reply loops stop on an unconfirmed chunk; proactive Telegram delivery no longer reports success after a missing middle chunk.

165 affected checks passed, plus a new full-caller regression for existing automatic channel fallback. Independent review found no remaining blocking source finding. The docs now explicitly limit no-replay to the Telegram channel attempt: auto mode can resend the complete message through another channel after a partial Telegram send. Live Telegram acceptance remains unverified.

Please re-review the updated head when convenient.

@obasilakis obasilakis 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.

Review + validate-pr: approve. No blockers. CI green, no conflicts against current dev.

The three points from the 2026-10-17 review are fixed: the 30s wait cap is restored and longer or malformed waits fail without retrying, the dead sendDocument path is gone, and the API description is logged with the token and URLs redacted.

Execution coverage. Each source file reverted on its own, then tests/unit/test_3746_telegram_long_replies.py re-run:

  • message_router.py reverted: 7 tests fail.
  • telegram_adapter.py reverted: 17+ tests fail, then test_large_nested_markup_falls_back_without_looping hangs in the old _split_message. Reopening 1500 <b> tags makes each remainder longer than the chunk it removed, which is the dev bug this PR fixes. That test has no timeout, so a regression shows up as a hung CI job. A pytest.mark.timeout (or running the split in a thread with a deadline) would make it fail fast.
  • proactive_message_service.py reverted: 3 tests fail.

249 related Telegram, proactive, completion-report and router tests pass.

Non-blocking, all in src/backend/adapters/telegram_adapter.py

  1. _send_message, bare except Exception:: the log line is fixed text and drops the exception, so a KeyError, a non-JSON 5xx body and a timeout all look the same. Log type(e).__name__; that keeps the token out and says what failed.
  2. _send_message: body = resp.json() runs before the status check, so a non-JSON error page (a proxy's HTML 502) throws before the status is logged. Parse in a try with a {} fallback, and log resp.status_code.
  3. _split_message, "cut landed inside a tag" fallback split_at = limit: limit counts UTF-16 units and is used here as a character index, so emoji-heavy text can overshoot. The units(chunk) > MAX check catches it and falls back to plain text, so nothing is lost. Reuse the UTF-16-sliced split_at computed above.

Info
4. Proactive auto mode re-sends the whole message through Slack or web after a partial Telegram failure. The docs and PR body say so.

@obasilakis obasilakis removed the status-needs-fix PR has an unaddressed review/validation finding; cleared by the author's next push (#2815) label Oct 19, 2026
@vybe
vybe merged commit 2db0e88 into Abilityai:dev Oct 19, 2026
26 checks passed
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.

3 participants