Skip to content

refactor(telegram): unify formatted-to-plain send degradation in one orchestrator - #121990

Merged
obviyus merged 1 commit into
mainfrom
refactor/telegram-plain-fallback-ladder
Aug 11, 2026
Merged

obviyus merged 1 commit into
mainfrom
refactor/telegram-plain-fallback-ladder

Conversation

@obviyus

@obviyus obviyus commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator

What Problem This Solves

The Telegram plugin's "send funnel parity" invariant (extensions/telegram/AGENTS.md: the durable funnel and the streaming funnel must degrade identically on formatted-send 400s) was enforced only by prose and review. The formatted→plain recovery was hand-rolled at 11 call sites across send-message-text.ts, send-edit.ts, bot/delivery.send.ts, and draft-stream.ts, and had already drifted: draft previews and message edits degraded only on HTML-parse errors, not on empty-content 400s, so the same model output that recovered gracefully as a final message threw mid-preview.

Why This Change Was Made

One orchestrator, withTelegramPlainFallback (in rich-plain-fallback.ts, the file that already owns the shared classification), now owns error classification (rich vs html trigger sets), the empty-fallback-text guard, the unified degrade log line, and plain-chunk planning. All 11 sites supply only their transport primitives (sendFormatted / sendPlain). withTelegramHtmlParseFallback and buildTelegramPlainFallbackPlan are deleted; a new degradation physically cannot land in one funnel only.

Intentional behavior changes (the drift this PR kills):

  • Draft preview sends/edits and editMessageTelegram now degrade to plain text on empty-content 400s, matching the durable funnel.
  • Degrade log lines unify to telegram <context> degrade=plain-fallback:<trigger>: <error> (previously rich-degrade=plain-fallback: plus per-site wordings).

Named follow-up: the voice-caption too-long recovery in bot/delivery.replies.ts remains a separate hand-rolled ladder (different recovery shape: resend captionless + separate text); unifying it belongs with the media ladder (sendTelegramCaptionedMediaWithFallback), not this text-send change.

User Impact

Streaming previews no longer die mid-stream on model output that renders empty after formatting; users get the plain-text preview instead of a stalled draft. No config changes, no API surface changes outside the plugin.

Evidence

  • Production LOC net −29 (+285/−314); tests +155/−35.
  • Table-driven parity test (rich-plain-fallback.test.ts): kind × {rich-entity-invalid, rich-structure-invalid, rich-content-required, html-parse, empty-content, non-degradable} → degrade vs rethrow.
  • Regression tests verified to fail on pre-fix code for the intended reason (plain retry never issued): draft-stream.test.ts "falls back to plain preview text when an HTML edit renders empty", send.test.ts "falls back to plain text when an HTML edit renders empty" — ran the new tests against the pre-change production sources: 3 failed there, all pass after.
  • Focused proof on head: node scripts/run-vitest.mjs over send*, rich-plain-fallback*, draft-stream*, bot/delivery* — 9 files, 526 tests passed (87s). pnpm tsgo:extensions clean, scripts/run-oxlint.mjs + telegram boundary dts clean, git diff --check clean.
  • Telegram E2E userbot proof on the final head to follow as a PR comment before merge.

@clawsweeper

clawsweeper Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

@obviyus
obviyus marked this pull request as ready for review August 11, 2026 10:05
@openclaw-barnacle openclaw-barnacle Bot added channel: telegram Channel integration: telegram size: L maintainer Maintainer-authored PR labels Aug 11, 2026
…orchestrator

Route rich and HTML send/edit degradation through one shared policy.
Add parity coverage and empty-content edit/preview regressions.
@obviyus
obviyus force-pushed the refactor/telegram-plain-fallback-ladder branch from 57584f2 to e2f0db6 Compare August 11, 2026 10:16
@obviyus

obviyus commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator Author

Telegram E2E proof (exact head e2f0db6)

Live drive via the QA userbot lane (TDLib user observer, mock provider, ephemeral gateway from this head; DM to the SUT bot, ids redacted).

Degradation drive — mock reply engineered so the rendered HTML contains a Telegram-supported-shape tag with a server-invalid value (<tg-emoji emoji-id="1">🦞</tg-emoji>; the renderer preserves tg-emoji client-side, Telegram rejects the fake id with a parse-class 400):

  • Recorded timeline: exactly one SUT message at ~2.9s, botApiMessageId 849, no crash, no stalled turn.
  • Delivered text (TDLib-observed): Degradation drive: 🦞 recovers as plain text OPENCLAW-T1-DEGRADE-PROOF
  • Raw TDLib update: zero entities and formatting tags stripped → the message was delivered by the plain-text fallback (no parse_mode), i.e. withTelegramPlainFallback recovered the rejected formatted send with a visible plain result.

Control drive — same pipeline, valid HTML (<b>OPENCLAW-T1-CONTROL-BOLD</b>):

  • Delivered text: Control drive: OPENCLAW-T1-CONTROL-BOLD stays formatted.
  • Raw TDLib update: textEntityTypeBold at offset 15, length 24 → proves this funnel does send parse_mode: "HTML" and the observer surfaces entities when Telegram accepts them.

The entity differential (bold entity present in control, zero entities after the invalid-emoji reply) is the discriminator: the degraded turn's HTML send was rejected and recovered as plain text end to end on real Telegram. Note the funnel intentionally suppresses expected-fallback 400s from API-error logging (shouldLog: !isTelegramHtmlParseError && !isTelegramEmptyContentError), so the recovery is proven from the channel-observable evidence rather than log lines.

Artifacts retained locally: events.ndjson (raw TDLib updates), summary.json timelines, scoped gateway logs, mock-openai-requests.ndjson for both drives.

The preview/edit empty-content parity fix is additionally pinned by the regression tests in this PR, which were verified to fail on pre-fix code (plain retry never issued).

@obviyus
obviyus enabled auto-merge (squash) August 11, 2026 10:23
@obviyus
obviyus merged commit 79016b4 into main Aug 11, 2026
112 checks passed
@obviyus
obviyus deleted the refactor/telegram-plain-fallback-ladder branch August 11, 2026 10:29
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Aug 12, 2026
…orchestrator (openclaw#121990)

The Telegram send-funnel parity invariant (durable and streaming funnels must degrade formatted sends identically) was prose-enforced and had drifted: draft previews and message edits only degraded on HTML-parse 400s, not empty-content 400s. One orchestrator (withTelegramPlainFallback) now owns classification, guard, logging, and plain-chunk planning for all 11 call sites; withTelegramHtmlParseFallback and buildTelegramPlainFallbackPlan are deleted. Net -29 production LOC. Behavior change: previews/edits recover empty-content 400s as plain text; degrade logs unify to `degrade=plain-fallback:<trigger>`.

Co-authored-by: Ayaan Zaidi <[email protected]>
Co-Authored-By: Claude Fable 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: telegram Channel integration: telegram maintainer Maintainer-authored PR size: L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant