Skip to content
TypeScript

fix(claude): Open diff shows Claude edits as a diff instead of raw JSON (#26529)

Shoubhit Dasheb565e13abeb
diff --git a/apps/server/package.json b/apps/server/package.json
index 5abc769412d..2735d71c653 100644
--- a/apps/server/package.json
+++ b/apps/server/package.json
@@ -58,6 +58,7 @@
"@t3tools/source-control-github": "workspace:*",
"@t3tools/source-control-gitlab": "workspace:*",
"character-entities": "2.0.2",
+ "diff": "8.0.3",
"effect": "catalog:",
"jose": "catalog:",
"node-pty": "^1.2.0-beta.15",
diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
index 47457be6609..ea5ee249a78 100644
--- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
+++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
@@ -8740,6 +8740,152 @@ describe("ClaudeAdapterV2 background wake turns", () => {
),
);
+ it.effect.each([
+ {
+ case: "an Edit",
+ toolName: "Edit",
+ toolInput: {
+ file_path: "/workspace/src/math.ts",
+ old_string: " return a - b;",
+ new_string: " return a + b;",
+ },
+ toolUseResult: {
+ filePath: "/workspace/src/math.ts",
+ oldString: " return a - b;",
+ newString: " return a + b;",
+ originalFile: "export function add(a: number, b: number) {\n return a - b;\n}\n",
+ structuredPatch: [
+ {
+ oldStart: 1,
+ oldLines: 3,
+ newStart: 1,
+ newLines: 3,
+ lines: [
+ " export function add(a: number, b: number) {",
+ "- return a - b;",
+ "+ return a + b;",
+ " }",
+ ],
+ },
+ ],
+ userModified: false,
+ replaceAll: false,
+ },
+ diffStr:
+ "--- /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",
+ },
+ {
+ case: "a Write that creates a file",
+ toolName: "Write",
+ toolInput: { file_path: "/workspace/notes.md", content: "# Notes\n\nShip it.\n" },
+ toolUseResult: {
+ type: "create",
+ filePath: "/workspace/notes.md",
+ content: "# Notes\n\nShip it.\n",
+ structuredPatch: [],
+ originalFile: null,
+ userModified: false,
+ },
+ diffStr: "--- /dev/null\n+++ /workspace/notes.md\n@@ -0,0 +1,3 @@\n+# Notes\n+\n+Ship it.\n",
+ },
+ {
+ case: "a Write that creates an empty file",
+ toolName: "Write",
+ toolInput: { file_path: "/workspace/.keep", content: "" },
+ toolUseResult: {
+ type: "create",
+ filePath: "/workspace/.keep",
+ content: "",
+ structuredPatch: [],
+ originalFile: null,
+ userModified: false,
+ },
+ diffStr: undefined,
+ },
+ ])("stores the diff panel patch for $case", (testCase) =>
+ Effect.scoped(
+ Effect.gen(function* () {
+ const harness = yield* makeWakeHarness;
+ const toolUseId = `toolu_01${testCase.toolName}Patch`;
+
+ yield* harness.runtime.startTurn(
+ makeClaudeTestTurnInput({
+ threadId: harness.threadId,
+ providerThread: harness.providerThread,
+ now: yield* DateTime.now,
+ attemptId: RunAttemptId.make(`attempt-claude-${testCase.toolName}-patch`),
+ text: "Change it.",
+ attachments: [],
+ }),
+ );
+ yield* Queue.offer(
+ harness.sdkMessages,
+ claudeSdkFrame({
+ type: "assistant",
+ message: {
+ model: "claude-sonnet-4-6",
+ id: `msg_${testCase.toolName}_patch`,
+ type: "message",
+ role: "assistant",
+ content: [
+ {
+ type: "tool_use",
+ id: toolUseId,
+ name: testCase.toolName,
+ input: testCase.toolInput,
+ },
+ ],
+ },
+ parent_tool_use_id: null,
+ uuid: "00000000-0000-4000-8000-000000000793",
+ session_id: WAKE_NATIVE_SESSION,
+ }),
+ );
+ yield* Queue.offer(
+ harness.sdkMessages,
+ claudeSdkFrame({
+ type: "user",
+ message: {
+ role: "user",
+ content: [
+ {
+ type: "tool_result",
+ tool_use_id: toolUseId,
+ content: `The file ${testCase.toolInput.file_path} has been updated successfully.`,
+ },
+ ],
+ },
+ parent_tool_use_id: null,
+ uuid: "00000000-0000-4000-8000-000000000794",
+ session_id: WAKE_NATIVE_SESSION,
+ tool_use_result: testCase.toolUseResult,
+ }),
+ );
+ yield* Queue.offer(
+ harness.sdkMessages,
+ makeResultFrame({ uuid: "00000000-0000-4000-8000-000000000795", result: "Done." }),
+ );
+ yield* harness.awaitTerminal(1);
+
+ const fileChange = harness.events.findLast(
+ (event) =>
+ event.type === "turn_item.updated" &&
+ event.turnItem.nativeItemRef?.nativeId === toolUseId,
+ );
+ assert.deepEqual(
+ fileChange?.type === "turn_item.updated" && fileChange.turnItem.type === "file_change"
+ ? { diffStr: fileChange.turnItem.diffStr }
+ : undefined,
+ { diffStr: testCase.diffStr },
+ );
+ }).pipe(
+ Effect.provide(
+ Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
+ ),
+ ),
+ ),
+ );
+
it.effect("answers an approval a held wake turn raises without waiting for the echo", () =>
Effect.scoped(
Effect.gen(function* () {
diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
index dd4fc8f30af..b1273cee720 100644
--- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
+++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
@@ -106,6 +106,7 @@ import {
resolveIdleCompaction,
} from "@t3tools/contracts";
+import { FILE_HEADERS_ONLY, formatPatch, structuredPatch } from "diff";
import * as Cause from "effect/Cause";
import * as Clock from "effect/Clock";
import * as Context from "effect/Context";
@@ -2965,6 +2966,44 @@ function claudeNativeToolOutputText(output: ClaudeNativeToolOutput): string {
return typeof value === "string" ? value : value === undefined ? "" : jsonStringifyForTool(value);
}
+const decodeClaudeFileEditResult = Schema.decodeUnknownOption(
+ Schema.Struct({
+ type: Schema.optional(Schema.String),
+ filePath: Schema.String,
+ content: Schema.optional(Schema.String),
+ structuredPatch: Schema.mutable(
+ Schema.Array(
+ Schema.Struct({
+ oldStart: Schema.Number,
+ oldLines: Schema.Number,
+ newStart: Schema.Number,
+ newLines: Schema.Number,
+ lines: Schema.mutable(Schema.Array(Schema.String)),
+ }),
+ ),
+ ),
+ }),
+);
+
+function claudeFileChangeText(output: ClaudeNativeToolOutput): string {
+ const result =
+ output.type === "structured_tool_use_result"
+ ? Option.getOrUndefined(decodeClaudeFileEditResult(output.value))
+ : undefined;
+ if (result === undefined) return claudeNativeToolOutputText(output);
+ const patch =
+ result.structuredPatch.length === 0 && result.type === "create" && result.content !== undefined
+ ? structuredPatch("/dev/null", result.filePath, "", result.content)
+ : {
+ oldFileName: result.filePath,
+ newFileName: result.filePath,
+ oldHeader: undefined,
+ newHeader: undefined,
+ hunks: result.structuredPatch,
+ };
+ return patch.hunks.length === 0 ? "" : formatPatch(patch, FILE_HEADERS_ONLY);
+}
+
function claudeCommandOutputText(output: ClaudeNativeToolOutput): string {
const value = claudeNativeToolOutputValue(output);
if (typeof value === "object" && value !== null) {
@@ -5498,7 +5537,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
const outputText =
itemType === "command_execution"
? claudeCommandOutputText(input.output)
- : claudeNativeToolOutputText(input.output);
+ : itemType === "file_change"
+ ? claudeFileChangeText(input.output)
+ : claudeNativeToolOutputText(input.output);
const turnItem: OrchestrationV2TurnItem =
itemType === "command_execution"
? {
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index f2eabadf0a4..7f1de3cd97b 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -897,6 +897,9 @@ importers:
character-entities:
specifier: 2.0.2
version: 2.0.2
+ diff:
+ specifier: 8.0.3
+ version: 8.0.3
effect:
specifier: 4.0.5
version: 4.0.5