Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@ThomsenDrake
Copy link
Contributor

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:

  • Regular message delivery (telegram/bot/delivery.ts)
  • Draft/typing previews (telegram/draft-stream.ts)
  • Direct sends (telegram/send.ts)

The sanitization catches HTTP status errors and role ordering errors, replacing them with user-friendly messages that guide users to recovery (/new command).

Additional Fixes

This PR also includes:

  • Consecutive user message prevention: Proactive check before adding user messages to prevent role ordering errors at the source (complements existing validateAnthropicTurns())
  • Enhanced error detection: Better pattern matching for role ordering errors including JSON-wrapped variants

Testing

Tested with:

  • Multiple concurrent bot instances (the original failure scenario)
  • Normal operation with single instance
  • Heartbeat cycles
  • Manual message sends

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 check

Note: Reopened from #966 on feature branch per workflow guidelines.

Drake Thomsen 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.
@steipete
Copy link
Contributor

Merged via squash.

  • Squash commit: 23e4ba8
  • Changes: moved sanitization to generic user-facing pipeline (replies + typing + extract), kept tool/thinking stripping, added sanitize tests, updated changelog + clawtributors.

Tests:

  • pnpm lint (warnings: duplicate keys in src/gateway/server/tests/test-utils.ts and src/plugins/loader.ts)
  • pnpm build (fails: duplicate keys + plugin provider type mismatch in src/plugins/*)
  • pnpm test (fails: src/auto-reply/reply.triggers.trigger-handling.filters-usage-summary-current-model-provider.test.ts)

@steipete steipete closed this Jan 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants