Skip to content
Closed
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
15 changes: 15 additions & 0 deletions packages/types/src/experiment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export const experimentIds = [
"preventFocusDisruption",
"imageGeneration",
"runSlashCommand",
"reReadAfterEdit",
"reReadAfterEditGranular",
] as const

export const experimentIdsSchema = z.enum(experimentIds)
Expand All @@ -22,12 +24,25 @@ export type ExperimentId = z.infer<typeof experimentIdsSchema>
* Experiments
*/

// Schema for granular re-read after edit settings
export const reReadAfterEditGranularSchema = z.object({
applyDiff: z.boolean().optional(),
multiApplyDiff: z.boolean().optional(),
writeToFile: z.boolean().optional(),
insertContent: z.boolean().optional(),
searchAndReplace: z.boolean().optional(),
})

export type ReReadAfterEditGranular = z.infer<typeof reReadAfterEditGranularSchema>

export const experimentsSchema = z.object({
powerSteering: z.boolean().optional(),
multiFileApplyDiff: z.boolean().optional(),
preventFocusDisruption: z.boolean().optional(),
imageGeneration: z.boolean().optional(),
runSlashCommand: z.boolean().optional(),
reReadAfterEdit: z.boolean().optional(),
reReadAfterEditGranular: reReadAfterEditGranularSchema.optional(),
})

