-
Notifications
You must be signed in to change notification settings - Fork 8
Suppress diff exceptions #167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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"; | ||
|
|
@@ -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 { | ||
| 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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| _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: .*$/, ""); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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--- "); | ||
| } | ||
|
|
||


There was a problem hiding this comment.
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#L302There was a problem hiding this comment.
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.