GeezerVison is showing github.com — fix(codex): Open diff shows every file of a multi-file patch (#40874) · AIdoesmyjob/t3code@f0f29d6
File treeExpand file treeCollapse file tree Open diff view settings apps/server/src/orchestration-v2/Adapters
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts +22Lines changed: 22 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12710,6 +12710,28 @@ describe("CodexAdapterV2 post-settle continuation", () => { | 12710 | 12710 | { operation: "delete", path: "/w/e.ts" }, | 12711 | 12711 | { operation: "add", path: "/w/a.ts" }, | 12712 | 12712 | ]); | | 12713 | + assert.equal( | | 12714 | + fileChanges[0]?.diffStr, | | 12715 | + [ | | 12716 | + 'diff --git "a//w/c.ts" "b//w/d.ts"', | | 12717 | + '--- "a//w/c.ts"', | | 12718 | + '+++ "b//w/d.ts"', | | 12719 | + "@@", | | 12720 | + 'diff --git "a//w/e.ts" "b//w/e.ts"', | | 12721 | + '--- "a//w/e.ts"', | | 12722 | + "+++ /dev/null", | | 12723 | + "@@ -1,1 +0,0 @@", | | 12724 | + "-gone", | | 12725 | + "\\ No newline at end of file", | | 12726 | + 'diff --git "a//w/a.ts" "b//w/a.ts"', | | 12727 | + "--- /dev/null", | | 12728 | + '+++ "b//w/a.ts"', | | 12729 | + "@@ -0,0 +1,1 @@", | | 12730 | + "+export {};", | | 12731 | + "\\ No newline at end of file", | | 12732 | + "", | | 12733 | + ].join("\n"), | | 12734 | + ); | 12713 | 12735 | }).pipe( | 12714 | 12736 | Effect.scoped, | 12715 | 12737 | Effect.provide( | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts +31-1Lines changed: 31 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -174,6 +174,35 @@ type CodexFileChangeKinds = Readonly< | 174 | 174 | Record<string, CodexSchema.V2ItemStartedNotification__PatchChangeKind> | 175 | 175 | >; | 176 | 176 |
| | 177 | +// Diff parsers split at Git headers once one exists, so each file of a multi-file patch needs its own. | | 178 | +function codexMultiFilePatch( | | 179 | + changes: ReadonlyArray<CodexSchema.V2ItemCompletedNotification__FileUpdateChange>, | | 180 | +): string { | | 181 | + return changes | | 182 | + .map(({ path, kind, diff }) => { | | 183 | + const oldPath = path; | | 184 | + const newPath = (kind.type === "update" && kind.move_path) || path; | | 185 | + const git = `diff --git ${JSON.stringify(`a/${oldPath}`)} ${JSON.stringify(`b/${newPath}`)}\n`; | | 186 | + if (kind.type === "update" && diff.startsWith("diff --git ")) return diff; | | 187 | + if (kind.type === "update" && diff.startsWith("--- ")) return `${git}${diff}`; | | 188 | + let hunks = diff; | | 189 | + // Codex records added and deleted files as their raw contents. | | 190 | + if (kind.type !== "update") { | | 191 | + const lines = diff.split("\n"); | | 192 | + const hasFinalNewline = diff.endsWith("\n"); | | 193 | + if (hasFinalNewline) lines.pop(); | | 194 | + const added = kind.type === "add"; | | 195 | + const range = `1,${lines.length}`; | | 196 | + hunks = `@@ -${added ? "0,0" : range} +${added ? range : "0,0"} @@\n${lines.map((line) => `${added ? "+" : "-"}${line}`).join("\n")}\n${hasFinalNewline ? "" : "\\ No newline at end of file\n"}`; | | 197 | + } | | 198 | + const before = kind.type === "add" ? "/dev/null" : JSON.stringify(`a/${oldPath}`); | | 199 | + const after = kind.type === "delete" ? "/dev/null" : JSON.stringify(`b/${newPath}`); | | 200 | + return `${git}--- ${before}\n+++ ${after}\n${hunks}`; | | 201 | + }) | | 202 | + .map((section) => (section.endsWith("\n") ? section : `${section}\n`)) | | 203 | + .join(""); | | 204 | +} | | 205 | + | 177 | 206 | function codexFileChangeApprovalPrompt(input: { | 178 | 207 | readonly reason?: string | null; | 179 | 208 | readonly grantRoot?: string | null; | @@ -4249,7 +4278,8 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( | 4249 | 4278 | updatedAt, | 4250 | 4279 | type: "file_change", | 4251 | 4280 | fileName: firstChange.path, | 4252 | | - diffStr: firstChange.diff, | | 4281 | + diffStr: | | 4282 | + item.changes.length > 1 ? codexMultiFilePatch(item.changes) : firstChange.diff, | 4253 | 4283 | changes: item.changes.map((change) => { | 4254 | 4284 | const movePath = change.kind.type === "update" ? change.kind.move_path : undefined; | 4255 | 4285 | return movePath | |
|