Skip to content

Conversation

@roomote
Copy link
Contributor

@roomote roomote bot commented Nov 13, 2025

Description

This PR attempts to address Issue #9239 where the x-ai/grok-code-fast-1 model through OpenRouter causes frequent "Edit Unsuccessful" errors and displays unusual thinking blocks.

Problem

The x-ai/grok models were sending malformed reasoning blocks that were interfering with tool parsing, particularly affecting the diff application tools. This led to:

  • Frequent "Edit Unsuccessful" errors
  • Corrupted tool call arguments
  • Inability to apply diffs after extended sessions

Solution

This PR adds robust handling for x-ai model-specific quirks:

1. Reasoning Block Sanitization (src/api/providers/roo.ts)

  • Sanitizes reasoning text from x-ai models by removing XML-like tags and diff markers
  • Prevents reasoning content from leaking into regular content streams
  • Validates and skips corrupted tool calls containing reasoning artifacts

2. Diff Content Cleaning (src/core/tools/MultiApplyDiffTool.ts & ApplyDiffTool.ts)

  • Cleans reasoning artifacts from XML before parsing
  • Removes orphaned thinking tags that might interfere with diff parsing
  • Validates diff content doesn't contain reasoning blocks

Changes

  • Modified to add x-ai model detection and reasoning sanitization
  • Updated to clean reasoning artifacts from diff XML
  • Enhanced to handle reasoning blocks in diff content

Testing

✅ All existing tests pass
✅ Tested with affected provider tests

Related Issue

Fixes #9239

Feedback and guidance are welcome!


Important

Fixes handling of malformed reasoning blocks from x-ai models by sanitizing and cleaning reasoning artifacts in roo.ts, ApplyDiffTool.ts, and MultiApplyDiffTool.ts.

  • Behavior:
    • Adds reasoning block sanitization in RooHandler in roo.ts to remove XML-like tags and diff markers from x-ai models.
    • Cleans reasoning artifacts from diff content in ApplyDiffTool.ts and MultiApplyDiffTool.ts.
    • Validates and skips corrupted tool calls and diff content containing reasoning artifacts.
  • Files:
    • roo.ts: Detects x-ai models, sanitizes reasoning text, and handles corrupted tool calls.
    • ApplyDiffTool.ts: Cleans reasoning artifacts from diff content and handles corrupted XML structure.
    • MultiApplyDiffTool.ts: Parses XML for diffs, cleans reasoning artifacts, and handles batch diff operations.
  • Testing:
    • All existing tests pass.
    • Tested with affected provider tests.

This description was created by Ellipsis for 6f99b2e. You can customize this summary. It will automatically update as commits are pushed.

- Add sanitization for x-ai model reasoning blocks to prevent interference with tool parsing
- Remove XML-like tags and diff markers from reasoning content
- Add validation to skip corrupted tool calls from x-ai models
- Clean reasoning artifacts from diff content before processing
- Handle interleaved thinking blocks in content stream

This fixes issue #9239 where x-ai/grok-code-fast-1 model was causing 'Edit Unsuccessful' errors due to malformed reasoning blocks interfering with the diff parsing logic.
@roomote roomote bot requested review from cte, jr and mrubens as code owners November 13, 2025 20:00
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. bug Something isn't working labels Nov 13, 2025
@roomote
Copy link
Contributor Author

roomote bot commented Nov 13, 2025

Rooviewer Clock   See task on Roo Cloud

Review complete. Found 2 issues that should be addressed:

  • Fix ineffective isProcessingReasoning flag in roo.ts - flag is set and immediately reset, making it non-functional
  • Add orphaned </reasoning> tag cleanup in MultiApplyDiffTool.ts for consistency with ApplyDiffTool

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

@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Nov 13, 2025
text: delta.reasoning,
text: reasoningText,
}
isProcessingReasoning = false
Copy link
Contributor

Choose a reason for hiding this comment

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

The flag isProcessingReasoning is reset immediately after yielding reasoning content. If a delta contains both reasoning and tool_calls, this may allow tool calls to be processed even when part of corrupted reasoning. Consider retaining the flag for the entire processing of the chunk.

// For x-ai models, validate diff content doesn't contain reasoning artifacts
if (isXAIModel && diffContent) {
// Check for reasoning block markers that shouldn't be in diff content
if (diffContent.includes("<think>") || diffContent.includes("</think>")) {
Copy link
Contributor

Choose a reason for hiding this comment

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

In the diff item processing, only <think> tags are sanitized. For consistency with the other modules, you might also want to check and remove <reasoning> tags if they appear in the diff content.

Comment on lines +157 to +162
isProcessingReasoning = true
yield {
type: "reasoning",
text: delta.reasoning,
text: reasoningText,
}
isProcessingReasoning = false
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The isProcessingReasoning flag is ineffective because it's set to true and then immediately back to false after yielding. This happens before the check on line 187 that's supposed to skip tool calls during reasoning processing. The flag will never be true when checked because it's reset synchronously. Consider removing the flag resets (lines 162 and 183) and only resetting when reasoning processing completes, or restructure the logic to properly track reasoning state across async yields.

Fix it with Roo Code or mention @roomote and request a fix.

// Check for corrupted XML structure due to reasoning blocks
if (cleanedXml.includes("</think>") && !cleanedXml.includes("<think>")) {
console.warn("[MultiApplyDiffTool] Detected orphaned reasoning closing tag in x-ai model response")
cleanedXml = cleanedXml.replace(/<\/think>/g, "")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Inconsistent orphaned tag handling compared to ApplyDiffTool.ts. This code only checks for orphaned </think> tags but not </reasoning> tags. The ApplyDiffTool handles both (lines 65-66). For consistency and completeness, this should also check for and remove orphaned </reasoning> tags using the same pattern.

Fix it with Roo Code or mention @roomote and request a fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. size:L This PR changes 100-499 lines, ignoring generated files.

Projects

Status: Triage

Development

Successfully merging this pull request may close these issues.

[BUG] x-ai/grok-code-fast-1 Edit Unsuccessful

3 participants