Skip to content

Commit 829bb03

Browse files
committed
fix(claude): Open diff shows Claude edits as a diff instead of raw JSON
Since #26304, Open diff opens the patch that a tool call recorded. The Claude adapter stored Claude's raw Edit and Write result there, so the panel showed JSON. The adapter now formats Claude's structuredPatch hunks as a unified patch.
1 parent 5211acf commit 829bb03

4 files changed

Lines changed: 192 additions & 1 deletion

File tree

‎apps/server/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"@t3tools/source-control-github": "workspace:*",
5959
"@t3tools/source-control-gitlab": "workspace:*",
6060
"character-entities": "2.0.2",
61+
"diff": "8.0.3",
6162
"effect": "catalog:",
6263
"jose": "catalog:",
6364
"node-pty": "^1.2.0-beta.15",

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

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8663,6 +8663,152 @@ describe("ClaudeAdapterV2 background wake turns", () => {
86638663
),
86648664
);
86658665

8666+
it.effect.each([
8667+
{
8668+
case: "an Edit",
8669+
toolName: "Edit",
8670+
toolInput: {
8671+
file_path: "/workspace/src/math.ts",
8672+
old_string: " return a - b;",
8673+
new_string: " return a + b;",
8674+
},
8675+
toolUseResult: {
8676+
filePath: "/workspace/src/math.ts",
8677+
oldString: " return a - b;",
8678+
newString: " return a + b;",
8679+
originalFile: "export function add(a: number, b: number) {\n return a - b;\n}\n",
8680+
structuredPatch: [
8681+
{
8682+
oldStart: 1,
8683+
oldLines: 3,
8684+
newStart: 1,
8685+
newLines: 3,
8686+
lines: [
8687+
" export function add(a: number, b: number) {",
8688+
"- return a - b;",
8689+
"+ return a + b;",
8690+
" }",
8691+
],
8692+
},
8693+
],
8694+
userModified: false,
8695+
replaceAll: false,
8696+
},
8697+
diffStr:
8698+
"--- /workspace/src/math.ts\n+++ /workspace/src/math.ts\n@@ -1,3 +1,3 @@\n export function add(a: number, b: number) {\n- return a - b;\n+ return a + b;\n }\n",
8699+
},
8700+
{
8701+
case: "a Write that creates a file",
8702+
toolName: "Write",
8703+
toolInput: { file_path: "/workspace/notes.md", content: "# Notes\n\nShip it.\n" },
8704+
toolUseResult: {
8705+
type: "create",
8706+
filePath: "/workspace/notes.md",
8707+
content: "# Notes\n\nShip it.\n",
8708+
structuredPatch: [],
8709+
originalFile: null,
8710+
userModified: false,
8711+
},
8712+
diffStr: "--- /dev/null\n+++ /workspace/notes.md\n@@ -0,0 +1,3 @@\n+# Notes\n+\n+Ship it.\n",
8713+
},
8714+
{
8715+
case: "a Write that creates an empty file",
8716+
toolName: "Write",
8717+
toolInput: { file_path: "/workspace/.keep", content: "" },
8718+
toolUseResult: {
8719+
type: "create",
8720+
filePath: "/workspace/.keep",
8721+
content: "",
8722+
structuredPatch: [],
8723+
originalFile: null,
8724+
userModified: false,
8725+
},
8726+
diffStr: undefined,
8727+
},
8728+
])("stores the diff panel patch for $case", (testCase) =>
8729+
Effect.scoped(
8730+
Effect.gen(function* () {
8731+
const harness = yield* makeWakeHarness;
8732+
const toolUseId = `toolu_01${testCase.toolName}Patch`;
8733+
8734+
yield* harness.runtime.startTurn(
8735+
makeClaudeTestTurnInput({
8736+
threadId: harness.threadId,
8737+
providerThread: harness.providerThread,
8738+
now: yield* DateTime.now,
8739+
attemptId: RunAttemptId.make(`attempt-claude-${testCase.toolName}-patch`),
8740+
text: "Change it.",
8741+
attachments: [],
8742+
}),
8743+
);
8744+
yield* Queue.offer(
8745+
harness.sdkMessages,
8746+
claudeSdkFrame({
8747+
type: "assistant",
8748+
message: {
8749+
model: "claude-sonnet-4-6",
8750+
id: `msg_${testCase.toolName}_patch`,
8751+
type: "message",
8752+
role: "assistant",
8753+
content: [
8754+
{
8755+
type: "tool_use",
8756+
id: toolUseId,
8757+
name: testCase.toolName,
8758+
input: testCase.toolInput,
8759+
},
8760+
],
8761+
},
8762+
parent_tool_use_id: null,
8763+
uuid: "00000000-0000-4000-8000-000000000793",
8764+
session_id: WAKE_NATIVE_SESSION,
8765+
}),
8766+
);
8767+
yield* Queue.offer(
8768+
harness.sdkMessages,
8769+
claudeSdkFrame({
8770+
type: "user",
8771+
message: {
8772+
role: "user",
8773+
content: [
8774+
{
8775+
type: "tool_result",
8776+
tool_use_id: toolUseId,
8777+
content: `The file ${testCase.toolInput.file_path} has been updated successfully.`,
8778+
},
8779+
],
8780+
},
8781+
parent_tool_use_id: null,
8782+
uuid: "00000000-0000-4000-8000-000000000794",
8783+
session_id: WAKE_NATIVE_SESSION,
8784+
tool_use_result: testCase.toolUseResult,
8785+
}),
8786+
);
8787+
yield* Queue.offer(
8788+
harness.sdkMessages,
8789+
makeResultFrame({ uuid: "00000000-0000-4000-8000-000000000795", result: "Done." }),
8790+
);
8791+
yield* harness.awaitTerminal(1);
8792+
8793+
const fileChange = harness.events.findLast(
8794+
(event) =>
8795+
event.type === "turn_item.updated" &&
8796+
event.turnItem.nativeItemRef?.nativeId === toolUseId,
8797+
);
8798+
assert.deepEqual(
8799+
fileChange?.type === "turn_item.updated" && fileChange.turnItem.type === "file_change"
8800+
? { diffStr: fileChange.turnItem.diffStr }
8801+
: undefined,
8802+
{ diffStr: testCase.diffStr },
8803+
);
8804+
}).pipe(
8805+
Effect.provide(
8806+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
8807+
),
8808+
),
8809+
),
8810+
);
8811+
86668812
it.effect("answers an approval a held wake turn raises without waiting for the echo", () =>
86678813
Effect.scoped(
86688814
Effect.gen(function* () {

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ import {
104104
resolveIdleCompaction,
105105
} from "@t3tools/contracts";
106106

107+
import { FILE_HEADERS_ONLY, formatPatch, structuredPatch } from "diff";
107108
import * as Cause from "effect/Cause";
108109
import * as Clock from "effect/Clock";
109110
import * as Context from "effect/Context";
@@ -2941,6 +2942,44 @@ function claudeNativeToolOutputText(output: ClaudeNativeToolOutput): string {
29412942
return typeof value === "string" ? value : value === undefined ? "" : jsonStringifyForTool(value);
29422943
}
29432944

2945+
const decodeClaudeFileEditResult = Schema.decodeUnknownOption(
2946+
Schema.Struct({
2947+
type: Schema.optional(Schema.String),
2948+
filePath: Schema.String,
2949+
content: Schema.optional(Schema.String),
2950+
structuredPatch: Schema.mutable(
2951+
Schema.Array(
2952+
Schema.Struct({
2953+
oldStart: Schema.Number,
2954+
oldLines: Schema.Number,
2955+
newStart: Schema.Number,
2956+
newLines: Schema.Number,
2957+
lines: Schema.mutable(Schema.Array(Schema.String)),
2958+
}),
2959+
),
2960+
),
2961+
}),
2962+
);
2963+
2964+
function claudeFileChangeText(output: ClaudeNativeToolOutput): string {
2965+
const result =
2966+
output.type === "structured_tool_use_result"
2967+
? Option.getOrUndefined(decodeClaudeFileEditResult(output.value))
2968+
: undefined;
2969+
if (result === undefined) return claudeNativeToolOutputText(output);
2970+
const patch =
2971+
result.structuredPatch.length === 0 && result.type === "create" && result.content !== undefined
2972+
? structuredPatch("/dev/null", result.filePath, "", result.content)
2973+
: {
2974+
oldFileName: result.filePath,
2975+
newFileName: result.filePath,
2976+
oldHeader: undefined,
2977+
newHeader: undefined,
2978+
hunks: result.structuredPatch,
2979+
};
2980+
return patch.hunks.length === 0 ? "" : formatPatch(patch, FILE_HEADERS_ONLY);
2981+
}
2982+
29442983
function claudeCommandOutputText(output: ClaudeNativeToolOutput): string {
29452984
const value = claudeNativeToolOutputValue(output);
29462985
if (typeof value === "object" && value !== null) {
@@ -5473,7 +5512,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
54735512
const outputText =
54745513
itemType === "command_execution"
54755514
? claudeCommandOutputText(input.output)
5476-
: claudeNativeToolOutputText(input.output);
5515+
: itemType === "file_change"
5516+
? claudeFileChangeText(input.output)
5517+
: claudeNativeToolOutputText(input.output);
54775518
const turnItem: OrchestrationV2TurnItem =
54785519
itemType === "command_execution"
54795520
? {

‎pnpm-lock.yaml‎

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)