Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
a3c6f86
feat(experiments): make chat-diff the default approval path (L2, #1375)
easonliang28 Aug 27, 2026
165bff4
test(experiments): cover unset-experiment default routing in all writ…
easonliang28 Aug 27, 2026
c0ef476
test(experiments): add explicit enabled-state routing tests for chat-…
easonliang28 Aug 27, 2026
d038774
chore(ci): empty commit — re-trigger CI and the CodeRabbit current-he…
easonliang28 Aug 30, 2026
7519533
Merge commit '0d937c050810ebc305b34368bf6d6ae8c1f89966' into feat/l2-…
easonliang28 Sep 4, 2026
311a530
Merge remote-tracking branch 'upstream/main' into feat/l2-chat-diff-d…
easonliang28 Sep 14, 2026
df3cedb
Merge remote-tracking branch 'upstream/main' into feat/l2-chat-diff-d…
Oct 2, 2026
8c02fa0
Merge commit 'refs/shapes/upstream-main' into feat/l2-chat-diff-default
Oct 2, 2026
bce2f46
Merge branch 'main' into feat/l2-chat-diff-default
easonLiangWorldedtech Oct 5, 2026
5b80cbf
fix(settings): persist only the experiments the user toggled
easonLiangWorldedtech Oct 26, 2026
217568d
Merge remote-tracking branch 'upstream/main' into feat/l2-chat-diff-d…
easonLiangWorldedtech Oct 26, 2026
56ad004
fix(settings): keep submitted experiment ids until the host state ref…
easonLiangWorldedtech Oct 26, 2026
ed047f0
test(settings): forward Save disabled state and prove the experiment …
easonLiangWorldedtech Oct 26, 2026
00d6ec6
test(settings): cover buffered experiments and the stored-false rever…
easonLiangWorldedtech Oct 26, 2026
d0e245a
fix(experiments): compare against the last submitted value and cover …
easonLiangWorldedtech Oct 27, 2026
a8b349d
test(experiments): type the ApplyDiffTool fixture and clear pending b…
easonLiangWorldedtech Oct 27, 2026
cebc4fe
fix(experiments): clear pending baselines on session reset and cover …
easonLiangWorldedtech Oct 27, 2026
11913c9
test(experiments): cover ApplyPatchTool rejections and the confirmed-…
easonLiangWorldedtech Oct 27, 2026
46d2868
test(experiments): assert the update-file direct-save success result
easonLiangWorldedtech Oct 27, 2026
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
220 changes: 220 additions & 0 deletions src/core/tools/__tests__/applyDiffTool.experiments.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
import fs from "fs/promises"

import type { DiffResult, ToolResponse } from "../../../shared/tools"
import type { Task } from "../../task/Task"

type Experiments = Record<string, boolean>

interface ProviderFixture {
getState: () => Promise<{ experiments?: Experiments }>
}

interface DiffViewFixture {
editType: string | undefined
originalContent: string
open: (path: string) => Promise<void>
update: (content: string, final: boolean) => Promise<void>
reset: () => Promise<void>
revertChanges: () => Promise<void>
saveChanges: (diagnosticsEnabled?: boolean, writeDelayMs?: number) => Promise<void>
saveDirectly: (
path: string,
content: string,
open?: boolean,
diagnosticsEnabled?: boolean,
writeDelayMs?: number,
) => Promise<void>
scrollToFirstDiff: () => void
pushToolWriteResult: () => Promise<string>
}

/** The structural slice of Task this tool touches; the single cast to Task happens at construction. */
interface TaskFixture {
cwd: string
consecutiveMistakeCount: number
consecutiveMistakeCountForApplyDiff: Map<string, number>
didEditFile: boolean
api: { getModel: () => { id: string } }
rooIgnoreController: { validateAccess: (path: string) => boolean }
rooProtectedController: { isWriteProtected: (path: string) => boolean }
providerRef: { deref: () => ProviderFixture | undefined }
diffViewProvider: DiffViewFixture
fileContextTracker: { trackFileContext: (path: string, source: string) => Promise<void> }
say: (type: string, text?: string) => Promise<void>
recordToolError: (tool: string, error?: string) => void
sayAndCreateMissingParamError: (tool: string, param: string) => Promise<string>
processQueuedMessages: () => void
diffStrategy: {
getName: () => string
applyDiff: (original: string, diff: string) => Promise<DiffResult>
}
}
import { fileExistsAtPath } from "../../../utils/fs"
import type { ToolCallbacks } from "../BaseTool"
import { applyDiffTool } from "../ApplyDiffTool"

vi.mock("@roo-code/telemetry", () => ({
TelemetryService: { instance: { captureDiffApplicationError: vi.fn() } },
}))

vi.mock("fs/promises", () => ({
default: {
readFile: vi.fn().mockResolvedValue(""),
},
}))

vi.mock("../../../utils/fs", () => ({
fileExistsAtPath: vi.fn().mockResolvedValue(true),
}))

vi.mock("../../../utils/path", () => ({
getReadablePath: vi.fn().mockReturnValue("test/file.txt"),
}))

vi.mock("../../prompts/responses", () => ({
formatResponse: {
toolError: vi.fn((message: string) => `Error: ${message}`),
rooIgnoreError: vi.fn((filePath: string) => `Access denied: ${filePath}`),
createPrettyPatch: vi.fn(() => "mock-diff"),
},
}))

vi.mock("../../diff/stats", () => ({
sanitizeUnifiedDiff: vi.fn((diff: string) => diff),
computeDiffStats: vi.fn(() => ({ additions: 1, deletions: 1 })),
}))

vi.mock("vscode", () => ({
window: { showWarningMessage: vi.fn() },
env: { openExternal: vi.fn() },
Uri: { parse: vi.fn() },
}))

describe("ApplyDiffTool focus-disruption routing (L2)", () => {
const testFilePath = "test/file.txt"
const diffContent = "<<<<<<< SEARCH\nline one\n=======\nline one edited\n>>>>>>> REPLACE"

let fixture: TaskFixture
let mockTask: Task
let mockAskApproval: ReturnType<typeof vi.fn<ToolCallbacks["askApproval"]>>
let mockHandleError: ReturnType<typeof vi.fn<ToolCallbacks["handleError"]>>
let pushedResult: ToolResponse | undefined
let approved: boolean

beforeEach(() => {
vi.clearAllMocks()
pushedResult = undefined
approved = true
vi.mocked(fileExistsAtPath).mockResolvedValue(true)
vi.mocked(fs.readFile).mockResolvedValue("line one\nline two\nline three\n")

mockAskApproval = vi.fn().mockImplementation(async () => approved)
mockHandleError = vi.fn().mockResolvedValue(undefined)

fixture = {
cwd: "/workspace",
api: { getModel: () => ({ id: "claude-test" }) },
consecutiveMistakeCount: 0,
consecutiveMistakeCountForApplyDiff: new Map<string, number>(),
didEditFile: false,
rooIgnoreController: { validateAccess: vi.fn().mockReturnValue(true) },
rooProtectedController: { isWriteProtected: vi.fn().mockReturnValue(false) },
providerRef: { deref: vi.fn().mockReturnValue({ getState: vi.fn().mockResolvedValue({}) }) },
diffViewProvider: {
editType: undefined,
originalContent: "",
open: vi.fn().mockResolvedValue(undefined),
update: vi.fn().mockResolvedValue(undefined),
reset: vi.fn().mockResolvedValue(undefined),
revertChanges: vi.fn().mockResolvedValue(undefined),
saveChanges: vi.fn().mockResolvedValue(undefined),
saveDirectly: vi.fn().mockResolvedValue(undefined),
scrollToFirstDiff: vi.fn(),
pushToolWriteResult: vi.fn().mockResolvedValue("Changes successfully applied."),
},
fileContextTracker: { trackFileContext: vi.fn().mockResolvedValue(undefined) },
say: vi.fn().mockResolvedValue(undefined),
recordToolError: vi.fn(),
sayAndCreateMissingParamError: vi.fn().mockResolvedValue("Missing param error"),
processQueuedMessages: vi.fn(),
diffStrategy: {
getName: () => "test",
applyDiff: vi.fn().mockResolvedValue({ success: true, content: "updated content" } as DiffResult),
},
}
// One documented construction-site assertion: every member above is individually typed.
mockTask = fixture as unknown as Task
})

function setExperiments(experiments: Record<string, boolean>) {
mockTask.providerRef.deref = vi.fn().mockReturnValue({
getState: vi.fn().mockResolvedValue({ experiments }),
}) as unknown as Task["providerRef"]["deref"]
}

async function runApplyDiff() {
await applyDiffTool.execute({ path: testFilePath, diff: diffContent }, mockTask, {
askApproval: mockAskApproval,
handleError: mockHandleError,
toolCallId: "tool-call-1",
pushToolResult: vi.fn((result: ToolResponse) => {
pushedResult = result
}),
})
expect(mockHandleError).not.toHaveBeenCalled()
}

it("uses the direct chat-diff save when no experiment value is stored", async () => {
setExperiments({})

await runApplyDiff()

expect(mockTask.diffViewProvider.saveDirectly).toHaveBeenCalledWith(
testFilePath,
"updated content",
false,
true,
0,
)
expect(mockTask.diffViewProvider.open).not.toHaveBeenCalled()
expect(mockTask.diffViewProvider.saveChanges).not.toHaveBeenCalled()
expect(mockTask.didEditFile).toBe(true)
expect(String(pushedResult)).toContain("Changes successfully applied.")
})

it("uses the diff-editor path when the user opted out", async () => {
setExperiments({ preventFocusDisruption: false })

await runApplyDiff()

expect(mockTask.diffViewProvider.open).toHaveBeenCalledWith(testFilePath)
expect(mockTask.diffViewProvider.update).toHaveBeenCalledWith("updated content", true)
expect(mockTask.diffViewProvider.saveChanges).toHaveBeenCalled()
Comment thread
easonLiangWorldedtech marked this conversation as resolved.
expect(mockTask.diffViewProvider.saveDirectly).not.toHaveBeenCalled()
expect(String(pushedResult)).toContain("Changes successfully applied.")
})

it("does not save when the direct chat-diff approval is rejected", async () => {
approved = false
setExperiments({})

await runApplyDiff()

expect(mockAskApproval).toHaveBeenCalled()
expect(mockTask.diffViewProvider.saveDirectly).not.toHaveBeenCalled()
expect(mockTask.diffViewProvider.open).not.toHaveBeenCalled()
expect(mockTask.diffViewProvider.saveChanges).not.toHaveBeenCalled()
expect(pushedResult).toBeUndefined()
})

it("reverts the diff editor and does not save when the opt-out approval is rejected", async () => {
approved = false
setExperiments({ preventFocusDisruption: false })

await runApplyDiff()

expect(mockTask.diffViewProvider.revertChanges).toHaveBeenCalled()
expect(mockTask.diffViewProvider.saveChanges).not.toHaveBeenCalled()
expect(mockTask.diffViewProvider.saveDirectly).not.toHaveBeenCalled()
})
})
Loading
Loading