fix: Only attempt to fix diff if original isnt formatted correctly (#50)

This commit is contained in:
Brace Sproul 2025-05-29 16:12:21 -07:00 • committed by GitHub
parent 3d28198985
commit 7144fbbc22
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -56,13 +56,26 @@ export const applyPatchTool = tool(
} }
let patchedContent: string | false; let patchedContent: string | false;
let fixedDiff: string | false = false;
try { try {
logger.info(`Applying patch to file ${file_path}`); logger.info(`Applying patch to file ${file_path}`);
const fixedDiff = fixGitPatch(diff, { patchedContent = applyPatch(readFileOutput, diff);
} catch (e) {
try {
logger.warn("Failed to apply patch, trying to fix diff", {
error: e,
});
const fixedDiff_ = fixGitPatch(diff, {
[file_path]: readFileOutput, [file_path]: readFileOutput,
}); });
patchedContent = applyPatch(readFileOutput, fixedDiff); patchedContent = applyPatch(readFileOutput, fixedDiff_);
} catch (e) { logger.info("Successfully fixed diff and applied patch to file", {
file_path,
});
if (patchedContent) {
fixedDiff = fixedDiff_;
}
} catch (_) {
logger.error("Failed to apply patch", { logger.error("Failed to apply patch", {
...(e instanceof Error ...(e instanceof Error
? { name: e.name, message: e.message, stack: e.stack } ? { name: e.name, message: e.message, stack: e.stack }
@ -74,6 +87,7 @@ export const applyPatchTool = tool(
status: "error", status: "error",
}; };
} }
}
if (patchedContent === false) { if (patchedContent === false) {
logger.error( logger.error(
@ -99,11 +113,13 @@ export const applyPatchTool = tool(
}; };
} }
logger.info( let resultMessage = `Successfully applied diff to \`${file_path}\` and saved changes.`;
`Successfully applied diff to \`${file_path}\` and saved changes.`, logger.info(resultMessage);
); if (fixedDiff) {
resultMessage += `\n\nNOTE: The generated diff was NOT formatted properly, and had to be fixed. The diff which was applied is:\n<fixed-diff>\n${fixedDiff}\n</fixed-diff>`;
}
return { return {
result: `Successfully applied diff to \`${file_path}\` and saved changes.`, result: resultMessage,
status: "success", status: "success",
}; };
}, },