Repository navigation
feat(experiments): make chat-diff the default approval path (L2, #1375) #1384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
easonLiangWorldedtech
wants to merge
19
commits into
Zoo-Code-Org:main
Choose a base branch
from
easonLiangWorldedtech:feat/l2-chat-diff-default
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 165bff4
test(experiments): cover unset-experiment default routing in all writ…
easonliang28 c0ef476
test(experiments): add explicit enabled-state routing tests for chat-…
easonliang28 d038774
chore(ci): empty commit — re-trigger CI and the CodeRabbit current-he…
easonliang28 7519533
Merge commit '0d937c050810ebc305b34368bf6d6ae8c1f89966' into feat/l2-…
easonliang28 311a530
Merge remote-tracking branch 'upstream/main' into feat/l2-chat-diff-d…
easonliang28 df3cedb
Merge remote-tracking branch 'upstream/main' into feat/l2-chat-diff-d…
8c02fa0
Merge commit 'refs/shapes/upstream-main' into feat/l2-chat-diff-default
bce2f46
Merge branch 'main' into feat/l2-chat-diff-default
easonLiangWorldedtech 5b80cbf
fix(settings): persist only the experiments the user toggled
easonLiangWorldedtech 217568d
Merge remote-tracking branch 'upstream/main' into feat/l2-chat-diff-d…
easonLiangWorldedtech 56ad004
fix(settings): keep submitted experiment ids until the host state ref…
easonLiangWorldedtech ed047f0
test(settings): forward Save disabled state and prove the experiment …
easonLiangWorldedtech 00d6ec6
test(settings): cover buffered experiments and the stored-false rever…
easonLiangWorldedtech d0e245a
fix(experiments): compare against the last submitted value and cover …
easonLiangWorldedtech a8b349d
test(experiments): type the ApplyDiffTool fixture and clear pending b…
easonLiangWorldedtech cebc4fe
fix(experiments): clear pending baselines on session reset and cover …
easonLiangWorldedtech 11913c9
test(experiments): cover ApplyPatchTool rejections and the confirmed-…
easonLiangWorldedtech 46d2868
test(experiments): assert the update-file direct-save success result
easonLiangWorldedtech File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
220 changes: 220 additions & 0 deletions
220
src/core/tools/__tests__/applyDiffTool.experiments.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
| 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() | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.