fix(telegram): surface agent errors + balance markdown/HTML across chunks#996
Open
minhdang03 wants to merge 1 commit intonextlevelbuilder:devfrom
Open
fix(telegram): surface agent errors + balance markdown/HTML across chunks#996minhdang03 wants to merge 1 commit intonextlevelbuilder:devfrom
minhdang03 wants to merge 1 commit intonextlevelbuilder:devfrom
Conversation
…unks External channels (Telegram, FB, Zalo) silently suppressed agent run errors to empty content, leaving users staring at a stopped typing indicator with no clue whether the bot died or was still thinking. Send a localized generic fallback (vi/en/zh) instead so users know to retry. LLM output cut mid-bold (max_tokens truncation, finish_reason=length, or stream terminate) leaked literal `**` markers to Telegram — the non-greedy bold regex silently dropped unmatched markers. Pre-sanitize raw content to strip unpaired `**`/`__` before formatting. chunkHTML's tag-safety check prevented cuts inside a tag but did NOT balance inline formatting (`<b>/<i>/<u>/<s>/<code>`) that legitimately spans chunk boundaries — e.g. `<b>foo\n\nbar</b>` split at `\n\n` produced chunk 1 with unclosed `<b>` (Telegram rejects) and chunk 2 with orphan `</b>`. Now each chunk auto-closes unclosed tags and the next chunk re-opens them. Also log a warning when ThinkStage sees `finish_reason=length` with no tool calls — text-only truncation still reaches the user (as a cut message) but was previously invisible to operators. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Infrastructure fixes for two Telegram-agent bugs observed while debugging a Vietnamese rideshare bot ("Gia Hân"). Both bugs affect any agent on an external channel, not just the specific one.
finish_reason=length, stream cut) mid-bold, the raw text ended with**70and the non-greedy regex silently passed it through as literal**— users saw• Anh thực nhận: **70. Pre-sanitize unpaired**/__before formatting.chunkHTMLpreviously avoided cuts inside a tag but did not balance inline formatting (<b>/<i>/<u>/<s>/<code>) across chunk boundaries — a long<b>foo\n\nbar</b>split at\n\nproduced chunk 1 with unclosed<b>(Telegram rejects) and chunk 2 with orphan</b>. Now each chunk auto-closes unclosed tags and the next chunk re-opens them.ThinkStagenow logs a warning whenfinish_reason=lengthhits text-only responses (no tool calls) — previously invisible to operators, though it still reaches the user as a cut message.Why it matters
From user bug report reproducing in production on the "Gia Hân" Telegram agent:
Both symptoms are explained by the two code bugs above; fixing the markdown/HTML balance removes the literal
**leak, and fixing the silent-error path means the user gets an actionable fallback instead of a stopped typing indicator with no message.Files changed
Test plan
Out of scope (flagged for follow-up)
The companion bugs in the user's report about "Gia Hân forgets the 125$ it just acknowledged" and "fills customer data from an earlier trip into a new parse" are prompt-engineering issues on that specific agent, not gateway bugs — the history is passed correctly and USER.md is loaded. They need prompt-level rule changes applied in the agent's context files, not code changes.
Possible follow-up code changes worth discussing separately:
🤖 Generated with Claude Code