Skip to content

Commit d0832b0

Browse files
clean: remove recovery from unified diff in add/delete file changes
incorrect assumption that add/diff changes may have unified diff
1 parent 482c59b commit d0832b0

3 files changed

Lines changed: 16 additions & 96 deletions

File tree

src/CodexToolCallMapper.ts

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -274,17 +274,10 @@ async function createPatchContent(change: FileUpdateChange): Promise<ToolCallCon
274274
}
275275

276276
async function createAddFileContent(change: FileUpdateChange): Promise<ToolCallContent | null> {
277-
let newText;
278-
if (isUnifiedDiff(change.diff)) {
279-
newText = applyPatch("", change.diff)
280-
if (!newText) return null;
281-
} else {
282-
newText = change.diff;
283-
}
284277
return {
285278
type: "diff",
286279
oldText: null,
287-
newText: newText,
280+
newText: change.diff, // app-server always returns file content instead of diff
288281
path: change.path,
289282
_meta: {
290283
kind: "add",
@@ -338,13 +331,9 @@ function createUpdateDiffContent(path: string, oldText: string, newText: string)
338331
}
339332

340333
async function createDeleteFileContent(change: FileUpdateChange): Promise<ToolCallContent> {
341-
// If the patch deletes a file, the old content may be only available from the diff.
342-
const fileContent = await readFileContent(change.path);
343-
const oldContent = fileContent ?? (isUnifiedDiff(change.diff) ? patchToDeletedContent(change.diff) : change.diff);
344-
345334
return {
346335
type: "diff",
347-
oldText: oldContent,
336+
oldText: change.diff, // app-server always returns file content instead of diff
348337
newText: "",
349338
path: change.path,
350339
_meta: {
@@ -364,44 +353,3 @@ async function readFileContent(filePath: string): Promise<string | null> {
364353
function recoverCorruptedDiff(diff: string): string {
365354
return diff.replace(/\nMoved to: .*$/, "");
366355
}
367-
368-
function isUnifiedDiff(content: string): boolean {
369-
return content.startsWith("--- ") || content.includes("\n--- ");
370-
}
371-
372-
/**
373-
* Recreates the content of a deleted file from the unified diff.
374-
* @param unifiedDiff The unified diff of the file deletion patch
375-
*/
376-
function patchToDeletedContent(unifiedDiff: string): string | null {
377-
try {
378-
const [patch] = parsePatch(unifiedDiff);
379-
if (!patch || patch.hunks.length === 0) {
380-
return null;
381-
}
382-
383-
const oldLines: string[] = [];
384-
let hasNoTrailingNewlineMarker = false;
385-
386-
for (const hunk of patch.hunks) {
387-
for (const line of hunk.lines) {
388-
if (line === "\\ No newline at end of file") {
389-
hasNoTrailingNewlineMarker = true;
390-
continue;
391-
}
392-
if (line.startsWith("-") || line.startsWith(" ")) {
393-
oldLines.push(line.slice(1));
394-
}
395-
}
396-
}
397-
398-
if (oldLines.length === 0) {
399-
return "";
400-
}
401-
402-
const oldText = oldLines.join("\n");
403-
return hasNoTrailingNewlineMarker || !unifiedDiff.endsWith("\n") ? oldText : `${oldText}\n`;
404-
} catch {
405-
return null;
406-
}
407-
}

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

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,7 @@ describe('CodexEventHandler - file change events', () => {
5555
{
5656
path: '/test/project/NewFile.kt',
5757
kind: { type: 'add' },
58-
diff: `--- /dev/null
59-
+++ /test/project/NewFile.kt
60-
@@ -0,0 +1,5 @@
61-
+package test.project
62-
+
63-
+class NewFile {
64-
+ fun hello() = "Hello"
65-
+}`,
58+
diff: 'package test.project\n\nclass NewFile {\n fun hello() = "Hello"\n}\n',
6659
},
6760
],
6861
status: 'completed',
@@ -90,18 +83,12 @@ describe('CodexEventHandler - file change events', () => {
9083
{
9184
path: '/test/project/FileA.kt',
9285
kind: { type: 'add' },
93-
diff: `--- /dev/null
94-
+++ /test/project/FileA.kt
95-
@@ -0,0 +1 @@
96-
+class FileA`,
86+
diff: 'class FileA\n',
9787
},
9888
{
9989
path: '/test/project/FileB.kt',
10090
kind: { type: 'add' },
101-
diff: `--- /dev/null
102-
+++ /test/project/FileB.kt
103-
@@ -0,0 +1 @@
104-
+class FileB`,
91+
diff: 'class FileB\n',
10592
},
10693
],
10794
status: 'completed',
@@ -158,12 +145,7 @@ describe('CodexEventHandler - file change events', () => {
158145
{
159146
path: '/test/project/OldFile.kt',
160147
kind: { type: 'delete' },
161-
diff: `--- /test/project/OldFile.kt
162-
+++ /dev/null
163-
@@ -1,3 +0,0 @@
164-
-package test.project
165-
-
166-
-class OldFile {}`,
148+
diff: 'package test.project\n\nclass OldFile {}',
167149
},
168150
],
169151
status: 'completed',
@@ -224,12 +206,7 @@ describe('CodexEventHandler - file change events', () => {
224206
{
225207
path: '/test/project/OldFile.kt',
226208
kind: { type: 'delete' },
227-
diff: `--- /test/project/OldFile.kt
228-
+++ /dev/null
229-
@@ -1,3 +0,0 @@
230-
-package test.project
231-
-
232-
-class OldFile {}`,
209+
diff: 'package test.project\n\nclass OldFile {}',
233210
},
234211
],
235212
status: 'completed',
@@ -274,19 +251,19 @@ describe('CodexEventHandler - file change events', () => {
274251
);
275252
});
276253

277-
it('should ignore broken unified diffs in file changes', async () => {
278-
const fileChange: ThreadItem = {
254+
it('should ignore broken unified diffs in update file changes', async () => {
255+
const fileChange: ThreadItem & { type: 'fileChange' } = {
279256
type: 'fileChange',
280257
id: 'file-change-broken-diff',
281258
changes: [
282259
{
283-
path: '/test/project/BrokenFile.kt',
284-
kind: { type: 'add' },
260+
path: '/test/project/OldFile.kt',
261+
kind: { type: 'update', move_path: null },
285262
diff:
286-
`--- /dev/null
287-
+++ /test/project/BrokenFile.kt
263+
`--- /test/project/OldFile.kt
264+
+++ /test/project/OldFile.kt
288265
@@ broken @@
289-
+class BrokenFile
266+
+class UpdatedFile
290267
`,
291268
},
292269
],

src/__tests__/CodexACPAgent/load-session.test.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,8 @@ describe("CodexACPAgent - loadSession", () => {
108108
{
109109
path: "/test/project/Added.txt",
110110
kind: { type: "add" },
111-
diff: `--- /dev/null
112-
+++ /test/project/Added.txt
113-
@@ -0,0 +1,2 @@
114-
+Hello
115-
+World
116-
`,
117-
},
111+
diff: "Hello\nWorld\n",
112+
}
118113
],
119114
status: "completed",
120115
},

0 commit comments

Comments
 (0)