fix(core): Fix unable to cancel edit tool (#9299)

This commit is contained in:
Sandy Tao
2025-09-24 12:16:00 -07:00
committed by GitHub
parent 66c2184fe5
commit ad59be0c81
6 changed files with 277 additions and 0 deletions
+55
View File
@@ -471,6 +471,34 @@ describe('EditTool', () => {
);
expect(patchedContent).toBe(expectedFinalContent);
});
it('should rethrow calculateEdit errors when the abort signal is triggered', async () => {
const filePath = path.join(rootDir, 'abort-confirmation.txt');
const params: EditToolParams = {
file_path: filePath,
old_string: 'old',
new_string: 'new',
};
const invocation = tool.build(params);
const abortController = new AbortController();
const abortError = new Error('Abort requested');
const calculateSpy = vi
.spyOn(invocation as any, 'calculateEdit')
.mockImplementation(async () => {
if (!abortController.signal.aborted) {
abortController.abort();
}
throw abortError;
});
await expect(
invocation.shouldConfirmExecute(abortController.signal),
).rejects.toBe(abortError);
calculateSpy.mockRestore();
});
});
describe('execute', () => {
@@ -515,6 +543,33 @@ describe('EditTool', () => {
);
});
it('should reject when calculateEdit fails after an abort signal', async () => {
const params: EditToolParams = {
file_path: path.join(rootDir, 'abort-execute.txt'),
old_string: 'old',
new_string: 'new',
};
const invocation = tool.build(params);
const abortController = new AbortController();
const abortError = new Error('Abort requested during execute');
const calculateSpy = vi
.spyOn(invocation as any, 'calculateEdit')
.mockImplementation(async () => {
if (!abortController.signal.aborted) {
abortController.abort();
}
throw abortError;
});
await expect(invocation.execute(abortController.signal)).rejects.toBe(
abortError,
);
calculateSpy.mockRestore();
});
it('should edit an existing file and return diff with fileName', async () => {
const initialContent = 'This is some old text.';
const newContent = 'This is some new text.'; // old -> new
+6
View File
@@ -251,6 +251,9 @@ class EditToolInvocation implements ToolInvocation<EditToolParams, ToolResult> {
try {
editData = await this.calculateEdit(this.params, abortSignal);
} catch (error) {
if (abortSignal.aborted) {
throw error;
}
const errorMsg = error instanceof Error ? error.message : String(error);
console.log(`Error preparing edit: ${errorMsg}`);
return false;
@@ -336,6 +339,9 @@ class EditToolInvocation implements ToolInvocation<EditToolParams, ToolResult> {
try {
editData = await this.calculateEdit(this.params, signal);
} catch (error) {
if (signal.aborted) {
throw error;
}
const errorMsg = error instanceof Error ? error.message : String(error);
return {
llmContent: `Error preparing edit: ${errorMsg}`,
@@ -274,6 +274,36 @@ describe('SmartEditTool', () => {
filePath = path.join(rootDir, testFile);
});
it('should reject when calculateEdit fails after an abort signal', async () => {
const params: EditToolParams = {
file_path: path.join(rootDir, 'abort-execute.txt'),
instruction: 'Abort during execute',
old_string: 'old',
new_string: 'new',
};
const invocation = tool.build(params);
const abortController = new AbortController();
const abortError = new Error(
'Abort requested during smart edit execution',
);
const calculateSpy = vi
.spyOn(invocation as any, 'calculateEdit')
.mockImplementation(async () => {
if (!abortController.signal.aborted) {
abortController.abort();
}
throw abortError;
});
await expect(invocation.execute(abortController.signal)).rejects.toBe(
abortError,
);
calculateSpy.mockRestore();
});
it('should edit an existing file and return diff with fileName', async () => {
const initialContent = 'This is some old text.';
const newContent = 'This is some new text.';
@@ -511,4 +541,37 @@ describe('SmartEditTool', () => {
expect(params.new_string).toBe(modifiedContent);
});
});
describe('shouldConfirmExecute', () => {
it('should rethrow calculateEdit errors when the abort signal is triggered', async () => {
const filePath = path.join(rootDir, 'abort-confirmation.txt');
const params: EditToolParams = {
file_path: filePath,
instruction: 'Abort during confirmation',
old_string: 'old',
new_string: 'new',
};
const invocation = tool.build(params);
const abortController = new AbortController();
const abortError = new Error(
'Abort requested during smart edit confirmation',
);
const calculateSpy = vi
.spyOn(invocation as any, 'calculateEdit')
.mockImplementation(async () => {
if (!abortController.signal.aborted) {
abortController.abort();
}
throw abortError;
});
await expect(
invocation.shouldConfirmExecute(abortController.signal),
).rejects.toBe(abortError);
calculateSpy.mockRestore();
});
});
});
+6
View File
@@ -490,6 +490,9 @@ class EditToolInvocation implements ToolInvocation<EditToolParams, ToolResult> {
try {
editData = await this.calculateEdit(this.params, abortSignal);
} catch (error) {
if (abortSignal.aborted) {
throw error;
}
const errorMsg = error instanceof Error ? error.message : String(error);
console.log(`Error preparing edit: ${errorMsg}`);
return false;
@@ -575,6 +578,9 @@ class EditToolInvocation implements ToolInvocation<EditToolParams, ToolResult> {
try {
editData = await this.calculateEdit(this.params, signal);
} catch (error) {
if (signal.aborted) {
throw error;
}
const errorMsg = error instanceof Error ? error.message : String(error);
return {
llmContent: `Error preparing edit: ${errorMsg}`,