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
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,10 @@
"esbuild": "^0.25.9",
"prettier": "^3.6.2",
"typescript": "^5.9.2"
},
"overrides": {
"@smithy/smithy-client": "^4.10.2",
"@smithy/core": "^3.20.0",
"@smithy/middleware-serde": "^4.2.8"
}
}
60 changes: 52 additions & 8 deletions src/tools/FileEditTool/FileEditTool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@ import { emitReminderEvent } from '@services/systemReminder'
import { recordFileEdit } from '@services/fileFreshness'
import { NotebookEditTool } from '@tools/NotebookEditTool/NotebookEditTool'
import { DESCRIPTION } from './prompt'
import { applyEdit } from './utils'
import { applyEdit, applyEditWithEnhancements } from './utils'
import { hasWritePermission } from '@utils/permissions/filesystem'
import { PROJECT_FILE } from '@constants/product'
import { debug } from '@utils/debugLogger'

const inputSchema = z.strictObject({
file_path: z.string().describe('The absolute path to the file to modify'),
old_string: z.string().describe('The text to replace'),
new_string: z.string().describe('The text to replace it with'),
new_string: z.string().describe('The text to replace it with (must be different from old_string)'),
replace_all: z.boolean().optional().default(false).describe('Replace all occurences of old_string (default false)'),
old_str_start_line_number: z.number().optional().describe('Optional hint: 1-based start line number of old_string'),
old_str_end_line_number: z.number().optional().describe('Optional hint: 1-based end line number of old_string'),
})

