Skip to content

Commit 6e221ea

Browse files
committed
fix(acp): old-style diffs keep their own file next to Git patches
The client diff parser splits only at `diff --git` headers once the patch contains one. An oldText/newText section after a Git patch had its hunk attached to that file, and one before a Git patch was dropped. Give those sections a Git header when another section has one. Output with no Git patch is unchanged.
1 parent 56b5838 commit 6e221ea

2 files changed

Lines changed: 43 additions & 5 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ describe("acpToolCallDiffPatch", () => {
373373
"-b",
374374
"+B",
375375
"",
376+
'diff --git "a//repo/c.ts" "b//repo/c.ts"',
376377
"--- /repo/c.ts",
377378
"+++ /repo/c.ts",
378379
"@@ -1,1 +1,1 @@",
@@ -381,6 +382,31 @@ describe("acpToolCallDiffPatch", () => {
381382
"",
382383
].join("\n"),
383384
);
385+
assert.equal(
386+
acpToolCallDiffPatch([
387+
{ type: "diff", path: "/repo/new.txt", oldText: null, newText: "hello\n" },
388+
{
389+
type: "diff",
390+
changes: [{ operation: "modify", path: "/repo/a.ts" }],
391+
patch: { format: "git_patch", text: "@@ -1 +1 @@\n-a\n+A\n" },
392+
},
393+
]),
394+
[
395+
'diff --git "a//repo/new.txt" "b//repo/new.txt"',
396+
"--- /dev/null",
397+
"+++ /repo/new.txt",
398+
"@@ -0,0 +1,1 @@",
399+
"+hello",
400+
"",
401+
'diff --git "a//repo/a.ts" "b//repo/a.ts"',
402+
'--- "a//repo/a.ts"',
403+
'+++ "b//repo/a.ts"',
404+
"@@ -1 +1 @@",
405+
"-a",
406+
"+A",
407+
"",
408+
].join("\n"),
409+
);
384410
});
385411
});
386412

‎packages/provider-acp/src/server/adapter.ts‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,7 +1095,7 @@ export function acpToolCallDiffPatch(content: unknown): string | undefined {
10951095
const diff = unknownRecord(entry);
10961096
return diff?.type === "diff" ? [diff] : [];
10971097
});
1098-
const patches = diffs.flatMap((diff) => {
1098+
const patches = diffs.flatMap((diff): ReadonlyArray<{ text: string; path?: string }> => {
10991099
const patch = unknownRecord(diff.patch);
11001100
if (typeof patch?.text === "string") {
11011101
const change =
@@ -1112,10 +1112,12 @@ export function acpToolCallDiffPatch(content: unknown): string | undefined {
11121112
const after =
11131113
change.operation === "delete" ? "/dev/null" : JSON.stringify(`b/${change.path}`);
11141114
return [
1115-
`diff --git ${JSON.stringify(`a/${oldPath}`)} ${JSON.stringify(`b/${change.path}`)}\n--- ${before}\n+++ ${after}\n${patch.text.trimStart()}`,
1115+
{
1116+
text: `diff --git ${JSON.stringify(`a/${oldPath}`)} ${JSON.stringify(`b/${change.path}`)}\n--- ${before}\n+++ ${after}\n${patch.text.trimStart()}`,
1117+
},
11161118
];
11171119
}
1118-
return [patch.text];
1120+
return [{ text: patch.text }];
11191121
}
11201122
if (typeof diff.path !== "string" || typeof diff.newText !== "string") return [];
11211123
const oldText = typeof diff.oldText === "string" ? diff.oldText : undefined;
@@ -1133,9 +1135,19 @@ export function acpToolCallDiffPatch(content: unknown): string | undefined {
11331135
);
11341136
return structured === undefined || structured.hunks.length === 0
11351137
? []
1136-
: [formatPatch(structured, FILE_HEADERS_ONLY)];
1138+
: [{ text: formatPatch(structured, FILE_HEADERS_ONLY), path: diff.path }];
11371139
});
1138-
return patches.length === 0 ? undefined : patches.join("\n");
1140+
// Diff parsers split at Git headers once one exists, so every section then needs its own.
1141+
const hasGitHeader = patches.some(({ text }) => /^diff --git /m.test(text));
1142+
return patches.length === 0
1143+
? undefined
1144+
: patches
1145+
.map(({ text, path }) =>
1146+
hasGitHeader && path !== undefined
1147+
? `diff --git ${JSON.stringify(`a/${path}`)} ${JSON.stringify(`b/${path}`)}\n${text}`
1148+
: text,
1149+
)
1150+
.join("\n");
11391151
}
11401152

11411153
function pathFromToolCall(toolCall: AcpToolCallState): string | undefined {

0 commit comments

Comments
 (0)