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,23 +56,37 @@ 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);
[file_path]: readFileOutput,
});
patchedContent = applyPatch(readFileOutput, fixedDiff);
} catch (e) { } catch (e) {
logger.error("Failed to apply patch", { try {
...(e instanceof Error logger.warn("Failed to apply patch, trying to fix diff", {
? { name: e.name, message: e.message, stack: e.stack } error: e,
: { error: e }), });
}); const fixedDiff_ = fixGitPatch(diff, {
const errMessage = e instanceof Error ? e.message : "Unknown error"; [file_path]: readFileOutput,
return { });
result: `FAILED TO APPLY PATCH: The diff could not be applied to file '${file_path}'.\n\nError: ${errMessage}`, patchedContent = applyPatch(readFileOutput, fixedDiff_);
status: "error", logger.info("Successfully fixed diff and applied patch to file", {
}; file_path,
});
if (patchedContent) {
fixedDiff = fixedDiff_;
}
} catch (_) {
logger.error("Failed to apply patch", {
...(e instanceof Error
? { name: e.name, message: e.message, stack: e.stack }
: { error: e }),
});
const errMessage = e instanceof Error ? e.message : "Unknown error";
return {
result: `FAILED TO APPLY PATCH: The diff could not be applied to file '${file_path}'.\n\nError: ${errMessage}`,
status: "error",
};
}
} }
if (patchedContent === false) { if (patchedContent === false) {
@ -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",
}; };
}, },