Skip to content

Commit de2ebcf

Browse files
committed
fix: preserve explicit ACP patches and empty tool call contents
1 parent 7b33a7b commit de2ebcf

4 files changed

Lines changed: 57 additions & 4 deletions

File tree

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,25 @@ const ACP_TEST_DRIVER = ProviderDriverKind.make("acp-test");
111111
const decodeUnknownJson = Schema.decodeUnknownOption(Schema.fromJsonString(Schema.Unknown));
112112

113113
describe("acpToolCallDiffPatch", () => {
114-
it("builds a patch per file from ACP v1 oldText/newText, with /dev/null for a new file", () => {
114+
it("builds per-file patches from ACP hunks and recorded old/new text", () => {
115+
for (const operation of ["add", "delete"]) {
116+
const text = operation === "add" ? "@@ -0,0 +1 @@\n+content\n" : "@@ -1 +0,0 @@\n-content\n";
117+
assert.equal(
118+
acpToolCallDiffPatch([
119+
{
120+
type: "diff",
121+
changes: [{ operation, path: "/repo/a.ts" }],
122+
patch: { format: "git_patch", text },
123+
},
124+
]),
125+
[
126+
'diff --git "a//repo/a.ts" "b//repo/a.ts"',
127+
operation === "add" ? "--- /dev/null" : '--- "a//repo/a.ts"',
128+
operation === "delete" ? "+++ /dev/null" : '+++ "b//repo/a.ts"',
129+
text,
130+
].join("\n"),
131+
);
132+
}
115133
assert.equal(
116134
acpToolCallDiffPatch([
117135
{ type: "diff", path: "/repo/new.txt", oldText: null, newText: "hello\n" },

‎apps/web/src/lib/toolCallDiff.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ describe("toolCallDiff", () => {
2727
for (const operation of ["add", "delete"]) {
2828
for (const [content, ending] of [
2929
["first\nsecond", ""],
30+
["@@ -0,0 +1 @@\n+content", "\n"],
31+
["@@ -1 +0,0 @@\n-content", "\n"],
3032
["first\nsecond", "\n"],
3133
["--- notes\nsecond", "\n"],
3234
["diff --git notes\nsecond", "\n"],
@@ -59,6 +61,20 @@ describe("toolCallDiff", () => {
5961
});
6062

6163
it("keeps content-only writes out of the patch and preserves empty recorded content", () => {
64+
for (const operation of ["add", "delete"]) {
65+
expect(
66+
toolCallDiff({
67+
fileName: "empty.ts",
68+
diffStr: "",
69+
changes: [{ operation, path: "empty.ts" }],
70+
}).content,
71+
).toEqual([
72+
{
73+
label: operation === "add" ? "Recorded content after" : "Recorded content before",
74+
text: "",
75+
},
76+
]);
77+
}
6278
expect(toolCallDiff({ fileName: "a.ts", oldStr: "before", newStr: "" })).toEqual({
6379
patch: undefined,
6480
content: [

‎apps/web/src/lib/toolCallDiff.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ export function toolCallDiff(
6060
item: Pick<FileChange, "diffStr" | "fileName" | "changes" | "oldStr" | "newStr">,
6161
) {
6262
const diff = recordedText(item.diffStr);
63-
const before = recordedText(item.oldStr);
64-
const after = recordedText(item.newStr);
63+
const operation = item.changes?.[0]?.operation;
64+
const emptyContent = diff.text === "" ? "" : undefined;
65+
const before = recordedText(item.oldStr ?? (operation === "delete" ? emptyContent : undefined));
66+
const after = recordedText(item.newStr ?? (operation === "add" ? emptyContent : undefined));
6567
return {
6668
patch: toolCallPatch({ ...item, ...(diff.text === undefined ? {} : { diffStr: diff.text }) }),
6769
content: diff.text?.trim()

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,24 @@ export function acpToolCallDiffPatch(content: unknown): string | undefined {
838838
});
839839
for (const diff of diffs) {
840840
const patch = unknownRecord(diff.patch);
841-
if (typeof patch?.text === "string") return patch.text;
841+
if (typeof patch?.text === "string") {
842+
const change =
843+
Array.isArray(diff.changes) && diff.changes.length === 1
844+
? unknownRecord(diff.changes[0])
845+
: undefined;
846+
if (
847+
patch.format === "git_patch" &&
848+
patch.text.trimStart().startsWith("@@ ") &&
849+
typeof change?.path === "string"
850+
) {
851+
const oldPath = typeof change.oldPath === "string" ? change.oldPath : change.path;
852+
const before = change.operation === "add" ? "/dev/null" : JSON.stringify(`a/${oldPath}`);
853+
const after =
854+
change.operation === "delete" ? "/dev/null" : JSON.stringify(`b/${change.path}`);
855+
return `diff --git ${JSON.stringify(`a/${oldPath}`)} ${JSON.stringify(`b/${change.path}`)}\n--- ${before}\n+++ ${after}\n${patch.text.trimStart()}`;
856+
}
857+
return patch.text;
858+
}
842859
}
843860
const v1Patches = diffs.flatMap((diff) => {
844861
if (typeof diff.path !== "string" || typeof diff.newText !== "string") return [];

0 commit comments

Comments
 (0)