export type In = typeof inputSchema
Expand Down Expand Up @@ -121,7 +125,7 @@ export const FileEditTool = {
}
},
async validateInput(
{ file_path, old_string, new_string },
{ file_path, old_string, new_string, replace_all, old_str_start_line_number, old_str_end_line_number },
{ readFileTimestamps },
) {
if (old_string === new_string) {
Expand Down Expand Up @@ -200,7 +204,17 @@ export const FileEditTool = {

const enc = detectFileEncoding(fullFilePath)
const file = readFileSync(fullFilePath, enc)

// Try exact match first
if (!file.includes(old_string)) {
// If line numbers are provided, we might try fuzzy matching later in call()
// For now, just indicate the string wasn't found
if (old_str_start_line_number !== undefined && old_str_end_line_number !== undefined) {
debug.trace('edit', `String not found verbatim, will try fuzzy matching with line hints: ${old_str_start_line_number}-${old_str_end_line_number}`)
// Allow validation to pass - we'll try enhanced matching in call()
return { result: true }
}

return {
result: false,
message: `String to replace not found in file.`,
Expand All @@ -211,21 +225,29 @@ export const FileEditTool = {
}

const matches = file.split(old_string).length - 1
if (matches > 1) {
if (matches > 1 && !replace_all) {
// If line numbers are provided, we can try to disambiguate
if (old_str_start_line_number !== undefined) {
debug.trace('edit', `Found ${matches} matches, will use line number hint to disambiguate`)
return { result: true }
}

return {
result: false,
message: `Found ${matches} matches of the string to replace. For safety, this tool only supports replacing exactly one occurrence at a time. Add more lines of context to your edit and try again.`,
message: `Found ${matches} matches of the string to replace. For safety, this tool only supports replacing exactly one occurrence at a time. Either add more lines of context to your edit, provide line number hints (old_str_start_line_number/old_str_end_line_number), or set replace_all=true.`,
meta: {
isFilePathAbsolute: String(isAbsolute(file_path)),
matchCount: matches,
},
}
}

return { result: true }
},
async *call({ file_path, old_string, new_string }, { readFileTimestamps }) {
const { patch, updatedFile } = applyEdit(file_path, old_string, new_string)

async *call(
{ file_path, old_string, new_string, replace_all, old_str_start_line_number, old_str_end_line_number },
{ readFileTimestamps }
) {
const fullFilePath = isAbsolute(file_path)
? file_path
: resolve(getCwd(), file_path)
Expand All @@ -240,6 +262,26 @@ export const FileEditTool = {
const originalFile = existsSync(fullFilePath)
? readFileSync(fullFilePath, enc)
: ''

// Use enhanced editing with fuzzy matching and line number support
const { patch, updatedFile, usedFuzzyMatching, matchedLine } = applyEditWithEnhancements(
file_path,
old_string,
new_string,
{
replaceAll: replace_all || false,
startLineNumber: old_str_start_line_number,
endLineNumber: old_str_end_line_number,
enableFuzzyMatching: true,
lineNumberErrorTolerance: 0.2,
}
)

// Log if fuzzy matching was used
if (usedFuzzyMatching) {
debug.trace('edit', `Used fuzzy matching for edit at line ${matchedLine}`)
}

writeTextContent(fullFilePath, updatedFile, enc, endings)

// Record Agent edit operation for file freshness tracking
Expand Down Expand Up @@ -268,6 +310,8 @@ export const FileEditTool = {
newString: new_string,
originalFile,
structuredPatch: patch,
usedFuzzyMatching,
matchedLine,
}
yield {
type: 'result',
Expand Down
93 changes: 45 additions & 48 deletions src/tools/FileEditTool/prompt.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,48 @@
import { NotebookEditTool } from '@tools/NotebookEditTool/NotebookEditTool'

export const DESCRIPTION = `This is a tool for editing files. For moving or renaming files, you should generally use the Bash tool with the 'mv' command instead. For larger edits, use the Write tool to overwrite files. For Jupyter notebooks (.ipynb files), use the ${NotebookEditTool.name} instead.

Before using this tool:

1. Use the View tool to understand the file's contents and context

2. Verify the directory path is correct (only applicable when creating new files):
- Use the LS tool to verify the parent directory exists and is the correct location

To make a file edit, provide the following:
1. file_path: The absolute path to the file to modify (must be absolute, not relative)
2. old_string: The text to replace (must be unique within the file, and must match the file contents exactly, including all whitespace and indentation)
3. new_string: The edited text to replace the old_string

The tool will replace ONE occurrence of old_string with new_string in the specified file.

CRITICAL REQUIREMENTS FOR USING THIS TOOL:

1. UNIQUENESS: The old_string MUST uniquely identify the specific instance you want to change. This means:
- Include AT LEAST 3-5 lines of context BEFORE the change point
- Include AT LEAST 3-5 lines of context AFTER the change point
- Include all whitespace, indentation, and surrounding code exactly as it appears in the file

2. SINGLE INSTANCE: This tool can only change ONE instance at a time. If you need to change multiple instances:
- Make separate calls to this tool for each instance
- Each call must uniquely identify its specific instance using extensive context

3. VERIFICATION: Before using this tool:
- Check how many instances of the target text exist in the file
- If multiple instances exist, gather enough context to uniquely identify each one
- Plan separate tool calls for each instance

WARNING: If you do not follow these requirements:
- The tool will fail if old_string matches multiple locations
- The tool will fail if old_string doesn't match exactly (including whitespace)
- You may change the wrong instance if you don't include enough context

When making edits:
- Ensure the edit results in idiomatic, correct code
- Do not leave the code in a broken state
- Always use absolute file paths (starting with /)

If you want to create a new file, use:
- A new file path, including dir name if needed
- An empty old_string
- The new file's contents as new_string

Remember: when making multiple file edits in a row to the same file, you should prefer to send all edits in a single message with multiple calls to this tool, rather than multiple messages with a single call each.
export const DESCRIPTION = `Performs exact string replacements in files.

Usage:
- You must use your \`Read\` tool at least once in the conversation before editing. This tool will error if you attempt an edit without reading the file.
- When editing text from Read tool output, ensure you preserve the exact indentation (tabs/spaces) as it appears AFTER the line number prefix. The line number prefix format is: spaces + line number + tab. Everything after that tab is the actual file content to match. Never include any part of the line number prefix in the old_string or new_string.
- ALWAYS prefer editing existing files in the codebase. NEVER write new files unless explicitly required.
- Only use emojis if the user explicitly requests it. Avoid adding emojis to files unless asked.
- The edit will FAIL if \`old_string\` is not unique in the file. Either provide a larger string with more surrounding context to make it unique or use \`replace_all\` to change every instance of \`old_string\`.
- Use \`replace_all\` for replacing and renaming strings across the file. This parameter is useful if you want to rename a variable for instance.

For moving or renaming files, use the Bash tool with 'mv' command. For larger edits, use the Write tool. For Jupyter notebooks (.ipynb files), use ${NotebookEditTool.name}.

Parameters:
1. file_path: Absolute path to the file (must start with /)
2. old_string: Text to replace (must be unique in file, match exactly including whitespace)
3. new_string: Replacement text
4. replace_all: (optional) If true, replace all occurrences of old_string
5. old_str_start_line_number: (optional) Start line number hint for disambiguating multiple matches
6. old_str_end_line_number: (optional) End line number hint for disambiguating multiple matches

SMART MATCHING FEATURES:
- Fuzzy matching: If exact match fails, attempts intelligent fuzzy matching when line numbers are provided
- Line number tolerance: Tolerates minor line number drift from file modifications
- Tab indent auto-fix: Automatically handles tab vs space indentation mismatches

REQUIREMENTS:
1. UNIQUENESS: old_string MUST uniquely identify the change location
- Include 3-5 lines of context before AND after the change point
- Preserve all whitespace and indentation exactly

2. SINGLE INSTANCE: Changes one instance at a time (unless replace_all=true)
- For multiple changes, make separate tool calls

3. VERIFICATION: Before editing
- Read the file first using Read tool
- Check how many instances of target text exist
- If multiple matches exist, provide line number hints or more context

WARNINGS:
- Tool fails if old_string matches multiple locations (without line hints)
- Tool fails if old_string doesn't match exactly (fuzzy matching may help)
- Always ensure edit results in valid, idiomatic code

NEW FILE CREATION:
- Use new file path, empty old_string, and file contents as new_string
`
Loading