Skip to content
TypeScript

fix(acp): file changes show the patch of every diff in a tool call (#28631)

Abhishek B Rddd36a53a59f
diff --git a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts
index 3efa9047d58..7234c561b91 100644
--- a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts
+++ b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts
@@ -342,6 +342,71 @@ describe("acpToolCallDiffPatch", () => {
"",
].join("\n"),
);
+ assert.equal(
+ acpToolCallDiffPatch([
+ {
+ type: "diff",
+ changes: [{ operation: "modify", path: "/repo/a.ts" }],
+ patch: {
+ format: "git_patch",
+ text: "diff --git a/repo/a.ts b/repo/a.ts\n@@ -1 +1 @@\n-a\n+A\n",
+ },
+ },
+ {
+ type: "diff",
+ changes: [{ operation: "modify", path: "/repo/b.ts" }],
+ patch: {
+ format: "git_patch",
+ text: "diff --git a/repo/b.ts b/repo/b.ts\n@@ -1 +1 @@\n-b\n+B\n",
+ },
+ },
+ { type: "diff", path: "/repo/c.ts", oldText: "c\n", newText: "C\n" },
+ ]),
+ [
+ "diff --git a/repo/a.ts b/repo/a.ts",
+ "@@ -1 +1 @@",
+ "-a",
+ "+A",
+ "",
+ "diff --git a/repo/b.ts b/repo/b.ts",
+ "@@ -1 +1 @@",
+ "-b",
+ "+B",
+ "",
+ 'diff --git "a//repo/c.ts" "b//repo/c.ts"',
+ "--- /repo/c.ts",
+ "+++ /repo/c.ts",
+ "@@ -1,1 +1,1 @@",
+ "-c",
+ "+C",
+ "",
+ ].join("\n"),
+ );
+ assert.equal(
+ acpToolCallDiffPatch([
+ { type: "diff", path: "/repo/new.txt", oldText: null, newText: "hello\n" },
+ {
+ type: "diff",
+ changes: [{ operation: "modify", path: "/repo/a.ts" }],
+ patch: { format: "git_patch", text: "@@ -1 +1 @@\n-a\n+A\n" },
+ },
+ ]),
+ [
+ 'diff --git "a//repo/new.txt" "b//repo/new.txt"',
+ "--- /dev/null",
+ "+++ /repo/new.txt",
+ "@@ -0,0 +1,1 @@",
+ "+hello",
+ "",
+ 'diff --git "a//repo/a.ts" "b//repo/a.ts"',
+ '--- "a//repo/a.ts"',
+ '+++ "b//repo/a.ts"',
+ "@@ -1 +1 @@",
+ "-a",
+ "+A",
+ "",
+ ].join("\n"),
+ );
});
});
diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts
index ad0b716f37c..622bc63c933 100644
--- a/packages/provider-acp/src/server/adapter.ts
+++ b/packages/provider-acp/src/server/adapter.ts
@@ -1095,7 +1095,7 @@ export function acpToolCallDiffPatch(content: unknown): string | undefined {
const diff = unknownRecord(entry);
return diff?.type === "diff" ? [diff] : [];
});
- for (const diff of diffs) {
+ const patches = diffs.flatMap((diff): ReadonlyArray<{ text: string; path?: string }> => {
const patch = unknownRecord(diff.patch);
if (typeof patch?.text === "string") {
const change =
@@ -1111,15 +1111,17 @@ export function acpToolCallDiffPatch(content: unknown): string | undefined {
const before = change.operation === "add" ? "/dev/null" : JSON.stringify(`a/${oldPath}`);
const after =
change.operation === "delete" ? "/dev/null" : JSON.stringify(`b/${change.path}`);
- return `diff --git ${JSON.stringify(`a/${oldPath}`)} ${JSON.stringify(`b/${change.path}`)}\n--- ${before}\n+++ ${after}\n${patch.text.trimStart()}`;
+ return [
+ {
+ text: `diff --git ${JSON.stringify(`a/${oldPath}`)} ${JSON.stringify(`b/${change.path}`)}\n--- ${before}\n+++ ${after}\n${patch.text.trimStart()}`,
+ },
+ ];
}
- return patch.text;
+ return [{ text: patch.text }];
}
- }
- const v1Patches = diffs.flatMap((diff) => {
if (typeof diff.path !== "string" || typeof diff.newText !== "string") return [];
const oldText = typeof diff.oldText === "string" ? diff.oldText : undefined;
- const patch = structuredPatch(
+ const structured = structuredPatch(
oldText === undefined ? "/dev/null" : diff.path,
diff.path,
oldText ?? "",
@@ -1131,9 +1133,21 @@ export function acpToolCallDiffPatch(content: unknown): string | undefined {
maxEditLength: oldText && diff.newText ? ACP_V1_DIFF_MAX_EDITS : Number.POSITIVE_INFINITY,
},
);
- return patch === undefined || patch.hunks.length === 0 ? [] : [patch];
+ return structured === undefined || structured.hunks.length === 0
+ ? []
+ : [{ text: formatPatch(structured, FILE_HEADERS_ONLY), path: diff.path }];
});
- return v1Patches.length === 0 ? undefined : formatPatch(v1Patches, FILE_HEADERS_ONLY);
+ // Diff parsers split at Git headers once one exists, so every section then needs its own.
+ const hasGitHeader = patches.some(({ text }) => /^diff --git /m.test(text));
+ return patches.length === 0
+ ? undefined
+ : patches
+ .map(({ text, path }) =>
+ hasGitHeader && path !== undefined
+ ? `diff --git ${JSON.stringify(`a/${path}`)} ${JSON.stringify(`b/${path}`)}\n${text}`
+ : text,
+ )
+ .join("\n");
}
function pathFromToolCall(toolCall: AcpToolCallState): string | undefined {