Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 65 additions & 36 deletions src/CodexToolCallMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type {
ThreadItem,
} from "./app-server/v2";
import type { JsonValue } from "./app-server/serde_json/JsonValue";
import {logger} from "./Logger";

type CodexItemStatus = CommandExecutionStatus | PatchApplyStatus | McpToolCallStatus | DynamicToolCallStatus;
type AcpToolCallStatus = "pending" | "in_progress" | "completed" | "failed";
Expand Down Expand Up @@ -257,56 +258,84 @@ function createSearchTitle(query: string | null, path: string | null): string {
}

async function createPatchContent(change: FileUpdateChange): Promise<ToolCallContent | null> {
if (change.kind.type === "add" && !isUnifiedDiff(change.diff)) {
// For new files, diff may contain raw file content instead of a patch.
return {
type: "diff",
oldText: null,
newText: change.diff,
path: change.path,
_meta: {
kind: "add",
},
};
}

if (change.kind.type === "delete") {
// If the patch deletes a file, the old content may be only available from the diff.
const oldContent = await readFile(change.path, { encoding: "utf8"} ).catch(() =>
isUnifiedDiff(change.diff) ? patchToDeletedContent(change.diff) : change.diff
);

return {
type: "diff",
oldText: oldContent,
newText: "",
path: change.path,
_meta: {
kind: "delete",
}
try {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like unified diff handling for add/delete are redundant - see fn format_file_change_diff(change: &FileChange) -> String { https://github.com/openai/codex/blob/67b805fc111706aca5b32d465c94d95659bab6aa/codex-rs/app-server-protocol/src/protocol/item_builders.rs#L302

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still not sure that it never leaks via content. We have explicit commits and test for add/delete recovery. I tried to avoid any semantic change here and suggest putting this cleanup out of the scope of this PR.

switch (change.kind.type) {
case "add":
return await createAddFileContent(change);
case "delete":
return await createDeleteFileContent(change);
case "update":
return await createUpdateFileContent(change);
}
}

const oldContent = change.kind.type === "add" ? "" : await readFile(change.path, { encoding: "utf8" }).catch(() => null);
if (oldContent === null) {
} catch (error) {
logger.log(`Error processing file update change: ${error}`);
return null;
}
}

const newContent = applyPatch(oldContent, change.diff);
if (newContent === false) {
return null;
async function createAddFileContent(change: FileUpdateChange): Promise<ToolCallContent | null> {
let newText;
if (isUnifiedDiff(change.diff)) {
newText = applyPatch("", change.diff)
if (!newText) return null;
} else {
newText = change.diff;
}
return {
type: "diff",
oldText: change.kind.type === "add" ? null : oldContent,
oldText: null,
newText: newText,
path: change.path,
_meta: {
kind: "add",
},
};
}

async function createUpdateFileContent(change: FileUpdateChange): Promise<ToolCallContent | null> {
const oldContent = await readFile(change.path, { encoding: "utf8" }).catch(() => null);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'm seeing an error here where, when running the agent on bypass permissions, the edit is already applied to the file when reading the file here, so the applyPatch fails and the "Editing files" ACP tool call is sent with empty content.

if (oldContent === null) return null;

const unifiedDiff = recoverCorruptedDiff(change.diff);
const newContent = applyPatch(oldContent, unifiedDiff);
if (!newContent) return null;

return {
type: "diff",
oldText: oldContent,
newText: newContent,
path: change.path,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if file was actually moved? Don't we want to use PatchChangeKind#moved_path?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I think that it's impossible to express move in a single diff event. We probably need to split it into delete + add pair.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Confirmed
image

_meta: {
kind: change.kind.type,
kind: "update",
},
};
}

async function createDeleteFileContent(change: FileUpdateChange): Promise<ToolCallContent> {
// If the patch deletes a file, the old content may be only available from the diff.
const oldContent = await readFile(change.path, { encoding: "utf8"} ).catch(() =>
isUnifiedDiff(change.diff) ? patchToDeletedContent(change.diff) : change.diff
);

return {
type: "diff",
oldText: oldContent,
newText: "",
path: change.path,
_meta: {
kind: "delete",
}
}
}

/**
* Fix unified diff content corrupted by codex agent.
* Removes synthetic "Moved to" from the end.
*/
function recoverCorruptedDiff(diff: string): string {
return diff.replace(/\nMoved to: .*$/, "");
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It actually adds 2 \n

}

function isUnifiedDiff(content: string): boolean {
return content.startsWith("--- ") || content.includes("\n--- ");
}
Expand Down
63 changes: 63 additions & 0 deletions src/__tests__/CodexACPAgent/file-change-events.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import type { SessionState } from '../../CodexAcpServer';
import type { ServerNotification } from '../../app-server';
import { createFileChangeUpdate } from '../../CodexToolCallMapper';
import type { ThreadItem } from '../../app-server/v2';
import { createCodexMockTestFixture, createTestSessionState, setupPromptAndSendNotifications, type CodexMockTestFixture } from '../acp-test-utils';
import {AgentMode} from "../../AgentMode";

Expand Down Expand Up @@ -271,4 +273,65 @@ describe('CodexEventHandler - file change events', () => {
'data/file-change-delete-raw-content.json'
);
});

it('should ignore broken unified diffs in file changes', async () => {
const fileChange: ThreadItem = {
type: 'fileChange',
id: 'file-change-broken-diff',
changes: [
{
path: '/test/project/BrokenFile.kt',
kind: { type: 'add' },
diff:
`--- /dev/null
+++ /test/project/BrokenFile.kt
@@ broken @@
+class BrokenFile
`,
},
],
status: 'completed',
};

const updateEvent = await createFileChangeUpdate(fileChange);
expect(updateEvent).toMatchObject({
content: [],
});
});

it('should parse update diffs with move metadata appended', async () => {
mockFileContent('/test/project/OriginalFile.kt', 'old code line\n');

const fileChange: ThreadItem = {
type: 'fileChange',
id: 'file-change-move-metadata',
changes: [
{
path: '/test/project/OriginalFile.kt',
kind: {
type: 'update',
move_path: '/test/project/NewFile.kt',
},
diff:
`@@ -1 +1 @@
-old code line
+new code line


Moved to: /test/project/NewFile.kt`,
},
],
status: 'inProgress',
};

const updateEvent = await createFileChangeUpdate(fileChange);
expect(updateEvent).toMatchObject({
content: [
{
oldText: 'old code line\n',
newText: 'new code line\n',
},
],
});
});
});
Loading