Skip to content

Commit a639237

Browse files
fix: recover corrupted unified diff content
1 parent 58d93e4 commit a639237

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

src/CodexToolCallMapper.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,8 @@ async function createUpdateFileContent(change: FileUpdateChange): Promise<ToolCa
296296
const oldContent = await readFile(change.path, { encoding: "utf8" }).catch(() => null);
297297
if (oldContent === null) return null;
298298

299-
const newContent = applyPatch(oldContent, change.diff);
299+
const unifiedDiff = recoverCorruptedDiff(change.diff);
300+
const newContent = applyPatch(oldContent, unifiedDiff);
300301
if (!newContent) return null;
301302

302303
return {
@@ -327,6 +328,14 @@ async function createDeleteFileContent(change: FileUpdateChange): Promise<ToolCa
327328
}
328329
}
329330

331+
/**
332+
* Fix unified diff content corrupted by codex agent.
333+
* Removes synthetic "Moved to" from the end.
334+
*/
335+
function recoverCorruptedDiff(diff: string): string {
336+
return diff.replace(/\nMoved to: .*$/, "");
337+
}
338+
330339
function isUnifiedDiff(content: string): boolean {
331340
return content.startsWith("--- ") || content.includes("\n--- ");
332341
}

src/__tests__/CodexACPAgent/file-change-events.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,4 +298,40 @@ describe('CodexEventHandler - file change events', () => {
298298
content: [],
299299
});
300300
});
301+
302+
it('should parse update diffs with move metadata appended', async () => {
303+
mockFileContent('/test/project/OriginalFile.kt', 'old code line\n');
304+
305+
const fileChange: ThreadItem = {
306+
type: 'fileChange',
307+
id: 'file-change-move-metadata',
308+
changes: [
309+
{
310+
path: '/test/project/OriginalFile.kt',
311+
kind: {
312+
type: 'update',
313+
move_path: '/test/project/NewFile.kt',
314+
},
315+
diff:
316+
`@@ -1 +1 @@
317+
-old code line
318+
+new code line
319+
320+
321+
Moved to: /test/project/NewFile.kt`,
322+
},
323+
],
324+
status: 'inProgress',
325+
};
326+
327+
const updateEvent = await createFileChangeUpdate(fileChange);
328+
expect(updateEvent).toMatchObject({
329+
content: [
330+
{
331+
oldText: 'old code line\n',
332+
newText: 'new code line\n',
333+
},
334+
],
335+
});
336+
});
301337
});

0 commit comments

Comments
 (0)