fix(telegram): prevent raw API errors from reaching users#975
Closed
ThomsenDrake wants to merge 4 commits intoopenclaw:mainfrom
Closed
fix(telegram): prevent raw API errors from reaching users#975ThomsenDrake wants to merge 4 commits intoopenclaw:mainfrom
ThomsenDrake wants to merge 4 commits intoopenclaw:mainfrom
Conversation
added 4 commits
January 15, 2026 16:58
Add sanitization layer at final message delivery points to catch and replace raw API error messages (like '400 Incorrect role information') with user-friendly error messages. This prevents errors from appearing as confusing bot messages when: - Multiple bot instances create race conditions - Session state becomes corrupted - API validation errors occur after message formatting Changes: - Add sanitizeOutboundText() in telegram/bot/delivery.ts - Add sanitizeDraftText() in telegram/draft-stream.ts - Add sanitizeOutboundText() in telegram/send.ts - Sanitize both regular messages and draft/typing previews The sanitization acts as a final safety net - errors should still be formatted earlier in the pipeline, but this ensures no raw errors escape to end users.
Add preemptive check before adding user messages to session history to prevent Anthropic API '400 Incorrect role information' errors. This complements existing validateAnthropicTurns() by preventing the issue at the source rather than fixing it after the fact. The check skips prompting when the last message is already a user message, which can occur when: - A previous heartbeat didn't receive a response - A user message errored before getting an assistant response - Multiple instances cause race conditions Changes: - Add role ordering check in pi-embedded-runner/run/attempt.ts - Enhanced error handling in auto-reply/reply/agent-runner-execution.ts - Log warnings when skipping prompts due to role ordering
Expand error message pattern matching to catch additional variants of role ordering errors including JSON-wrapped errors and HTTP status prefix variants. Also handle cases where errorMessage is present even when stopReason isn't 'error'.
Update package manager version and merge test updates from upstream.
Contributor
|
Merged via squash.
Tests:
|
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
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.