-
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
Merged
+186
−107
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
de91f29
clean: introduce exhaustive switch into `createPatchContent` function
AlexandrSuhinin 58d93e4
fix: suppress exceptions caused by broken unified diffs from codex
AlexandrSuhinin a639237
fix: recover corrupted unified diff content
AlexandrSuhinin 482c59b
fix: recover moved fileUpdate diff from new file
AlexandrSuhinin d0832b0
clean: remove recovery from unified diff in add/delete file changes
AlexandrSuhinin 49f7b28
fix: improve matching for corrupted diff
AlexandrSuhinin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import type { ToolCallContent } from "@agentclientprotocol/sdk"; | ||
| import { applyPatch, parsePatch } from "diff"; | ||
| import { applyPatch, parsePatch, reversePatch } from "diff"; | ||
| import { readFile } from "node:fs/promises"; | ||
| import path from "node:path"; | ||
| import type { UpdateSessionEvent } from "./ACPSessionConnection"; | ||
|
|
@@ -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,93 +258,98 @@ 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> { | ||
| return { | ||
| type: "diff", | ||
| oldText: change.kind.type === "add" ? null : oldContent, | ||
| newText: newContent, | ||
| oldText: null, | ||
| newText: change.diff, // app-server always returns file content instead of diff | ||
| path: change.path, | ||
| _meta: { | ||
| kind: change.kind.type, | ||
| kind: "add", | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| function isUnifiedDiff(content: string): boolean { | ||
| return content.startsWith("--- ") || content.includes("\n--- "); | ||
| async function createUpdateFileContent(change: FileUpdateChange): Promise<ToolCallContent | null> { | ||
| if (change.kind.type !== "update") return null; | ||
|
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. We don't have similar guards in other execution branches. Let's drop it? |
||
|
|
||
| const unifiedDiff = recoverCorruptedDiff(change.diff); | ||
| const movePath = change.kind.move_path; | ||
|
|
||
| const oldContent = await readFileContent(change.path); | ||
| if (oldContent !== null) { | ||
| const patchedContent = applyPatch(oldContent, unifiedDiff); | ||
| if (patchedContent === false) return null; | ||
| return createUpdateDiffContent(movePath ?? change.path, oldContent, patchedContent); | ||
| } | ||
|
|
||
| if (!movePath) return null; | ||
| const newContent = await readFileContent(movePath); | ||
| if (newContent === null) return null; | ||
|
|
||
| const revertedPatch = revertPatch(unifiedDiff); | ||
| if (!revertedPatch) return null; | ||
|
|
||
| const revertedContent = applyPatch(newContent, revertedPatch); | ||
| if (revertedContent === false) return null; | ||
|
|
||
| return createUpdateDiffContent(movePath, revertedContent, newContent); | ||
| } | ||
|
|
||
| /** | ||
| * Recreates the content of a deleted file from the unified diff. | ||
| * @param unifiedDiff The unified diff of the file deletion patch | ||
| */ | ||
| function patchToDeletedContent(unifiedDiff: string): string | null { | ||
| try { | ||
| const [patch] = parsePatch(unifiedDiff); | ||
| if (!patch || patch.hunks.length === 0) { | ||
| return null; | ||
| } | ||
| function revertPatch(unifiedDiff: string) { | ||
| const [patch] = parsePatch(unifiedDiff); | ||
| if (!patch) return null; | ||
|
|
||
| const oldLines: string[] = []; | ||
| let hasNoTrailingNewlineMarker = false; | ||
|
|
||
| for (const hunk of patch.hunks) { | ||
| for (const line of hunk.lines) { | ||
| if (line === "\\ No newline at end of file") { | ||
| hasNoTrailingNewlineMarker = true; | ||
| continue; | ||
| } | ||
| if (line.startsWith("-") || line.startsWith(" ")) { | ||
| oldLines.push(line.slice(1)); | ||
| } | ||
| } | ||
| } | ||
| return reversePatch(patch); | ||
| } | ||
|
|
||
| if (oldLines.length === 0) { | ||
| return ""; | ||
| } | ||
| function createUpdateDiffContent(path: string, oldText: string, newText: string): ToolCallContent { | ||
| return { | ||
| type: "diff", | ||
| oldText, | ||
| newText, | ||
| path, | ||
| _meta: { | ||
| kind: "update", | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| const oldText = oldLines.join("\n"); | ||
| return hasNoTrailingNewlineMarker || !unifiedDiff.endsWith("\n") ? oldText : `${oldText}\n`; | ||
| } catch { | ||
| return null; | ||
| async function createDeleteFileContent(change: FileUpdateChange): Promise<ToolCallContent> { | ||
| return { | ||
| type: "diff", | ||
| oldText: change.diff, // app-server always returns file content instead of diff | ||
| newText: "", | ||
| path: change.path, | ||
| _meta: { | ||
| kind: "delete", | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async function readFileContent(filePath: string): Promise<string | null> { | ||
| return await readFile(filePath, { encoding: "utf8" }).catch(() => null); | ||
| } | ||
|
|
||
| /** | ||
| * Fix unified diff content corrupted by codex agent. | ||
| * Removes synthetic "Moved to" from the end. | ||
| */ | ||
| function recoverCorruptedDiff(diff: string): string { | ||
| return diff.replace(/\n\nMoved to: .*$/, ""); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.