Commit Graph

6 Commits

Author SHA1 Message Date
Teknium
1af8f7ba23 refactor(telegram): move send-failure helpers out of adapter.py
adapter.py grew 6,705 -> 6,710 lines on this branch; files over the 2,000-line
cap may only shrink. Move the @username "Chat not found" hint (now shared by
the gateway send path and standalone_send) and _classify_send_exception into
telegram_send_errors.py, which already owns send-failure classification.
explain_username_chat_not_found accepts a standalone result dict or a gateway
SendResult, so the adapter calls it directly. adapter.py: 6,710 -> 6,692
(main: 6,707).
2026-11-13 16:42:58 -08:00
Teknium
7e25f692ae fix(telegram): an id-less sendRichMessage is an unknown delivery, not a success
Narrows the salvaged confirmation step to the adapter that produces the shape. A generic
"success without an id = unconfirmed" rule in send_final_ledgered would mark every final
from adapters that never return ids (Signal, SimpleX, webhook, msgraph_webhook, raft) as
failed, and the ledger would redeliver each one as a duplicate.

_try_send_rich now returns delivery_uncertain for a result with no message_id: it is not
resent on the spot (_send_retry_is_final, no legacy fallback), and the ledger row stays
failed, so the timed redelivery sends it once with the may-be-a-duplicate marker.

Co-authored-by: manelsen <manelsen@users.noreply.github.com>
2026-11-12 02:08:38 -08:00
Teknium
62e53838e3 refactor(platforms): the oversized adapters end at main's line count
Greptile P2 "Oversized adapters keep growing": telegram/adapter.py (+5),
discord/adapter.py (+3) and slack/adapter.py (+1) are past the 2,000-line target.
The rich-send never_sent stamp moves to telegram_send_errors.rich_failure_result,
next to maybe_delivered_result, and the Discord/Slack edits are folded, so all
three files end at main's length.
2026-11-12 01:49:28 -08:00
Teknium
e98a7006f8 fix(telegram): a send reset after the request was written is never resent
A connection reset / RemoteProtocolError after sendMessage was written may
already be in the chat, yet the legacy send loop resent it twice and
_send_with_retry retried the whole send again, so one reply could post
several times. send() and the rich path now classify with the shared
telegram_send_errors.send_error_kind: a LOST request (left, no readable
answer) is raised at once and returned delivery_uncertain + not retryable,
which _send_retry_is_final honours (no gateway retry, no plain fallback).
Connect / pool failures (never left) are still retried.

Live (real adapter + PTB, stand-in posts the message then aborts the
connection): 2 visible copies on main, 1 here; connect-refused control is
still retryable.

Fixes #64238
2026-11-09 15:02:49 -08:00
Teknium
367f1f9f98 fix(telegram): an album answered with Telegram's JSON 5xx is resent as the same album
Finding: SERVER_ERROR (Telegram JSON 5xx) is retried same-request for text
but the album is final, so a transient JSON 502 left it undelivered with a
"may already be in the chat" warning.

The shared classifier already treats Telegram's own JSON 5xx (PTB's unchained
NetworkError('Bad Gateway'), 'Service Unavailable', ...) as not delivered, and
the text lane repeats the same request with backoff. _send_chunk now does the
same for sendMediaGroup, in both the gateway adapter and the standalone album
lanes: the same request, the files rewound, with the text lane's budget (3
tries, 1s/2s), now named once in telegram_send_errors (SAME_REQUEST_ATTEMPTS,
same_request_backoff) and read by both. It never splits into per-photo sends
for that case. An HTML-body 5xx (NetworkError chained to the failed body
parse), a 504, a timeout or a reset stay ambiguous with one request; a JSON
5xx that outlasts the budget is ambiguous too.

Real PTB 22.8 HTTPXRequest matrix (reviewer's p_matrix.py, 108 cells): only
the sa_album/ad_album json500/502/503 cells change (1 -> 2 sendMediaGroup,
success, no warning); text, voice and photo+caption cells are unchanged.
Tests red on 06d9f56e48f for the standalone and adapter paths. The PR's own
test_media_group_5xx_is_ambiguous_and_never_resent pinned the old ruling for
the JSON 502 case; that case is now an HTML 502 page (still one request,
ambiguous) and the JSON 502 is covered by the resend tests.
2026-11-08 20:53:58 -08:00
Teknium
b89e9e7821 fix(telegram): a 5xx album or an HTML 5xx page is never resent in another shape
Review finding "Telegram 504 / non-JSON 5xx still resends".

telegram_album.reply_lost treated an unchained NetworkError ("Gateway Timeout",
"Bad Gateway") as a refusal and sent every photo again one by one, while the
standalone text retry already held a timeout final. A 5xx whose body is a
fronting proxy's HTML page reads "... Parsing the server response ... failed",
which the text paths took for a Bot API entity-parse error and resent as plain
text.

New plugins/platforms/telegram/telegram_send_errors.py is the one classifier
for both lanes: send_error_kind (refused / flood / unsent / server_error /
lost / local), may_have_delivered (album: any 5xx or lost answer is ambiguous,
never resent per photo) and is_entity_parse_error (plain-text fallback only on
a BadRequest naming the formatting, or a local error; never a NetworkError).
_telegram_retry_delay, the standalone text and caption fallbacks, the album
sender, the adapter's _send_chunk_markdown_or_plain and its voice-caption
fallback all read it.
2026-11-08 20:53:58 -08:00