export type Experiments = z.infer<typeof experimentsSchema>
Expand Down
11 changes: 9 additions & 2 deletions src/core/tools/applyDiffTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,17 @@ export async function applyDiffToolLegacy(
? "\n<notice>Making multiple related changes in a single apply_diff is more efficient. If other changes are needed in this file, please include them as additional SEARCH/REPLACE blocks.</notice>"
: ""

// Check if RE_READ_AFTER_EDIT experiment is enabled for applyDiff
const isReReadAfterEditEnabled = experiments.isReReadAfterEditEnabled(state?.experiments ?? {}, "applyDiff")

const reReadSuggestion = isReReadAfterEditEnabled
? `\n\n<review_suggestion>The file has been edited. Consider using the read_file tool to review the changes and ensure they are correct and complete.</review_suggestion>`
: ""

if (partFailHint) {
pushToolResult(partFailHint + message + singleBlockNotice)
pushToolResult(partFailHint + message + singleBlockNotice + reReadSuggestion)
} else {
pushToolResult(message + singleBlockNotice)
pushToolResult(message + singleBlockNotice + reReadSuggestion)
}

await cline.diffViewProvider.reset()
Expand Down
9 changes: 8 additions & 1 deletion src/core/tools/insertContentTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,14 @@ export async function insertContentTool(
// Get the formatted response message
const message = await cline.diffViewProvider.pushToolWriteResult(cline, cline.cwd, !fileExists)

pushToolResult(message)
// Check if RE_READ_AFTER_EDIT experiment is enabled for insertContent
const isReReadAfterEditEnabled = experiments.isReReadAfterEditEnabled(state?.experiments ?? {}, "insertContent")

const reReadSuggestion = isReReadAfterEditEnabled
? `\n\n<review_suggestion>Content has been inserted into the file. Consider using the read_file tool to review the changes and ensure they are correct and complete.</review_suggestion>`
: ""

pushToolResult(message + reReadSuggestion)

await cline.diffViewProvider.reset()

Expand Down
17 changes: 16 additions & 1 deletion src/core/tools/multiApplyDiffTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -676,8 +676,23 @@ ${errorDetails ? `\nTechnical details:\n${errorDetails}\n` : ""}
? "\n<notice>Making multiple related changes in a single apply_diff is more efficient. If other changes are needed in this file, please include them as additional SEARCH/REPLACE blocks.</notice>"
: ""

// Check if RE_READ_AFTER_EDIT experiment is enabled for multiApplyDiff
const provider = cline.providerRef.deref()
const state = await provider?.getState()
const isReReadAfterEditEnabled = experiments.isReReadAfterEditEnabled(
state?.experiments ?? {},
"multiApplyDiff",
)

// Count how many files were successfully edited
const editedFiles = operationResults.filter((op) => op.status === "approved").length
const reReadSuggestion =
isReReadAfterEditEnabled && editedFiles > 0
? `\n\n<review_suggestion>${editedFiles === 1 ? "The file has" : `${editedFiles} files have`} been edited. Consider using the read_file tool to review the changes and ensure they are correct and complete.</review_suggestion>`
: ""

// Push the final result combining all operation results
pushToolResult(results.join("\n\n") + singleBlockNotice)
pushToolResult(results.join("\n\n") + singleBlockNotice + reReadSuggestion)
cline.processQueuedMessages()
return
} catch (error) {
Expand Down
12 changes: 11 additions & 1 deletion src/core/tools/searchAndReplaceTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,17 @@ export async function searchAndReplaceTool(
false, // Always false for search_and_replace
)

pushToolResult(message)
// Check if RE_READ_AFTER_EDIT experiment is enabled for searchAndReplace
const isReReadAfterEditEnabled = experiments.isReReadAfterEditEnabled(
state?.experiments ?? {},
"searchAndReplace",
)

const reReadSuggestion = isReReadAfterEditEnabled
? `\n\n<review_suggestion>The file has been modified via search and replace. Consider using the read_file tool to review the changes and ensure they are correct and complete.</review_suggestion>`
: ""

pushToolResult(message + reReadSuggestion)

// Record successful tool usage and cleanup
cline.recordToolUsage("search_and_replace")
Expand Down
12 changes: 11 additions & 1 deletion src/core/tools/writeToFileTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,17 @@ export async function writeToFileTool(
// Get the formatted response message
const message = await cline.diffViewProvider.pushToolWriteResult(cline, cline.cwd, !fileExists)

pushToolResult(message)
// Check if RE_READ_AFTER_EDIT experiment is enabled for writeToFile
const isReReadAfterEditEnabled = experiments.isReReadAfterEditEnabled(
state?.experiments ?? {},
"writeToFile",
)

const reReadSuggestion = isReReadAfterEditEnabled
? `\n\n<review_suggestion>The file has been ${fileExists ? "edited" : "created"}. Consider using the read_file tool to review the ${fileExists ? "changes" : "content"} and ensure ${fileExists ? "they are" : "it is"} correct and complete.</review_suggestion>`
: ""

pushToolResult(message + reReadSuggestion)

await cline.diffViewProvider.reset()

Expand Down
96 changes: 96 additions & 0 deletions src/shared/__tests__/experiments-granular.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { describe, it, expect } from "vitest"
import type { Experiments, ReReadAfterEditGranular } from "@roo-code/types"
import { experiments } from "../experiments"

describe("granular re-read after edit experiment", () => {
describe("isReReadAfterEditEnabled", () => {
it("should return true when legacy reReadAfterEdit is enabled", () => {
const config: Experiments = {
reReadAfterEdit: true,
}

expect(experiments.isReReadAfterEditEnabled(config, "applyDiff")).toBe(true)
expect(experiments.isReReadAfterEditEnabled(config, "multiApplyDiff")).toBe(true)
expect(experiments.isReReadAfterEditEnabled(config, "writeToFile")).toBe(true)
expect(experiments.isReReadAfterEditEnabled(config, "insertContent")).toBe(true)
expect(experiments.isReReadAfterEditEnabled(config, "searchAndReplace")).toBe(true)
})

it("should return false when legacy reReadAfterEdit is disabled and no granular settings", () => {
const config: Experiments = {
reReadAfterEdit: false,
}

expect(experiments.isReReadAfterEditEnabled(config, "applyDiff")).toBe(false)
expect(experiments.isReReadAfterEditEnabled(config, "multiApplyDiff")).toBe(false)
expect(experiments.isReReadAfterEditEnabled(config, "writeToFile")).toBe(false)
expect(experiments.isReReadAfterEditEnabled(config, "insertContent")).toBe(false)
expect(experiments.isReReadAfterEditEnabled(config, "searchAndReplace")).toBe(false)
})

it("should return true for specific edit types when granular settings are enabled", () => {
const config: Experiments = {
reReadAfterEdit: false,
reReadAfterEditGranular: {
applyDiff: true,
multiApplyDiff: false,
writeToFile: true,
insertContent: false,
searchAndReplace: true,
},
}

expect(experiments.isReReadAfterEditEnabled(config, "applyDiff")).toBe(true)
expect(experiments.isReReadAfterEditEnabled(config, "multiApplyDiff")).toBe(false)
expect(experiments.isReReadAfterEditEnabled(config, "writeToFile")).toBe(true)
expect(experiments.isReReadAfterEditEnabled(config, "insertContent")).toBe(false)
expect(experiments.isReReadAfterEditEnabled(config, "searchAndReplace")).toBe(true)
})

it("should prioritize legacy setting over granular when both are present", () => {
const config: Experiments = {
reReadAfterEdit: true, // Legacy enabled
reReadAfterEditGranular: {
applyDiff: false, // Granular disabled
multiApplyDiff: false,
writeToFile: false,
insertContent: false,
searchAndReplace: false,
},
}

// Legacy setting should override granular settings
expect(experiments.isReReadAfterEditEnabled(config, "applyDiff")).toBe(true)
expect(experiments.isReReadAfterEditEnabled(config, "multiApplyDiff")).toBe(true)
expect(experiments.isReReadAfterEditEnabled(config, "writeToFile")).toBe(true)
expect(experiments.isReReadAfterEditEnabled(config, "insertContent")).toBe(true)
expect(experiments.isReReadAfterEditEnabled(config, "searchAndReplace")).toBe(true)
})

it("should handle partial granular settings", () => {
const config: Experiments = {
reReadAfterEdit: false,
reReadAfterEditGranular: {
applyDiff: true,
// Other fields undefined
} as ReReadAfterEditGranular,
}

expect(experiments.isReReadAfterEditEnabled(config, "applyDiff")).toBe(true)
expect(experiments.isReReadAfterEditEnabled(config, "multiApplyDiff")).toBe(false)
expect(experiments.isReReadAfterEditEnabled(config, "writeToFile")).toBe(false)
expect(experiments.isReReadAfterEditEnabled(config, "insertContent")).toBe(false)
expect(experiments.isReReadAfterEditEnabled(config, "searchAndReplace")).toBe(false)
})

it("should return false when no experiments config is provided", () => {
const config: Experiments = {}

expect(experiments.isReReadAfterEditEnabled(config, "applyDiff")).toBe(false)
expect(experiments.isReReadAfterEditEnabled(config, "multiApplyDiff")).toBe(false)
expect(experiments.isReReadAfterEditEnabled(config, "writeToFile")).toBe(false)
expect(experiments.isReReadAfterEditEnabled(config, "insertContent")).toBe(false)
expect(experiments.isReReadAfterEditEnabled(config, "searchAndReplace")).toBe(false)
})
})
})
23 changes: 23 additions & 0 deletions src/shared/__tests__/experiments-reReadAfterEdit.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { describe, it, expect } from "vitest"
import { EXPERIMENT_IDS, experiments, experimentDefault } from "../experiments"

describe("RE_READ_AFTER_EDIT experiment", () => {
it("should include RE_READ_AFTER_EDIT in EXPERIMENT_IDS", () => {
expect(EXPERIMENT_IDS.RE_READ_AFTER_EDIT).toBe("reReadAfterEdit")
})

it("should have RE_READ_AFTER_EDIT in default configuration", () => {
expect(experimentDefault.reReadAfterEdit).toBe(false)
})

it("should correctly check if RE_READ_AFTER_EDIT is enabled", () => {
const disabledConfig = { reReadAfterEdit: false }
expect(experiments.isEnabled(disabledConfig, EXPERIMENT_IDS.RE_READ_AFTER_EDIT)).toBe(false)

const enabledConfig = { reReadAfterEdit: true }
expect(experiments.isEnabled(enabledConfig, EXPERIMENT_IDS.RE_READ_AFTER_EDIT)).toBe(true)

const emptyConfig = {}
expect(experiments.isEnabled(emptyConfig, EXPERIMENT_IDS.RE_READ_AFTER_EDIT)).toBe(false)
})
})
23 changes: 19 additions & 4 deletions src/shared/__tests__/experiments.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// npx vitest run src/shared/__tests__/experiments.spec.ts

import type { ExperimentId } from "@roo-code/types"
import type { ExperimentId, Experiments as ExperimentsType } from "@roo-code/types"

import { EXPERIMENT_IDS, experimentConfigsMap, experiments as Experiments } from "../experiments"

Expand All @@ -25,36 +25,51 @@ describe("experiments", () => {

describe("isEnabled", () => {
it("returns false when POWER_STEERING experiment is not enabled", () => {
const experiments: Record<ExperimentId, boolean> = {
const experiments: ExperimentsType = {
powerSteering: false,
multiFileApplyDiff: false,
preventFocusDisruption: false,
imageGeneration: false,
runSlashCommand: false,
reReadAfterEdit: false,
reReadAfterEditGranular: undefined,
}
expect(Experiments.isEnabled(experiments, EXPERIMENT_IDS.POWER_STEERING)).toBe(false)
})

it("returns true when experiment POWER_STEERING is enabled", () => {
const experiments: Record<ExperimentId, boolean> = {
const experiments: ExperimentsType = {
powerSteering: true,
multiFileApplyDiff: false,
preventFocusDisruption: false,
imageGeneration: false,
runSlashCommand: false,
reReadAfterEdit: false,
reReadAfterEditGranular: undefined,
}
expect(Experiments.isEnabled(experiments, EXPERIMENT_IDS.POWER_STEERING)).toBe(true)
})

it("returns false when experiment is not present", () => {
const experiments: Record<ExperimentId, boolean> = {
const experiments: ExperimentsType = {
powerSteering: false,
multiFileApplyDiff: false,
preventFocusDisruption: false,
imageGeneration: false,
runSlashCommand: false,
reReadAfterEdit: false,
reReadAfterEditGranular: undefined,
}
expect(Experiments.isEnabled(experiments, EXPERIMENT_IDS.POWER_STEERING)).toBe(false)
})
})

describe("RE_READ_AFTER_EDIT_GRANULAR", () => {
it("is configured correctly", () => {
expect(EXPERIMENT_IDS.RE_READ_AFTER_EDIT_GRANULAR).toBe("reReadAfterEditGranular")
expect(experimentConfigsMap.RE_READ_AFTER_EDIT_GRANULAR).toMatchObject({
enabled: false,
})
})
})
})
40 changes: 37 additions & 3 deletions src/shared/experiments.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
import type { AssertEqual, Equals, Keys, Values, ExperimentId, Experiments } from "@roo-code/types"
import type {
AssertEqual,
Equals,
Keys,
Values,
ExperimentId,
Experiments,
ReReadAfterEditGranular,
} from "@roo-code/types"

export const EXPERIMENT_IDS = {
MULTI_FILE_APPLY_DIFF: "multiFileApplyDiff",
POWER_STEERING: "powerSteering",
PREVENT_FOCUS_DISRUPTION: "preventFocusDisruption",
IMAGE_GENERATION: "imageGeneration",
RUN_SLASH_COMMAND: "runSlashCommand",
RE_READ_AFTER_EDIT: "reReadAfterEdit",
RE_READ_AFTER_EDIT_GRANULAR: "reReadAfterEditGranular",
} as const satisfies Record<string, ExperimentId>

type _AssertExperimentIds = AssertEqual<Equals<ExperimentId, Values<typeof EXPERIMENT_IDS>>>

type ExperimentKey = Keys<typeof EXPERIMENT_IDS>

interface ExperimentConfig {
enabled: boolean
enabled: boolean | ReReadAfterEditGranular
}

export const experimentConfigsMap: Record<ExperimentKey, ExperimentConfig> = {
Expand All @@ -22,16 +32,40 @@ export const experimentConfigsMap: Record<ExperimentKey, ExperimentConfig> = {
PREVENT_FOCUS_DISRUPTION: { enabled: false },
IMAGE_GENERATION: { enabled: false },
RUN_SLASH_COMMAND: { enabled: false },
RE_READ_AFTER_EDIT: { enabled: false },
RE_READ_AFTER_EDIT_GRANULAR: {
enabled: {
applyDiff: false,
multiApplyDiff: false,
writeToFile: false,
insertContent: false,
searchAndReplace: false,
},
},
}

export const experimentDefault = Object.fromEntries(
Object.entries(experimentConfigsMap).map(([_, config]) => [
EXPERIMENT_IDS[_ as keyof typeof EXPERIMENT_IDS] as ExperimentId,
config.enabled,
]),
) as Record<ExperimentId, boolean>
) as Experiments

export const experiments = {
get: (id: ExperimentKey): ExperimentConfig | undefined => experimentConfigsMap[id],
isEnabled: (experimentsConfig: Experiments, id: ExperimentId) => experimentsConfig[id] ?? experimentDefault[id],
isReReadAfterEditEnabled: (experimentsConfig: Experiments, editType: keyof ReReadAfterEditGranular): boolean => {
// If the legacy RE_READ_AFTER_EDIT is enabled, it applies to all edit types
if (experimentsConfig.reReadAfterEdit) {
return true
}

// Check if granular settings are enabled and if the specific edit type is enabled
const granularSettings = experimentsConfig.reReadAfterEditGranular
if (granularSettings && granularSettings[editType]) {
return true
}

return false
},
} as const
Loading
Loading