Skip to content

Conversation

@daniel-lxs
Copy link
Member

@daniel-lxs daniel-lxs commented Dec 9, 2025

Summary

Add API error telemetry to the OpenRouter provider using PostHog's captureException for structured error tracking.

Changes

  • Added ApiProviderError generic error class in @roo-code/types for structured error tracking via PostHog (can be reused by other providers)
  • Added telemetry exception capture to createMessage method (both SDK errors and streaming errors)
  • Added telemetry exception capture to completePrompt method (both SDK errors and response errors)
  • Uses shouldReportApiErrorToTelemetry() to filter out expected errors like 429 rate limits
  • Added comprehensive tests for error telemetry capture scenarios

Depends on

Test plan

  • All 16 OpenRouter tests pass
  • All 4595 total tests pass
  • Verified 429 rate limit errors are NOT captured
  • Verified other API errors ARE captured

Fixes ApiError: Expected toolResult blocks for mismatched IDs

## Problem
Tool_result blocks sometimes had incorrect tool_use_id values, causing
the Anthropic API to reject requests with errors like:
"Expected toolResult blocks at messages.6.content for the following Ids:
toolu_ABC, but found: toolu_XYZ"

## Solution
Added centralized validation in addToApiConversationHistory() that:
- Validates tool_result IDs against tool_use IDs from previous assistant message
- Fixes mismatches using position-based matching
- Logs warnings for debugging when corrections are made

## Changes
- Added src/core/task/validateToolResultIds.ts with validation logic
- Added src/core/task/__tests__/validateToolResultIds.spec.ts (12 tests)
- Modified src/core/task/Task.ts to call validation
- Simplified src/core/assistant-message/presentAssistantMessage.ts
  by removing redundant source-level validation
@daniel-lxs daniel-lxs requested review from cte, jr and mrubens as code owners December 9, 2025 19:36
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. Enhancement New feature or request labels Dec 9, 2025
@roomote
Copy link
Contributor

roomote bot commented Dec 9, 2025

Rooviewer Clock   See task on Roo Cloud

Review complete. No new issues found.

The latest commit (ba3c0ce) reverts the unrelated tool_result ID validation changes that were accidentally included from #9952. This addresses the reviewer feedback. The revert is clean and correctly removes the validateToolResultIds.ts module and all references to it in Task.ts.

Previous reviews

Mention @roomote in a comment to request specific changes to this pull request or fix all unresolved issues.

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Dec 9, 2025
@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Dec 9, 2025
Copy link
Collaborator

@mrubens mrubens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use the posthog error handling instead of just events? https://posthog.com/docs/error-tracking

- Replace .slice().reverse().find() with direct backwards loop
- Avoids creating two intermediate arrays for large conversation histories
- Applied to both addToApiConversationHistory and flushPendingToolResultsToHistory
- Replace manual backwards loop with findLastIndex in both
  addToApiConversationHistory and flushPendingToolResultsToHistory
- Improves clarity and consistency with existing code patterns
- Add new TelemetryEventName.API_ERROR to telemetry types
- Log API errors to PostHog with sanitized error messages
- Use sanitizeErrorMessage to redact sensitive info (URLs, paths, IPs, emails)
- Add tests for telemetry capture in error scenarios
- Add EXPECTED_API_ERROR_CODES and shouldReportApiErrorToTelemetry to types package
- Filter out 429 rate limit errors from telemetry (expected behavior)
- Add tests to verify 429 errors don't trigger telemetry
@daniel-lxs daniel-lxs force-pushed the feat/openrouter-api-error-telemetry branch from 5b144be to 0d0ab5d Compare December 9, 2025 23:18
@dosubot dosubot bot added size:XL This PR changes 500-999 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Dec 9, 2025
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Dec 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Enhancement New feature or request Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. lgtm This PR has been approved by a maintainer size:L This PR changes 100-499 lines, ignoring generated files.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

6 participants