Skip to content

Commit 3c9ef09

Browse files
committed
🤖 fix: prefer ui_only diffs for compaction
Ensure edited file diff extraction prefers ui_only.file_edit.diff when present. --- _Generated with `mux` • Model: `openai:gpt-5.2-codex` • Thinking: `xhigh` • Cost: `8.50`_
1 parent f8fe0e6 commit 3c9ef09

2 files changed

Lines changed: 43 additions & 3 deletions

File tree

‎src/common/utils/messages/extractEditedFiles.test.ts‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, expect, it } from "bun:test";
22
import { createPatch } from "diff";
3+
import { FILE_EDIT_DIFF_OMITTED_MESSAGE } from "@/common/types/tools";
34
import type { MuxMessage } from "@/common/types/message";
45
import { extractEditedFileDiffs, extractEditedFilePaths } from "./extractEditedFiles";
56

@@ -11,6 +12,7 @@ function createAssistantMessage(
1112
toolName: string;
1213
filePath: string;
1314
diff: string;
15+
uiOnlyDiff?: string;
1416
success?: boolean;
1517
}>
1618
): MuxMessage {
@@ -23,7 +25,19 @@ function createAssistantMessage(
2325
toolName: tc.toolName,
2426
state: "output-available" as const,
2527
input: { file_path: tc.filePath },
26-
output: { success: tc.success ?? true, diff: tc.diff },
28+
output: {
29+
success: tc.success ?? true,
30+
diff: tc.diff,
31+
...(tc.uiOnlyDiff
32+
? {
33+
ui_only: {
34+
file_edit: {
35+
diff: tc.uiOnlyDiff,
36+
},
37+
},
38+
}
39+
: {}),
40+
},
2741
})),
2842
};
2943
}
@@ -129,6 +143,27 @@ describe("extractEditedFileDiffs", () => {
129143
expect(result[0].diff).toBe(diff);
130144
});
131145

146+
it("should prefer ui_only diffs when present", () => {
147+
const originalContent = "line1\nline2\nline3";
148+
const newContent = "line1\nmodified\nline3";
149+
const diff = makeDiff("/path/to/file.ts", originalContent, newContent);
150+
151+
const messages: MuxMessage[] = [
152+
createAssistantMessage([
153+
{
154+
toolName: "file_edit_replace_string",
155+
filePath: "/path/to/file.ts",
156+
diff: FILE_EDIT_DIFF_OMITTED_MESSAGE,
157+
uiOnlyDiff: diff,
158+
},
159+
]),
160+
];
161+
162+
const result = extractEditedFileDiffs(messages);
163+
expect(result).toHaveLength(1);
164+
expect(result[0].diff).toBe(diff);
165+
});
166+
132167
it("should combine multiple non-overlapping diffs for the same file", () => {
133168
// Edit 1: change line 2
134169
const original = "line1\nline2\nline3\nline4\nline5";

‎src/common/utils/messages/extractEditedFiles.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { MuxMessage } from "@/common/types/message";
2+
import { getToolOutputUiOnly } from "@/common/utils/tools/toolOutputUiOnly";
23
import { FILE_EDIT_TOOL_NAMES } from "@/common/types/tools";
34
import { MAX_EDITED_FILES, MAX_FILE_CONTENT_SIZE } from "@/common/constants/attachments";
45
import { applyPatch, createPatch, parsePatch } from "diff";
@@ -198,7 +199,11 @@ export function extractEditedFileDiffs(messages: MuxMessage[]): FileEditDiff[] {
198199
if (part.state !== "output-available") continue;
199200

200201
const output = part.output as FileEditToolOutput | undefined;
201-
if (!output?.success || !output.diff) continue;
202+
if (!output?.success) continue;
203+
204+
const uiOnly = getToolOutputUiOnly(output);
205+
const diff = uiOnly?.file_edit?.diff ?? output.diff;
206+
if (!diff) continue;
202207

203208
const input = part.input as FileEditToolInput | undefined;
204209
const filePath = input?.file_path;
@@ -208,7 +213,7 @@ export function extractEditedFileDiffs(messages: MuxMessage[]): FileEditDiff[] {
208213
if (!diffsByPath.has(filePath)) {
209214
diffsByPath.set(filePath, []);
210215
}
211-
diffsByPath.get(filePath)!.push(output.diff);
216+
diffsByPath.get(filePath)!.push(diff);
212217

213218
// Update edit order (move to end if already exists)
214219
const idx = editOrder.indexOf(filePath);

0 commit comments

Comments
 (0)