Skip to content

Conversation

@daniel-lxs
Copy link
Member

@daniel-lxs daniel-lxs commented Nov 15, 2025

Problem

After PR #9159 added native tool support, users experienced a false 'tool has already been used in this message' error on every turn when using XML protocol (default).

Root Cause

In Task.ts, when the stream completes, the code was unconditionally merging parsed content blocks with native tool blocks:

const nativeToolBlocks = this.assistantMessageContent.filter((block) => block.type === 'tool_use')
const parsedBlocks = this.assistantMessageParser.getContentBlocks()
this.assistantMessageContent = [...parsedBlocks, ...nativeToolBlocks]

For XML protocol, parsedBlocks already contained the tool_use blocks parsed from XML text. The merge duplicated them - adding the same tool twice. When the first tool executed and set didAlreadyUseTool = true, the duplicate was incorrectly blocked.

Solution

Added protocol detection to only merge native tool blocks when using native protocol:

if (isNativeProtocol(toolProtocol)) {
    // Native protocol: merge parsed text with native tool blocks (have IDs)
    const nativeToolBlocks = this.assistantMessageContent.filter(
        (block): block is ToolUse<any> => block.type === 'tool_use' && (block as any).id !== undefined
    )
    this.assistantMessageContent = [...parsedBlocks, ...nativeToolBlocks]
} else {
    // XML protocol: use only parsed blocks (no merging needed)
    this.assistantMessageContent = parsedBlocks
}

Also fixed the approval feedback flow to avoid prematurely setting the didAlreadyUseTool flag.

Testing

  • All existing tests pass
  • TypeScript compilation successful
  • Manually verified fix resolves the issue

Important

Fixes duplicate tool blocks in Task.ts by adding protocol detection to handle merging correctly based on protocol type.

  • Behavior:
    • Fixes duplicate tool blocks causing 'tool has already been used' error in Task.ts by adding protocol detection.
    • Merges native tool blocks only for native protocol; uses parsed blocks for XML protocol.
    • Fixes approval feedback flow to prevent premature didAlreadyUseTool flag setting.
  • Functions:
    • Modifies merging logic in Task.ts to check protocol type before merging tool blocks.
  • Testing:
    • All existing tests pass.
    • TypeScript compilation successful.
    • Manual verification confirms issue resolution.

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

- Add protocol detection in Task.ts to prevent merging parsed tool blocks with native tool blocks when using XML protocol
- Fix approval feedback flow in presentAssistantMessage.ts to add feedback directly without calling pushToolResult
- Resolves bug introduced in PR #9159 where tool blocks were duplicated, causing false 'already used' errors
@dosubot dosubot bot added size:S This PR changes 10-29 lines, ignoring generated files. bug Something isn't working labels Nov 15, 2025
@roomote
Copy link
Contributor

roomote bot commented Nov 15, 2025

Rooviewer Clock   See task on Roo Cloud

Review completed - no issues found.

The fix correctly addresses the duplicate tool blocks issue by:

  • Detecting the tool protocol from workspace configuration
  • Filtering native tool blocks by checking for the id property that distinguishes them from XML-parsed blocks
  • Only merging native blocks when using native protocol, avoiding duplicates in XML protocol flow

The implementation is well-structured and uses appropriate type guards consistent with codebase patterns.

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

@daniel-lxs daniel-lxs moved this from Triage to PR [Needs Review] in Roo Code Roadmap Nov 15, 2025
@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Nov 15, 2025
@hannesrudolph hannesrudolph added PR - Needs Review and removed Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. labels Nov 15, 2025
@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Nov 15, 2025
@mrubens mrubens merged commit 93c6d97 into main Nov 15, 2025
27 checks passed
@mrubens mrubens deleted the fix/duplicate-tool-blocks-xml-protocol branch November 15, 2025 00:37
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Nov 15, 2025
@github-project-automation github-project-automation bot moved this from PR [Needs Review] to Done in Roo Code Roadmap Nov 15, 2025
mini2s added a commit to zgsm-ai/costrict that referenced this pull request Nov 17, 2025