fix(telegram): prevent raw API errors from reaching users #975
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
Adds sanitization layer at Telegram message delivery points to prevent raw API error messages (like '400 Incorrect role information') from appearing as bot messages to end users.
Problem
When multiple bot instances create race conditions or session state becomes corrupted, raw API errors can leak through to Telegram as confusing bot messages. This happened when I accidentally ran two gateway instances simultaneously.
Solution
Add final sanitization layer at three critical points:
telegram/bot/delivery.ts)telegram/draft-stream.ts)telegram/send.ts)The sanitization catches HTTP status errors and role ordering errors, replacing them with user-friendly messages that guide users to recovery (
/newcommand).Additional Fixes
This PR also includes:
validateAnthropicTurns())Testing
Tested with:
No raw errors reach users after these changes.
Files Changed
src/telegram/bot/delivery.ts- Add sanitizeOutboundText()src/telegram/draft-stream.ts- Add sanitizeDraftText()src/telegram/send.ts- Add sanitizeOutboundText()src/agents/pi-embedded-runner/run/attempt.ts- Add role ordering checkNote: Reopened from #966 on feature branch per workflow guidelines.