Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions src/core/tools/ApplyDiffTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { Task } from "../task/Task"
import { formatResponse } from "../prompts/responses"
import { fileExistsAtPath } from "../../utils/fs"
import { RecordSource } from "../context-tracking/FileContextTrackerTypes"
import { unescapeHtmlEntities } from "../../utils/text-normalization"
import { EXPERIMENT_IDS, experiments } from "../../shared/experiments"
import { computeDiffStats, sanitizeUnifiedDiff } from "../diff/stats"
import type { ToolUse } from "../../shared/tools"
Expand All @@ -26,11 +25,7 @@ export class ApplyDiffTool extends BaseTool<"apply_diff"> {

async execute(params: ApplyDiffParams, task: Task, callbacks: ToolCallbacks): Promise<void> {
const { askApproval, handleError, pushToolResult } = callbacks
let { path: relPath, diff: diffContent } = params

if (diffContent && !task.api.getModel().id.includes("claude")) {
diffContent = unescapeHtmlEntities(diffContent)
}
const { path: relPath, diff: diffContent } = params

try {
if (!relPath) {
Expand Down
11 changes: 4 additions & 7 deletions src/core/tools/ExecuteCommandTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { Task } from "../task/Task"

import { ToolUse, ToolResponse } from "../../shared/tools"
import { formatResponse } from "../prompts/responses"
import { unescapeHtmlEntities } from "../../utils/text-normalization"
import { ExitCodeDetails, RooTerminalCallbacks, RooTerminalProcess } from "../../integrations/terminal/types"
import { TerminalRegistry } from "../../integrations/terminal/TerminalRegistry"
import { Terminal } from "../../integrations/terminal/Terminal"
Expand Down Expand Up @@ -43,9 +42,7 @@ export class ExecuteCommandTool extends BaseTool<"execute_command"> {
return
}

const canonicalCommand = unescapeHtmlEntities(command)

const ignoredFileAttemptedToAccess = task.rooIgnoreController?.validateCommand(canonicalCommand)
const ignoredFileAttemptedToAccess = task.rooIgnoreController?.validateCommand(command)

if (ignoredFileAttemptedToAccess) {
await task.say("rooignore_error", ignoredFileAttemptedToAccess)
Expand All @@ -55,7 +52,7 @@ export class ExecuteCommandTool extends BaseTool<"execute_command"> {

task.consecutiveMistakeCount = 0

const didApprove = await askApproval("command", canonicalCommand)
const didApprove = await askApproval("command", command)

if (!didApprove) {
return
Expand All @@ -79,15 +76,15 @@ export class ExecuteCommandTool extends BaseTool<"execute_command"> {

// Check if command matches any prefix in the allowlist
const isCommandAllowlisted = commandTimeoutAllowlist.some((prefix) =>
canonicalCommand.startsWith(prefix.trim()),
command.startsWith(prefix.trim()),
)

// Convert seconds to milliseconds for internal use, but skip timeout if command is allowlisted
const commandExecutionTimeout = isCommandAllowlisted ? 0 : commandExecutionTimeoutSeconds * 1000

const options: ExecuteCommandOptions = {
executionId,
command: canonicalCommand,
command,
customCwd,
terminalShellIntegrationDisabled,
commandExecutionTimeout,
Expand Down
5 changes: 0 additions & 5 deletions src/core/tools/WriteToFileTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { fileExistsAtPath, createDirectoriesForFile } from "../../utils/fs"
import { stripLineNumbers, everyLineHasLineNumbers } from "../../integrations/misc/extract-text"
import { getReadablePath } from "../../utils/path"
import { isPathOutsideWorkspace } from "../../utils/pathUtils"
import { unescapeHtmlEntities } from "../../utils/text-normalization"
import { EXPERIMENT_IDS, experiments } from "../../shared/experiments"
import { convertNewFileToUnifiedDiff, computeDiffStats, sanitizeUnifiedDiff } from "../diff/stats"
import type { ToolUse } from "../../shared/tools"
Expand Down Expand Up @@ -81,10 +80,6 @@ export class WriteToFileTool extends BaseTool<"write_to_file"> {
newContent = newContent.split("\n").slice(0, -1).join("\n")
}

if (!task.api.getModel().id.includes("claude")) {
newContent = unescapeHtmlEntities(newContent)
}

const fullPath = relPath ? path.resolve(task.cwd, relPath) : ""
const isOutsideWorkspace = isPathOutsideWorkspace(fullPath)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ vitest.mock("../../prompts/responses", () => ({
rooIgnoreError: vitest.fn((msg) => `RooIgnore Error: ${msg}`),
},
}))
vitest.mock("../../../utils/text-normalization", () => ({
unescapeHtmlEntities: vitest.fn((text) => text),
}))
vitest.mock("../../../shared/package", () => ({
Package: {
name: "roo-cline",
Expand Down
47 changes: 23 additions & 24 deletions src/core/tools/__tests__/executeCommandTool.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import * as vscode from "vscode"
import { Task } from "../../task/Task"
import { formatResponse } from "../../prompts/responses"
import { ToolUse, AskApproval, HandleError, PushToolResult } from "../../../shared/tools"
import { unescapeHtmlEntities } from "../../../utils/text-normalization"

// Mock dependencies
vitest.mock("execa", () => ({
execa: vitest.fn(),
Expand Down Expand Up @@ -107,36 +105,37 @@ describe("executeCommandTool", () => {
})

/**
* Tests for HTML entity unescaping in commands
* This verifies that HTML entities are properly converted to their actual characters
* Tests for HTML entity preservation in commands
* Commands should be passed to the terminal exactly as provided
*/
describe("HTML entity unescaping", () => {
it("should unescape &lt; to < character", () => {
const input = "echo &lt;test&gt;"
const expected = "echo <test>"
expect(unescapeHtmlEntities(input)).toBe(expected)
})
describe("HTML entity preservation", () => {
it("should preserve HTML entities in commands (not convert them)", async () => {
mockToolUse.params.command = "echo &lt;test&gt;"
mockToolUse.nativeArgs = { command: "echo &lt;test&gt;" }

it("should unescape &gt; to > character", () => {
const input = "echo test &gt; output.txt"
const expected = "echo test > output.txt"
expect(unescapeHtmlEntities(input)).toBe(expected)
})
await executeCommandTool.handle(mockCline as unknown as Task, mockToolUse, {
askApproval: mockAskApproval as unknown as AskApproval,
handleError: mockHandleError as unknown as HandleError,
pushToolResult: mockPushToolResult as unknown as PushToolResult,
})

it("should unescape &amp; to & character", () => {
const input = "echo foo &amp;&amp; echo bar"
const expected = "echo foo && echo bar"
expect(unescapeHtmlEntities(input)).toBe(expected)
expect(mockAskApproval).toHaveBeenCalledWith("command", "echo &lt;test&gt;")
})

it("should handle multiple mixed HTML entities", () => {
const input = "grep -E 'pattern' &lt;file.txt &gt;output.txt 2&gt;&amp;1"
const expected = "grep -E 'pattern' <file.txt >output.txt 2>&1"
expect(unescapeHtmlEntities(input)).toBe(expected)
it("should preserve ampersand entities in commands", async () => {
mockToolUse.params.command = "echo foo &amp;&amp; echo bar"
mockToolUse.nativeArgs = { command: "echo foo &amp;&amp; echo bar" }

await executeCommandTool.handle(mockCline as unknown as Task, mockToolUse, {
askApproval: mockAskApproval as unknown as AskApproval,
handleError: mockHandleError as unknown as HandleError,
pushToolResult: mockPushToolResult as unknown as PushToolResult,
})

expect(mockAskApproval).toHaveBeenCalledWith("command", "echo foo &amp;&amp; echo bar")
})
})

// Now we can run these tests
describe("Basic functionality", () => {
it("should execute a command normally", async () => {
// Setup
Expand Down
22 changes: 4 additions & 18 deletions src/core/tools/__tests__/writeToFileTool.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type { MockedFunction } from "vitest"
import { fileExistsAtPath, createDirectoriesForFile } from "../../../utils/fs"
import { isPathOutsideWorkspace } from "../../../utils/pathUtils"
import { getReadablePath } from "../../../utils/path"
import { unescapeHtmlEntities } from "../../../utils/text-normalization"
import { everyLineHasLineNumbers, stripLineNumbers } from "../../../integrations/misc/extract-text"
import { ToolUse, ToolResponse } from "../../../shared/tools"
import { writeToFileTool } from "../WriteToFileTool"
Expand Down Expand Up @@ -47,10 +46,6 @@ vi.mock("../../../utils/path", () => ({
getReadablePath: vi.fn().mockReturnValue("test/path.txt"),
}))

vi.mock("../../../utils/text-normalization", () => ({
unescapeHtmlEntities: vi.fn().mockImplementation((content) => content),
}))

vi.mock("../../../integrations/misc/extract-text", () => ({
everyLineHasLineNumbers: vi.fn().mockReturnValue(false),
stripLineNumbers: vi.fn().mockImplementation((content) => content),
Expand Down Expand Up @@ -97,7 +92,6 @@ describe("writeToFileTool", () => {
const mockedCreateDirectoriesForFile = createDirectoriesForFile as MockedFunction<typeof createDirectoriesForFile>
const mockedIsPathOutsideWorkspace = isPathOutsideWorkspace as MockedFunction<typeof isPathOutsideWorkspace>
const mockedGetReadablePath = getReadablePath as MockedFunction<typeof getReadablePath>
const mockedUnescapeHtmlEntities = unescapeHtmlEntities as MockedFunction<typeof unescapeHtmlEntities>
const mockedEveryLineHasLineNumbers = everyLineHasLineNumbers as MockedFunction<typeof everyLineHasLineNumbers>
const mockedStripLineNumbers = stripLineNumbers as MockedFunction<typeof stripLineNumbers>
const mockedPathResolve = path.resolve as MockedFunction<typeof path.resolve>
Expand All @@ -116,7 +110,6 @@ describe("writeToFileTool", () => {
mockedFileExistsAtPath.mockResolvedValue(false)
mockedIsPathOutsideWorkspace.mockReturnValue(false)
mockedGetReadablePath.mockReturnValue("test/path.txt")
mockedUnescapeHtmlEntities.mockImplementation((content) => content)
mockedEveryLineHasLineNumbers.mockReturnValue(false)
mockedStripLineNumbers.mockImplementation((content) => content)

Expand Down Expand Up @@ -327,20 +320,13 @@ describe("writeToFileTool", () => {
expect(mockCline.diffViewProvider.update).toHaveBeenCalledWith("", true)
})

it("unescapes HTML entities for non-Claude models", async () => {
it("preserves HTML entities in content regardless of model", async () => {
mockCline.api.getModel.mockReturnValue({ id: "gpt-4" })
const contentWithEntities = "&lt;div&gt;John&apos;s &amp; Jane&apos;s &quot;test&quot;&lt;/div&gt;"

await executeWriteFileTool({ content: "&lt;test&gt;" })

expect(mockedUnescapeHtmlEntities).toHaveBeenCalledWith("&lt;test&gt;")
})

it("skips HTML unescaping for Claude models", async () => {
mockCline.api.getModel.mockReturnValue({ id: "claude-3" })

await executeWriteFileTool({ content: "&lt;test&gt;" })
await executeWriteFileTool({ content: contentWithEntities })

expect(mockedUnescapeHtmlEntities).not.toHaveBeenCalled()
expect(mockCline.diffViewProvider.update).toHaveBeenCalledWith(contentWithEntities, true)
})

it("strips line numbers from numbered content", async () => {
Expand Down
Loading