fix(core): auto-correct file paths in smart edit where possible (#9526)

This commit is contained in:
anthony bushong
2025-09-26 13:05:15 -07:00
committed by GitHub
parent e909993dd1
commit 0d22b22c82
4 changed files with 159 additions and 6 deletions
+75 -3
View File
@@ -242,6 +242,78 @@ describe('SmartEditTool', () => {
});
});
describe('correctPath', () => {
it('should correct a relative path if it is unambiguous', () => {
const testFile = 'unique.txt';
fs.writeFileSync(path.join(rootDir, testFile), 'content');
const params: EditToolParams = {
file_path: testFile,
instruction: 'An instruction',
old_string: 'old',
new_string: 'new',
};
const validationResult = (tool as any).correctPath(params);
expect(validationResult).toBeNull();
expect(params.file_path).toBe(path.join(rootDir, testFile));
});
it('should correct a partial relative path if it is unambiguous', () => {
const subDir = path.join(rootDir, 'sub');
fs.mkdirSync(subDir);
const testFile = 'file.txt';
const partialPath = path.join('sub', testFile);
const fullPath = path.join(subDir, testFile);
fs.writeFileSync(fullPath, 'content');
const params: EditToolParams = {
file_path: partialPath,
instruction: 'An instruction',
old_string: 'old',
new_string: 'new',
};
const validationResult = (tool as any).correctPath(params);
expect(validationResult).toBeNull();
expect(params.file_path).toBe(fullPath);
});
it('should return an error for a relative path that does not exist', () => {
const params: EditToolParams = {
file_path: 'test.txt',
instruction: 'An instruction',
old_string: 'old',
new_string: 'new',
};
const result = (tool as any).correctPath(params);
expect(result).toMatch(/File not found for 'test.txt'/);
});
it('should return an error for an ambiguous path', () => {
const subDir1 = path.join(rootDir, 'module1');
const subDir2 = path.join(rootDir, 'module2');
fs.mkdirSync(subDir1, { recursive: true });
fs.mkdirSync(subDir2, { recursive: true });
const ambiguousFile = 'component.ts';
fs.writeFileSync(path.join(subDir1, ambiguousFile), 'content 1');
fs.writeFileSync(path.join(subDir2, ambiguousFile), 'content 2');
const params: EditToolParams = {
file_path: ambiguousFile,
instruction: 'An instruction',
old_string: 'old',
new_string: 'new',
};
const validationResult = (tool as any).correctPath(params);
expect(validationResult).toMatch(/ambiguous and matches multiple files/);
});
});
describe('validateToolParams', () => {
it('should return null for valid params', () => {
const params: EditToolParams = {
@@ -253,15 +325,15 @@ describe('SmartEditTool', () => {
expect(tool.validateToolParams(params)).toBeNull();
});
it('should return error for relative path', () => {
it('should return an error if path is outside the workspace', () => {
const params: EditToolParams = {
file_path: 'test.txt',
file_path: path.join(os.tmpdir(), 'outside.txt'),
instruction: 'An instruction',
old_string: 'old',
new_string: 'new',
};
expect(tool.validateToolParams(params)).toMatch(
/File path must be absolute/,
/must be within one of the workspace directories/,
);
});
});